sql INSERT的替代语法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql INSERT的替代语法相关的知识,希望对你有一定的参考价值。

\!h You know the album has three columns but forgot their order:
INSERT INTO album (artist_id, album_id, album_name) -- you make your own order
VALUES (7, 2, "Oedipus Schmoedipus"); 

-- Using this approach, you only need to insert values for some columns, not all.
-- A good example of this is the 'played' table:
INSERT INTO played (artist_id, album_id, track_id)
VALUES (7, 1, 1); 
-- the fourth column, 'played', already has a default of CURRENT_TIMESTAMP. This means it will automatically insert the current date and time.
 
\!h Same approach but for bulk insertion:
INSERT INTO played (artist_id, album_id, track_id)
VALUES (7, 1, 2), (7, 1, 3), (7, 1, 4); -- inserts 3 new track_ids
-- can accidentally omit values for columns
-- to fix that, use the DEFAULT keyword:
INSERT INTO played (7, 1, 2, DEFAULT);

\!h Another alternative INSERT syntax:
INSERT INTO played
SET artist_id = 7, album_id = 1, track_id = 1; -- column name and value together
-- can't be used for bulk insertion
-- can accidentally omit values for columns
-- need to remember and type column names

以上是关于sql INSERT的替代语法的主要内容,如果未能解决你的问题,请参考以下文章

INSERT INTO 语句的语法错误

SQL基础语法—insert语句

INSERT INTO 语句的语法错误

SQL INSERT INTO 语法 [重复]

Spark SQL:INSERT INTO 语句语法

INSERT INTO 语句的语法错误