2

I am trying to create SQL table with SQL keyword column named "lock", but "lock" seems is a SQL keyword where the workbench thought I would want to execute the "lock" function.

Create table A(
Lock Varchar(255)
);

*Putting the "Lock" into a square bracket --> [lock] doesn't work for me.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Kai Tzer
  • 53
  • 5

2 Answers2

4

You should put back ticks ` around the reserved word:

Create table A(
   `Lock` Varchar(255)
);

You can find a good explanation about reserved words and the list of them here.

Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.

If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.

You should try to avoid the use of this words, it may be confusing for some people to watch a code with this name. Try adding a logical name to it , like TabName_Lock.

sagi
  • 40,026
  • 6
  • 59
  • 84
3

MySQL uses the ` character to escape object names:

Create table A(
    `Lock` Varchar(255)
);
Mureinik
  • 297,002
  • 52
  • 306
  • 350