1、## SQL 数据操作语言 (DML)
SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。
这些查询和更新语句都来自 SQL 的 DML 部分:

SELECT - 从数据库表中获取数据
UPDATE - 更新数据库表中的数据
DELETE - 从数据库表中删除数据
INSERT INTO - 向数据库表中插入数据

2、## SQL 数据定义语言 (DDL)
SQL 的数据定义语言部分使我们有能力创建或删除表格。我们也可以定义索引(键),规定表之间的链接,以及施加表间的约束。
SQL 中最重要的 DDL 语句:
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引

3、## 下面从DML到DDL分别举例各场景的SQL语言应用
以下数据操作使用样例数据来源SQL语言学习样例数据

------------------------------------------------------数据库查询部分--------------------------------------------------------------

–检索单列、多列、所有列
select prod_name from Products;

select prod_id,prod_name,prod_price
from products;

select * from Products;

–检索不同的值
select vend_id from products;

select distinct vend_id
from products;

select distinct vend_id,prod_price
from products; /不能部分使用distinct/

–限制结果
select top 5 prod_name from products; --不同DBMS语句不同

–使用注释
/select prod_id,prod_name,prod_price
from products;
/
select prod_name from Products;

–子句(排序数据)
select prod_name
from Products
order by prod_name; --order by 是select语句中最后一条子句

–按多个列排序
select prod_id,prod_price ,prod_name
from products
order by prod_price ,prod_name;

–按列位置排序
select prod_id,prod_price ,prod_name
from products
order by 2,3;

–指定排序方向
select prod_id,prod_price ,prod_name
from products
order by prod_price desc;

select prod_id,prod_price ,prod_name
from products
order by prod_price desc,prod_name;

–where 子句的使用
select prod_name,prod_price
from products
where prod_price=3.49;

select prod_name,prod_price
from products
where prod_price<10;

–不匹配检查
select vend_id,prod_name
from products
where vend_id <>‘DLL01’;

select vend_id,prod_name
from products
where vend_id !=‘DLL01’;

–范围值检查
select prod_name,prod_price
from products
where prod_price between 5 and 10;

–空值检查
select prod_name
from products
where prod_price is null;

select cust_name
from Customers
where cust_email is null;

–and 操作符
select prod_id,prod_name,prod_price
from products
where vend_id=‘DLL01’ and prod_price <=4;

–or 操作符
select prod_name,prod_price
from products
where vend_id=‘DLL01’ or vend_id=‘BRS01’;

–求值顺序(列出价格为10美元及以上,且由DLL01或BRS01制造的所有产品)
select prod_name,prod_price
from products
where prod_price >=10 and (vend_id=‘DLL01’ or vend_id=‘BRS01’);

select vend_id,prod_name,prod_price
from products
where vend_id=‘DLL01’ or vend_id=‘BRS01’ and prod_price >=10 ; --and优先级比or高

–in操作符
select vend_id,prod_name,prod_price
from products
where vend_id in (‘DLL01’,‘BRS01’); --功能与or相当,且比一组or操作符执行更快

–not操作符
select prod_name
from products
where not vend_id =‘DLL01’; --此处功能与<>相同,与其他操作符组合使用,否定其后跟着的条件

–like操作符、"%"通配符
select prod_id,prod_name
from Products
where prod_name like ‘Fish%’;

select prod_id,prod_name
from Products
where prod_name like ‘%bean bag%’;

select prod_id,prod_name
from Products
where prod_name like ‘F%y’; --"%"不能匹配null

–"“通配符
select prod_id,prod_name
from Products
where prod_name like ‘__ inch teddy bear’; --”
"只能匹配一个字符

–"[]"通配符–指定字符集
select cust_contact
from Customers
where cust_contact like ‘[JM]%’
order by cust_contact;

select cust_contact
from Customers
where cust_contact like ‘[^JM]%’
order by cust_contact; --前缀字符^否定

select cust_contact
from Customers
where not cust_contact like ‘[JM]%’
order by cust_contact;

–拼接字段
select vend_name+’(’+vend_country+’)’
from Vendors
order by vend_name;

select rtrim(vend_name)+’(’+rtrim(vend_country)+’)’
from Vendors
order by vend_name; --去掉右边空格,trim(),rtrim()右,ltrim()左

–使用别名
select rtrim(vend_name)+’(’+rtrim(vend_country)+’)’
as vend_title
from Vendors
order by vend_name;

–执行算数计算
select prod_id,quantity,item_price
from OrderItems
where order_num =20008;

select prod_id,
quantity,
item_price,
quantity*item_price as expanded_price
from OrderItems
where order_num =20008;

–文本处理函数
select vend_name,UPPER(vend_name) as vend_name_upcase
from Vendors
order by vend_name; --转换为大写

–soundex()函数:发音比较
select cust_name,cust_contact
from Customers
where cust_contact = ‘Micheal Green’;

select cust_name,cust_contact
from Customers
where soundex(cust_contact) = soundex(‘Micheal Green’);

–日期和时间处理函数
select order_num
from Orders
where DATEPART(YY,order_date)=2012;

/Oracle中实现
select order_num
from Orders
where to_number(to_char(order_date,‘yyyy’))=2012;
/

select order_num,order_date
from Orders
where order_date between CONVERT(VARCHAR(23),‘2012-02-01’,121)
and convert(VARCHAR(23),‘12-31-2012’,121) ; --convert函数把日期转换为新数据类型的通用函数

–avg()函数:忽略值为null的行
select AVG(prod_price) as avg_prce
from Products;

select AVG(prod_price) as avg_prce
from Products
where vend_id=‘DLL01’;

–count()函数:count()包含空值(null)的所有行数;count(column)特定列有值的行计数,忽略null值
select COUNT(
) as num_cust
from Customers;

select COUNT(cust_email) as num_cust
from Customers;

–max()、min()函数:可用于数据、日期、文本,用于文本是,返回按该列排序后的最后一行(最前面一行)
select MAX(prod_price) as max_price
from Products;

select MIN(prod_price) as min_price
from Products;

–sum()函数:忽略值为null的行
select sum(quantity) as items_ordered
from orderitems
where order_num=20005;

select sum(item_price * quantity) as total_price
from orderitems
where order_num=20005;

–聚集不同值:distinct不能用于count(*)
select AVG(distinct prod_price) as avg_prce
from Products
where vend_id=‘DLL01’;

–组合聚集函数
select COUNT(*) as num_items,
MIN(prod_price) as price_min,
MAX(prod_price) as price_max,
AVG(prod_price) as price_avg
from Products;

–创建分组:group by子句中必须是检索列或有效的表达式,且必须出现在where之后,order by 之前
select vend_id,COUNT(*) as num_prods
from Products
group by vend_id;

–过滤分组having
select cust_id,COUNT () as orders
from Orders
group by cust_id
having COUNT(
)>=2; --having需与group by结合使用

select vend_id,COUNT() as num_prods
from Products
where prod_price >=4
group by vend_id
having COUNT(
)>=2; --having过滤分组,where过滤行

–分组和排序:group by 和 order by
select order_num,COUNT() as items
from OrderItems
group by order_num
having COUNT(
)>=3;

select order_num,COUNT() as items
from OrderItems
group by order_num
having COUNT(
)>=3
order by items,order_num;
/*select 子句及顺序
1 select 必须
2 from 仅在从表选择数据时使用
3 where 非必须
4 group by 仅在按组计算聚集时使用
5 having 非必须
6 order by 非必须 */

–利用子查询进行过滤
select cust_name,cust_contact–3
from Customers
where cust_id in (select cust_id --2
from Orders
where order_num in (select order_num --1
from OrderItems
where prod_id=‘RGAN01’ )) --非最有效方法

–作为计算字段使用子查询
select cust_name,
cust_state,
(select COUNT(*)
from Orders
where Orders.cust_id=Customers.cust_id) as orders
from Customers
order by cust_name

–创建联结
select vend_name,prod_name,prod_price
from Vendors,Products
where vendors.vend_id=products.vend_id; --等值联结

–内联结
select vend_name,prod_name,prod_price
from Vendors inner join Products
on vendors.vend_id=products.vend_id;

–联结多个表
select prod_name,vend_name,prod_price,quantity
from OrderItems,Products,Vendors
where products.vend_id=vendors.vend_id
and orderitems.prod_id=Products.prod_id
and order_num=20007;

–子查询及联结的比较
select cust_name,cust_contact
from Customers,Orders,OrderItems
where customers.cust_id=orders.cust_id
and orders.order_num=orderitems.order_num
and orderitems.prod_id=‘RGAN01’;

–使用表别名
select cust_name,cust_contact
from Customers as C,Orders as O,OrderItems as OI
where C.cust_id=O.cust_id
and O.order_num=OI.order_num
and OI.prod_id=‘RGAN01’;

–自联结
select c1.cust_id,c1.cust_name,c1.cust_contact
from Customers c1,Customers c2
where c1.cust_name=c2.cust_name
and c2.cust_contact=‘Jim Jones’; --一般情况下,联结的处理效率比子查询快

–自然联结
select C.*,O.order_num,O.order_date,
OI.prod_id,OI.quantity,OI.item_price
from Customers as C,Orders as O,OrderItems as OI
where C.cust_id=O.cust_id
and OI.order_num=O.order_num
and prod_id=‘RGAN01’;

–外联结
select c.cust_id,o.order_num
from Customers c inner join Orders o
on c.cust_id=o.cust_id; --内联结

select c.cust_id,o.order_num
from Customers c left outer join Orders o
on c.cust_id=o.cust_id; --左外联结

select c.cust_id,o.order_num
from Customers c right outer join Orders o
on c.cust_id=o.cust_id; --右外联结
/调整from或where子句中表的顺序,左外和右外可以转换/

select c.cust_id,o.order_num
from Customers c full outer join Orders o
on c.cust_id=o.cust_id; --全外联结

–交叉联结:没有where子句,结果产生笛卡尔积
select c.cust_id,o.order_num
from Customers c cross join Orders o;

select c.cust_id,o.order_num
from Customers c,Orders o; --产生结果与交叉联结结果一样

–使用带聚集函数的联结
select c.cust_id,
COUNT(o.order_num) as num_ord
from Customers C inner join Orders O
on c.cust_id=o.cust_id
group by c.cust_id;

select c.cust_id,
COUNT(o.order_num) as num_ord
from Customers C left outer join Orders O
on c.cust_id=o.cust_id
group by c.cust_id;

–创建组合查询
select cust_name,cust_contact,cust_email
from Customers
where cust_state in (‘IL’,‘IN’,‘MI’)
union
select cust_name,cust_contact,cust_email
from Customers
where cust_name = ‘Fun4All’; --自动删除重复行

select cust_name,cust_contact,cust_email
from Customers
where cust_state in (‘IL’,‘IN’,‘MI’)
or cust_name = ‘Fun4All’; --输出结果与上面union相同

–包含或取消重复的行
select cust_name,cust_contact,cust_email
from Customers
where cust_state in (‘IL’,‘IN’,‘MI’)
union all
select cust_name,cust_contact,cust_email
from Customers
where cust_name = ‘Fun4All’;

–对组合查询结果排序
select cust_name,cust_contact,cust_email
from Customers
where cust_state in (‘IL’,‘IN’,‘MI’)
union
select cust_name,cust_contact,cust_email
from Customers
where cust_name = ‘Fun4All’
order by cust_name,cust_contact; --只能存在一条order by子句,且放在最后

---------------------------------------------------------------数据库表操作部分-------------------------------------------------------

–1.插入数据(insert)

–插入完整的行
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’); --明确给出列名,该列允许null值或默认值

–插入检索出的数据
insert into Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
select cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email
from CustNew
where cust_id=‘1000000010’; --select的列名不要求匹配insert列名,使用的是select中列的位置

/insert into Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
select cust_id,
cust_country,
cust_contact,
cust_city,
cust_state,
cust_zip,
cust_name,
cust_address,
cust_email
from CustNew
where cust_id=‘1000000011’;
/

–从一个表复制到另一个表:运行中创建表
select *
into CustCopy
from Customers;

–2.更新和删除数据(update、delete)

–更新数据
update Customers
set cust_email=‘kim@thetoystore.com’
where cust_id=‘1000000005’;

update Customers
set cust_contact=‘Sam Roberts’,
cust_email=‘sam@toyland.com’
where cust_id=‘1000000006’;

update Customers
set cust_email=null
where cust_id=‘1000000005’; --删除某列的值

–删除数据
delete from Customers
where cust_id=‘1000000010’;

–更快的删除
truncate table custcopy;

–3.创建表

–创建表create
CREATE TABLE ProductsCopy
(
prod_id char(10) NOT NULL ,
vend_id char(10) NOT NULL ,
prod_name char(255) NOT NULL ,
prod_price decimal(8,2) NOT NULL ,
prod_desc varchar(1000) NULL
);

–使用null值
CREATE TABLE VendorsCopy
(
vend_id char(10) NOT NULL ,
vend_name char(50) NOT NULL ,
vend_address char(50) ,
vend_city char(50) ,
vend_state char(5) ,
vend_zip char(10) ,
vend_country char(50)
); --null为默认设置

–指定默认值default
CREATE TABLE OrderItemsCopy
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL default 1,
item_price decimal(8,2) NOT NULL
);

–更新表alter
alter table vendors
add vend_phone char(20);
–删除
alter table vendors
drop column vend_phone;

–删除表drop
drop table CustCopy;

–重命名表:使用存储过程sp_rename
EXEC sp_rename ‘VendorsCopy’, ‘vendorscop’
–重命名列
EXEC sp_rename ‘vendorscop.[vend_city]’, ‘city’, ‘COLUMN’
–注意: 更改对象名的任一部分都可能会破坏脚本和存储过程。

–4.使用视图

–创建视图create view
create view ProductCustomers as
select cust_name,cust_contact,prod_id
from Customers as C,Orders as O,OrderItems as OI
where C.cust_id=O.cust_id
and O.order_num=OI.order_num;

–查询
select cust_name,cust_contact
from ProductCustomers
where prod_id=‘RGAN01’;

–删除视图
drop view ProductCustomers;

–用视图重新格式化检索出的数据
create view VendorseLocations as
select rtrim(vend_name)+’(’+rtrim(vend_country)+’)’
as vend_title
from Vendors;

select * from VendorseLocations;

–用视图过滤不想要的数据
create view CustomerEMailList as
select cust_id,cust_name,cust_email
from Customers
where cust_email is not null;

–使用视图与计算字段
create view OrderItemsExpanded as
select prod_id,
quantity,
item_price,
quantity*item_price as expanded_price
from OrderItems;

–5.使用存储过程

–执行存储过程
execute AddNewProduct(‘JTS01’,
‘Stuffed Eiffel Tower’,
6.49,
‘Plush Stuffed toy with the text La
Tower Eiffel in red white and blue’); --未创建此存储过程不能执行

–创建存储过程
create procedure MailingListCount
as
declare @cnt integer
select @cnt = COUNT(*)
from Customers
where not cust_email is null;
return @cnt;
–调用
declare @ReturnValue int
execute @ReturnValue=MailingListCount;
select @ReturnValue;

–创建新订单
create procedure NewOrder @cust_id char(10)
as
declare @order_num integer --声明一个变量来存储订单号
select @order_num=MAX(order_num) --检索当前最大订单号
from Orders
select @order_num=@order_num+1 --订单号增加1
insert into Orders(order_num,order_date,cust_id) --插入新订单
values(@order_num,GETDATE(),@cust_id)
return @order_num; --返回订单号

–创建新订单(与上一条相同):自动生成订单号,使用全局变量@@IDENTITY
create procedure NewOrder @cust_id char(10)
as
insert into Orders(cust_id) --插入新订单
values(@cust_id)
select order_num =@@IDENTITY; --返回订单号

–调用
declare @OrderNum int
execute @OrderNum=NewOrder @cust_id=1000000001; --直接赋予顾客id,取系统当前时间
select * from Orders;

–6.管理事务处理
/*概念:
1.事务(transaction)指一组SQL语将句;
2.回退(rollback)只撤销指定SQL语句的过程;可以回退insert、update、delete;
3.提交(commit)指将未存储的SQL语句结果写入数据库表;
4.保留点(savepoint)指事务处理中设置的临时占位符(placeholder),可以对它发布回退(与回退整个事务处理不同)。 */

–使用rollback
begin transaction
delete from OrderItems;
rollback transaction;

–使用commit
begin transaction
delete OrderItems where order_num=20001
delete Orders where order_num=20006
commit transaction; --必须两条delete都成功才提交

–使用保留点(占位符)
save transaction delete1;
rollback transaction delete1;

begin transaction
insert into Customers(cust_id,cust_name)
values(‘1000000012’,‘Toys Emporium’);
save transaction StartOrder; --保留点
insert into Orders(order_num, order_date, cust_id)
values(20101,‘2001/12/1’,‘1000000012’);
if @@ERROR <> 0
rollback transaction StartOrder; --变量@@ERROR返回非0的值,表示有错误发生
insert into OrderItems(order_num,order_item,prod_id,quantity,item_price)
values(20102,1,‘BR01’,100,5.49);
if @@ERROR <> 0
rollback transaction StartOrder;
insert into OrderItems(order_num,order_item,prod_id,quantity,item_price)
values(20100,2,‘BR03’,100,10.99);
if @@ERROR <> 0
rollback transaction StartOrder;
commit transaction ;
–无法回滚 StartOrder。找不到该名称的事务或保存点。???

/*删除新增数据重新插入数据
delete from OrderItems
delete from Orders
delete from Customers

INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES(‘1000000001’, ‘Village Toys’, ‘200 Maple Lane’, ‘Detroit’, ‘MI’, ‘44444’, ‘USA’, ‘John Smith’, ‘sales@villagetoys.com’);
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
VALUES(‘1000000002’, ‘Kids Place’, ‘333 South Lake Drive’, ‘Columbus’, ‘OH’, ‘43333’, ‘USA’, ‘Michelle Green’);
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES(‘1000000003’, ‘Fun4All’, ‘1 Sunny Place’, ‘Muncie’, ‘IN’, ‘42222’, ‘USA’, ‘Jim Jones’, ‘jjones@fun4all.com’);
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES(‘1000000004’, ‘Fun4All’, ‘829 Riverside Drive’, ‘Phoenix’, ‘AZ’, ‘88888’, ‘USA’, ‘Denise L. Stephens’, ‘dstephens@fun4all.com’);
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
VALUES(‘1000000005’, ‘The Toy Store’, ‘4545 53rd Street’, ‘Chicago’, ‘IL’, ‘54545’, ‘USA’, ‘Kim Howard’);

INSERT INTO Orders(order_num, order_date, cust_id)
VALUES(20005, ‘2012-05-01’, ‘1000000001’);
INSERT INTO Orders(order_num, order_date, cust_id)
VALUES(20006, ‘2012-01-12’, ‘1000000003’);
INSERT INTO Orders(order_num, order_date, cust_id)
VALUES(20007, ‘2012-01-30’, ‘1000000004’);
INSERT INTO Orders(order_num, order_date, cust_id)
VALUES(20008, ‘2012-02-03’, ‘1000000005’);
INSERT INTO Orders(order_num, order_date, cust_id)
VALUES(20009, ‘2012-02-08’, ‘1000000001’);


INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 1, ‘BR01’, 100, 5.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 2, ‘BR03’, 100, 10.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 1, ‘BR01’, 20, 5.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 2, ‘BR02’, 10, 8.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 3, ‘BR03’, 10, 11.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 1, ‘BR03’, 50, 11.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 2, ‘BNBG01’, 100, 2.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 3, ‘BNBG02’, 100, 2.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 4, ‘BNBG03’, 100, 2.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 5, ‘RGAN01’, 50, 4.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 1, ‘RGAN01’, 5, 4.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 2, ‘BR03’, 5, 11.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 3, ‘BNBG01’, 10, 3.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 4, ‘BNBG02’, 10, 3.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 5, ‘BNBG03’, 10, 3.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 1, ‘BNBG01’, 250, 2.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 2, ‘BNBG02’, 250, 2.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 3, ‘BNBG03’, 250, 2.49); */

–7.使用游标

–创建游标
declare CustCursor CURSOR
for
select * from Customers
where cust_email is null;

–使用游标
OPEN CustCursor;
FETCH …

–关闭游标
CLOSE CustCursor;

–例子
declare CustCursor CURSOR --创建
for
select * from Customers
where cust_email is null
declare @cust_id char(10),
@cust_name char(50),
@cust_address char(50),
@cust_city char(50),
@cust_state char(5),
@cust_zip char(10),
@cust_country char(50),
@cust_contact char(50),
@cust_email char(255)
OPEN CustCursor --使用
fetch next from CustCursor --检索一行并保存到声明的变量中
into @cust_id,@cust_name,@cust_address,
@cust_city,@cust_state,@cust_zip,
@cust_country,@cust_contact,@cust_email
while @@FETCH_STATUS = 0 --循环,取不出更多行时终止(退出循环)
begin

fetch next from CustCursor
into @cust_id,@cust_name,@cust_address,
@cust_city,@cust_state,@cust_zip,
@cust_country,@cust_contact,@cust_email
update Customers --使用具体的处理代码,
set cust_email=‘kim@thetoystore.com’ --为什么每一行都插入了???
end

close CustCursor;

–8.约束

–主键
/满足条件:
1.任意两行的主键值都不同;
2.每行都具有一个主键值(即列中不允许null值);
3.包含主键值的列从不修改或更新;
4.主键值不能重用。如果从表中删除某一行,其主键值不分配给新行。
/

–创建主键
CREATE TABLE VendorsCopy
(
vend_id char(10) NOT NULL PRIMARY KEY, --系统随机主键名
vend_name char(50) NOT NULL ,
vend_address char(50) NULL ,
vend_city char(50) NULL ,
vend_state char(5) NULL ,
vend_zip char(10) NULL ,
vend_country char(50) NULL
);

–删除主键
alter table VendorsCopy
drop constraint PK__VendorsC__32BF447E22AA2996;

–创建主键
alter table VendorsCopy
add constraint pk_vendorscopy
primary key(vend_id);

–外键
–创建外键
CREATE TABLE OrdersCopy
(
order_num int NOT NULL primary key,
order_date datetime NOT NULL ,
cust_id char(10) NOT NULL references Customers(cust_id)
);

alter table OrdersCopy
add constraint fk_orderscop
foreign key(cust_id) references Customers(cust_id); --可重复建外键???

–唯一约束
/*与主键的区别:
1.表可包含多个唯一约束,但每个表只允许一个主键;
2.唯一约束可包含null值;
3.唯一约束列可修改或更新;
4.唯一约束列的值可重复使用;
5.与主键不一样,唯一约束列不能用来定义外键。 */

–创建唯一约束
CREATE TABLE Employees
(
emp_id char(10) NOT NULL PRIMARY KEY,
emp_name char(50) NOT NULL ,
emp_sex char(2) NULL ,
emp_idcard char(18) NOT NULL UNIQUE,
emp_nation char(50) NULL
);

alter table Employees
add constraint UQ_idcard
unique (emp_idcard); --重复???

–检查约束
/*用途:
1.检查最大值最小值,如防止0个物品的订单(及时0是合法数);
2.指定范围,如保证发货日期大于等于今天的日期,但不超过今天起一年后的日期;
3.值允许特定的值,如性别字段中值允许M或F。 */

–创建检查约束
CREATE TABLE OrderItemsCopy
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL check (quantity > 0),
item_price decimal(8,2) NOT NULL
);

alter table Employees
add constraint CK_sex
check (emp_sex like ‘[MF]’); --[]通配符表示数据集

–索引:用于数据过滤及排序
–创建
create index prod_name_ind
on products (prod_name);

–触发器:用于update,insert,delete
/*用途:
1.保证数据一致;
2.基于某个表的变动在其他表上执行活动;
3.进行额外的验证并根据需要回退数据;
4.计算计算列的值或更新时间戳。 */

–创建
create trigger customer_state
on customers
for insert,update
as
update Customers
set cust_state = UPPER(cust_state)
where Customers.cust_id=(select cust_id from inserted);

–验证
insert into Customers
values(‘1000000010’,
‘Toy Land’,
‘123 Any Street’,
‘Beijing’,
‘bj’,
‘11111’,
‘CHINA’,
null,
null);

–数据库安全:grant(授权) 、revoke(回收权限)

/*
1)授权命令 grant
Grant <权限> on 表名[(列名)] to 用户 With grant option
或 GRANT <权限> ON <数据对象> FROM <数据库用户>

数据对象可以是表名或列名
权限表示对表的操作,如select,update,insert,delete
注:授权命令是由数据库管理员使用的,若给用户分配权限时带With grant option子句,
则普通用户获权后,可把自己的权限授予其他用户。

2)回收权限 revoke
REVOKE <权限> ON <数据对象> FROM <数据库用户名> */

SQL语言入门学习,这一篇就够了相关推荐

  1. 渗透测试工程师零基础学习教程2023年最新版,想入门学习这一篇就够了。

    什么是渗透测试? 渗透测试是指通过模拟黑客攻击的方式,评估一个系统或网络的安全性能,以发现潜在的漏洞或安全弱点.渗透测试通常包括对目标系统或网络进行多种攻击方式的测试,如密码破解.漏洞利用.社会工程学 ...

  2. MS SQL 的入门学习

    SQL 语言入门教程 第一课简介 SQL 是英文Structured Query Language 的缩写,意思为结构化查询语言. SQL 语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANS ...

  3. lua语言入门学习(一)搭建基本的环境并实现运行

    lua语言入门学习 本篇文章用来记录自己写lua语言的过程 文章目录 lua语言入门学习 前言 一.lua语言是什么? 二.使用步骤 1.搭建环境 2.开始运行 总结 前言 最近来到了一家网络游戏公司 ...

  4. c语言10个人 三向成绩,C语言入门学习精华:这样学习C语言最有效

    C语言入门学习精华:这样学习C语言最有效 c语言死了吗? 本材料描述了使用C语言的高级技能,并努力将您的C语言能力从"基本"提升到"高级".然而,学习态度比学习 ...

  5. c语言入门自学免费app,C语言入门学习最新版下载-C语言入门学习app手机版v1.0.2 安卓版-腾飞网...

    C语言入门学习app手机版是一款c语言编程自学软件,零基础也可以学习,里面有海量教学视频,针对c语言不同程度的讲解都囊括其中.随时随地学习编程都可以,不用担心自己没有基础.还支持在手机上敲代码编程哦. ...

  6. 郑州计算机c语言培训机构,c语言入门学习选郑州哪家计算机专业学校

    C语言是一门稀缺的贵族语言,会C语言的被IT界高看,C语言的地位很高,学C语言确实难,但是有正确的方法成为C程序员是非常有效的,会C语言相当于拥有了一定地位,未来非常有前景,那么如何入门C语言呢?选择 ...

  7. PostgreSQL修炼之道之SQL语言入门(四)

    目录 第三章 SQL语言入门(二) 3.4 查询语句 3.4.1 单表查询语句 3.4.2 过滤条件的查询 3.5 其他SQL语句 3.5.1 INSERT INTO... SELECT语句 3.5. ...

  8. c语言入门自学手机版,c语言入门自学app下载-C语言入门学习 安卓版v1.0.2-PC6安卓网...

    C语言入门学习app是一款C语言零基础自学软件.C语言入门自学app提供海量精品学习资源,从小白入门到基础进阶都有,帮你轻松学习编程. 软件介绍 C语言入门学习app是一款专业的编程入门学习App,致 ...

  9. c语言入门自学手机版,C语言入门学习app下载-C语言入门学习app最新版下载 V1.0.2-友情手机站...

    C语言入门学习app是一款0基础自学软件,这里有着丰富C语音相关课程学习,大家在这里是可以便捷搜索查找,随时都是可以找到适合感兴趣课程学习,都是一些优质课程知识提供大家,学员在这里是可以高效学习,海恩 ...

  10. c语言入门自学手机版,C语言入门学习软件下载-C语言入门学习手机版v1.0.2 - 起点软件园...

    C语言入门学习是一款最新推出上线于安卓手机平台的专业编程学习软件,C语言入门学习app收录了海量入门视频课程,清晰易懂的详细讲解专为刚入门学习C语言的小白量身打造,不同章节都有相应的习题,根据习题成绩 ...

最新文章

  1. 几经沉浮,人工智能前路何方?
  2. python 编译exe
  3. 2017-11-29 黑盒测试实践(小组作业)小组工作记录
  4. linux内核计数函数,linux中的内核引用计数器
  5. java ftp遍历所有子文件_Java 遍历指定文件夹及子文件夹下的文件
  6. yarn:info There appears to be trouble with your network connection. Retrying...
  7. 难以回答的问题:注册监听器有什么用?
  8. it试用评估_it员工转正自我评价
  9. css text-transform实现英文字母或拼音大小写转换
  10. php实现室内地图导航,叠加室内地图-室内地图-示例中心-JS API 示例 | 高德地图API...
  11. android组件搭配
  12. php 无父类 用parrent,Leaflet_扩展Leaflet:类(2017-10-26)
  13. 洛谷P1548 [NOIP1997 普及组] 棋盘问题
  14. 计算机系统时钟中断,什么是时钟中断时钟中断的举例
  15. java 打开word(docx)替换内容,并插入图片(盖章)
  16. Qt OpenGL(08)通过递归细分正二十面体逼近球面
  17. 金士顿固态硬盘不认盘修复_上海金士顿固态硬盘维修 上海台电固态硬盘不识别数据恢复中心...
  18. 守望者的逃离(贪心,动态规划)
  19. tkinter标签Lable组件的详细讲解
  20. 成长路上最好的伙伴——敌人

热门文章

  1. 如何使用matlab进行音频信号处理+代码编译中会遇到的问题+matlab安装包
  2. Java菜鸟教程怎么用_菜鸟教程 Java 学习笔记 (基础教程)
  3. WebService 入门教程(Java)
  4. oracle 后台执行sql,后台执行oracle sql脚本
  5. STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
  6. 解决visio图片生成PDF有留白的问题
  7. wordpress数据字典
  8. python 遗传算法_Python实现遗传算法的代码
  9. cocos2dx-lua 骨骼动画spine使用心得(cocos2dx版本 3.17 spine版本3.6.53)
  10. c语言进程池原理及实现