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

SQL server 学习笔记2

时间:2015-04-20 20:47:24      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

select *from lol
--top 关键字
select top 3 *from lol--查询前三行 
select top 3 *from lol where age>22 --加入条件
select top 3 name,wuqi from lol where age>22 --
--关键字 distinct 查询不同项 去重
select distinct name from lol --查询不同名字的行
--order by 升序
select *from lol order by age asc--升序
select *from lol order by age desc--降序  默认不写的话是升序
select *from lol order by age,sex --在年龄基础上再按性别排  不改变第一组的结果基础上,再按第二组进行排序
--分组 group by+列
--对那一列进行分组 就只能显示哪一列
select age from lol group by age  --对某一列分组 相当于去重显示 
-- select 列 from lol group by 列
go
--运算符 查询年龄加5岁的结果
select * from lol where age+5>27
--算术运算符 +-*/%
--比较运算符 >< >= <= != <> !> !<
--逻辑运算符 and or not(修饰符,不能单独用)
--修饰符 all any some in not
--all 所有的意思 结合一个范围来使用
--in 在什么范围之内
select * from lol where age in (22,23)--代表着等于第一个参数 or 等于第二个参数
select * from lol where age not in (22,23) --年龄不在这个范围之内 not 修饰作用
--查询年龄不在(身高是164的人的年龄)范围之内的学生信息
--select * from student where high=164
--select * from student where age!=23
--子查询:使用查询语句查询一列数据出来,然后作为其他查询的查询条件的参数来使用
--查询身高不在(年龄是22岁的人身高)范围之内的学生信息
--select * from student where high not in(select high from student where age=22)
--注意是一列的信息作为参数
go
--外键:受约束的表叫外键表,约束的表叫主键表
--要加外键,首先得有主键表
--要删除主键表的数据,必须先删除外键表的数据
--作为外键的数据源的列,必须是一个唯一键(必须是主键或者是unique)
create table teacher
(
tno int primary key not null,
tname varchar
)
create table student1
(
sno int primary key,
sname varchar,
tno int references teacher(tno),--受teacher表中tno约束
cid varchar(50) unique --不能重复的
)
alter table lol add high int --添加一列 允许为空
alter table lol drop column zsx -- 删除一列
select *from lol

update lol set guishudi=‘全真教 where code=2
insert into lol values(人王伏羲,,35,西王母,伏羲八卦,不周山,185)
select *from lol where age>=25 and high>=180
select * from lol order by high desc
select*from lol where age not in(select age from lol where high=188)
select name from lol where high=(select max(high) from lol )--身高最高的人的名字
select COUNT(*)from lol --表中有多少条数据
exec sp_renamelol.duixiang,wife,column --修改列名
--格式是 exec sp_rename‘表.原列名‘,‘新列名‘,‘column‘

 

 

SQL server 学习笔记2

标签:

原文地址:http://www.cnblogs.com/happinesshappy/p/4442459.html

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