标签:
--启用 xp_cmdshell
exec sp_configure ‘show advanced options‘, 1
reconfigure
exec sp_configure ‘xp_cmdshell‘, 1
reconfigure
go
--调用doc命令
exec xp_cmdshell ‘md E:\qlzx‘, no_output
--判断数据库studentDB是否存在
if exists (select * from sys.sysobjects where [name] = ‘studentDB‘)
drop database studentDB --删除数据库
go
--创建数据库studentDB
create database studentDB
on
(
name = ‘student_mdf‘,
filename = ‘E:\qlzx\student_mdf.mdf‘,
size = 3mb,
maxsize = 100mb,
filegrowth = 1mb
)
log on
(
name = ‘student_ldf‘,
filename = ‘E:\qlzx\student_ldf.ldf‘,
size = 3mb,
maxsize = 100mb,
filegrowth = 1mb
)
go
--使用数据库studentDB
use studentDB
go
--创建学生信息表(studentInfo)
create table studentInfo
(
studentId int not null identity(1,1) primary key, --学生编号(主键约束)
studentNo char(10) not null unique, --学生学号(唯一约束)
studentName nvarchar(6) not null, --学生姓名
studentSex char(1) not null, --学生性别
studentBirthday smalldatetime not null, --学生生日
studentAddress nvarchar(50) not null default ‘地址不详‘, --学生地址(默认约束)
studentMobile char(11) not null check(len(studentMobile)=11), --学生电话(检查约束)
classNo int --班级编号
)
go
--创建学生成绩表(score)
create table score
(
studentId int not null foreign key references studentInfo(studentId) --外键约束
)
go
--添加一条数据
insert into studentInfo values(‘20151120‘,‘张三‘,‘M‘,‘1994-08-08‘,default,‘15875237666‘,1)
go
insert into studentInfo values(‘20151123‘,‘张三‘,‘F‘,‘1994-08-08‘,default,‘15875237666‘,1)
go
--添加多条数据
insert into studentInfo
select ‘20151121‘,‘晓红‘,‘F‘,‘1994-07-09‘,‘揭阳市‘,‘15875237777‘,2 union all
select ‘20151122‘,‘李民‘,‘M‘,‘1994-10-07‘,‘东莞市‘,‘15875237888‘,3
go
--修改数据
update studentInfo set studentName = ‘草莓‘ where studentId = 1
go
--删除数据
delete from studentInfo where studentAddress = ‘揭阳市‘
go
--查询所有数据
select * from studentInfo
go
--查询部分列
select studentNo 学号, studentName 姓名, studentSex 性别 from studentInfo
go
--使用表达式查询
select studentNo, studentName, datediff(YY,studentBirthday,GETDATE()) from studentInfo
go
--使用all和distinct,是否去除重复
select all studentSex from studentInfo
go
select distinct studentSex from studentInfo
go
--限制固定行数
select top 2 * from studentInfo
go
--percent(百分比)
select top 50 percent * from studentInfo
go
--空值查询
select * from studentInfo where studentAddress is null
go
--模糊查询
select * from studentInfo where studentName like ‘草%‘
go
--and(并) or(或) not(取反)
select * from studentInfo where studentName = ‘草莓‘ and studentSex = ‘M‘
go
select * from studentInfo where studentName = ‘草莓‘ or studentSex = ‘F‘
go
select * from studentInfo where not studentName = ‘草莓‘
go
--order by(排序) desc--降序 asc--升序
select * from studentInfo order by studentBirthday asc
go
--不使用 with ties 就无法显示并列数据,反之可以
select top 1 * from studentInfo order by classNo asc
go
select top 1 with ties * from studentInfo order by classNo asc
go
--group by(分组)
--聚合函数(sum--总和 max--最大值 min--最小值 avg--平均值 count--数量)
select Avg(studentId) from studentInfo
go
--into子句(创建新表并将查询到的结果插入到新表中)
select * into studatTemo from studentInfo
go
--内联接
select * from studentInfo s inner join score c on s.studentId = c.studentId
go
标签:
原文地址:http://www.cnblogs.com/709481260qq/p/4986471.html