SQL必知必会 知识点3(第十五-十七章)-插入、更新、删除、创建

参考文献:《SQL必知必会 第三版》

#插入数据 insert
#插入完整的行

insert into Customers
values ('1000000006','Toy Land','123 Any Street','New York','NY','11111','USA',MULL,NULL);
——插入一个新客户到Customers表。存储到每个表列中的数据在values子句中给出,对每个列必须提供一个值,如果没有则应该使用NULL值(假定表允许对该列指定空值)

#插入部分行
insert into Customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country)
values('1000006','Tony Land','123 Any Street','New York','NY','11111','USA');

#插入检索出的数据
#insert select(增补数据到一个已经存在的表)

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 从CustNews中将所有数据导入Customers。(事实上,DBMS不关心select返回的列名,select 中的第一列将用来填充表列中指定的第一个列,第二列将用来填充表列中指定的第二个列)

#从一个表复制到另一个表
#select into(复制数据到一个新表)

select *
into CustCopy
from Customers;

#更新和删除数据 
#update、delete

update Customers
set cust_contact = 'Sam Roberts',cust_email = '[email protected]'
where cust_id = '100000006';
——更新客户100000006的cust_contact和cust_email列
update Customers
set cust_email = NULL
where cust_id = '100000006';
——NULL用来去除cust_email列中的值(为了删除某个列的值,课设置它位NULL,加入表定义允许NULL值)
 
#delete from Customers
where cust_id = '100000006';
——删除客户100000006(如果省略where子句,它将删除表中每个客户)

(delete语句从表中删除行,但是不删除表本身)
(如果想从表中删除所有行,可以使用truncate table语句,不记录数据的变动,速度更快)
(SQL没有撤销按钮,应该非常小心地使用update和delete)

#创建和操纵表
#create table

create table OrderItems
(
    order_num integer not null,
    order_item interger not null,
    prod_id char(10) not null,
    quantity integer not null default 1,
    item_price decimal(8,2) not null
);
——创建OrderItems表,它包含构成订单的各项(订单本身存储在Orders表中)。quantity列存档订单中每个物品的数量。default 1表示默认数量1。
(NULL为默认设置,如果不指定 not NULL,则认为指定的NULL)
alter table Vendors
add vend_phone char(20);
——给Vendors表增加一个名为vend_phone的列,其数据类型为char
alter table Vendors
drop column vend_phone;
——删除Vendors表中的vend_phone列(这个例子并非所有DBMS都有效)
Drop table CustCopy;
——删除CustCopy表(删除表没有确认,也不能撤销,执行此语句将永久删除该表)
 

猜你喜欢

转载自blog.csdn.net/c_pumpkin/article/details/81707396
今日推荐