SQL Server 复习 增删改查 视图 事务 存储过程 触发器
2013-11-13 21:29
507 查看
drop table classInfo;
create table classInfo
(
classId int not null ,
className varchar(30) not null,
classCount int
)
--插入一条数据
insert into classInfo(classId,className,classCount) values(1,'JAVA1班',32);
insert into classInfo(classId,className,classCount) values(2,'JAVA2班',22);
insert into classInfo(classId,className,classCount) values(3,'Android',40);
--删除一条数据
delete from classInfo where classId=1;
--更改一个字段
update classInfo set classCount=24 where classId = 1;
select * from classInfo
--添加一个字段
alter table classInfo add classGrade int null
--删除一个字段
alter table classInfo drop column classGrade
select * from stuInfo_tb
update stuInfo_tb set classId = 2 where stuId = 1003;
--删除一个字段的约束,再删除这个字段
alter table stuInfo_tb
drop constraint IX_stuTelNumber;
alter table stuInfo_tb
drop column stuTelNumber;
--设置classId为主键
alter table classInfo
add constraint PK_classId primary key(classId);
--设置stuInfo_tb的classId为外键,与classInfo表中的classId相对应
alter table stuInfo_tb add constraint FK_classInfo_stuInfo
foreign key(classId) references classInfo(classId)
--创建一个视图
if exists(select * from sysobjects
where name = 'view_stu')
drop view view_stu
go
create view view_stu
as
select stuNam,stuGender,className,classCount
from classInfo,stuInfo_tb where
classInfo.classId = stuInfo_tb.classId;
--创建一个视图
if exists(select * from sysobjects
where name = 'view_stu')
drop view view_stu
go
create view view_stu
as
select stuNam,stuGender,className,classCount
from classInfo inner join stuInfo_tb on
classInfo.classId = stuInfo_tb.classId;
--删除一个视图:
drop view view_stu;
--查询视图
select * from view_stu;
--事务:
-- 事务的四大特征:原子性,一致性,隔离性,永久性
-- 事务分为begin 、rollback、commit transaction三种情况
--创建一个事务
begin transaction
declare @count int
declare @count1 int
set @count = 0
set @count1 = 2
select * from classInfo where classId = @count1
set @count = @count + @@error
update classInfo set classCount = 43 where classId = @count1
set @count = @count + @@error
if(@count <> 0)
begin
rollback transaction
end
else
begin
commit transaction
end
--删除一个存储过程
drop procedure pro_stu;
--创建一个存储过程,不能给局部变量设置默认值,但可以给全局变量设置默认值
create procedure pro_stu
@classNo int = 1
as
select * from classInfo where classId = @classNo;
--创建一个存储过程,带输出参数
create procedure pro_cls
@classNo int = 1 ,
@className varchar(30) output
as
select @className = className from classInfo where classId = @classNo;
--执行存储过程
declare @ss varchar(30)
exec pro_cls 2,@ss output
print '----->'+ @ss
--触发器:
-- 是基于在一个表上,当该表发生delete、insert或者update
-- 中的一个动作,则触发对应的触发器并执行触发器;当表上建立了触发器,
-- 则在执行所对应的动作时,就会产生一个deleted表或者inserted表,通过该表可以
-- 查找到被删除或者插入这条记录的所有信息
--删除一个触发器:
drop trigger tri_Name
--创建一个触发器:
create trigger tri_Name
on stuInfo_tb
for delete
as
delete from classInfo where classId = (
select classId from deleted
)
select * from classInfo
select * from stuInfo_tb
insert into stuInfo_tb(stuNam,stuGender,stuAge,classId)
values('小花','男',23,3)
delete from stuInfo_tb where classId = 3
create table classInfo
(
classId int not null ,
className varchar(30) not null,
classCount int
)
--插入一条数据
insert into classInfo(classId,className,classCount) values(1,'JAVA1班',32);
insert into classInfo(classId,className,classCount) values(2,'JAVA2班',22);
insert into classInfo(classId,className,classCount) values(3,'Android',40);
--删除一条数据
delete from classInfo where classId=1;
--更改一个字段
update classInfo set classCount=24 where classId = 1;
select * from classInfo
--添加一个字段
alter table classInfo add classGrade int null
--删除一个字段
alter table classInfo drop column classGrade
select * from stuInfo_tb
update stuInfo_tb set classId = 2 where stuId = 1003;
--删除一个字段的约束,再删除这个字段
alter table stuInfo_tb
drop constraint IX_stuTelNumber;
alter table stuInfo_tb
drop column stuTelNumber;
--设置classId为主键
alter table classInfo
add constraint PK_classId primary key(classId);
--设置stuInfo_tb的classId为外键,与classInfo表中的classId相对应
alter table stuInfo_tb add constraint FK_classInfo_stuInfo
foreign key(classId) references classInfo(classId)
--创建一个视图
if exists(select * from sysobjects
where name = 'view_stu')
drop view view_stu
go
create view view_stu
as
select stuNam,stuGender,className,classCount
from classInfo,stuInfo_tb where
classInfo.classId = stuInfo_tb.classId;
--创建一个视图
if exists(select * from sysobjects
where name = 'view_stu')
drop view view_stu
go
create view view_stu
as
select stuNam,stuGender,className,classCount
from classInfo inner join stuInfo_tb on
classInfo.classId = stuInfo_tb.classId;
--删除一个视图:
drop view view_stu;
--查询视图
select * from view_stu;
--事务:
-- 事务的四大特征:原子性,一致性,隔离性,永久性
-- 事务分为begin 、rollback、commit transaction三种情况
--创建一个事务
begin transaction
declare @count int
declare @count1 int
set @count = 0
set @count1 = 2
select * from classInfo where classId = @count1
set @count = @count + @@error
update classInfo set classCount = 43 where classId = @count1
set @count = @count + @@error
if(@count <> 0)
begin
rollback transaction
end
else
begin
commit transaction
end
--删除一个存储过程
drop procedure pro_stu;
--创建一个存储过程,不能给局部变量设置默认值,但可以给全局变量设置默认值
create procedure pro_stu
@classNo int = 1
as
select * from classInfo where classId = @classNo;
--创建一个存储过程,带输出参数
create procedure pro_cls
@classNo int = 1 ,
@className varchar(30) output
as
select @className = className from classInfo where classId = @classNo;
--执行存储过程
declare @ss varchar(30)
exec pro_cls 2,@ss output
print '----->'+ @ss
--触发器:
-- 是基于在一个表上,当该表发生delete、insert或者update
-- 中的一个动作,则触发对应的触发器并执行触发器;当表上建立了触发器,
-- 则在执行所对应的动作时,就会产生一个deleted表或者inserted表,通过该表可以
-- 查找到被删除或者插入这条记录的所有信息
--删除一个触发器:
drop trigger tri_Name
--创建一个触发器:
create trigger tri_Name
on stuInfo_tb
for delete
as
delete from classInfo where classId = (
select classId from deleted
)
select * from classInfo
select * from stuInfo_tb
insert into stuInfo_tb(stuNam,stuGender,stuAge,classId)
values('小花','男',23,3)
delete from stuInfo_tb where classId = 3
相关文章推荐
- SQL SERVER 2005视图、索引、存储过程、触发器、游标及事务名词解释
- 【转】视图、索引、存储过程 、触发器、游标及事务
- sql server 2000/2005 判断存储过程、触发器、视图是否存在并删除
- 触发器,存储过程,视图,事务的语法
- MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句
- mysql事务、触发器、视图、存储过程、函数
- 基于mysql事务、视图、存储过程、触发器的应用分析
- DBGridEh控制单元格显示以及存储过程 触发器和视图以及事务的使用
- sql server 2000/2005 判断存储过程、触发器、视图是否存在并删除
- MySQL事务、存储过程、视图、自定义函数、触发器、事件的简单使用及理解
- MySql基础教程二(视图,存储过程,游标,触发器,事务,权限,数据类型)
- mysql 事务 视图 存储过程 触发器
- [转]sql server 2000/2005 判断存储过程、触发器、视图是否存在并删除
- python_fullstack数据库(五)-视图、触发器、存储过程、函数、事务、锁、备份
- Oracle数据库之视图、索引、存储过程、触发器、事务、函数
- 基于mysql事务、视图、存储过程、触发器的应用分析
- sql server 2000/2005 判断存储过程、触发器、视图是否存在并删除等
- 黑马程序员_学习日记50_619数据库开发及ADO.Net(临时表、表变量、视图、变量、if…else…和while、事务、存储过程、触发器)
- sql server 存储过程,触发器,事务
- 创建数据库、表、主外键、各种约束、存储过程、视图、索引、事务使用、触发器、创建登录账号、数据库用户 Sql语句示例