插入数据
INSERT是用来插入(或添加)行到数据库表的【INSERT语句一般不会产生输出】
插入可以用几种方式使用:
1)插入完整的行
INSERT INTO Customers
VALUES(NULL,
‘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(‘Pep E.LaPew’,
‘100 Main Street’,
‘Los Angeles’,
‘CA’,
‘90046’,
‘USA’,
NULL,
NULL);
2)插入多个行
INSERT INTO Customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
VALUES(‘Pep E.LaPew’,
‘100 Main Street’,
‘Los Angeles’,
‘CA’,
‘90046’,
‘USA’);
INSERT INTO Customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
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)
VALUES(
‘Pep E.LaPew’,
‘100 Main Street’,
‘Los Angeles’,
‘CA’,
‘90046’,
‘USA’
),
(
‘M.Martian’,
’42 Galaxy Way’,
’New York’,
‘NY’,
’11213’,
‘USA’
);
3)插入检索出的数据
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;
INSERT SELECT中SELECT语句可包含WHERE子句以过滤插入的数据