What are the MySQL Reserved Words?

Topics Covered

In MySQL, there are specific words that hold a distinct functionality. These words are called "Reserved Words" and they cannot be used as regular words in the program because they are already assigned a specific function to perform.

For example, words like SELECT, INSERT, and UPDATE are reserved words in MySQL. If you try to use these words as variable names, column names, or table names in your database, MySQL will not allow it because it needs those words to perform specific actions in the program.

So these words have a special meaning in the program and cannot be used as regular words. If you need to use a MySQL Reserved Word as a column name then you can enclose the reserved word in backticks when defining the column name.

As an example, let's say you intend to generate a table that contains a column called "select". Since SELECT is a reserved word in MySQL, you can enclose it in backticks like this:

By enclosing the reserved word SELECT in backticks, you can use it as a column name in your table.

Note: It's generally a good practice to avoid using reserved words as column names to prevent any potential conflicts or confusion.

TABLE for MySQL Reserved Words

Below is the list of some common MySQL Reserved Words that you should avoid using as identifiers (such as table names or column names or variable names):

table for mysql reserved words

Words that are Disallowed by ANSI SQL but Allowed by MySQL as Column or Table Names

Certain words are disallowed by ANSI SQL as column or table names because they are reserved words or keywords. MySQL allows some keywords to be used as unquoted identifiers. Let's see these keywords in detail:

ACTION

ACTION is one of the reserved keywords in MySQL that is used for triggering events. When defining a trigger, you can use the AFTER keyword to specify when the trigger should execute, followed by the INSERT, UPDATE, or DELETE keywords to indicate the type of event that triggers the action. Then you can use the ACTION keyword to specify the action that should be taken when the trigger is executed. It cannot be used as a table name or column name. However, if you still want to use it as a column or table name, you can enclose it in backticks. Here's an example:

In the above example, the action keyword is used as a column name which is a reserved keyword.

BIT

BIT is a data type in MySQL that is used to store binary data which can be either 0 or 1. We can declare it with a specified length, that shows the number of bits that can be stored. For example, BIT can store up to 4 bits.

In this example, the bit table has two columns: id and value. The value column uses the BIT data type, which can store either 0 or 1 values. The bit table is used to store binary values represented as a bit.

DATE

DATE keyword in MySQL is used to represent a date value in the format of year-month-date (YYYY-MM-DD). Dates can be stored using this particular data type. DATE is a reserved keyword in MySQL, it can still be used as a column name or table name if enclosed in backticks. For example:

In the above example, a table having the name name_of_table is created with a DATE column having the name date_column_name used to store a date value.

ENUM

A data type that represents a set of predefined values can be defined using the ENUM keyword in MySQL. It can contain only one value from the set of provided values, or it can be set to NULL if no value is chosen. Here's an example of creating a table with ENUM as a column name:

In the above example, a table named name_of_table is created with a column storing values of the ENUM type.

NO

NO keyword in MySQL is used in the context of constraints to specify that a certain action is not allowed and you can enforce data integrity and avoid accidental changes to your data. Let's see an example of using "NO" as a column name enclosed in backticks:

In this example, the backticks are used to enclose the column name "no" because it is a reserved word in MySQL.

TEXT

The TEXT keyword in MySQL is a data type used for storing large amounts of text data in your database up to up to 65,535 characters.

In this example, the table name is "name_of_table" and one of its columns is named "text".

TIME

The TIME keyword in MySQL is used to define a column that can store time values. It stores the time values in the format of hh:mm:ss. Here is an example of creating a table with a column named TIME, enclosed in backticks:

TIMESTAMP

The TIMESTAMP keyword in MySQL is used to define a column that stores both date and time values. It can store a timestamp value in the format YYYY-MM-DD hh:mm:ss. Here's an example of a column named timestamp in a MySQL table:

In this example, the name_of_table has three columns: id, value, and timestamp. The timestamp column is defined with the TIMESTAMP data type, which is used to store date and time values. The PRIMARY KEY constraint is applied to the id column to ensure that each row in the table has a unique identifier.

Conclusion

  • MySQL reserved words are special words in MySQL that cannot be generally used as regular words in the program because they are already assigned a specific function.
  • Reserved words such as SELECT, INSERT, and UPDATE is used by MySQL to perform specific actions.
  • To use reserved words as column names, you have to enclose them in backticks.
  • It is generally advisable to avoid using reserved words as column names to prevent any potential conflicts or confusion.
  • This article details some common MySQL reserved words that can be used as column or table names, such as ACTION, BIT, DATE, ENUM, NO, TEXT, and TIME, and provides examples of their usage.