本文出自:
实验目标:熟悉实体完整性,参照完整性,事务的处理;
/*1.在数据库school表中建立表Stu_uion,进行主键约束,在没有违反实体完整性的前提下插入并更新一条记录*/Use school create table stu_uion( sno char(5) not null unique, sname char(8), ssex char(1), sage int, sdept char(20), constraint pk_stu_uion primary key (sno));insert stu_uion values('10000', 'Wangmin', '1', 23, 'CS');update stu_uion set sno = ' ' where sdept = 'CS';update stu_uion set sno = '92002' where sname = 'Wangmin';select * from stu_uion;/*2.3.演示违反实体完整性的插入,更新操作*/Use schoolinsert stu_uion values('10000', 'Li hua', '1', 23, 'CS');/*unique*/update stu_uion set sno = NULL where sno = '10000'; /*not null*//*4.演示事务的处理,包括事务的建立,处理,以及出错事务的回退*/Use schoolset xact_abort on/*设置xact_abort 为on时,如果transaction(事务)语句出现错误,那么整个事务都会回滚 *如果设置其为off时,只回滚出错的语句*/begin transaction t1insert into stu_uion values('95009', 'Li yong', 'M', 25, 'EE');insert into stu_uion values('95003', 'wang hao', '0', 25, 'EE');insert into stu_uion values('95005', 'wang hao', '0', 25, 'EE');select * from stu_uion ;commit transaction t1/*5.通过建立scholarship表 ,插入数据,演示当前与现有的数据环境不等时,无法建立实体完整性以及参照完整性*/Use schoolCreate table Scholarship( M_ID varchar(10), Stu_id char(10), R_money int);insert into scholarship values('0001', '700000', 5000);insert into scholarship values('0001', '800000', 5000);select * from scholarship;/*constraint*/Use schoolalter table scholarship addconstraint pk_scholarship primary key(M_ID);/*pk: primary key*//*存在两个0001,无法建立主键约束*//**scholarship中的数据,不满足stu_id和students表中的sid对应性,创建参照完整性失败*/Use schoolalter table scholarship addconstraint fk_scholarship foreign key (Stu_id) references students(sid);
作者:svitter 发表于2014-5-5 15:12:29
阅读:142 评论:0