现在的位置: 首页 > 综合 > 正文

SQL语句复习:insert,update,delete,select

2013年02月22日 ⁄ 综合 ⁄ 共 5560字 ⁄ 字号 评论关闭

 最近一直在学习SQL server的内容。昨天考了一下试。感觉真的是不容易啊。特别是一些复杂的查询。搞得我头昏脑胀的。不过也是由于自己的知识掌握的还不够扎实啊。所以今天复习了一下T-SQl语句的增删改查。发现的确是有很多都忘记了。现在把结果写出来。以后可不要忘了呀。

--SQL语句复习 --一,插入insert语句 --1,insert单行数据 insert into students(sName,sAddress,Ssex,Semail) values('姓名','地址','性别','邮件地址')

--2,insert多行数据,把其他表中现有的数据经过选取后插入到数据库中 --把表Student对应的Sname,Saddress,Semail三列的全部行, --插入到TongXuLu表中的姓名,地址,电子邮件三列中 insert into students(姓名,地址,电子邮件) select Sname,Saddress,semail from students

--3,insert多行数据到新建的表中,表不能事先存在 --将创建TongXueLu表,同时创建三个字段Sname,Saddress,Semail,和标识列studentID --并且把Student表中的这三列数据项全部插入到新表中。 select Students.Sname,Students.Saddress,Students.Semail,identity(int,1,1) as studentID--标识列列名 into TongXuLu from Students

--4,手写多行数据通过Union合并插入 insert students(Sname,Sgrade,Ssex) select '张可',7,1 union select '张三',4,0 union select '陈刚',4,1 union select '王娟娟',7,0

--二,update数据 update students set Ssex=1,Sgrade=Sgrade+1 where Ssex=0

--三,delect数据 --1,删除指定数据 delete from students where Ssex=0

--2,删除所有数据 --第一种方法 delete from students --第二种方法,比delete执行速度快,使用的系统资源和事务日志资源更少 --但是不能用于有外键约束的表 truncate table students

--附加 --1,把所有男同学的成绩加5分 update Sscore set score=score+5 where StudentID(select Scode from students where Ssex=1)

--2,只复制Students的表结构,不复制数据 select identity(int 1,1) as InfoID,Sname,Saddress,Sgrade,Semail,Ssex into studnetInfo from students where (1<0)

--四,查询 --(一)一般数据查询 --1,查询所有的数据行和列 select * from Students

--2,查询部分数据行列 --只查询地址为'河南新乡'的学员,并且只显示编号,姓名和地址列 select Scode,Sname,Saddress from students where Saddress='河南新乡'

--3,查询中使用列名 select scode as 学生编号,sname as 学生姓名,saddress as 学员地址 from students where saddress<>'河南新乡'

--4,合并自段得到新列 select firstname+'.'+lastname as '姓名' from employee

--5重命名列的另一种方法,用等号 select '姓名'=firstname+'.'+lastname from employee

--6,查询空行,非空是is not null select Sname from students where Semail is null

--7,查询中使用常量列 --查询输出多了一列"学校名称",该列的所有数据都是"河北新龙" select 姓名=sname,地址=saddress,'河北新龙' as 学校名称

--8,查询返回限制的行数,用top关键字 select top 5 sname,saddress,from students where sex=0

--9,按一定的百分比提取数据,用percent关键字 select top 20 percent Sname,Saddress from students where sex=0

--(二)查询排序 --1,用order by 列名 asc升序排序,asc可省略 --把所有成绩都降低10%后加5分,再按照及格成绩的的高低进行排列 select studentID as 学生编号,(Score*0.9+5) as 综合成绩 from Score where (score*0.9+5)>60 order by score

--2,用order by 列名 desc降序排列 --查询作者表和雇员表,合并查到的所有姓名信息,并按照姓名降序排列 select Au_name+'.'+Au_fname as emp from authors Union select fname+'.'+lname as emp from employee order by emp desc

--3,按照多个字段排序。 --在学员成绩的基础上,再按照课程ID进行排序 select studentID as 学员编号,Score as 成绩 from Score where score>60 order by Score,CourseID

--(三)在查询中使用函数 --1,字符串函数 --(1)charindex:用来寻找一个指定的字符串在另一个字符串中的起始位置 select charindex('ACCP','My ACCP Course',1) --返回:4

--(2)len:返回传递给它的字符串长度 select len('SQL') --返回:3

--(3)upper:把传递给它的字符串转换为大写 select upper('sql server') --返回:SQL SERVER

--(4)ltrim:清除左边的空格 --(5)rtrim:清除右边的空格

--(6)right:从字符串右边返回指定数目的字符 select right('买买提.吐尔松',3) --返回:吐尔松

--(7)replace:替换一个字符串的字符 select replace('杨克','克','兰') --返回:杨兰

--(8)stuff:在一个字符串中,删除指定长度的字符,并在该位置插入一个新的字符串 select stuff('ABCDEFG',2,3,'我的音乐我的世界') --返回:A我的音乐我的世界EFG

--2,日期函数 --(1)getDate:获取当前日期

--(2)dateadd:将指定的数值添加到指定的日期部分后的日期 select dateadd(mm,4,'01/01/99') --返回以当前日期格式返回05/01/99

--(3)datediff:两个日期之间的指定日期部分的区别 select datediff(mm,'01/01/99','05/01/99') --返回:4

--(4)dateName:日期中指定日期部分的字符串形式 select datename(dw,'01/01/2000') --返回:Saturday

--(5)datepart:日期中指定日期部分的整数形式 select datepart(day,'01/15/2000') --返回:15

--3,数学函数 --(1)abs:取数值表达式的绝对值 select abs(-45) --返回:45

--(2)ceiling:取大于或等于指定数值,表达式的最小整数 select ceiling(43.5) --返回:44

--(3)floor:取小于或等于指定数值,表达式的最大整数 select floor(43.5) --返回:43

--(4)power:取幂值 select power(5,2) --返回:25

--(5)round:四舍五入为指定精度 select round(43.543,1) --返回:43.5

--(6)sign:对于正数返回1,对于负数返回-1,对于0返回0 select sign(-45) --返回:-1

--(7)sqrt:取浮点表达式的平方根 select sqrt(9) --返回:3

--4,系统函数 --(1)convert:转变数据类型 select convert(varchar(5),12345) --返回:字符串12345

--(2)current_user:返回当前用户的名字 select current_user

--(3)datalength:返回字节数 select datalength('中国A联盟') --返回:9

--(4)host_name:返回你所登录的计算机名字 select host_name()

--(5)system_user():返回当前用户所登录的计算机名字 select system_user

--(6)User_name:从给定的用户ID返回用户名 select user_name(1) --返回:从任意数据库中返回"dbo"

--(四)模糊查询 --1,like --查找姓张的同学 select * from students where sName like '张%'   --查询不是八月份发行的A卡或C卡 select * from Card where ID like '00[^8]%[A,C]%'

--2,between --查询不在1992年8月1号到1993年8月一号订购的读书列表 select * from Sales where ord_date not between '1992-8-1' and '1993-8-1'

--3,in --在列举值中查询 select * from students where sAddress in ('北京','上海','广州')

--(五)聚合函数 --1,sum:求所以数值的和,只用于数字类型的列 select sum(ytd_sales) from titles where type='business'

--2,avg:求平均值 --查询及格线以上的学员的平均成绩 select avg(score) as 平均成绩 from score where score>=60

--3,max:最大值;min:最小值 select avg(score) as 平均成绩,max(score) as 最高分,min(score) as 最低分 from score where score>=60

--4,count:返回非空值的计数,使用count(*)可以不必指定特定的列而计算所有的行数 --查询及格人数 select count(*) as 及格人数 from Score where Score>=60

--(六)分组查询 --1,group by --查询以课程号分组的平均成绩 select CourseID avg(score) as 课程平均成绩 from score group by CourseID

--查询以课程号和学生号分组的平均成绩 select studentID as 学生编号,CourseID as 内部测试,avg(Score) as 内部测试平均成绩 from scroe group by studentID,CourseID

--2,使用having语句对分组后的条件进行删选 --查询“有多少员工的工资不低于2000的部门编号,并且该部门的人数至少两人” select 部门编号,count(*) from 员工信息表 where 工资>=2000 group by 部门编号 having count(*)>1

--(七)表连接查询 --(一)内连接查询 --1,在where子句中指定联接条件 --查询学员姓名和成绩的SQL select Students.Sname,Score.CourseID,Score.Score from Students,Score where Students.Scode=Score.studentID

--2,在from子句中使用join...on --上面的SQL语句可以这样实现: select S.Sname,C.CourseID,C.score from Students as S inner join Score as C on (S.Scode=C.studentID)

--3,三表联接 select S.sname as 学院姓名,CS.CourseName as 课程名称,C.Score as 考试成绩 from students as S inner join Score as C on(S.Scode=C.StudentID) inner join Course as CS on (CS.CourseID=C.CourseID)

--(二)外联接查询:在外部联接中参与的联接的表有主从之分,以主表的 --每行数据去匹配从表中的数据列,符合联接条件的数据将直接返回到结果集中 --对那些不符合条件的列,将被填上Null值后再返回到结果集中

--1,左外联:使用left join 或 left outer join --统计所有学员的考试情况,没有考试的也要显示出来 select S.sname,C.CourseID,C.Score from students as S left outer join Score as C on S.scode=C.StudentID

--2,右外联:使用right join 或 right outer join --和左外联用法一样。只是要包含右边中所有匹配的行。

【reprinted from http://81146566.blog.163.com/blog/static/269551162009620115627226/

抱歉!评论已关闭.