sql的语句汇总

1.创建学生表

create table students

(Sno char(9)primary key,

   Sname char(20),

  Sage smallint,

  Sdept char (20)

2.查询学生表

select *from students

3.插入学生表信息(insert into)

insert into students(Sno,Sname,Sage,Sdept) values('001','张三','男','19','软件工程系'),

('002','李四','男','20','软件工程系'),

('003','王五','女','19','数字媒体系'),

('004','陈六','女','19','数字媒体系'),

('005','郭七','女','18','动漫系')

4.条件查询

select *fromstudents where Sno=‘004’     ----显示所有列表

select Sname from students where Sno='004'   ----只显示姓名的列表

select Sname, Ssex,*from students where Sno='004'  ----显示所有,前面两列显示姓名和性别

select *from students where Sage in (18,20)     ----查找年龄包含(18,20)

select *from students where Sage not in (18,20)    ----查找年龄不包含(18,20)

select *from students where Sno=‘004’ or Sage=‘18’   ----查找学号为004或者年龄为18的语句,符合任意条件都可以显示

select *from students where Sno=‘004’ and Sage=‘18’ ----- 查找学号为004并且年龄为18的语句,两个条件需要同时符合

select count (Sno)from students where Sno is not null    ----查找学号不为空的学号的数量

select distinct (Sno)from students where Sno is not null  -----查找学号不为空,且唯一的语句

select countdistinct(Sno))from students where Sno is not null          -----同时使用count和distinct ,显示学号唯一值的数量

5.模糊查询 (like)

select *from students where Sdept like '%软件%'      ---模糊查找,系别为软件的记录

select *from students where Sdept like '_件%'              ----忘记前面的单词是什么,只记得后面的

6.更新语句(update.....set)

update students set Ssex=‘男’ where Sname='郭七'

7.分组语句(group by)

8.排序语句(order by)

   asc(升序排列),desc(降序排列)

9.重命名(as)

select  列表名 as 新列表名 from  表名

10.having 的用法

having的用法与where相似,表示条件,having主要与合计函数一起使用sum()max()average().....

11.alter   table对表进行增加、删除、修改列

alter table 表名

add 列表名 类型            -----增加列

alter table 表名

drop column 列表名      --------删除列

alter table 表名

alter table 表名

alter column  列表名  类型 - ---修改类型

12.备份表

select  *into B from A  将A备份给B

13.union 并操作

select *from table_1 where 条件 union select *from table_2 where 条件

注意:select必须拥有相同数量的列,列也必须拥有相同的数据类型,select语句中列的顺序也必须相似,适用用于主表和子表的查询

14.存储过程

create proc proc_student
@sex varchar(2)
as
select *from students where Ssex=@sex

用exec调用存储过程

exec proc_student @sex='男'

--------------------------------------------------------------------------------------------------------------------------------------------

触发器的禁用于恢复

禁用:
ALTER TABLE trig_example DISABLE TRIGGER trig1

GO
恢复:
ALTER TABLE trig_example ENABLE TRIGGER trig1
GO
--禁用某个表上的所有触发器
ALTER TABLE 你的表 DISABLE TRIGGER all
--启用某个表上的所有触发器
ALTER TABLE 你的表 enable TRIGGER all
--禁用所有表上的所有触发器
exec sp_msforeachtable 'ALTER TABLE ? DISABLE TRIGGER all'
--启用所有表上的所有触发器
exec sp_msforeachtable 'ALTER TABLE ? enable TRIGGER all'



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值