This is an article where mainly written to discuss an error generated when a certain query command is being executed. The query command executed is personally executed in a MySQL Command Console. It can also carried out in a MySQL Editor based on GUI. The query command is specifically performed to add a foreign key in the database. It is performed and executed but the execution itself is failed and MySQL generated an error as specified in the title. The error message is ERROR 1064(42000): You have an error in your SQL syntax. Below is an example of the query executed in MySQL Command Console which is primarily carried out to add foreign key :
mysql> alter table server_db add constraint fk_db_server foreign_key(id_server) references server(id_server) on update cascade on delete cascade;
The above query is a query which is used to add foreign key specifically can be seen in an article titled ‘MySQL Add Foreign Key in MySQL Command Console’ which can be seen in the following link.
Since the error specified is actually given the main focus of the error which is ‘You have an error in your SQL syntax’, the first step for solving and handling the error is actually trying to find the incorrect syntax involving the query for adding foreign key. As shown in the article titled ‘MySQL Add Foreign Key in MySQL Command Console’ in this link, there is an incorrect syntax of the SQL syntax for the query specified above. It is actually in the following reserved keyword ‘foreign_key’. As shown in the article stated previously, the correct keyword which is reserved for adding foreign key is actually ‘foreign key’.
So, to solve the above problem, just change the SQL syntax query into the following query :
mysql> alter table server_db add constraint fk_db_server foreign key(id_server) references server(id_server) on update cascade on delete cascade;
The execution process which is successfully carried out can be seen as the following process :
mysql> alter table server_db add constraint fk_db_server foreign key(id_server) references server(id_server) on update cascade on delete cascade; Query OK, 0 rows affected (0,03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
Below is the diagram of Entity Relationship Diagram which is specifically shows the relationship for adding a new foreign key :
One thought on “MySQL Database Generate ERROR 1064 (42000): You have an error in your SQL syntax Add Foreign Key”