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

前端 未结 1 1916
-上瘾入骨i
-上瘾入骨i 2021-01-20 22:18

When using this query :

INSERT INTO order (order_quantity)
           VALUES (\'50\')

I\'m getting an error :

You ha

1条回答
  •  梦毁少年i
    2021-01-20 22:56

    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];
    

    0 讨论(0)
提交回复
热议问题