SQL Server 2005入门到精通(案例详解)

SQL Server 2005基础应用

 

一.数据库的基本操作

--创建数据库

create database new_db2

on primary

(

    name='new.mdf',

    filename='e:\new.mdf',

    size=5mb,

    maxsize=50mb,

    filegrowth=10%

)

--收缩数据库

alter database new_db

modify file

(

    name='new_db',

    size=15mb

)

--压缩数据库

dbcc shrinkdatabase('new_db',1)

--重命名数据库

exec sp_renamedb 'new_db','Jasxu_db'

--删除数据库

drop database new_db2

二.数据库表的基本操作

--创建数据库

create database st_db

on primary

(

    name='st.mdf',

    filename='e:\st,mdf',

    size=5mb,

    maxsize=50mb,

    filegrowth=20%

)

--删除Jasxu_db数据库

drop database Jasxu_db

--在st_db数据库中编辑

use  st_db

--创建表

create table table_name

(

学号int primary key identity,--这里的identity意思就是将标志规范设置为递增

名称char(6) not null,

专业方向varchar(10) not null,

系部代码char(2) not null,

备注varchar(50)

)

--查看表的基本信息

exec sp_help table_name

--重命名表

exec sp_rename 'table_name','new_table'

--重命名列

exec sp_rename 'new_table.备注','其他','column'

--添加新列

alter table new_table add 新列char(10)

--更改列的数据类型

alter table new_table

alter column 新列int not null

--删除列

alter table new_table

drop column 新列

--删除表

drop table new_table

--案例解析

create table t2

(

id int not null,

us varchar(30)

)

--查询表里面的内容

select * from t1

--删除表的所有数据

truncate table t1

--创建主键约束

alter table t1

add constraint pk

primary key clustered (id)

--创建外键约束

alter table t2

add constraint wz

foreign key (id)

references t1(id)--references代表参照哪个表的主键设置外键

三.数据库表的增加、删除、修改

--创建系部表

create table 系部

(

    系部代码char(6) not null primary key,

    系部名称varchar(30) not null,

    系主任char(8)

)

--创建专业表

create table 专业表

(

    专业代码char(4) not null primary key,

    专业名称varchar(20) not null,

    系部代码char(6) constraint wz11 references 系部(系部代码)

)

--创建班级表

create table 班级表

(

    班级代码char(9) not null primary key,

    班级名称varchar(20),

    专业代码char(4) constraint wz1 references 专业表(专业代码),

    系部代码char(6) constraint wz2 references 系部(系部代码),

    备注varchar(50)

)

--创建学生表

create table 学生表

(

    学号char(12) not null primary key,

    姓名char(8),

    性别char(2),

    出生日期datetime,

    入学时间datetime,

    班级代码char(9) constraint wz3 references 班级表(班级代码),

    系部代码char(6) constraint wz4 references 系部(系部代码),

    专业代码char(4) constraint wz5 references 专业表(专业代码)

)

--在new_table表中添加数据

insert into new_table values('Jasxu','计算机','01','无')

--选择性的插入数据

insert into new_table(名称,专业方向,系部代码) values('xsw','软件工程','02')

--省略values的insert语句

insert into new_table (名称,专业方向,系部代码) select 名称,专业方向,系部代码from new_table

--修改new_table表

update new_table set 系部代码='01'

update new_table set 专业方向='软件工程' where 专业方向='计算机'

--删除new_table中的内容

delete new_table where 专业方向='软件工程'

delete new_table where 学号='10'

四.数据库表的简单查询

--查询new_table表中所有信息内容

select * from new_table

select 学号,名称,专业方向,系部代码,其他from new_table

--输出表中的部分字段

select 学号,名称from new_table

--选择表中若干记录(去掉结果中的重复行)

select distinct 系部代码from new_table

--限制返回的行数

select top 3 * from new_table

--查询学号大于的信息

select * from new_table where 学号>13

--确定范围(between and)

select * from new_table where 学号between 12 and 16

--确定集合(in,not in)

select * from new_table where 学号in(12,13,14,15)

select * from new_table where 学号not in(12,13,14,15)

--字符匹配

select * from new_table where 名称like '徐_'--两个字的姓名

select * from new_table where 名称like '徐__'--三个字的姓名

select * from new_table where 名称like '徐%'--%代表任意长度

select * from new_table where 名称like '徐\%' escape '\'--通配符的转换

--清空数据

truncate table new_table

--插入数据

insert into new_table values('张学友','网络','01','没有','411')

insert into new_table values('刘德华','计算机','02','没有','412')

insert into new_table values('舒淇','计算机','01','没有','413')

insert into new_table values('梁咏琪','动漫','02','没有','431')

insert into new_table values('杨千嬅','计算机','01','没有','465')

insert into new_table values('李宇春','动漫','02','没有','485')

insert into new_table values('蔡依林','网络','01','没有','468')

insert into new_table values('郑源','计算机','02','没有','510')

insert into new_table values('陈楚生','动漫','01','没有','550')

insert into new_table values('张韶涵','计算机','02','没有','421')

insert into new_table values('猛非','动漫','01','没有','423')

insert into new_table values('郑秀文','网络','02','没有','411')

insert into new_table values('林俊杰','计算机','01','没有','511')

insert into new_table values('羽泉','计算机','01','没有','500')

insert into new_table values('郭富城','网络','02','没有','400')

insert into new_table values('黄品源','动漫','02','没有','589')

insert into new_table values('梁朝伟','计算机','02','没有','530')

insert into new_table values('李克勤','网络','01','没有','520')

insert into new_table values('陈小春','国际金融','02','没有','512')

insert into new_table values('刘若英','证券期货','02','没有','421')

insert into new_table values('刘嘉玲','房地产金融','01','没有','428')

insert into new_table values('谭咏麟','房地产金融','02','没有','498')

insert into new_table values('张学友','证券期货','01','没有','454')

insert into new_table values('张卫健','证券期货','02','没有','515')

insert into new_table values('周传雄','房地产金融','01','没有','532')

insert into new_table values('周星驰','国际金融','02','没有','423')

insert into new_table values('游鸿明','房地产金融','02','没有','447')

insert into new_table values('言承旭','国际金融','02','没有','488')

insert into new_table values('许志安','国际金融','01','没有','582')

insert into new_table values('叶倩文','房地产金融','01','没有','495')

insert into new_table values('叶世荣','房地产金融','02','没有','499')

insert into new_table values('张雨生','证券期货','02','没有','531')

insert into new_table values('周润发','国际金融','01','没有','531')

insert into new_table values('张信哲','证券期货','01','没有','424')

insert into new_table values('周渝民','证券期货','02','没有','412')

insert into new_table values('太极乐队','证券期货','02','没有','423')

--查询new_table表

select * from new_table

--涉及空值的查询

select * from new_table where 其他is null

select * from new_table where not 其他is null

--用指定使用结果值来创建一个表(注意:在表前加一个#创建出来的是临时表)

select 学号,名称,高考分数into score_table from new_table

select * from score_table

--对结果进行分组

select 系部代码from new_table group by 系部代码

select 专业方向from new_table group by 专业方向

select 专业方向from new_table group by 专业方向having 专业方向<>'动漫'--having起到筛选作用

--排序查询(asc升序desc降序)

select * from new_table order by 高考分数asc

select * from new_table order by 高考分数desc

五.数据表中对数据进行统计

--查询数据库表new_table中的信息

select * from new_table

select * from new_table order by 高考分数desc

select top 3 * from new_table order by 高考分数desc

--查询总人数

select count(*) as 总人数from new_table--这里的as是为列重命名

select count(学号) as 总人数from new_table

select count(其他) as 总人数from new_table

--计算整个班级高考的总分数

select sum(高考分数) as 总分from new_table

--计算整个班级高考的平均分数

select avg(高考分数) as 平均分from new_table

--计算整个班级高考的最大值

select max(高考分数) as 最大值from new_table

--计算整个班级高考的最小值

select min(高考分数) as 最小值from new_table

--对查询结果集中的所有记录进行汇总统计,并显示所有参加汇总记录的详细信息

select * from new_table order by 专业方向compute sum(高考分数)

select 专业方向,count(*) as 总人数from new_table group by 专业方向

--统计专业方向一共多少

select 专业方向,count(*) as 总人数from new_table group by 专业方向compute count(专业方向)

--统计系部总人数

select 专业方向,count(*) as 总人数from new_table group by 专业方向compute sum(count(*))

六.数据库中表的连接查询

--插入数据这里的go起到连接作用

insert into 系部(系部代码,系部名称,系主任) values('01','计算机系','老张')

go

insert into 系部(系部代码,系部名称,系主任) values('02','经济管理系','老陈')

go

insert into 系部(系部代码,系部名称,系主任) values('03','机械系','老李')

go

insert into 系部(系部代码,系部名称,系主任) values('04','计算机系','老梁')

go

 

 

 

insert into 专业表(专业代码,专业名称,系部代码) values('0101','软件工程','01')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0102','网络工程','01')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0103','信息工程','01')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0201','工商管理','02')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0202','物流管理','02')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0301','模具加工','03')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0302','机电一体化','03')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0401','应用数学','04')

go

insert into 专业表(专业代码,专业名称,系部代码) values('0402','金融数学','04')

go

 

 

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010101','软件工程班','0101','01','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010102','软件工程班','0101','01','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010103','网络工程班','0102','01','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010104','网络工程班','0102','01','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010105','信息工程班','0103','01','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010106','工商管理班','0201','02','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010107','物流管理班','0202','02','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010108','模具加工班','0301','03','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('010109','应用数学班','0401','04','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('0101010','金融数学班','0402','04','暂无')

go

insert into 班级表(班级代码,班级名称,专业代码,系部代码,备注) values('0101011','金融数学班','0402','04','暂无')

go

 

insert into 学生表values('010101000000','刘德华','男','1988-5-5','2010-9-1','010101','01','0101')

go

insert into 学生表values('010101000001','张学友','男','1988-1-4','2010-9-1','010102','02','0102')

go

insert into 学生表values('010101000002','梁静茹','女','1988-2-1','2010-9-1','010103','03','0103')

go

insert into 学生表values('010101000003','陈奕迅','男','1983-5-3','2010-9-1','010104','04','0201')

go

insert into 学生表values('010101000004','张韶涵','女','1987-8-6','2010-9-1','010105','01','0202')

go

insert into 学生表values('010101000005','林俊杰','男','1988-6-6','2010-9-1','010106','02','0301')

go

insert into 学生表values('010101000006','孙燕姿','女','1984-5-3','2010-9-1','010107','03','0302')

go

insert into 学生表values('010101000007','周华健','男','1986-8-6','2010-9-1','010108','04','0401')

go

insert into 学生表values('010101000008','尚雯婕','女','1988-6-6','2010-9-1','010109','01','0402')

go

insert into 学生表values('010101000009','任贤齐','男','1984-5-3','2010-9-1','0101010','02','0101')

go

insert into 学生表values('010101000010','魏晨','男','1986-8-6','2010-9-1','0101011','03','0102')

go

insert into 学生表values('010101000011','庞龙','男','1988-6-6','2010-9-1','010101','04','0103')

go

insert into 学生表values('010101000012','刘若英','女','1988-5-3','2010-9-1','010102','01','0201')

go

insert into 学生表values('010101000013','李圣杰','男','1989-8-6','2010-9-1','010103','02','0202')

go

insert into 学生表values('010101000014','吴克群','男','1989-2-9','2010-9-1','010104','03','0301')

go

 

--连接查询(交叉查询)

select * from 学生表cross join 班级表

--列举学生表中的学生姓名和性别班级表中的班级名称

select 学生表.姓名,学生表.性别,班级表.班级名称from 学生表cross join 班级表

--将两个表中相同列合为一列

select 学生表.姓名,学生表.性别,班级表.班级名称from 学生表cross join 班级表where 学生表.班级代码=班级表.班级代码

--自然连接

select * from 学生表join 班级表on 学生表.班级代码=班级表.班级代码

select 学生表.姓名,学生表.性别,班级表.班级名称from 学生表join 班级表on 学生表.班级代码=班级表.班级代码

--表的自身连接(需要取别名)

select a.姓名,b.性别from 学生表as a join 学生表as b on a.学号=b.学号

--外连接表查询

create table 产品

(

    产品编号char(9) not null,

    产品名称varchar(20) not null

)

create table 产品销售

(

    产品编号char(9) not null,

    销量int

)

insert into 产品values('001','显示器')

insert into 产品values('002','键盘')

insert into 产品values('003','鼠标')

insert into 产品销售values('001','25')

insert into 产品销售values('003','35')

insert into 产品销售values('005','30')

select * from 产品

select * from 产品销售

--左外连接

select * from 产品left join 产品销售on 产品.产品编号=产品销售.产品编号

--右外连接

select * from 产品right join 产品销售on 产品.产品编号=产品销售.产品编号

--完全外连接

select * from 产品full join 产品销售on 产品.产品编号=产品销售.产品编号

--合并结果集(把重复的过滤掉了)

select 姓名,性别,出生日期from 学生表union select 姓名,性别,出生日期from 学生表

七.数据库中多表查询和子查询

--多表连接查询

select 学生表.学号,学生表.姓名,学生表.性别,班级表.班级名称,专业表.专业名称,系部.系部名称from 学生表join 班级表on 学生表.班级代码=班级表.班级代码join 专业表on 学生表.专业代码=专业表.专业代码join 系部on 学生表.系部代码=系部.系部代码

select 学生表.学号,学生表.姓名,学生表.性别,班级表.班级名称from 学生表join 班级表on 学生表.班级代码=班级表.班级代码and 性别='男'

--嵌套查询(子查询)带有IN运算符的子查询,in运算符的子查询返回的结果是集合

select * from 系部

select * from 学生表where 系部代码='01'

select * from 学生表where 班级代码in (select 班级代码from 班级表where 专业代码in (select 专业代码from 专业表where 系部代码in (select 系部代码from 系部where 系部代码in(01))))

--带有比较运算符的子查询

select * from 学生表where 出生日期> (select 出生日期from 学生表where 姓名='刘德华')

--带有any(满足条件中的任何一个)或all(大于结果中的所有值)的子查询

select * from score_table order by 高考分数desc

select * from score_table where 高考分数> any (select 高考分数from score_table where 学号in(29,25))

select * from score_table where 高考分数> all (select 高考分数from score_table where 学号in(29,25))

--带有exists运算符的子查询

select * from score_table where exists (select * from score_table where 名称='张学友')

八.数据库中数据完整性约束

select * from 系部where 系部代码=(select 系部代码from 专业表where 专业代码=(select 专业代码from 班级表where 班级代码=(select 班级代码from 学生表where 姓名='刘德华')))

--使用sql语句创建唯一约束

alter table 系部add constraint wywy unique nonclustered(系部名称)

--创建检查约束

alter table score_table add constraint ck_name check(高考分数>300 and 高考分数<600)

--创建默认约束

alter table new_table add constraint df default '我叫徐守威' for '其他'

--删除约束

alter table score_table drop constraint ck_name

九.数据库中数据规则

--创建规则

create rule gz as @a>300 and @a<600

--绑定规则

execute sp_bindrule 'gz','new_table.高考分数'

select * into new_table1 from new_table

--解除规则

execute sp_unbindrule 'new_table.高考分数'

--删除规则

drop rule gz

--创建默认

create default df_name as '男'

--绑定默认

execute sp_bindefault 'df_name','学生表.性别'

--解除默认

execute sp_unbindefault 'df_name','学生表.性别'

--删除默认

drop default df_name

十.数据库中索引

--创建索引

create clustered index 索引名on score_table(名称)

create unique clustered index 索引名on score_table(名称)

--查看索引信息

execute sp_helpindex score_table

--删除索引

drop index score_table.索引名

十一.数据库视图

--创建视图

create view v1 as

select 学生表.学号,学生表.姓名,班级表.班级名称,专业表.专业名称,系部.系部名称from 学生表

join 班级表on 学生表.班级代码=班级表.班级代码

join 专业表on 班级表.专业代码=专业表.专业代码

join 系部on 专业.系部代码=系部.系部代码

--创建一般视图

create view 视图名

as

select * from score_table

--创建加密视图

create view 加密视图名

with encryption

as

select * from score_table

--创建视图及表的架构绑定

create view 视图及表的架构绑定

with schemabinding

as

select 姓名,性别from dbo.score_table

--在视图中增加、删除数据

insert into 视图名(姓名,性别) values('xushouwei','男')

update 视图名set 姓名='徐守威' where 姓名='xushouwei'

delete 视图名where 姓名='徐守威'

--删除视图

drop view 视图名

--系统存储过程查看视图

execute sp_helptext 视图名

十二.数据库存储过程

--创建存储过程

create procedure p

as

select 学生表.学号,学生表.姓名,班级表.班级名称,专业表.专业名称,系部.系部名称from 学生表

join 班级表on 学生表.班级代码=班级表.班级代码

join 专业表on 班级表.专业代码=专业表.专业代码

join 系部on 专业表.系部代码=系部.系部代码

--执行存储过程

execute p

--为存储过程加上参数

create procedure p1

@sex varchar(10),

@id varchar(10)

as

select 学生表.学号,学生表.姓名,班级表.班级名称,专业表.专业名称,系部.系部名称from 学生表

join 班级表on 学生表.班级代码=班级表.班级代码and 学生表.性别=@sex

join 专业表on 班级表.专业代码=专业表.专业代码

join 系部on 专业表.系部代码=系部.系部代码and 系部.系部代码=@id

--执行带参数的存储过程

execute p1'男','01'

--创建带返回参数的存储过程

create procedure p2

@name varchar(10),

@getnum varchar(10) output

as

select @getnum=学号from 学生表where 姓名=@name

--执行带返回参数的存储过程

execute p2'刘德华',''

--查看存储过程

execute    sp_helptext

execute    sp_depends

execute    sp_help

--删除存储过程

drop procedure p

drop procedure p,p1

drop procedure p,p1,p2

十三.数据库触发器

--创建触发器(执行插入操作)

create trigger myinsert

on 产品

for insert

as

declare @a char(10)

select @a=产品编号from inserted

insert into 产品销售values(@a,0)

--查询触发器中的数据

select * from 产品

select * from 产品销售

--执行插入操作

insert into 产品values('01','电视')

insert into 产品values('02','电脑')

--创建触发器(执行删除操作)

create trigger mydelete

on 产品

for delete

as

declare @a char(10)

select @a=产品编号from deleted

delete 产品销售where 产品编号=@a

--执行删除操作

delete 产品where 产品编号='02'

--查看触发器信息

execute sp_helptrigger 产品

--删除触发器

drop trigger myinsert

drop trigger mydelete

十四.数据库函数

--avg求平均分函数

select 班级名称,avg(学生表.高考分数) from 班级表join 学生表on 班级表.班级代码=学生表.班级代码group by 班级名称

--max求最大值

select 班级名称,max(学生表.高考分数) from 班级表join 学生表on 班级表.班级代码=学生表.班级代码group by 班级名称

--min求最小值

select 班级名称,min(学生表.高考分数) from 班级表join 学生表on 班级表.班级代码=学生表.班级代码group by 班级名称

--abs(x)返回绝对值

select abs(-8)

--ceiling(x)返回大于或等于所给数字的最小整数

select ceiling(3)

--pi()(pi值)

select pi()

--power(x,y)返回x的y次方

select power(2,3)

--rand()返回~1之间的随机数

select rand()

--返回数据库的版本号

select @@version

--获取当前语言

select @@language

--当前时间

select getdate()

--取出天

select day(getdate())

--取出月

select month(getdate())

--取出年

select year(getdate())

--加三天(“d”表示天,“m”表示月,“y”表示年)

select dateadd(d,3,getdate()) as Jasxu_dateadd

--取出时间的某一部分(“d”表示天,“m”表示月,“y”表示年)

select datename(d,'2013-12-12')

select datename(d,getdate())

--排名函数

select 姓名,rank() over(order by 高考分数desc) as 名次,高考分数from 学生表

posted @ 2013-12-11 20:51  徐守威  阅读(941)  评论(0编辑  收藏  举报