1、删除
①若想删除表中全部数据,建议使用truncate
truncate特点:
1>truncate语句不能跟where条件(无法根据条件来删除,只能全部删除数据)
2> 同时自动编号恢复到初始值。
3>使用truncate删除表中所有数据要比delete效率高的多。
4> truncate删除数据,不触发delete触发器。
truncate table 表名
②一般删除的语法
delete from 表名 where ...
delete语句如果不加where条件,表示将表中所有数据都删除,加Where条件后,会按照Where条件进行删除。
若用delete语句删除含有自动编号的表数据时,自动编号不会恢复,会继续编号
2、约束
--为EmpId增加一个主键约束
alter table Employees add constraint PK_Employees_EmpId primary key(EmpId)
--非空约束,为EmpName增加一个非空约束(修改列)
alter table Employees alter column EmpName varchar(50) not null
--为EmpName增加一个唯一约束
alter table Employees add constraint UQ_Employees_EmpName unique(EmpName)
--为性别增加一个默认约束,默认为'男'
alter table Employees add constraint DF_Employees_EmpGender default('男') for EmpGender
--为性别增加一个检查约束,要求性别只能是:'男' or '女'
alter table Employees add constraint CK_Employees_EmpGender check(EmpGender='男' or EmpGender='女')
--为年龄增加一个检查约束:年龄必须在0-120岁之间,含岁与岁。
alter table Employees add constraint CK_Employees_EmpAge check(EmpAge>=0 and EmpAge<=120)
3、数据查询
-- * 表示显示所有列
-- 查询语句没有加where条件表示查询所有行
select *
from TblStudent
---只查询表中的部分列
select tsid,tsname,tsgender from TblStudent
--根据条件,只查询部分行(使用where条件筛选部分行显示)
select * from TblStudent where tsclassId=5
--为查询结果集中的列起别名
select tsid as 学生编号,tsname as 学生姓名,tsgender as 性别 from TblStudent
--并不是说select必须配合from一起来使用,可以单独使用select
select
当前系统时间=getdate()
select
班长='严守卫',
班花='待定',
班草='待定',
班主任='宋词'
select * from MyStudent
--distinct关键字 ,针对已经查询出的结果然后去除重复
select distinct * from TblStudent
select distinct tsname,tsgender,tsaddress from TblStudent
--Between…and … 在...之间,(闭区间,包含两个端点值)
--查询年龄在20-30岁之间的男学生
--查询math成绩在80-90分之间的所有学生
select * from TblScore where tMath between 80 and 90
4、排序
--按照年龄,降序排序
select * from TblStudent order by tsage desc --降序排序
--按照年龄,升序排序
select * from TblStudent order by tsage asc --升序排序
select * from TblStudent order by tsage --默认就是升序排序
select * from TblScore order by tMath desc
select * from TblScore order by tEnglish
--查询数学成绩最高的前5名
select top 5 * from TblScore order by tMath desc
--获取数学成绩最低的前5个
select top 5 * from TblScore order by tMath
--如果top后跟的不是数字,而是一个表达式一定要使用()把表达式括起来。
select top (2*2) * from TblScore order by tMath desc
--会查询出4条数据。
select top 35 percent * from TblScore order by tMath desc
--获取年龄最小的五个学生
select*from MyStudent
select top 5 * from MyStudent order by FAge asc
--获取年纪最大的10%的学生
select top 10 percent * from MyStudent order by FAge desc
5、聚合函数
1.sum()求总和
2.count() 求记录条数
3.max() 求最大值
4.min() 求最小值
5.avg() 求平均值
--统计出所有人的年龄的总和
select sum(age) as 年龄总和 from NewPerson(表名,此处是newperson)
--计算年龄最大的
select max(age) as 年龄最大 from NewPerson
--计算年龄最小的
select min(age) as 年龄最小 from NewPerson
--计算年龄的平均值
select avg(age) as 平均年龄 from NewPerson
--统计总共有多少条记录
select COUNT(autoId) as 记录的条数 from NewPerson
聚合函数的一些其他问题
1.聚合函数不统计空值
select count(tsid) from TblStudent
select avg(tsage) as 平均年龄 from TblStudent
select tsage from TblStudent where tsage is not null order by tsage desc --avg()也是不统计空值
select sum(tsage) from TblStudent --sum()对于null值,认为是0
2.如果使用聚合函数的时候,没有手动group by分组,那么聚合函数会把整个表中的数据作为一组来统计
6.模糊查询
--模糊查询
--通配符: _ % []
-- _表示任意的单个字符
--姓张,两个字
select * from MyStudent where FName like '张_'
--姓张,三个字的
select * from MyStudent where fname like '张__'
-- % 匹配任意多个任意字符
--无论姓名字数,只要第一个字符是'张'的就查询出来
select * from MyStudent where FName like '张%'
select * from MyStudent where FName like '张%'
--[] 表示筛选
select * from TblStudent
select * from TblStudent where tsname like '张[0-9]妹'
select * from TblStudent where tsname like '张_妹'
select * from TblStudent where tsname like '张[a-z]妹'
select * from TblStudent where tsname like '张[a-z0-9]妹'
select * from TblStudent where tsname like '张[^0-9]妹'
select * from TblStudent where tsname not like '张[0-9]妹'
update TblStudent set tsname=replace(tsname,'(女)','')
7.空值处理
-----------------空值处理--------------------------------------
select * from TblStudent
--查询所有年龄是null的同学信息
--null值无法使用=或<>来进行比较
--unknown
--判断null值必须使用is null或者is not null
select * from TblStudent where tsage is null --有结果显示
select * from TblStudent where tsage=null --无报错 但无任何结果显示
--查询所有年龄不是null的同学
select * from TblStudent where tsage <> null --无报错 但无任何结果显示
select * from TblStudent where tsage is not null --有结果显示
select * from TblStudent where tsage=25
select * from TblStudent where tsage<>25
--任何值与null进行计算,得到的结果还是null
select 2000+null