T-SQL基础 学习笔记

来源:互联网 发布:昆明询知培训中心 编辑:程序博客网 时间:2024/05/21 17:42

T-SQL

 

--新建数据库create database Studentdbgo--使用数据库use Studentdbgo--新建表create table Username(StudentNo int identity(1,1) not null,--学号并设置标识列LoginPwd nvarchar(20) not null,--密码StudentName nvarchar(20) not null,--姓名Sex char(2) not null,--性别GradeId int not null,--年级Phone nvarchar(20)not null,--电话Address nvarchar(40) not null,--地址BornDate datetime,--生日Email nvarchar(20) not null,--邮件)--创建一个主键约束alter table Usernameadd constraint Keyname--主键的别名primary key(StudentNo)--通过select into语句将现有表中的数据添加到新表中select identity(int,1,1) as StudentNointo NewTablefrom Username--通过UNION关键字合并数据进行插入insert Username(LoginPwd,StudentName,Sex,GradeId,Phone,Address,BornDate,Email)select '123','秦浩天','男','S2','13518159261','北京抚琴南路',1992-05-12,'qht@163.com' UNIONselect '51842','王冰','女','Y2','15883958283','上海交大西门',1990-10-05,'wangbin@xina.com'--使用UPDATE更新数据update Usernameset StudentName='秦浩然'where StudentName='秦浩天'update Scoreset Score=Score+5where Score <= 95--使用delete删除数据delete from Usernamewhere StudentName='秦浩然'--使用truncate table删除数据truncate table Username--查询部分行或列select LoginPwd,StudentName,Addressfrom Usernamewhere StudentName='王冰'--反之:where StudentName<>'王冰'--查询中使用别名select StudentName as 学生姓名,Address as 地址,Phone as 电话号码from Usernamewhere StudentName <> '王冰'--查询结果合并select StudentName+'.'+LoginPwd as 姓名.密码from Usernameselect 姓名.密码 = StudentName+'.'+LoginPwdfrom Username--查询空值select StudnetNamefrom Usernamewhere LoginPwd is null--用常量指定学校select 姓名=StudentName,地址=Address,'北京大学' as 学校名称from Username--显示前3条数据select top 3 *from Username--按数据量的百分比显示此处显示20%select top 20 percent StudnetName,Addressfrom Username--使用order by排序,默认为ascselect *from Scoreorder by Score,desc--年龄如果为空姓名按降序排序select *from Studentwhere Age is nullorder by StudentName desc--用来寻找一个指定字符串在另一个字符串中的起始位置select charindex('name','My name is hqs',1)--返回int值:4--返回字符串长度select len('不是穿上情侣装就可以装情侣')--返回int值:26--字母转换为大写select upper('my name is tom')--返回大写值:MY NAME IS TOM--清除字符左边的空格select ltrim(' I love you ')--返回:I lovey you--清除字符右边的空格select rtrim(' me too ')--返回: me too--从字符串右边返回指定数目的字符select right('北京.上海',2)--返回:上海--替换一个字符串select replace('爱上你等于爱上了错''上','下')--返回:爱下你等于爱下了错--删除指定长度,并插入新的字符串select stuff('123456',2,4,'中华人民')--返回:1中华人民6--获取系统当前时间select getdate()--返回系统当前时间:2009-05-11 12:00:00.000--将指定的数值添加到指定的日期部分后的日期select dateadd(mm,4,'01/05/1992')--返回:05/05/1992 注意这里的:4和日期中的天数--两个日期之间的间隔select datediff(mm,'01/05/1992','05/05/1992')--返回:4--指定字符串形式select datename(dw,'01/01/2000')--返回:当前是星期几--年:yy 季度:qq 月份:mm 一年中第几天:dy 天:dd 周:wk 一周星期几:dw 小时:hh 分钟:mi 秒钟:ss 毫秒:ms--指定日期中的整数select datepart(day,'01/15/2000')--返回15--返回0到1之间的随机float值select rand()--返回:0.7952618415626--取数值表达式的绝对值select abs(-43)--返回:43--向上取整数select ceiling(43.5)--返回:44--向下取整数select floor(43.5)--返回:43--取数值表达式的幂值select power(5,2)--返回:25--四舍五入为指定精度select round(43.541,1)--返回:43.500--对于正数返回+1,对于负数返回-1,对于0返回0select sign(-12)--返回:-1--取浮点表达式的平方根select sqrt(9)--返回:3--转变数据类型select convert(varchar(5),'12345')--返回int型:12345--返回当前用户的名字select current_user--返回:你登录的用户名--返回用于用于指定表达式的字节数select datalength('我的世界我做主')--返回:7--返回当前用户所登录的计算机名字select host_name()--返回:你所登录的计算机的名字--返回当前所登录的用户名称 select system_user--返回:你当前所登录的用户名--给定用户的ID返回用户名select user_name(1)--返回:从任意数据库中返回"dbo"--使用like进行模糊查询select * from Username where StudentName like '王%'--between在某个范围内进行查询select * from Username where BornDate not between '2010-01-01' and '2010-12-12'--使用in在列举值内进行查询select StudentName as 学生姓名 from Usernamewhere Address in('北京','广州','上海')order by Address--sum()求和select sum(Score) as 学号为23的学生总分 from Score where StudentNo=23--avg()求平均select avg(Score) as 平均成绩 from Scorewhere Score>=60--max()最大值 min()最小值select avg(Score) as 平均成绩,max(Score) as 最高分,min(Score) as 最低分 from Score where Score >=60--count() 集中计数select count(*) as 及格人数 from Score where Score>=60--group by分组查询select CourseId,avg(Score) as 课程平均成绩from Scoregroup by CourseId--查询男女学生的人数各是多少select count(*) as 人数,SSex from Usernamegroup by SSex--查询每个年级的总人数select count(*) as 年级人数,SGrade from Studentsgroup by SGrade--查询每个科目的平均分,并按照由低到高的顺序排列select CourseId, avg(Score) as 课程平均成绩 from Scoregroup by CourseIdorder by avg(Score) desc--多列分组查询select count(*) as 人数,SGrade as 年级,SSex as 性别 from Studentsgroup by SGrade,SSexorder by SGrade--查看一个班人数超过15个的年级select count(*) as 人数,SGrade as 年级 from Studentsgroup by SGradehaving count(*)>15--查询平均分及格的课程信息select CourseId as 课程编号,avg(Score) as 课程平均成绩from Scoregroup by CourseIdhaving avg(Score)>=60--查询每门课程及格总人数和及格学生的平均分select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Scorewhere Score>=60group by ScourseId--查询每门课程及格总人数和及格平均分在80分以上的记录select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Scorewhere Score>=60group by CourseIdhaving avg(Score)>=80--查询有多个员工工资不低于2000的部门编号select 部门编号,count(*) from 员工信息表where 工资>=2000group by 部门编号having count(*)>1--在where子句中指定连接条件select StudentName,CourseId,Scorefrom Students,Scorewhere Scode = StudentId--在from 子句中使用inner join..onselect S.StudentName,C.CourseId,C.Scorefrom Students as Sinner join Score as C on (S.Scode = C.StudentId)--左外连接查询select S.SName,C.CourseId,C.Scorefrom Students as Sleft outer join Score as C on S.Scode = C.StudentId--右外连接查询select Titles.Title_id,Titles.Title, Publishers.Pub_namefrom titlesright outer join Publishers on Titles.Pub_id=Publishers.Pub_id

 

原创粉丝点击