skip to Main Content

I was writing a MySQL table create statement when I encountered the following error with the following code:

You have an error in your SQL syntax; it seems the error is around: 'Keys( id INT PRIMARY KEY AUTO_INCREMENT, ip VARCHAR(45) NOT NULL, email VA' at line 1
CREATE TABLE Keys(
  id INT PRIMARY KEY AUTO_INCREMENT,
  ip VARCHAR(45) NOT NULL,
  email VARCHAR(255) NOT NULL
)

This code follows all MySQL guidelines as far as I know.
Could anyone help me find out the error here?

Thanks!

I attempted to plug the code into various SQL syntax checkers and linters and I found the same error in all of the ones I tested.

2

Answers


  1. KEYS is a MySQl reserved word, so the table name needs escaping. (Or changing, so this doesn’t come up every time you make a query.)

    https://dev.mysql.com/doc/refman/8.0/en/keywords.html

    CREATE TABLE `Keys`(...)
    
    Login or Signup to reply.
  2. The error is caused by the use of the word "Keys" as the name of the table. "Keys" is a reserved keyword in SQL, which means it cannot be used as a table name without being enclosed in backticks. To fix the error, you should enclose the table name in backticks like this:

    CREATE TABLE

    Keys

    (
    id INT PRIMARY KEY AUTO_INCREMENT,
    ip VARCHAR(45) NOT NULL,
    email VARCHAR(255) NOT NULL
    );

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search