文档详情

第二次数据库实验-创建和修改数据表及数据完整性.doc

发布:2017-12-18约2.9千字共4页下载文档
文本预览下载声明
《数据库原理与应用》实验报告 (实验名称 :创建和修改数据表及数据完整性) 专 业 班 级 学 号  学生姓名 指导老师 怀化学院计算机科学与技术系 2111 年10 月 11日 《数据库原理与应用》实验报告 实验名称:创建和修改数据表及数据完整性 一、实验目的 熟悉有关数据表的创建和修改等工作,理解数据库模式的概念,了解主键约束、外键约束、UNIQUE约束和CHECK约束的创建和应用。要求学生熟练掌握使用企业管理器和T—SQL语句CREATE TABLE、ALTER TABLE及DROP TABLE语句对数据表进行管理。 在数据库company中创建以上五张表,并设置各表的主键。2.添加外键约束: 在销售主表sales的业务员编号字段saleid上添加外键约束,参照字段为员工表employee中的字段员工编号emp_no,约束名为FK_sale_id。在销售主表sales的客户号字段cust_id上添加外键约束,参照字段为客户表customer中的字段客户号cust_id,约束名为FK_cust_id。在销售明细表sale_item的订单编号字段order_no上添加外键约束,参照字段为销售主表sales中的字段订单编号order_no,约束名为FK_order_no。在销售明细表sale_item的产品编号字段prod_id上添加外键约束,参照字段为产品名称表product中的产品编号字段prod_id,约束名为FK_prod_id。添加核查约束: a) 将员工表employee中的薪水字段salary的值限定在1000至10000间,约束名为CK_salary。b) 将员工表employee中的员工编号字段emp_no设定为以“E”字母开头, 后面跟位数的编号,约束名为CK_emp_no。将员工表employee中的性别字段设定这取值只能是“男”和“女”。约束名为CK_sex。将销售主表sales中的发票号码字段invoice_no设定为以“I”字母开头,后面跟9位数的编号,约束名为CK_inno。.为销售主表sales中的字段发票号码invoice_no设置为唯一约束,约束名为UN_inno。 三、实验步骤与运行结果 crate database Company create table employee ( emp_no char(5) not null primary key, emp_name varchar(10) not null, sex char(2) not null, dept varchar(10) not null, title varchar(10) not null, date_hired datetime not null, biethday datetime null, salary int not null, telephone varchar(20) null, addr varchar(50) null, ) create table customer ( cust_id char(5) not null primary key, cust_name varchar(20) not null, addr varchar(40) not null, tel_no varchar(20) not null, zip char(6) null ) create table sales( order_no int not null primary key, cust_id char(5) not null , sale_id char(5) not null, tot_amt numeric(9,2) not null, order_date datetime not null, ) create table sale_item( order_no int not null, prod_id char(5) not null, qty int not null, unit_price numeric(7,2) not null, order_date datetime null ) create table product( prod_id char(5) not null primary key, prod_name varchar(20) not null ) select *from sales alter table sales ad
显示全部
相似文档