614 数据库高级

--================先通过设计器手动添加,然后通过代码来添加====
 
--============手动增加约束==========
--手动删除一列(删除EmpAddress列)
alter table Employees drop column EmpAddress
go
 
--手动增加一列(增加一列EmpAddr varchar(1000))
alter table Employees add EmpAddr varchar(1000)
 
--手动修改一下EmpEmail的数据类型(varchar(200))
alter table Employees alter column EmpAddr varchar(200)
go
 
--为EmpId增加一个主键约束
alter table Employees 
add constraint PK_Employees_EmpId primary key(EmpId)
 
--非空约束,为EmpName增加一个非空约束,修改列为not null
--增加一个非空约束其实就是修改列
alter table Employees 
alter column EmpName varchar(50) not null
go
 
--为EmpName增加一个唯一约束
alter table Employees add constraint
UQ_Employees_EmpName unique(EmpName)
Go
 
 
--为性别增加一个默认约束,默认为'男'
alter table Employees add constraint
DF_Employees_EmpGender default('') for EmpGender 
 
go
 
 
 
--为年龄增加一个检查约束:年龄必须在0-120岁之间,含0岁与120岁。
alter table Employees add constraint
CK_Emplpoyees_EmpAge check(empage>=0 and empage<=120)
go
 
--增加外键约束,表Employee中有一列EmpDeptId引用TblDepartment表中的DeptId
 
alter table Employees add  DeptId int not null
alter table Department add constraint
PK_Department_DeptId primary key(DepId)
 
alter table Employees add constraint
FK_Employees_Department foreign key(DeptId) 
references Department(DepId) on delete cascade
 
 
--先删除原来的外键
alter table Employees drop constraint FK_Employees_Department
 
 
 
--删除某个名字的约束
 
--一条语句删除多个约束,约束名用 逗号 隔开
alter table Employees drop constraint
 FK_Employees_Department,
 CK_Emplpoyees_EmpAge,
UQ_Employees_EmpName
 
--用一条语句为表增加多个约束。 
alter table Employees add 
constraint UQ_Employees_EmpName unique(EmpName),
constraint CK_Emplpoyees_EmpAge check(EmpAge>=0 and EmpAge<=120)
 
--===============给列起名的三种方式
 
select
Fid as 学号,
fname as 姓名,
fage 年龄,
性别=fgender,
数学成绩=fmath,
英语成绩=fenglish
from MyStudent
 
 --获得系统时间
select getdate()
 
--top 数字后加percent表示百分比,如果百分比最后计算出来的条数中有小数,则直接进位。
select top 30 percent * from MyStudent order by fage desc
 
--===================distinct去除重复数据=======
select distinct uname,uage from Test2
 
--==============数据库中的自带函数
--查询数学成绩中,最高分是多少分
select max(fMath) as 数学成绩最高分 from MyStudent
 
select min(fMath) as 数学成绩最低分 from MyStudent
 
--求总分,求和
select sum(fmath) as 总分 from MyStudent
 
--求班级中的总的记录条数(总人数)
select count(*) as 班级总人数 from MyStudent
--计算平均分的时候对空值不处理
select avg(uscore) from Test3
--count计算的时候也不考虑空值
select count(uscore) from Test3
--求平均分
select avg(fmath) as 平均分 from MyStudent
--====================sql 中的语法
--推荐使用between ... and ...
--between 18 and 24 and也相当于是>=18 and <=24
 
--在1,2,3中选择任意一个班级都返回true
--可以用in()语法替换多个or
select * from TblStudent
where tsclassid in (1,2,3)
 
--------------------------模糊查询=================================
--通配符%表示:任意多个任意字符
select * from Mystudent
where fname like '赵%'
 
--通配符 _ :表示任意的单个字符。
select * from Mystudent
 
 
--表示x与y之间只要不是'磊'或'伟'的任意单个字符都可以。
--[]通配符,表示中括号中的任意个字符,只选一个匹配。
--[^]表示除了中括号中的任意单个字符。
 
where fname like '赵__'select * from MyStudent
where fname like '%[磊伟]%'
 
select * from MyStudent
where fname like 'x[^磊伟]y'
 
 
--查询出姓名中包含百分号的
--用[]括起来,就表示转义了。
select * from MyStudent
where fname like '%[%]%'
 
select * from MyStudent
where fname like '%[_]%'
 
 
--查询出所有不姓赵的同学
select * from Mystudent
where fname not like '赵%' 
 
--1.请查询出学生表中所有数学成绩为null的人的信息
--null在数据库中表示unkonw(不知道),判断一个值是否为null,也就不能用=或者<>来判断
 
--查询所有fmath为null的值。
select * from MyStudent where fmath is null
 
--查询所有fmath为非null的值。
select * from MyStudent where fmath is not null
 
--null值与任何数据运算后得到的还是null值。
--就像不知道与1相加结果还是不知道。
 
--注意:同一列上的数据,数据类型必须一致,如果不一致就会报错。所以要求自己定义查询的时候,注意同一列数据类型一致。
--这里的'缺考',只存在与查询出的结果集中,表中的数据没有变化
数学成绩=isnull(cast(fmath as varchar(50)),'缺考')
 
 
--=================sql中的执行顺序===================
 
select 
--distinct / top 之类的关键字(这些都是一些现实的选项)
fgender as 性别,  --5>选择列
count(*) as 人数
from MyStudent --1>先从MyStudent表中拿到数据(全部数据的一个结果集)
where fage>30  --2>从MyStudent的数据中筛选出所有年龄大于30岁的人的信息(新结果集,都是年龄大于30的)
group by fgender --3>按照性别分组,分完组以后又得到一个新结果集(分组后的结果)
having count(*)>355 --4>基于分组以后的结果集,然后在筛选,筛选出那些组中记录大于500的组。
order by  人数 desc--6>最后把显示出来的结果排序
 
--=================================================
select 
--tsname as 姓名,
tsclassid as 班级Id,
count(*) as 班级人数
from TblStudent
where tsgender=''
group by TSClassId
--一般分组语句group by 都要与聚合函数配合使用,如果没有聚合函数,分组的意义不大。
 
--当在select查询语句中出现聚合函数时,这时不能在select查询中再出现其他列,除非:该列也在group子句中出现或者也在聚合函数中出现。
 
--========================类型转换============================、
--select 100+'hello'
--select 'hello'+100
 
select 100+'100'
 
select cast(100 as varchar(10))+'hello'
select convert(varchar(10),100)+'hello'
 
select convert(varchar(50),getdate())
 
select convert(varchar(50),getdate())
select convert(varchar(50),getdate(),101)
select convert(varchar(50),getdate(),100)
 
------------------------联合---------------------------------------
--union联合的作用就是将多个结果集并到了一起
select fname,fage from mystudent where fid>999
union all
select tsname,tsage from itcastcn..tblstudent where tsid>12
 
--===================================================
--当联合的时候注意:
--1.多个结果集中的列的数据类型必须一一对应
--2.列的个数必须一样
--联合的时候如果只写union,则会去除重复数据,如果写union all则不会去除重复数据。由于一般情况下我们都没有重复数据,所以也不需要去除,所以我们一般建议使用union all。
 
 
--================字符串函数=================
select len('哈哈hello') --返回字符的个数
select datalength('哈哈hello') --返回是字节个数
 
select lower('AaBb')
 
select UPPeR('AaBb')
 
select '==========='+rtrim(ltrim('     aaa       '))+'==============='
 
--从左边数,截取10个字符
select left('hello welcome to China.',10)
 
--从右边数,截取10个字符
select right('hello welcome to China.',10)
 
--从索引为2的开始,截取5个字符。
--注意:1.索引从1开始数。2.含当前索引。
select substring('abcdefghijklmn',2,5)
 
select replace('fjdsalfafdjaslkfjdsakjflksafjdsfjdslkfjdsljf','f','')
select replace(username,'','')
 
=================日期函数===============
select get date()
print convert(varchar(50),)
 
GETDATE() :取得当前日期时间 
DATEADD (datepart , number, date ),计算增加以后的日期。参数date为待计算的日期;参数number为增量;参数datepart为计量单位,可选值见备注。DATEADD(DAY, 3,date)为计算日期date的3天后的日期,而DATEADD(MONTH ,-8,date)为计算日期date的8个月之前的日期 。(入职一年以上的员工发1000$)
DATEDIFF ( datepart , startdate , enddate ) :计算两个日期之间的差额。 datepart 为计量单位,可取值参考DateAdd。
统计不同入学年数的学生个数:
select DateDiff(year,sInDate,getdate()),count(*) from student Group by DateDiff(year,sInDate,getdate())
DATEPART (datepart,date):返回一个日期的特定部分 
      year(date)   计算日期返回的年份
Datepart可选值
取值别名说明
yearyy,yyyy年份
quarterqq,q季度
monthmm,m月份
dayofyeardy,y当年度的第几天
daydd,d日
weekwk,ww当年度的第几周
weekdaydw,w星期几
hourhh小时
minutemi,n分
secondss,s秒
millisecondms毫秒
 
原文地址:https://www.cnblogs.com/cheshui/p/2610055.html