create database database2 数据库名称
On
 (
   name='database2_dat',名字
   filename='C:\Program Files\Microsoft SQL Server\MSSQL\Data\database2_dat.mdf',路惊
   size=10,maxsize=unlimited,filegrowth=10%文件大小最大只增长方式
 )
 log on 日志文件
(
  name='database2_log',
  filename='C:\Program Files\Microsoft SQL Server\MSSQL\Data\database2_log.ldf',
  size=1,maxsize=5,filegrowth=1
)
数据库文件和日志文件语句。。
 

filegroup fg1
 (  name='database52_data',
  filename='c:\program files\microsoft sql server\mssql\data\dddd_data.ndf'
  size=10,maxsize=20,filegrowth=5
 )
文件组文件语句。。必须要在数据库名称之后才能生效。。文件组包括在数据库里面。。
      2  重命名。。把WOKAO 文件改为LIU文件 语句
             方法一sp_renamedb wokao , liu 
             方法二  alter database liu
                   modify name=wokao   


             数据管理

 

      --创建数据库
create database liushuai
 on
 (name='liushuai',
  filename='c:\program files\microsoft sql server\mssql\data\liushuai_dat.mdf',
  size=10,
  maxsize=20,
  filegrowth=10%
  )
 use liushuai
--创建学生表
create table liushuai
 (
   sno int primary key, --学号。为整数,主键
   sname varchar(10) not null,--姓名为可变不为空
   stel char(12), --电话为固定12位
   sage smallint check(sage>=18 and sage<=25),--约束于在18-25之间
 ssex bit default(1)  --性别
   )
 --查看对象
 select * from liushuai 
--使用增量
 create table cs
 (sno int identity(1,1)
 )
--修改基本表
alter table }{--创建数据库
create database liushuai
 on
 (name='liushuai',
  filename='c:\program files\microsoft sql server\mssql\data\liushuai_dat.mdf',
  size=10,
  maxsize=20,
  filegrowth=10%
  )
 use liushuai
--创建学生表
create table liushuai
 (
   sno int primary key, --学号。为整数,主键
   sname varchar(10) not null,--姓名为可变不为空
   stel char(12), --电话为固定12位
   sage smallint check(sage>=18 and sage<=25),--约束于在18-25之间
 ssex bit default(1)  --性别
   )
 --查看对象
 select * from liushuai
--使用增量
 create table cs
 (sno int identity(1,1)
 )
--修改基本表
alter table <表名>
add [字段名 数据类型]--增加新量
alter table [表名]
drop column[列名]--删除该列
alter table [表明]
alter column[列名  类型]--修改该列类型
--课程表
create table course
 (
  cno int primary key,--课程号
  cname varchar(10)not null,--课程名不为空
  cpno int identity(2,1),--上一课程号,必须完成上一个号才可以完成该号
  ccredit decimal(4,1),--学分在111.1规定内
  cutime smallint --课时数为整数
  )
--学生选课表
create table sc 
(
  sno int identity(1,1),  --学号
  cno int unique,         --课程号唯一不允许取重复直
  grade decimal(4,1)      --成绩
)
add sbig int--增加新量
alter table liushuai
drop column sbig--删除该列
alter table liushuai
alter column sname varchar(11)--修改该列类型
--课程表
create table course
 (
  cno int primary key,--课程号 为主键(contraint pk primary key(cno,cpno)设这俩个为主键。只是俩个属性来表示。一个表中只能有个一主键,可以用俩个属性来表示。。呵呵)
  cname varchar(10)not null,--课程名不为空
  cpno int identity(2,1),--上一课程号,必须完成上一个号才可以完成该号
  ccredit decimal(4,1),--学分在111.1规定内
  cutime smallint --课时数为整数
  )
--学生选课表
create table sc 
(
  sno int identity(1,1),  --学号
  cno int unique,         --课程号唯一不允许取重复直
  grade decimal(4,1)      --成绩
)
      单行的修改。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
create database student
use student
create table student
(
   sno int primary key,
   sname varchar(10)not null,
   ssex bit default(1),
   stel char(12),
   sage smallint check:(sage>=18 and sage<=25)
  )
--插入数据
  insert into student(sno,sname)
  values (1,'李一')
  insert into student(sno,sname,ssex,stel,sage)
  values (2,'晓亮',1,123456123123,19)
--增加新列
 alter table wokao
 add stel char
--把student的stel插入到wokao
select * from student

 insert into wokao(stel)
 select stel
 from student

select sage不需要事先见表
into aaaa
from student

 


insert into wokao
 select sname,stel
 from student
--把都为空的年龄该为20
update student set sage=20
where sage is null
--当年龄为22时改为23
update student set sage=23
where sage=22
--删除年龄为23时的一行。。。。
delete from student
where sage=23
--所有年龄+1
update student
set sage=sage+1 
            约束规则。。。。。。。。。。。。。。。。。。。。。。。

create database student
use student
create table liushuai
(
  sno int,
  sname varchar(10),
  schengji decimal(4,2),
  ssex bit default(1),
  stel char(12),
  sage smallint check:(sage>=18 and sage<=25),
  constraint pk primary key (sno)  --设置sno 为主键
)
--查看主键信息
sp_pkeys liushuai
--浏览表中的所有约束建
sp_helpconstraint liushuai
--删除主键
alter table liushuai
drop constraint pk
sp_pkeys liushuai
--添加主键
alter table liushuai
add constraint pk primary key (sno)
--查看主键
sp_pkeys liushuai
--添加唯一建
alter table liushuai
add constraint up unique (sname) --姓名添加唯一建
alter table liushuai with nocheck

--禁用约束
alter table liushuai
nocheck constraint CK__liushuai__sage__7C8480AE
--禁用约束然后添加不符合要求的
insert into liushuai
values (2,'王2',82,1,123456789123,15)

--启用约束
alter table liushuai
check constraint CK__liushuai__sage__7C8480AE

 

drop table liu

create table liu
(
 sno int,
 sname varchar(10),
 stel char(12),
constraint fk foreign key(sno) references liushuai(sno)
on delete cascade
 )


alter table liu
drop  constraint CK
select * from shuai
--连续添加多个数据
insert into liushuai(Sno,sname)
select 9,'saf' union
select 2,'sg' union
select 3,'ags'

insert into liu(Sno)
select 1 union
select 2 union
select 9

alter table liu with nocheck
add constraint CK check(sno>0 and sno<5)

insert into liu(Sno)
select 12 union
select 0
                              使用规则。。。。。。。。。。。。。。。。。。。。。。
--使用规则
-创建一个表格
create table shuai
(
 sno int,
 sname varchar,
 gongzi int
)
--使用一个规则
create rule gongzi_rule
as @gongzi>=1000 and @gongzi<=1500
--绑定规则
sp_bindrule gongzi_rule,'shuai.gongzi'
 --添加数据
insert into shuai(gongzi)
values (500)
--解除规则
sp_unbindrule 'shuai.gongzi'
--列如
create table wang
(
 sno int primary key,
 sname varchar(10),
 sage smallint check:(sage>=18 and sage<=25)
)
create rule xingming_rule
as @xingming in('刘')
 sp_bindrule xingming_rule,'wang.sname'
insert into wang(sno,sname)
values(2,'刘')
create rule xing
--建立以个以姓刘的名字g规则
--先建立一个表格
create table yang
(
 sname varchar(12)
)
--第二建立一个规则
create rule ming
as @ming like '刘%'
--绑定规则
sp_bindrule ming,'yang.sname'
--添加数据
insert into yang(sname)
values ('刘帅')
select * from yang
--不符合要求
insert into yang(sname)
values ('杨华')

 

 

 

 

 

--删除student表中的主键约束
sp_pkeys student
alter table sc
drop constraint FK_SC_Student
alter table student
drop constraint PK_Student
select * from student
--向student表添加主键约束
alter table student
add constraint PK_student primary key(Sno)
--
create table stu
(
 Sno int,
 Sname varchar(10)
)
--向表中添加主键约束
alter table stu
alter column sno int not null

alter table stu
add constraint PK_stu primary key(sno)
--向student中sage列添加检查约束,不检查之前的数据
select * from student
alter table student with nocheck
add constraint CK_student check(sage>18 and sage<25)
insert into student(sage,Sno)
values (12,2005007)
alter table student
nocheck constraint CK_student

--禁用course表中的所有约束
alter table course
nocheck constraint all
--创建规则
create rule rule_sc
as
@score>=60 and @score<=100
--绑定规则
select * from sc
sp_bindrule rule_sc,'sc.grade'
--检验是否已绑定
insert into sc
values(2005005,4,59)
--解除绑定
sp_unbindrule 'sc.grade'
--创建邮箱数据类型'%@%'
sp_addtype 'Email','varchar(50)'
--创建规则
create rule rule_Email
as
@email like '%@%'
--将规则绑定到Email数据类型上
sp_bindrule 'rule_Email','Email'
--检验
create table tongxunlu
(
 Sname varchar(10),
 Semail Email
)
--插入记录,可插入
insert into tongxunlu
values('aa','wanghy_0716@163.com')
select * from tongxunlu
--插入一条记录
insert into tongxunlu
values('aa','wanghy_0716')
--不能插入,因为不符合Email数据类型的要求
--解除绑定
sp_unbindrule 'Email'
drop rule rule_Email

create table test8
(
  xingming varchar(20),
  xingbie varchar(2),
  nianling smallint
)
--创建默认值
Create   default     default_xb

As      '男'
--将默认值绑定到test8.xingbie
SP_bindefault default_xb,'test8.xingbie'
--单表查询
--显示表中所有行和列
select * from student
--显示部分列(查询所有学生的姓名和年龄)
select Sname,Sage
from student
--显示部分列和部分行(查询性别为男的学生姓名)
select Sname
from student
where Ssex=1

select * from sc
--查询学生的成绩(成绩+5)
select Grade+5
from sc
--查询所有学生的年龄(加1)
select Sname as 姓名,Sage+1 as 年龄
from student
--查询2005001的成绩
select sno 学号,grade 成绩
from sc
where sno=2005001

--查询前三个学生的信息
select top 3 * from student
--查询前50%的学生信息
select top 50 percent *
from student
--查询前50%的男生学生信息(姓名)
select top 50 percent Sname
from student
where Ssex=1
--查询计算机专业的学生信息
select * from student
where Sdept='计算机专业'
--显示面积大于300的仓库记录
use cangku
go
select * from 仓库
where 面积>300

--显示工资在600到1230之间的职工信息
select * from 职工
where 工资>600 and 工资<1230
select * from 职工
使用计算列。。。。。。。。。。。。。。。
销售价格降低0.5 price-price*0.5
select grade from sc
select grade-grade*0.5 from sc  成绩降低0.5
                         嵌套查询。。。。。。。。。。。。。。。。。。。。
select 列名 新列名,列名
from 表名,表名2
where 条件表达式
group by 列名
having 条件表达式(可以包含聚合函数)
order by 列名1 asc,列名2 desc
--查询每个学生的平均成绩
select sno,avg(grade) as 平均成绩
from sc
group by sno
--查询平均成绩在前三名的学生信息
select top 3 sno,avg(grade) 平均成绩
from sc
group by sno
order by avg(grade) desc

select * from student
--查询与刘晨同一专业的学生姓名
select sname from student
where sdept=
(
 select sdept from student
 where sname='刘晨'
) and sname<>'刘晨'
--查询计算机专业、生物专业和数学专业的学生姓名
select sname,sdept
from student
where sdept in('计算机专业','生物专业','数学专业')

--查询没参加考试的学生名单
select sname
from student
where sno in
(--找出SC表中没参加考试的学生学号
 select sno from sc
 where grade is null
)
--查询选修了一号课程的学生姓名和专业
select Sname,Sdept
from student
where sno in
(--找选修1号课程的学号
 select sno from sc
 where cno=1
)
--查找哪些城市至少有一个仓库的职工的工资为1250
select 城市
from 仓库
where 仓库号 in
(
 select 仓库号 from 职工
 where 工资=1250
)

use student
go
--参加考试的学号名单和成绩
select student.Sname,sc.Grade
from student,sc
where student.sno=sc.sno and grade is not null
--查询既不是计算机专业,也不是生物专业、数学专业的学生姓名
select sname from student
where sdept!='计算机专业' and sdept!='生物专业' and sdept!='数学专业'
--方法二
select sname from student
where sdept not in('计算机专业','生物专业','数学专业')
--判断cangku数据库是否存在,如果存在提示
--”数据库已存在"
use master
go
if exists(
 select * from sysdatabases
 where name='cangku'
)
print '数据库已存在'
--检查本次考试,本班如果没有人成绩达到
--90分以上,则每人减5分;
use student
go
if  exists(
 select sno from sc
 where grade>90
)
update sc
set grade=grade-5

select * from sc
--检查本次考试,本班如果没有人成绩达到
--95分以上,则每人加5分;
if not exists(
 select sno from sc
 where grade>95
)
update sc
set grade=grade+5
select * from sc
--检索有职工的工资大于或等于WH2仓库中
--其中一名职工工资的职工信息

use cangku
go
select 职工号,工资
from 职工
where 工资>any(
  select 工资 from 职工
  where 仓库号='wh2'
       )
--检索有职工的工资大于或等于WH2仓库中
--所有职工工资的职工信息
select * from 职工
where 工资>=all(
  select 工资 from 职工
  where 仓库号='wh2'
       )
--查询年龄大于20和专业为计算机专业的
--学生姓名、年龄
select sname,sage from student
where sage>20
union
select sname,sage from student
where sdept='计算机专业'

--1、找出和职工e4挣同样工资的所有职工信息
use cangku
select * from 职工
where 工资 in (
 select 工资 from 职工
 where 职工号='e4'
) and 职工号!='e4'

2、找出管理的仓库面积在300以下的职工信息
select *from 职工
where  仓库号 in(
      select 仓库号 from 仓库
      where 面积<300
)
--3、找出没有参加考试的学生姓名
 use student
 go
 select student.sname,sc.grade
 from student, sc
 where grade is null and student.sno=sc.sno
4、找出平均分最高的学生姓名
select Sname from student
where sno in
(
 select top 1 sno from sc
group by sno
order by avg(grade) desc
)


5、查询所有成绩都及格的学生信息
(可以毕业的学生信息)
 
  select distinct sno
  from sc
  where sno not in(
  select sno from sc
  where grade is null
       )
  group by sno
  having min(grade)>60

--9、求出选修了3门以上课程且所有成绩高于70分
--的学生的平均年龄
--找出选修了3门以上课程的学号
select avg(sage) from student
where sno in
(
 select sno
 from sc
 where sno not in(select sno from sc where grade is  null)
 group by sno
 having count(cno)>3 and min(grade)>70
)
select * from student


10、求出选修了‘数据结构’课程的男学生信息
(假设性别1的为男学生)
select * from student
where sno in(select sno from sc
             where cno in(
                         select cno from course
                         where cname='数据结构')) and ssex=1

11、查询选修了课程并且有考试成绩的学生的总人数
 
        接连查询。。。。。。。。。。。。。。。。。。。。
--7\查询没有选修1号课程的学生姓名
if exists(select cno from sc where cno=1)
select sname from student
where sno not in
(
 select sno from sc
 where cno=1
)
                    
select * from sc
8、求出选修了3门以上课程且所有成绩
高于75分的学生的最小年龄
select top 1 sage from student
where sno in
(
 select sno from sc
 where sno not in (select sno from sc
   where grade is  null
   )
  group by sno
 having count(cno)>3 and min(grade)>75
)
order by sage
select * from sc
9、求出选修了‘电子商务专业’课程(数据库和ASP)
的男学生信息
select * from student
where ssex=1 and sno in
(
 select sno from sc
 where cno in(
   select cno from course
   where cname='数据库'or cname='ASP语言'
   )
)
10 、将计算机专业全体学生的成绩每人增加3分
select grade+3 加分后的成绩 from sc
where sno in
(
 select sno from student
 where sdept='计算机专业'
)
11、删除计算机专业所有学生的选课记录
delete from sc
where sno in
(
 select sno from student
 where sdept='计算机专业'
)
select * from sc
12、查询选修了全部课程的学生姓名
select sname from student
where sno in
(
 select sno from sc
 group by sno
 having count(cno)=(
   select count(cno) from course
   )
)
select * from sc
12、查询选修了全部课程的学生姓名
分析:一、每个学生选修的课程数目
 select sno,count(cno) from sc
 group by sno
   二\课程表中总的课程数目
      select count(cno) from course
     三、判断一和二的结果是否相等,如果相等
 说明该学生选了所有科目
    
     四\根据学生学号找姓名
    select sname from student
 where sno in
(
 select sno from sc
 group by sno
 having count(cno)=(select count(cno) from course)
)

drop database student
13、查询至少选修了学生2005002选修全部课程
的学生学号(即所选修课程包含或者
等于2005002学生的课程)
分析:找出2005002选修的课程,然后找出哪个学生包含
 了2005002选的所有课程

select sno from sc
where cno  in
(
select cno from sc
where sno=2005002
) and sno!=2005002
group by sno
having count(cno)>=
(
 select count(cno) from sc
 where sno=2005002
)
 (a+b)*(c+d+e)=ac+ad+ae+bc+bd+be
select * from course,sc
--所有学生的选课情况
select sname,cname,grade
from student,course,sc
where student.sno=sc.sno and course.cno=sc.cno
--

select * from student
insert into course(cname)
values('linux')

insert into student(sno,sname)
values(2005007,'万雨晨')
查询学生的选课情况(内联接) 所关联的数据如果出现null则该数据不显示
select Sname,cno,grade
from student
inner join sc
on student.sno=sc.sno
--查询所有学生的选课情况(左联接)第一张表的内容全显示。第二满足的显示不满足的不显示
select Sname,Cno,grade
from student S
left join sc
on S.sno=SC.sno
--查询所有学生的选课情况(右联接)第一满足的显示第二的全显示
select Sname,Cno,grade
from sc
right join student S
on S.sno=SC.sno