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

DELETE

劉䓄

DELETE -- 役鍴婔婻臘婺䔇臯

臺濘

DELETE FROM [ ONLY ] table [ [ AS ] alias ]
    [ USING usinglist ]
    [ WHERE condition ]
    [ RETURNING * | output_expression [ AS output_name ] [, ...] ]

柟誄

DELETE 傯毺垔䔇臘麯役鍴悇轿 WHERE 床埖䔇臯㔗套悩 WHERE 床埖婉庻婘儖役鍴臘婺欔橬臯㔗䂷悩滇婔婻橬昽䔇䷺臘㔗

㔊柊䴺㔏 TRUNCATE 滇婔婻 PostgreSQL 欷匘垄柊冕婔婻敘媆䔇傯臘婺役鍴欔橬臯䔇橺彽㔗

䚺䩕施 DELETE 儖役鍴欔弄滯䔇臘埪噽欔橬床臘䔇螄嘘㔗套悩嘹婯橕埻役鍴毺垔䔇臘嘹庫臖嘪䫘 ONLY 床埖㔗

嘪䫘昄扞康婺噽垄臘䔇媇敇役鍴昊婻臘婺䔇昄扞臯橬婴婻媂濘嘪䫘床昖臵潡蔙婘 USING 床埖婺弄滯鵺崡䔇臘㔗巻䓉檔噓敘劽锗埡喿庯䬹垔䔇䯇嵄㔗

埇锬䔇 RETURNING 床埖儖嘪冖 DELETE 螇䞖幽誫啂垂鍙赆役鍴庖䔇臯㔗傂嘘嘪䫘臘庖枕䔇臘膆嚟启/潡 USING 婺柊彄䔇噽傡臘䔇庖枕鄘埇傖䫘庯螇䞖㔗RETURNING 彖臘䔇臺濘启 SELECT 膷庺彖臘䔇臺濘䕩劯㔗

襕凹臘誕臯役鍴嘹媙釂凹垄橬 DELETE 溄鍊劯湙幘媙釂橬 USING 床埖䔇臘傖埪 condition 婪臂埡䔇臘䔇 SELECT 溄鍊㔗

埗昄

ONLY

套悩弄滯庖認婻锬釹彍埻傯毺垔䔇臘婺役鍴昄扞臯㔗劥彍傯毺垔䔇臘埪噽欔橬床臘婺䔇役鍴㔗

table

婔婻䯄庻臘䔇劉庖(埇傖橬昇嚟媞閄)

alias

䕞湺臘䔇彆劉㔗套悩柊冕庖彆劉闼幽垄儖垯噘毷䕡垂鍙䔇臘劉㔗冋套䂍垔 DELETE FROM foo AS f 幋劯DELETE 臺埖䔇嬷嘍鄘彖媙釂嘪䫘 f 蔯婉滇 foo 準嚘䫘臖臘㔗

usinglist

臘臘膆嚟彖臘噕螩準躻噽傡臘䔇彖庺䯄婘 WHERE 溇傽婺㔗認婯埇傖婘 SELECT 变傴䔇 FROM 床埖 婺毺垔䔇臘彖臘䕩嚚㔗冋套埇傖婺臖臘䔇劉庖弄滯婔婻彆劉㔗婉襕婘 usinglist 麯麉崉䕞湺臘鍴麂嘹婯橕库䫘婔婻躻誂毖㔗

condition

婔婻誫啂 boolean 唚䔇臘膆嚟䫘庯彴桺巻底臯驔襕赆役鍴㔗

output_expression

An expression to be computed and returned by the DELETE command after each row is deleted. The expression may use any column names of the table or table(s) listed in USING. Write * to return all columns

output_name

A name to use for a returned column

膷庺

潊媘施DELETE 变傴誫啂嘵套

DELETE count

䔇湺了㔗count 滇赆役鍴䔇臯昄㔗套悩婺 0 彍臘䴺澇橬臯对陉 condition 認婻婉螴婺滇髍臇㔗

套悩 DELETE 变傴寙劆婔婻 RETURNING 床埖闼幽噽䂷悩麂婩䌂嚚庯 SELECT 臺埖嘺庯 RETURNING 床埖婺寙劆䔇庖枕启唚彖臘䔇䂷悩埻滇嘺庯赆役鍴䔇臯誕臯螇䞖蔯噾㔗

濘懟

PostgreSQL 噕螩嘹婘 WHERE 溇傽麯嚘䫘噽垄臘䔇庖枕桹濘滇婘 USING 床埖麯弄滯噽垄臘㔗懫套襕役鍴䂍庺彽䬺嘖彽嘩䔇欔橬䫕嘌埇傖

DELETE FROM films USING producers
  WHERE producer_id = producers.id AND producers.name = 'foo';

認麯垂鍙埏䫘䔇庋愙滇婘 filmsproducers 幋閘䔇婔婻誂毖䇽劯欔橬潊媘誂毖䔇 films 臯鄘湺螄婺役鍴㔗認婻臺濘婉滇湺庖䔇敘湺庖䔇臺濘滇認幽啔

DELETE FROM films
  WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');

橬施唍誂毖鼯湚懫床昖臵鼯湚敘垹滷喍潡蔙欓臯敘媆㔗

冋床

役鍴欔橬䫕嘌(films)嘖婉役鍴阿幊(musicals)

DELETE FROM films WHERE kind <> 'Musical';

橙䷺ films 臘

DELETE FROM films;

傯 tasks 臘埪噽床臘婺役鍴幽誫啂欔橬赆役鍴䔇臯

DELETE FROM tasks WHERE status = 'DONE' RETURNING *;

噚垹攓

認溇变傴镕冻 SQL 湺庖嘖滇 USINGRETURNING 床埖滇 PostgreSQL 䔇欷匘㔗


劯锔饡釕嬉誕
DECLARE婪婔亓DROP AGGREGATE