sql >> Database teknologi >  >> RDS >> Mysql

SQL INSERT-syntaks – Listet af DBMS

Denne artikel viser SQL'en INSERT syntaks, som implementeret af forskellige databasestyringssystemer (DBMS'er). Syntaksen er angivet nøjagtigt som hver leverandør har angivet den på deres hjemmeside. Klik på det relevante link for at se flere detaljer om syntaksen for en bestemt leverandør.

De dækkede DBMS'er er MySQL, SQL Server, PostgreSQL og Oracle Database.

MySQL

Fra MySQL 5.7 Reference Manual:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name,...)] 
    [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

Eller:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name,...)]
    SET col_name={expr | DEFAULT}, ...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

Eller:

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name,...)] 
    [(col_name,...)]
    SELECT ...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

SQL-server

Fra Transact-SQL-referencen:

[ WITH <common_table_expression> [ ,...n ] ]
INSERT 
{
        [ TOP ( expression ) [ PERCENT ] ] 
        [ INTO ] 
        { <object> | rowset_function_limited 
          [ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
        }
    {
        [ ( column_list ) ] 
        [ <OUTPUT Clause> ]
        { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n     ] 
        | derived_table 
        | execute_statement
        | <dml_table_source>
        | DEFAULT VALUES 
        }
    }
}
[;]

<object> ::=
{ 
    [ server_name . database_name . schema_name . 
      | database_name .[ schema_name ] . 
      | schema_name . 
    ]
  table_or_view_name
}

<dml_table_source> ::=
    SELECT <select_list>
    FROM ( <dml_statement_with_output_clause> ) 
      [AS] table_alias [ ( column_alias [ ,...n ] ) ]
    [ WHERE <search_condition> ]
        [ OPTION ( <query_hint> [ ,...n ] ) ]

<column_definition> ::=
 column_name <data_type>
    [ COLLATE collation_name ]
    [ NULL | NOT NULL ]

<data type> ::= 
[ type_schema_name . ] type_name 
    [ ( precision [ , scale ] | max ]

-- External tool only syntax
INSERT 
{
    [BULK]
    [ database_name . [ schema_name ] . | schema_name . ]
    [ table_name | view_name ]
    ( <column_definition> )
    [ WITH (
        [ [ , ] CHECK_CONSTRAINTS ]
        [ [ , ] FIRE_TRIGGERS ]
        [ [ , ] KEEP_NULLS ]
        [ [ , ] KILOBYTES_PER_BATCH = kilobytes_per_batch ]
        [ [ , ] ROWS_PER_BATCH = rows_per_batch ]
        [ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ]
        [ [ , ] TABLOCK ]
    ) ]
}
[; ]

PostgreSQL

Fra PostgreSQL 9.5-manualen:

[ WITH [ RECURSIVE ] with_query [, ...] ]
INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
    [ ON CONFLICT [ conflict_target ] conflict_action ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

where conflict_target can be one of:

    ( { index_column_name | ( index_expression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE index_predicate ]
    ON CONSTRAINT constraint_name

and conflict_action is one of:

    DO NOTHING
    DO UPDATE SET { column_name = { expression | DEFAULT } |
                    ( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) |
                    ( column_name [, ...] ) = ( sub-SELECT )
                  } [, ...]
              [ WHERE condition ]

Oracle-database

Fra Oracle Database Online Documentation 12c Udgivelse 1 (12.1):

INSERT [ hint ]
   { single_table_insert | multi_table_insert } ;

Nedenfor er en beskrivelse af klausulerne og deres komponentunderklausuler.

single_table_insert ::=

insert_into_clause
{ values_clause [ returning_clause ]
| subquery 
} [ error_logging_clause ]

insert_into_clause ::=

INTO dml_table_expression_clause [ t_alias ]
[ (column [, column ]...) ]

values_clause ::=

VALUES ({ expr | DEFAULT }
          [, { expr | DEFAULT } ]...
       )

returning_clause::=

{ RETURN | RETURNING } expr [, expr ]...
INTO data_item [, data_item ]...

multi_table_insert ::=

{ ALL
  { insert_into_clause [ values_clause ] [error_logging_clause] }...
| conditional_insert_clause
} subquery

conditional_insert_clause ::=

[ ALL | FIRST ]
WHEN condition
THEN insert_into_clause
  [ values_clause ]
  [ error_logging_clause ]
  [ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
[ WHEN condition
  THEN insert_into_clause
    [ values_clause ]
    [ error_logging_clause ]
    [ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
]...
[ ELSE insert_into_clause
  [ values_clause ]
  [ error_logging_clause ]
   [ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
]

DML_table_expression_clause::=

{ [ schema. ]
  { table
    [ partition_extension_clause
    | @ dblink
    ]
  | { view | materialized view } [ @ dblink ]
  }
| ( subquery [ subquery_restriction_clause ] )
| table_collection_expression
}

partition_extension_clause::=

{ PARTITION (partition)
| PARTITION FOR (partition_key_value [, partition_key_value]...)
| SUBPARTITION (subpartition)
| SUBPARTITION FOR (subpartition_key_value [, subpartition_key_value]...)
}

subquery_restriction_clause::=

WITH { READ ONLY
     | CHECK OPTION 
     } [ CONSTRAINT constraint ]

table_collection_expression ::=

TABLE (collection_expression) [ (+) ]

error_logging_clause ::=

LOG ERRORS 
  [ INTO [schema.] table ]
  [ (simple_expression) ]
  [ REJECT LIMIT { integer | UNLIMITED } ]

Om INSERT Erklæring

Den INSERT statement indsætter nye rækker i en tabel. Du kan indsætte en eller flere rækker, der er angivet med værdiudtryk, eller nul eller flere rækker, der stammer fra en forespørgsel.


  1. Introduktion til SQL Server

  2. BeanCreationException:Fejl ved oprettelse af bean med navnet 'flywayInitializer'

  3. Sådan bestiller du rækker efter gruppesum i SQL

  4. Fejl ved push af data til Heroku:tidszoneforskydning uden for rækkevidde