INSERT語句。
插入完整的行
INSERT INTO customers
VALUES(
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
?。簩γ總€列必須給出一個值,沒有值用NULL;各個列必須以他們在表定義中出現(xiàn)的次序填充;但這種語法不安全,太依賴順序,應避免使用。
安全方法:
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES('Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
?。?/p>
- 總是使用列的列表。
- 仔細地給出值。
- 省略列:該列為NULL;或在表定義中給出默認值。
- 提高整體性能:LOW_PRIORITY降低INSERT語句的優(yōu)先級,
INSERT LOW_PRIORITY INTO。
插入多個行
例:
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES('Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES('M. Martian',
'42 Galaxy Way',
'New York',
'NY',
'11213',
'USA');
或者,只要每條INSERT語句中的列名(和次序)相同:
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES(
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL
),
(
'M. Martian',
'42 Galaxy Way',
'New York',
'NY',
'11213',
'USA'
);
插入檢索出的數(shù)據(jù)
例:假如你想從另一表中合并客戶列表到你的customers表:
INSERT INTO customers(
cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
)
SELECT cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
FROM custnew;
!:實際上列名不一定要一一匹配。