Sunny个人整理!(sunny我的英文名)
创建数据库
create database student
on
(name=student_data,
filename='c:/data/student_data.mdf',
size=3,
filegrowth=1,
)
log on
(name=stu_log,
filename='c:/../stu_log.ldf',
size=1,
maxsize=20,
filegrowth=10%)
--创建数据库,及数据文件和日志文件
创建表
create table stu_grade
(stu_id char(10) not null,
--stu_id char(10) not null primary key,--其他表直接定义主键]
course_id char(3) not null,
grade int,
constraint pk_no1 primary key(stu_id ,course_id),
constraint fk_stu_id foreign key (stu_id ) references stu_info(stu_id ),
constraint fk_course_id foreign key (course_id ) references course_info(course_id ),)
--创建表及主键外键
--修改表
alter table stu——info
add code char(18)
alter table stu——info
add constraint RM unique (code)--RM 是约束的名字
alter table stu——info
drop constraint RM
alter table stu——info
add constraint RM check (grade between 0 and 100)
alter table stu——info
drop constraint RM foreign key (course_id ) references course_info(course_id )
--删除表先删除约束
alter table stu_grade
drop constraint fk_stu_id
drop table stu_info
insert into stu_info
values('20081300408','张山','1988-01-26','f','河南郑州',.......
'..',' ..', .....
)
--插入数据
update stu_info
set sdept='会计学院'
where stu_id ='20081300408'
--更新表
delete from stu_info
where sex is null--必须是 is null 而不能是 =null
--删除数据
数据查询
select ....from 表名
where......
--简单查询
select s.stu_id ,name,c.course_name, g.grade
from stu_info s join stu_grade g on s.stu_id=g.stu_id
join course_info c on c.course_id=g.course_id
--多表查询语句
select name from stu_info
where adddress like '%阳%'
--模糊查询
select stu_id, name,sdept from stu_info
where stu_id not in /in (select stu_id from stu_grade where course_id =001)
--查询没有选修001 这门课的学生
select avg(mark) from stu_info where sdept='信息学院'--平均成绩
select max(mark),min(mark) from stu_info--最大成绩,最小成绩
select sdept,sex ,count(*) from stu_info where sdept='信息学院' and sex='女'
group by sdept ,sex--信息学院女生人数
创建索引
create nonclustered index 索引名SY on course_infor (course_name)--非聚焦索引
create nonclustered index SY on stu_grade (stu_id,course_id )--创建复合索引
drop index stu_grade.SY--删除索引 (表名.索引名)!
create unique index SY on course_info(course_name)--修改 sy 为唯一索引
sp_helpindex course_info --查看 SY索引 信息
创建视图
create view view_name as select ....from.....where..group by..
--创建视图
create view view_name
as
select sdept ,count(*) '总人数' from stu_info
where sdept='信息学院'
group by sdept
--创建视图显示信息学院的总人数
alter view view_name with encrypion
as
select sdept ,count(*) '总人数' from stu_info
where sdept='信息学院'
group by sdept
--修改为加密视图
drop view view_name--删除视图
创建存储过程
create procedure pro_name @xh char(10)
as
select count(*) from stu_gread
where grade<60 and stu_id=@xh
--创建存储过程pro_name
drop procedure pro_name--删除存储过程
pro_name '200707001'--调用存储过程
触发器
create trigger tr1
on stu_info
after delete
as
delete from stu_grade where stu_id=(select stu_id from deleted)
--级联删除
create trigger tr2
on stu_info
after update
as
update stu_grade set stu_id=(select stu_id from insered )
where stu_id=(select stu_id from deleted)
--级联更新
select * into xs2 from stu_info where 1=2--创建表xs2作为储存开除学生的表
create trigger tr3
on stu_grade
for update ,insert
as
declare @m int
select @m=count(*) from stu_grade
where grade<60 and stu_id =(select stu_id from inserted )
if (@m>4)
insert into xs2 select * from stu_info
where stu_id =(select stu_id from inserted)
--(for和after为后触发器)
drop trigger tr3--删除触发器
数据库安全
sp_addlogin 'login_name','密码','默认数据库'--创建登录账户
sp_droplogin 'login_name'--删除登录账户
sp_addrole 'role_name'--创建数据库角色
sp_droprole 'role_name'--删除角色
backup database stu_info to sale_student--备份数据库
restore database stu_info from sale_student--还原数据库