How to use a reserved word in SQL as a table name?

♀尐吖头ヾ 提交于 2020-03-03 09:56:46

问题


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?


回答1:


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)

Yes, this query works :

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


来源:https://stackoverflow.com/questions/54479920/how-to-use-a-reserved-word-in-sql-as-a-table-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!