SQL 必知必会第十五课 插入数据
## 插入完整的行
insert into customers
values(1000000006,'Toy Land','123 Any Street','New York','NY','11111','USA', null, null);
-- 繁琐却更加保险的方法
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','11111','USA', null, null);
## 第二条不能执行的原因是一条同样的数据不能插入两遍
## 插入部分行
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country)
values(1000000006,'Toy Land','123 Any Street','New York','NY','11111','USA');
## 插入检索出的数据
insert into customers(cust_id,cust_contact, cust_email, cust_name, cust_address, cust_city, cust_sate,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;
## 一个表复制到另一个表
create table CustCoby as select * from customers;
## 只想复制部分列的话,可以列出部分列,不用通配符
## Challenges
insert into customers(cust_id, cust_name)
values(1000000007,'Adam'); -- 1
create table OrderCoby as (select * from OrderItems); -- 2
注意点
-
插入行时每一列都应该有一个值,若没有的话应该用null值填充
-
从其他列选择行插入可以插入多行数据
select into 须知:
- 任何select选项和句子都可以使用,包括where和group by
- 可以利用联结从多个表插入数据
- 不管从多少个表中检索数据,数据只能查如一个表中