东莞网站制作与网站建设,怎么建立一个平台,wordpress 点击媒体库,做网站要坚持--先通过设计器手动添加#xff0c;然后通过代码来添加--手动增加约束--手动删除一列(删除EmpAddress列)
alter table Employees drop column EmpAddress
go--手动增加一列(增加一列EmpAddr varchar(1000))
alter table Employees add EmpAddr varchar(1000)--手动修改一下Emp…--先通过设计器手动添加然后通过代码来添加 --手动增加约束 --手动删除一列(删除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(empage0 and empage120)
go--增加外键约束表Employee中有一列EmpDeptId引用TblDepartment表中的DeptIdalter 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 constraintFK_Employees_Department,CK_Emplpoyees_EmpAge,
UQ_Employees_EmpName--用一条语句为表增加多个约束。
alter table Employees add
constraint UQ_Employees_EmpName unique(EmpName),
constraint CK_Emplpoyees_EmpAge check(EmpAge0 and EmpAge120) --给列起名的三种方式 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 MyStudentselect 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 fage30 --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 100hello
--select hello100select 100100select cast(100 as varchar(10))hello
select convert(varchar(10),100)helloselect 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 fid999
union all
select tsname,tsage from itcastcn..tblstudent where tsid12 -- --当联合的时候注意 --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/archive/2012/07/26/2610055.html