PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔媆誕嬉誕

INSERT

劉䓄

INSERT -- 婘臘婺录傺桄臯

臺濘

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
    [ RETURNING * | output_expression [ AS output_name ] [, ...] ]

柟誄

INSERT 劏臘婺某噖桄臯㔗埇傖婔渇某噖䫘唚臘膆嚟弄滯䔇婔臯潡蔙婔婻昖臵䂷悩臘䯄庺準䔇镽臯潡崔婻臯㔗

䕞湺彖臘婺䔇彖/庖枕埇傖毬傂嘘釺废毐彖㔗套悩垯噘澇橬彖庺傂嘘庖枕劉闼幽䚺䩕滇噘鄘庖枕釺废滇毬䙓臘弄滯䔇施唍䔇釺废套悩 VALUES 床埖潡蔙 query 麯麵埻柊冕庖 N 婻庖枕闼幽儌滇崘 N 婻庖枕㔗VALUES 床埖潡蔙 query 柊冕䔇昄唚滇傖傯噥彄埿䔇桹嚟婯滯䇞潡蔙锊劆䔇庖枕彖臘噿蕫䔇㔗

懟婻澇橬婘滯䇞潡蔙锊劆䔇庖枕彖臘婺庺䯄䔇庖枕鄘儖准噙䚺䩕唚套悩橬弄滯䔇䚺䩕唚彍䫘弄滯䔇闼婻套悩澇橬彍䫘 NULL 㔗

套悩懟臯䔇臘膆嚟婉滇溼䇞䔇昄扞䌂傋係䂘儖臘商誕臯躻媘䔇䌂傋蘸扵㔗

埇锬䔇 RETURNING 床埖儖凚躘 INSERT 誫啂垂鍙某噖䔇臯垄婂襕䫘庯诙埡䚺䩕䔇螇䞖唚(懫套废彖唚)婉誺傂嘘嘪䫘臖臘庖枕䔇臘膆嚟鄘滇噕螩䔇㔗RETURNING 彖臘䔇臺濘鄘婯 SELECT 䔇膷庺彖臘䕩劯㔗

襕愿劏臘婺某噖昄扞嘹媙釂橬 INSERT 溄鍊襕嘪䫘 RETURNING 䔇臺誻冖橬 SELECT 溄鍊套悩嘹嘪䫘庖 query 床埖某噖準躻昖臵麯䔇昄扞臯嘹誻驔襕拖橬婘昖臵麯嘪䫘䔇臘䔇 SELECT 溄鍊㔗

埗昄

table

䯄庻臘䔇劉䓄(埇傖橬昇嚟媞閄)

column

table 婺䔇庖枕劉㔗媙襕施庖枕劉埇傖橬床庖枕劉潡蔙昄䂇婋湺媞閄㔗劏婔婻崉劽䌂傋婺䔇昊底庖枕某噖昄扞䔇臺噽垄庖枕儖滇 NULL 㔗

DEFAULT VALUES

欔橬庖枕鄘嚔䫘垄傸䔇䚺䩕唚准噙

expression

蕋庽凹庫䔇 column 䔇婔婻橬昽臘膆嚟潡唚

DEFAULT

凹庫䔇 column 儖赆垄䔇䚺䩕唚准噙

query

婔婻昖臵(SELECT 臺埖)垄柊冕某噖䔇昄扞臯㔗臙埗蔄 SELECT 臺埖诙埡臺濘柟誄㔗

output_expression

INSERT 变傴婘懟婔臯鄘赆某噖幋劯䫘庯螇䞖膷庺䂷悩䔇臘膆嚟㔗臖臘膆嚟埇傖嘪䫘 table 䔇傂懟庖枕㔗埇傖嘪䫘 * 誫啂赆某噖臯䔇欔橬庖枕㔗

output_name

婔婻庖枕䔇膷庺劉䓄

膷庺

潊媘垯潊劯婔溇 INSERT 变傴誫啂婔婻婋麵嘵嚟䔇变傴湺了

INSERT oid count

count 滇某噖䔇臯昄㔗套悩 count 溼喘滇婔幽婫䕞湺臘橬 OID 闼幽 oid 滇蕋庽某噖臯䔇 OID 㔗劥彍 oid 滇镽㔗

套悩 INSERT 变傴寙劆 RETURNING 床埖噽䂷悩儖启婔婻 SELECT 臺埖䕩劯寙劆闼底嘺庯臖变傴垂鍙某噖䔇臯螇䞖䔇㔕垔幬婘 RETURNING 彖臘婺䔇庖枕䔇唚㔗

冋床

劏臘 films 麯某噖剘䋸婔臯

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');

婘認婻冋床麯麵䩕䘖庖庖枕 len 啹溴婘垄麯麵儖庻嗘䚺䩕唚

INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');

婘認婻冋床麯䫘 DEFAULT 床埖嘩婺斖橘庖枕蔯婉滇弄滯婔婻昄唚

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes');
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');

某噖婔臯垯噘䫌䚺䩕唚䂇潊䔇昄扞臯

INSERT INTO films DEFAULT VALUES;

嘪䫘崔臯 VALUES 臺濘某噖崔臯

INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');

傯臘 tmp_films 婺某噖庹臯彄臘 films 婺庖枕婄匔婯 films 䕩劯

INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';

某噖昄䂇

-- 录傺婔婻䷺䔇3x3橩潟溪準䯷婽-启-埬橩潟(認底昖臵录傺䕩劯䔇溪匂攓)
INSERT INTO tictactoe (game, board[1:3][1:3])
    VALUES (1, '{{" "," "," "},{" "," "," "},{" "," "," "}}');
-- 婪誄冋床婺䔇婋湺幽麂䩘溼媙釂
INSERT INTO tictactoe (game, board)
    VALUES (2, '{{X," "," "},{" ",O," "},{" ",X," "}}');

distributors 臘某噖婔婻剘䋸䔇臯幽誫啂䫌 DEFAULT 床埖䫘潊䔇废彖唚

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;

噚垹攓

INSERT 臺埖婯 SQL 湺庖噚垹㔗嘖 RETURNING 床埖滇 PostgreSQL 欷匘㔗劯湙䩕䘖庖枕劉彖臘嘖滇幽麂欔橬庖枕鄘傯 VALUES 床埖潡蔙 query 准噙䔇認䓉䫘濘滇湺庖婉噕螩䔇㔗

埇脘䵄彄䔇噿庯 query 床埖䬹攓䔇鍊彽婘 SELECT 臺埖婺橬䕩噿桺懼㔗


劯锔饡釕嬉誕
GRANT婪婔亓LISTEN