sql
上一篇 /
下一篇 2014-11-21 14:43:23
/ 个人分类:数据库
--char表示定长字符,适合存放英文,一个字符占用1个字节
--nchar适合存放中文,一个字符占用2个字节
--varchar表示变长字符
--局部变量的定义,赋值,查询
declare @aa char(20)
select @aa='aaaaa'
set @aa='ccccc'
select @aa+'bbb'
--查看
数据库的版本(全局)
select @@version
--聚合函数即对一列值进行统计,如avg(),max(),min(),sum(),count()....
--标量函数(字符串函数,如Left(),Len(),Replace(),Upper()等)
declare @aab char(20)
set @aab='bccccca'
select left(@aab,2)
select len(@aab)
--group by 按班分组,查询每个班级有多少学生 select class,count(*) from student group by class
--having主要是对聚集函数的结果进行处理,相当条件,求平均分大于80的学生 select a.studentid,avg(mark) from student a join score s on a.studentid=s.studentid group by studentid having avg(mark)>80
--插入部分数据 insert student (id,name) values (001,'李三')
--将某表的某数据插入到现有表 insert oldtable select * from student where class=‘网络班’
--将某表的某数据插入到新表 select * into newtable from student where class=‘网络班’
--删除数据 delete student where class=‘网络班’
--删除不及格的学生信息 delete student where studentID in(select distinct studentID from score where mark<60
--或 delete student from student s join score a on s.studentid=a.studentid where mark<60
--更新数据 update student set name='lj',sex='nan' where studentid='001'
--条件判断语句(begin..end 是一个块,都执行)
declare @score decimal
set @score=50+rand()*50
if (@score<60)
begin
print '..不及格..'
print '..不及格..'
end
else
print '..及格..'
declare @score1 int
set @score1=(50+rand()*50)/10
select case @score1 when 5 then '..不及格..'
when 6 then '..及格..'
else '..优秀..'
end
/*
select name
case when mark<60 then '..不及格..'
when mark>60 then '..及格..'
else '..优秀..'
end
from student
*/
收藏
举报
TAG: