I have a mytable
table in my MySQL database.
It contains the following columns:
- id (INT) PRIMARY KEY - AUTO INCREMENT - DEFAULT NONE
- name (VARCHAR (220)) - DEFAULT NULL
- code (VARCHAR (220)) - DEFAULT NULL
Is it possible that if I insert something in this table I want the code
column to autogenerate its value based on the columns id
and name
.
If I tried to run this example query:
INSERT INTO mytable(name) VALUES ('myname');
I want to get the following result:
+----+--------+-------+
| id | name | code |
+----+--------+-------+
| 1 | myname | my1 |
+----+--------+-------+
The code
column default value must be the first 2 characters of myname
which is my
plus the id
column.
I can actually do this using PHP code but I think there must be a way using MySQL only.