顺序结构有点凌乱,敬请见谅
其他未声明的列取默认值
insert into '表'('列名1','列名2'...) values ('数据1','数据2'...)
insert into region(RegionID,RegionDescription) values(5,'Center')
按照属性(列)的顺序填写数据。
insert into '表' values ('数据1','数据2'...) --这里得包含所有的列的数据
insert into Sell values('A1001','2019/11/18',6000,-10,'JY0001','1301')
delete from '表' where '条件'
delete from customers where City='London'
update ‘表’ set ‘修改’ where ‘条件’
update orders set Freight=Freight*0.95 where EmployeeID=3 or EmployeeID=4
select * from '表'
select * from orders
group by
select CustomerID,avg(Freight) as avgFreight
from orders
group by CustomerID
order by
默认排序从小到大,desc从大到小
select productid
from orderdetails
order by quantity
top x 前x条记录
select top 10 b.Read_no,count(*)
from dbo.Borrow b
group by b.Read_no
top x percent 前x%条记录
select top 20 percent * from table
where是对当前表的条件控制
having是对新的列的条件控制
--5.找出当前至少借阅了2本图书的读者借书证号、姓名及所在单位。
select a.Read_no as 借书证号 ,b.Read_name as 姓名, b.Read_dept as 单位
from dbo.Borrow a ,dbo.Reader b
where a.Read_no=b.Read_no
group by a.Read_no,b.Read_name,b.Read_dept
having count(*) >=2
create table Inventory(
Goo_no char(8) not null,
price money not null,
num int not null,
In_time date not null
)
alter table 表名 add 新增字段名 字段类型 默认值…
alter table [stu] add [jj] int default 0
ALTER TABLE 表名 DROP COLUMN 字段名;
alter table [stu] drop column [jj]
alter table 表名 alter column 字段名 type
alter table [stu] alter column [jj] VARCHAR(200)
exec sp_rename ‘表名.原字段名’,‘新字段名’
exec sp_rename 'stu.jj','gg'
if exists (select * from sysobjects where name=‘Purchase_bak’)
drop table Purchase_bak
select * into StartPurchase from Purchase
先建立表的结构,再插入
select * into Purchase_bak from Purchase where 1=2
insert into Purchase_bak select * from Purchase where Pur_num =0
select * from Purchase_bak
drop table 表名
以插入数据为例
create trigger Tri_InsterBorrow
on dbo.Borrow for insert
as
print('run is success!')
declare @Rno char(10)
declare @Bno char(10)
set @Rno=(select i.Read_no from inserted i)
set @Bno=(select i.Book_no from inserted i)
if (select r.Card_status from dbo.Reader r where r.Read_no=@Rno ) like '挂失'
begin
print('卡丢失,借书失败')
rollback --回滚
end
else if (select b.B_status from dbo.Book b where b.Book_no=@Bno ) like '已借'
begin
print('书已经被借,借书失败')
rollback
end
else
begin
print('借书成功')
update dbo.Book set B_status = '已借' where Book_no=@Bno
end
go
-- 10、创建视图v1,要求查询'佳能公司',2019年12月份商品的销售情况和每一笔销售的纯利润,并对视图v1加密:
if exists (select * from sysobjects where name='V1')
drop view V1
go
create view V1 with encryption
as
select Sell_no as 销售编号,
Sell.Goo_no as 商品编码,
Goo_name as 商品名称,
Pur_price as 进货价,
Sell_prices as 销售价,
Sell_num as 销售数量,
(Sell_prices - Pur_price) * Sell_num as 销售纯利润
from (
Sell
inner join Purchase
on Sell.Goo_no = Purchase.Goo_no
)
inner join Goods
on Goods.Goo_no = Sell.Goo_no
where YEAR(Sell.Sell_date)=2019 and MONTH(Sell.Sell_date)=12
and Goods.Goo_no in (
select Goods.Goo_no
from Goods --
where Goods.Pro_name = '佳能公司'
)
go
select * from V1 --查看视图
if not exists (SELECT * FROM sysobjects WHERE name='PK_Purchase' and xtype='PK')
ALTER TABLE Purchase add constraint PK_Purchase primary key(Pur_no)
if not exists (SELECT * FROM sysobjects WHERE name='FK_Purchase1' and xtype='F')
ALTER TABLE Purchase add constraint FK_Purchase1 foreign key(Goo_no) references Goods(Goo_no)
if not exists (SELECT * FROM sysobjects WHERE name='FK_Purchase2' and xtype='F')
ALTER TABLE Purchase add constraint FK_Purchase2 foreign key(Emp_no) references Employees(Emp_no)
if not exists(select * from sysobjects where name='check_Sell')
begin
alter table Sell
with check --该约束是否应用于现有数据,with check表示应用于现有数据,with nocheck表示不应用于现有数据
add constraint check_Sell
check
not for replication --当复制代理在表中插入或更新数据时,禁用该约束。
(Sell_no like '[A-Z,a-z]%');
end
if not exists(select * from sysobjects where name='IX_EmployeesTeNo' and xtype='UQ')
alter table Employees add constraint IX_EmployeesTeNo unique (Empp_phone)
if not exists (select * from sysobjects where name='DF_Sell_date' and xtype='D')
alter table Sell add constraint DF_Sell_date default(getdate()) for Sell_date
最基本的一些函数,如avg,sum,min,max,count。
select CustomerID,avg(Freight) as avgFreight
from orders
group by CustomerID
在 sysobjects 中查询
if exists (select * from sysobjects where name='Purchase_bak')
定义用declare, @变量名 即使用, set 可以修改变量的值
declare @num int
set @num=(select inserted.Sell_num from inserted)
case when ‘条件’ then ‘条件真的时候的值’ else ‘条件为假的时候的值’ end
--12.统计每位读者借阅数据结构、C++程序设计、SQL编程和Java Web 应用开发四本书借阅情况。
select a.Read_no as 卡号 ,
sum (case when c.Bname='数据结构' then 1 else 0 end )as 'C++程序设计' ,
sum (case when c.Bname='C++程序设计' then 1 else 0 end )as 'C++程序设计' ,
sum (case when c.Bname='SQL 编程' then 1 else 0 end )as 'SQL 编程' ,
sum (case when c.Bname='Java Web 应用开发' then 1 else 0 end )as 'Java Web 应用开发'
from dbo.Borrow a ,dbo.Catalog c,dbo.Book b
where b.Book_no=a.Book_no and b.ISBN=c.ISBN
group by a.Read_no
year,month,day返回值为int行,分别返回哪年,哪月,哪日
select Sell.Emp_no,sum(Sell_num *Sell_prices) as sum_sale
from Sell
where YEAR(Sell.Sell_date)=2019 and MONTH(Sell.Sell_date)=12
group by Sell.Emp_no
直接用符号进行比较
select Employees.Emp_no as 员工号,sum(Sell.Sell_num*Sell.Sell_prices) as 销售总金额
from Employees,Sell
where Sell.Emp_no=Employees.Emp_no and Sell.Sell_date>=('2019-9-1') and Sell.Sell_date<('2020-1-1')
group by Employees.Emp_no
order by sum(Sell.Sell_num*Sell.Sell_prices) desc