内蒙古科技大学课程设计说明书
3.3创建约束
3.3.1 创建Check约束
alter table tourist –-check约束,只能为男女 add constraint CK_tourist_sex check(tourist_sex='男' or tourist_sex='女'); alter table staff add constraint CK_staff_sex check(staff_sex='男' or staff_sex='女'); alter table tourist –-check约束,身份证号不能大于18位 add constraint CK_tourist_num check(tourist_num<999999999999999999); alter table hotel –-check约束,星级不能大于五位数 add constraint CK_hotel_star check(hotel_star<99999);
3.3.2 创建唯一约束
alter table tour –-唯一约束,团名称不能重复 add constraint UQ_group_name UNIQUE(group_name); 21
内蒙古科技大学课程设计说明书
3.3.3 创建外键
alter table tour add constraint route_id foreign key (route_id) references route(route_id); add constraint staff2_id foreign key (staff2_id) references staff(staff_id); alter table tour alter table hotel add constraint tourist_id foreign key (tourist_id) references tourist(tourist_id); alter table route add constraint group_id foreign key (group_id) references tour(group_id); alter table serve add alter table serve add constraint staff_id foreign key (staff_id) references staff(staff_id); constraint tourist1_id foreign key (tourist1_id) references tourist(tourist_id); alter table ticket add constraint tourist2_id foreign key (tourist2_id) references tourist(tourist_id); alter table tourist constraint group1_id foreign key (group1_id) references tour(group_id); alter table tourist add add constraint staff1_id foreign key (staff1_id) references staff(staff_id);
3.4 创建视图
3.4.1 单表视图
22
内蒙古科技大学课程设计说明书
创建名为view_tourist的单表视图
[travel agency] usego create view view_tourist as select tourist.tourist_id,tourist.tourist_name,tourist.tourist_num,tourist.tourist_address FROM tourist Go
3.4.2 多表视图
创建名为view_tr的多表视图
[travel agency] usego create view view_tr as select tour.group_id,group_name,group_person,route.route_id,route_name FROM tour,route; Go 3.5 创建存储过程
[travel agency] usego create procedure SelProc –-创建名为SelProc的存储过程 as select * from hotel; use [travel agency] go create procedure Count1Proc –-创建名为CountlProc的存储过程 as select * from route 23
内蒙古科技大学课程设计说明书
第四章 数据库运行与测评
4.1 数据库功能实现
4.1.1数据增加
增加员工编号为160,姓名为王凯,性别为男,电话为1122340,学历为大专,工作经历为自主创业,建立该旅行社;
增加员工编号为161,姓名为赵建华,性别为男,电话为1122341,学历为本科,工作经历为2012年大学毕业,担任旅行社经理秘书。
use [travel agency] values('160','王凯','男','1122340','大专','自主创业,建立该旅行社'), ('161','赵建华','男','1122341','本科','2012年大学毕业,担任旅行社经理秘书'); insert into staff(staff_id,staff_name,staff_sex,staff_tel,staff_academic,staff_work)
4.1.2 查询数据
查询修改后的员工信息:
use [travel agency] select * from staff
24