码迷,mamicode.com
首页 > 数据库 > 详细

SQL server 基础语言

时间:2017-05-28 10:54:06      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:bsp   基本语法   esc   cat   自增   name   表示   表名   从表   


--查询 基本语法  select 列名 from 表名 where 查询条件
select ClassName from Student s,Class c where s.ClassId=c.Id and StudentName=‘张三‘

--模糊查询 有4中 第一种:_表示一个字符
select * from Student where StudentName like ‘_三‘

--模糊查询 有4中 第二种:%表示很多个字符
select * from Student where StudentName like ‘%三%‘

--模糊查询 有4中 第二种:[] 表示一个字符
select * from Student where StudentName like ‘%[2-5]‘

--模糊查询 有4中 第二种:[^]表示一个字符
select * from Student where StudentName like ‘%[^3,三]‘

--排序 默认为升序 asc 降序为desc order by
select * from Student order by ClassId asc,StudentName desc

--group by 分组 Count(*)查询总数的 having相当于where 但是接在group by 后面的
--有having的时候 可以有where
select ClassId,COUNT(*) as 人数 from Student where StudentName!=‘张三‘ group by ClassId having ClassId =1

--Max最大值 Min最小值 COUNT()总数量 AVG平均数 SUM总和
select * from Student
select MAX(Id),StudentName from Student group by StudentName
select Min(Id) from Student
select Count(Id) from Student group by StudentName
select AVG(Id) from Student group by StudentName
select SUM(Id) from Student group by StudentName

--插入语句
--insert into 表名(列名) values(值)
insert into Student values(2,‘王五‘)

--修改语句
--update 表名 set 列名=‘值‘,列名=‘值‘ where 条件 (如果没有where条件,则这一列的值都被改变)
update Student set StudentName=‘李四‘,ClassId=4 where Id=2

--删除 删除从表 如果想要删除主表中的数据 需要先把从表删除完
--delete 表名 where 条件 根据条件删除,删除过后 自增列不会发生改变
delete Student where ClassId=4 and StudentName=‘李四‘
delete Student where ClassId=1
delete Class where Id=1
--删除整个表格的数据
truncate table Student
truncate table Class

 

SQL server 基础语言

标签:bsp   基本语法   esc   cat   自增   name   表示   表名   从表   

原文地址:http://www.cnblogs.com/Sora-L/p/6915096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!