Search code examples
phpmysqlsqlmysql-error-1064create-table

PHP-autogenerated MySQL Create Table statement returns error


mysql> CREATE TABLE metadata 
( 
id int, 
created_by int, 
modified_by int, 
deleted_by int, 
created_at datetime, 
modified_at datetime, 
deleted_at datetime, 
key varchar(45), 
definition_type varchar(1), 
metadata_type varchar(1), 
no_of_decimals int, 
is_required tinyint, 
min_range decimal, 
max_range decimal, 
entity varchar(10), 
sequence int, 
meta_group varchar(2), 
PRIMARY KEY (id) );

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(45), definition_type varchar(1), metadata_type varchar(1), no_of_decimal' at line 1

I'm not entirely sure how to correct this error, as these kinds of things don't typically have specific answers online. How can I fix this error?


Solution

  • key is a reserved word, you should escape it with backticks ` in MySQL:

    `key` varchar(45)