Using reserved words in column names

this is simple code, but I just don’t know why I cannot use this word as a table object

CREATE TABLE IF NOT EXISTS users( key INT PRIMARY KEY NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, ); 

I realized that I can’t use the "key", if I use the key, mysql will ask me to check the syntax, but if I use "id" or any others, the table will be created.

Does anyone know how I can create an object name in a key? Not something important, since I can just use id instead of the key, but since I found this error, I don’t see if there is a way to make it work.

+5
sql database mysql
source share
2 answers

You can use key if you want. Just wrap it with a backtick,

 CREATE TABLE IF NOT EXISTS users ( `key` INT PRIMARY KEY NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, ); 

but as an adviser, refrain from using any reserved keyword to avoid future problems. :)

+14
source share

Trailing underscore

Simple solution: Add an ending underscore to each name.

SQL Specification SQL : 2011 clearly promises to never use trailing underscores for any keyword, either now or in the future.

Example: key_

See my answer to a similar question, h2 database column name is a reserved word.

+3
source share

All Articles