天天看點

mysql 保留字作表名,如何在SQL中使用保留字作為表名?

mysql 保留字作表名,如何在SQL中使用保留字作為表名?

When using this query :

INSERT INTO order (order_quantity)

VALUES ('50')

I'm getting an error :

You have an error in your SQL syntax; check the manual that

corresponds to your MariaDB server version for the right syntax to use

near 'order (order_quantity) VALUES('50')' at line 146

What's wrong with my query?

解決方案

Reserved words are not recommended for use as database, table, column, variable or other object names. If you desire to use a reserved word is used as an object name in ANSI standard syntax, it must be enclosed in double-quotes to allow the Relational Engine (whichever that one is) that the word is being used as an object and not as a keyword in the given context.

Here are some examples specific to different SQL engines:

order is a SQL Keyword, used to sort results (ORDER BY ...)

Wrap backticks around it if you are using MySQL or Maria DB

INSERT INTO `order` (order_quantity) VALUES ('50');

Wrap brackets around it if you are using MS SQL Server

INSERT INTO [order] (order_quantity) VALUES ('50');

Wrap double quotes around it if you are using pgSQL

INSERT INTO "order" (order_quantity) VALUES ('50');

In example, nothing (but common sense) prevents you from creating a database named INSERT with a table INTO having a column VALUE(42)

mysql 保留字作表名,如何在SQL中使用保留字作為表名?

Yes, this query works :

USE [INSERT];

SELECT [INTO].[VALUE(42)] FROM [INTO];