INSERT可以用来(一般权限较高)
1.插入完整行
2.插入行的一部分
3.插入某些查询的结果
正常的一般语句,有列次序的方式(更安全):
INSERT INTO Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES('1000000006',
'Toy Land',
'123 Any Street',
'New York',
'NY',
'1111',
'USA',
NULL,
NULL);
插入部分行:(如果列可以省略,要满足两个条件:该列定义允许NULL值或是表定义中给出默认值)
INSERT INTO Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
VALUES('1000000006',
'Toy Land',
'New York',
'NY',
'1111',
'USA');
插入检索出的数据:从一个新表将数据合并到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;
#可以包含WHERE子句
从一个表复制到另一个表,MYSQL&MariaDB
CREATE TABLE CustCopy AS
SELECT * FROM Customers;
- 任何SELECT选项和子句都可以使用;
- 可以使用联结从多个表插入数据;
- 不管从多少个表中检索数据,数据口智能插入一个表中
SELECT INTO 是实验新SQL语句前进行表复制的很好工具,先进行复制,可在复制的数据上测试SQL代码,而不会影响实际数据。