For faster navigation, this Iframe is preloading the Wikiwand page for 插入 (SQL).

插入 (SQL)

SQL裡,可以利用INSERT陈述式對資料表插入一个元组或子查询结果。

基本格式

[编辑]

插入元组的语句格式为:

  • INSERT INTO 資料表名 (欄位1, [欄位2, ... ]) VALUES (常數值1, [常數值2, ...])

这段代码的作用,是将新元组插入指定的資料表中,欄位与常數值一一对应。如果欄位没有被定义,则填入欄位預設值。

例如将 John Doe 的电话号码插入 phone_book 資料表中:

INSERT INTO phone_book (name, tel_number) VALUES('John Doe', '555-1212');

其中 INTO 子句指明了目标表名 phone_book,並指定新增加的元组在哪些欄位上要赋值,其欄位排列顺序可与 CREATE TABLE 中的顺序不同。VALUES 子句用來对新元组的各欄位指定輸入值,字符串常数則需用单引号括起来。

另外,也可以不指定欄位名,而使用如下这种简化的格式:

  • INSERT INTO 資料表名 VALUES (常數值1, [常數值2, ...])

使用这种格式,是假設要填入欄位的顺序,与 CREATE TABLE 時的欄位顺序完全相同。採用这种格式,则上例可表示为:

INSERT INTO phone_book VALUES ('John Doe', '555-1212');

参考

[编辑]
  • 王珊 萨师煊. 数据库系统概论 (M) 4. 北京: 高等教育版社. 2006. ISBN 7-04-019583-6. 
{{bottomLinkPreText}} {{bottomLinkText}}
插入 (SQL)
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?