sqlserver 2008学习笔记一
2015-06-17 14:46
806 查看
1、创建数据库
create database Test1
on( --创建数据库文件
name=Test1_data,
filename='f:\SqlserverDB\Test1_data.mdf',
size=8,
maxsize=12,
filegrowth=10%
)
log on( --创建日志文件
name=Test1_log,
filename='f:\SqlserverDB\Test1_log.ldf',
size=3,
maxsize=8,
filegrowth=10%
)
2、增删数据库文件
(1)新增
use Test1
alter database Test1
add file( --新增数据库文件
name=Test2_data,
filename='f:\SqlserverDB\Test2_data.mdf',
size=8,
maxsize=12,
filegrowth=10%
)
alter database Test1 ------------------每增加一个文件都要这一行代码
add log file( --新增日志文件
name=Test2_log,
filename='f:\SqlserverDB\Test2_log.ldf',
size=3,
maxsize=8,
filegrowth=10%
)
(2)删除------------------------不用后缀名------------------
alter database Test1
remove file Test2_data
remove file Test2_log
3、重命名数据库
exec sp_renamedb Test1,Test --exec表示将要执行某函数,sp_renamedb函数可以重命名数据库
4、创建数据表
create table Student
(
Name varchar(50),
Age int
)
5、重命名数据表
exec sp_rename Student,Students
6、增删字段(可以给它设置默认值)
use Test1
alter table Students
add Msg text 【default '没有描述信息'】 --Msg字段,类型为text,默认值。。。。
7、修改数据表的字段
(1)改字段类型
use Test1
alter table Student
alter column Msg varchar(500) ---将text类型改为varchar类型
(2)删除字段
use Test1
alter table Student
drop column Mag
8、插入记录 --(执行此语句时注意是否允许为null值)
use Test1
insert into Student(Name,Age,Msg) values ('xiaoyang',21,'钟小扬') --好像没有into也可以
9、更新记录
use Test1
update Student set Name='小扬' where (...)
update Student set Name='小扬',Age=100 where (...)
(1)update语句可以对一批记录进行更新
use Test1
update Student set Age=Age+5; --将所有学生的年龄加5(还可以通过where设置条件)
也可以为:Age+=5
10、删除记录
use Test1
delete from Student --清空记录,但是标识列仍然在标志,不会从初始值重新标志
delete from Student where(...) --删除带符合条件的记录
11、记录查询
()select * from Student --全表查询
()select * from Student where (...) --符合条件的所有字段的记录
()select Name,Age from Student where(...) --符合条件的指定字段的记录
12、逻辑运算符
()Age>20 or Age<10 --大于20或者小于10
()Name='小明' or Age<20 --名字为小明的或者年龄小于20的
()and表示两个条件都要满足
Name='小明' and Age<20 --名字为小明的且年龄小于20的
()not运算符取反
where (not Age>20) --年龄小于20的,它与(!)经常有相似功能
where (Age!>20)
13、运算符优先级
()括号
()Not、正好、负号
()乘、除
()加、减
()比较运算符
()and
()or
14、in运算符
()where Name in ('小扬','小明','小红') --找名字有这些的
()where Name not in ('小扬','小明','小红') --找名字没有这些的
15、是否空值
()where Name is null --Name为空值的
()where Name not is null
16、范围之内
where Age between 20 and 25 --年龄在20到25之间
where Age not between 20 and 25
相等于:where Age>20 and Age<25
17、消除字段重复值查询(在字段名前面添加distinct) ---------select distinct 字段名 from 表名 。。。。。----------
use Test1
select distinct Age from Student --将年龄这一列的值列出来,如果年龄有相同的,只留一个
select distinct Age,Name from Student --Age与Name组合相同的就保留一个
18、嵌套查询(可用distinct关键字去除臃余数据)
use Test1
select Name from Student where Age=(select distinct Age from Student where Name='小扬') --查询等于小扬的年龄的记录的Name列
19、定义字段为标志自增量和主键
use Test1
create table Person
(
Id int identity(1,2) primary key, --标识列的第一个是种子,第二个是自增量;并且该列设为主键
Name varchar(50),
Age int
)
20、对查询结果进行排序
()一级排序
use Test1
select * from Student order by Age asc/desc --按年龄从小到大/从大到小排序
select * from Student order by Name asc/desc --按姓名从小到大/从大到小排序(字母顺序)
()多级排序
select * from Person order by Age asc,pay asc --先按年龄排序,年龄相等按工资排序
21、统计函数(大小写不敏感)函数中接受列参数,可以使表达式但是如果count(Age+id)则只列出Age和id都不为null的记录
COUNT()统计行数
use Test1
select COUNT(Name) as 统计 from Student --统计名字这一列有多少个,显示统计结果的列叫统计列(不包括null值)
select COUNT(Name) from Student
--在无名显示列中显示结果
select COUNT(*) as 统计 from Student
--统计有多少条记录,。。
SUM()对一列进行求和
use Test1
select SUM(pay) as 总工资 from Student --对工资这一列求和
AVG()对一列求平均值
use Test1
select AVG(pay) as 总工资 from Student
max/min()对一列求最大值/最小值
use Test1
select max(pay) as 总工资 from Student
(需求)找出工资大于平均工资的记录。。。嵌套查询
22、模糊查询
use Test1
select * from Student where Name like '%小%' --查询有‘小’字的记录
select * from Student where Name like '小%' --查询以‘小’开头的记录
select * from Student where Name like '%小' --查询以‘小’结尾的记录
23、指定显示的字段名
use Test1
select Name as 姓名,Age as 年龄 from Student
24、有奖金和工资字段,查询每个人的工资和奖金的和
use Test1
select Name as 姓名,pay+奖金 as 总薪水 from Student
25、(compute by)对查询结果分组-----按年龄分组统计,对每一组计算他们的“最小工资”等值-------(查询结果和统计分两个表显示)
use Test1
select * from Student order by Age compute min(工资),max(工资),avg(工资),sum(工资) by Age
26、select语句查询结果是一个表格,表格的字段我们可以从原数据表中选,也可以自定义字段。 -----自己总结的
()原表格选
use Test1
select * from Student --全部字段来源于原数据表
()自定义字段
use Test1
select min(Age) as 最小年龄,avg(pay) as 年龄的平均值 from Student ---定义了‘最小年龄’和‘年龄的平均值’两个字段
27、(group by)---要求select中的列包含在group分组中,所以select中的列都是经过分组的
select 毕业院校,MAX(工资),MIN(工资), AVG(工资),SUM(工资+奖金) from 职员表 group by 毕业院校
()从职员表中查询,查询按照‘毕业院校’分组,每个学校一个组进行统计
()查询结果有5个字段:毕业院校、最高工资、最低工资、平均工资、薪水
select 毕业院校,MAX(工资),MIN(工资),SUM(工资+奖金) from 职员表 group by 毕业院校 having SUM(工资)>5000
()除了上面的结果外,要求只有工资大于5000的才显示出来
()having 条件筛选要与 group by 组合,紧跟在group语句后面
28、分组查询的嵌套
use Test1
select * from 职员表 where 院校 in (select 院校 from 职员表 group by 院校 having SUM(工资)>5000)
()查询(毕业院系分组中每组工资的总和“也就是改组全部职员工资的和”大于5000的院校的)职员记录
29、exists()括号中如果包含一个或者多个记录行则返回true,否则返回false。返回true时select被执行并且有结果;false时没有结果。
use Test1
select Name from Student where exists (select Age from Student where Age=21)
use Test1
select Name from Student where not exists (select Age from Student where Age=21)
30、any、all判断
use Test1
select Name from Student where Age>any(select Age from Student) --查询满足年龄大于(子查询的年龄集合)中任意一个值的记录
use Test1
select Name from Student where Age>all(select Age from Student) --查询满足年龄大于(子查询的年龄集合)中全部值的记录
31、查询前几条记录
use Test1
select top 10 * from Student --查询前10条记录
use Test1
select top 10 percent * from Student --查询前10%的记录
32、将执行结果保存到新表中,如果表不存在就新建表
use Test1
select * into 新表 from Student where ...
33、集合并、集合交。可以连接两个或者多个select语句
!!!要求 1、每个查询必须返回相同数目的列
2、列的数据类型相同(可以转成相同类型的也可以)
3、union运算符合并了多个查询,但是仅包含唯一的记录,如果要返回所有记录,不管是否唯一,在union后加“all”语句
4、可用“order by”来排序,但是只能在全语句最后面使用一次,并且只能用第一个“select”语句中的列名。
()union集合并
use Test1
select * from Student union select * from Student --将两个查询结果一起显示,是混合显示,(好像有一定的排序规则)
select * from Student union all select * from Student
()interset集合交
use Test1
select * from Student interset select * from Student ----将两个查询结果一起显示,是分开各自的表显示
34、连接查询
use Test1
select * from Student,Person --Person表的每条记录与Student表的每条记录都联合一次。总行数两表的行数相乘。
select * from Student,Person where Student.Name=Person.Name --两表的名字相同的记录才会连接并显示出来
select * from 职员表,项目负责人表 where 职员表.Name=项目负责人表.负责人
select * from 职员表,项目负责人表 where Name=负责人 --同上:要求Name和负责人字段都是唯一字段
35、超链接的内连接
select * from Student inner join Person on Student.Name=Person.Name --如果条件判断的列在两表中唯一,可以不用表名索引
36、超链接的左右连接
select * from Student left join Person on Student.Name=Person.Name --左边表的记录全部显示,右边表只有符合条件的才显示
select * from Student right join Person on Student.Name=Person.Name --右边表的记录全部显示,左边表只有符合条件的才显示
37、全连接
select * from Student full join Person on Student.Name=Person.Name --左右表都全部显示,没有匹配的用null填充
create database Test1
on( --创建数据库文件
name=Test1_data,
filename='f:\SqlserverDB\Test1_data.mdf',
size=8,
maxsize=12,
filegrowth=10%
)
log on( --创建日志文件
name=Test1_log,
filename='f:\SqlserverDB\Test1_log.ldf',
size=3,
maxsize=8,
filegrowth=10%
)
2、增删数据库文件
(1)新增
use Test1
alter database Test1
add file( --新增数据库文件
name=Test2_data,
filename='f:\SqlserverDB\Test2_data.mdf',
size=8,
maxsize=12,
filegrowth=10%
)
alter database Test1 ------------------每增加一个文件都要这一行代码
add log file( --新增日志文件
name=Test2_log,
filename='f:\SqlserverDB\Test2_log.ldf',
size=3,
maxsize=8,
filegrowth=10%
)
(2)删除------------------------不用后缀名------------------
alter database Test1
remove file Test2_data
remove file Test2_log
3、重命名数据库
exec sp_renamedb Test1,Test --exec表示将要执行某函数,sp_renamedb函数可以重命名数据库
4、创建数据表
create table Student
(
Name varchar(50),
Age int
)
5、重命名数据表
exec sp_rename Student,Students
6、增删字段(可以给它设置默认值)
use Test1
alter table Students
add Msg text 【default '没有描述信息'】 --Msg字段,类型为text,默认值。。。。
7、修改数据表的字段
(1)改字段类型
use Test1
alter table Student
alter column Msg varchar(500) ---将text类型改为varchar类型
(2)删除字段
use Test1
alter table Student
drop column Mag
8、插入记录 --(执行此语句时注意是否允许为null值)
use Test1
insert into Student(Name,Age,Msg) values ('xiaoyang',21,'钟小扬') --好像没有into也可以
9、更新记录
use Test1
update Student set Name='小扬' where (...)
update Student set Name='小扬',Age=100 where (...)
(1)update语句可以对一批记录进行更新
use Test1
update Student set Age=Age+5; --将所有学生的年龄加5(还可以通过where设置条件)
也可以为:Age+=5
10、删除记录
use Test1
delete from Student --清空记录,但是标识列仍然在标志,不会从初始值重新标志
delete from Student where(...) --删除带符合条件的记录
11、记录查询
()select * from Student --全表查询
()select * from Student where (...) --符合条件的所有字段的记录
()select Name,Age from Student where(...) --符合条件的指定字段的记录
12、逻辑运算符
()Age>20 or Age<10 --大于20或者小于10
()Name='小明' or Age<20 --名字为小明的或者年龄小于20的
()and表示两个条件都要满足
Name='小明' and Age<20 --名字为小明的且年龄小于20的
()not运算符取反
where (not Age>20) --年龄小于20的,它与(!)经常有相似功能
where (Age!>20)
13、运算符优先级
()括号
()Not、正好、负号
()乘、除
()加、减
()比较运算符
()and
()or
14、in运算符
()where Name in ('小扬','小明','小红') --找名字有这些的
()where Name not in ('小扬','小明','小红') --找名字没有这些的
15、是否空值
()where Name is null --Name为空值的
()where Name not is null
16、范围之内
where Age between 20 and 25 --年龄在20到25之间
where Age not between 20 and 25
相等于:where Age>20 and Age<25
17、消除字段重复值查询(在字段名前面添加distinct) ---------select distinct 字段名 from 表名 。。。。。----------
use Test1
select distinct Age from Student --将年龄这一列的值列出来,如果年龄有相同的,只留一个
select distinct Age,Name from Student --Age与Name组合相同的就保留一个
18、嵌套查询(可用distinct关键字去除臃余数据)
use Test1
select Name from Student where Age=(select distinct Age from Student where Name='小扬') --查询等于小扬的年龄的记录的Name列
19、定义字段为标志自增量和主键
use Test1
create table Person
(
Id int identity(1,2) primary key, --标识列的第一个是种子,第二个是自增量;并且该列设为主键
Name varchar(50),
Age int
)
20、对查询结果进行排序
()一级排序
use Test1
select * from Student order by Age asc/desc --按年龄从小到大/从大到小排序
select * from Student order by Name asc/desc --按姓名从小到大/从大到小排序(字母顺序)
()多级排序
select * from Person order by Age asc,pay asc --先按年龄排序,年龄相等按工资排序
21、统计函数(大小写不敏感)函数中接受列参数,可以使表达式但是如果count(Age+id)则只列出Age和id都不为null的记录
COUNT()统计行数
use Test1
select COUNT(Name) as 统计 from Student --统计名字这一列有多少个,显示统计结果的列叫统计列(不包括null值)
select COUNT(Name) from Student
--在无名显示列中显示结果
select COUNT(*) as 统计 from Student
--统计有多少条记录,。。
SUM()对一列进行求和
use Test1
select SUM(pay) as 总工资 from Student --对工资这一列求和
AVG()对一列求平均值
use Test1
select AVG(pay) as 总工资 from Student
max/min()对一列求最大值/最小值
use Test1
select max(pay) as 总工资 from Student
(需求)找出工资大于平均工资的记录。。。嵌套查询
22、模糊查询
use Test1
select * from Student where Name like '%小%' --查询有‘小’字的记录
select * from Student where Name like '小%' --查询以‘小’开头的记录
select * from Student where Name like '%小' --查询以‘小’结尾的记录
23、指定显示的字段名
use Test1
select Name as 姓名,Age as 年龄 from Student
24、有奖金和工资字段,查询每个人的工资和奖金的和
use Test1
select Name as 姓名,pay+奖金 as 总薪水 from Student
25、(compute by)对查询结果分组-----按年龄分组统计,对每一组计算他们的“最小工资”等值-------(查询结果和统计分两个表显示)
use Test1
select * from Student order by Age compute min(工资),max(工资),avg(工资),sum(工资) by Age
26、select语句查询结果是一个表格,表格的字段我们可以从原数据表中选,也可以自定义字段。 -----自己总结的
()原表格选
use Test1
select * from Student --全部字段来源于原数据表
()自定义字段
use Test1
select min(Age) as 最小年龄,avg(pay) as 年龄的平均值 from Student ---定义了‘最小年龄’和‘年龄的平均值’两个字段
27、(group by)---要求select中的列包含在group分组中,所以select中的列都是经过分组的
select 毕业院校,MAX(工资),MIN(工资), AVG(工资),SUM(工资+奖金) from 职员表 group by 毕业院校
()从职员表中查询,查询按照‘毕业院校’分组,每个学校一个组进行统计
()查询结果有5个字段:毕业院校、最高工资、最低工资、平均工资、薪水
select 毕业院校,MAX(工资),MIN(工资),SUM(工资+奖金) from 职员表 group by 毕业院校 having SUM(工资)>5000
()除了上面的结果外,要求只有工资大于5000的才显示出来
()having 条件筛选要与 group by 组合,紧跟在group语句后面
28、分组查询的嵌套
use Test1
select * from 职员表 where 院校 in (select 院校 from 职员表 group by 院校 having SUM(工资)>5000)
()查询(毕业院系分组中每组工资的总和“也就是改组全部职员工资的和”大于5000的院校的)职员记录
29、exists()括号中如果包含一个或者多个记录行则返回true,否则返回false。返回true时select被执行并且有结果;false时没有结果。
use Test1
select Name from Student where exists (select Age from Student where Age=21)
use Test1
select Name from Student where not exists (select Age from Student where Age=21)
30、any、all判断
use Test1
select Name from Student where Age>any(select Age from Student) --查询满足年龄大于(子查询的年龄集合)中任意一个值的记录
use Test1
select Name from Student where Age>all(select Age from Student) --查询满足年龄大于(子查询的年龄集合)中全部值的记录
31、查询前几条记录
use Test1
select top 10 * from Student --查询前10条记录
use Test1
select top 10 percent * from Student --查询前10%的记录
32、将执行结果保存到新表中,如果表不存在就新建表
use Test1
select * into 新表 from Student where ...
33、集合并、集合交。可以连接两个或者多个select语句
!!!要求 1、每个查询必须返回相同数目的列
2、列的数据类型相同(可以转成相同类型的也可以)
3、union运算符合并了多个查询,但是仅包含唯一的记录,如果要返回所有记录,不管是否唯一,在union后加“all”语句
4、可用“order by”来排序,但是只能在全语句最后面使用一次,并且只能用第一个“select”语句中的列名。
()union集合并
use Test1
select * from Student union select * from Student --将两个查询结果一起显示,是混合显示,(好像有一定的排序规则)
select * from Student union all select * from Student
()interset集合交
use Test1
select * from Student interset select * from Student ----将两个查询结果一起显示,是分开各自的表显示
34、连接查询
use Test1
select * from Student,Person --Person表的每条记录与Student表的每条记录都联合一次。总行数两表的行数相乘。
select * from Student,Person where Student.Name=Person.Name --两表的名字相同的记录才会连接并显示出来
select * from 职员表,项目负责人表 where 职员表.Name=项目负责人表.负责人
select * from 职员表,项目负责人表 where Name=负责人 --同上:要求Name和负责人字段都是唯一字段
35、超链接的内连接
select * from Student inner join Person on Student.Name=Person.Name --如果条件判断的列在两表中唯一,可以不用表名索引
36、超链接的左右连接
select * from Student left join Person on Student.Name=Person.Name --左边表的记录全部显示,右边表只有符合条件的才显示
select * from Student right join Person on Student.Name=Person.Name --右边表的记录全部显示,左边表只有符合条件的才显示
37、全连接
select * from Student full join Person on Student.Name=Person.Name --左右表都全部显示,没有匹配的用null填充
相关文章推荐
- MongoDB 的 GridFS 详细分析
- MongoDB 的 GridFS 详细分析
- 去掉mysql重复的索引
- Redis学习第四课:Redis List类型及操作
- UNDO表空间使用率过高
- oracle数据库知识
- ACCESS与SQL Server下SQL Like 查询的不同
- Linux下安装Redis及Linux下php安装Redis扩展
- 数据库索引
- oracle当需要commit
- sql优化技巧
- Memcache(MC)系列(八)Memcache ketama一致性哈希算法
- MSSQL 模仿 MYSQL 的IPV4与BIGINT类型互转函数
- sqlite3_英英词典
- EBS R12中SLA子分类帐与子模块的关联关系
- order by 的列名不能参数化,要拼sql
- Linux下centos系统安装redis和php-redis
- 数据库最大连接池max pool size
- 收藏:SQL Server 数据库改名
- MongoDb的bin目录下文件mongod,mongo,mongostat命令的说明及使用