[] brackets in sql statements

前端 未结 8 2399
深忆病人
深忆病人 2020-11-28 12:24

What do the brackets do in a sql statement?

For example, in the statement:

insert into table1 ([columnname1], columnname2) values (val1, val2)

相关标签:
8条回答
  • 2020-11-28 13:00

    They allow you to use keywords (such as date) in the name of the column, table, etc...

    Since this is a bad practice to begin with, they are generally not included. The only place you should see them being used is by people starting out with sql queries that don't know any better. Other than that they just clutter up your query.

    0 讨论(0)
  • 2020-11-28 13:01

    This is Microsoft SQL Server nonstandard syntax for "delimited identifiers." SQL supports delimiters for identifiers to allow table names, column names, or other metadata objects to contain the following:

    • SQL reserved words: "Order"
    • Words containing spaces: "Order qty"
    • Words containing punctuation: "Order-qty"
    • Words containing international characters
    • Column names that are case-sensitive: "Order" vs. "order"

    Microsoft SQL Server uses the square brackets, but this is not the syntax standard SQL uses for delimited identifiers. Standardly, double-quotes should be used for delimiters.

    In Microsoft SQL Server, you can enable a mode to use standard double-quotes for delimiters as follows:

    SET QUOTED_IDENTIFIER ON;
    
    0 讨论(0)
  • 2020-11-28 13:08

    They are meant to escape reserved keywords or invalid column identifiers.

    CREATE TABLE test
    (
      [select] varchar(15)
    )
    
    INSERT INTO test VALUES('abc')
    
    SELECT [select] FROM test
    
    0 讨论(0)
  • 2020-11-28 13:10

    Anything inside the brackets is considered a single identifier (e.g. [test machine]. This can be used to enclose names with spaces or to escape reserve words (e.g. [order], [select], [group]).

    0 讨论(0)
  • 2020-11-28 13:14

    if you use any column name which is same as any reserved keyword in sql, in that case you can put the column name in square bracket to distinguish between your custom column name and existing reserved keyword.

    0 讨论(0)
  • 2020-11-28 13:17

    They are simply delimiters that allow you to put special characters (like spaces) in the column or table name e.g.

    insert into [Table One] ([Column Name 1], columnname2) values (val1, val2)
    
    0 讨论(0)
提交回复
热议问题