Unable to insert data into another column where data is the output of the function applied to the data is a column in same table

Viewed 31

1.I wanted to add data into the attribute DAY by applying the function day() on to the corresponding values of attribute DATE. I want to insert the value into the permanent table.

  1. And for future data entries, what should I edit so that DAY attribute is automatically calculated from the DATE inserted.
mysql> SELECT * FROM SCHEDULE;
+----+---------+------------+----------+----------+--------+------+
| ID | TYPE    | DATE       | START    | END      | VENUE  | DAY  |
+----+---------+------------+----------+----------+--------+------+
|  1 | SCHOOL  | 2022-09-12 | 07:15:00 | 12:30:00 | SCHOOL | NULL |
|  2 | SCHOOL  | 2022-09-13 | 07:15:00 | 12:30:00 | SCHOOL | NULL |
|  3 | SCHOOL  | 2022-09-14 | 07:15:00 | 12:30:00 | SCHOOL | NULL |
|  4 | FIITJEE | 2022-09-14 | 15:30:00 | 19:45:00 | C-14   | NULL |
|  5 | SCHOOL  | 2022-09-15 | 07:15:00 | 12:30:00 | SCHOOL | NULL |
|  6 | SCHOOL  | 2022-09-16 | 07:15:00 | 12:30:00 | SCHOOL | NULL |
|  7 | FIITJEE | 2022-09-17 | 09:30:00 | 16:15:00 | C-14   | NULL |
+----+---------+------------+----------+----------+--------+------+
7 rows in set (0.00 sec)```

TABLE FORMAT IS AS FOLLOWS:
```mysql> DESCRIBE SCHEDULE
    -> ;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| ID    | int        | NO   | PRI | NULL    | auto_increment |
| TYPE  | varchar(7) | YES  |     | NULL    |                |
| DATE  | date       | YES  |     | NULL    |                |
| START | time       | YES  |     | NULL    |                |
| END   | time       | YES  |     | NULL    |                |
| VENUE | varchar(6) | YES  |     | NULL    |                |
| DAY   | varchar(9) | YES  |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql>

EDIT

I changed the table structure but it is showing DATE instead of DAY at the supposed place.

The output I am expecting is Day names like Monday for Dates like 2022-09-12 but instead I am getting 12 as Date.

enter image description here

1 Answers

The mistake is in the datatype specification of the DAY attribute. It should be:

 DAY VARCHAR(9) GENERATED ALWAYS AS(DAYNAME(DATE))

Here DAYNAME is used instead of DAY.

To solve it, one can use MODIFY COLUMN command.

Related