@rg070836rg
2016-07-03T06:49:47.000000Z
字数 2502
阅读 1872
数据库实验
实验五:T-SQL(3)1 实验目的1. 熟悉数据库的交互式SQL工具。2. 熟悉通过SQL对数据库进行操作。3. 完成作业的上机练习。2 实验平台利用SQLServer及其交互式工具SSMS来熟悉T-SQL。3 实验内容及要求所有操作均要求用T-SQL命令完成。根据以下要求认真填写实验报告,记录所有的实验用例。1. 给表student列sno增加检查长度为8位的约束并测试。2. 给表student列ssex的输入限定为男、女两个值并测试。3. 给表sc列grade的输入限定为0到100并测试。4. 给表sc的列增加外键约束并测试。5. 给表student增加列idcard表示身份证号并限定输入长度为18位,且最后一位奇数表示男,偶数表示女,这个值必须与ssex一致,并请测试。
use Students;create table Student(Sno char(8) not null primary key,Sname char(8) null,Ssex char(4) null,Sage int null,Sdept char(10) null)
alter table Student add constraint ck_tudent_ssex check (len(Sno)=8)
//有效用例insert into Student values('19130201','丁鹏','男',20,'Software')

//无效用例insert into Student values('191302021','王韵婷','女',20,'Software')

use Students;create table Student(Sno char(8) not null primary key,Sname char(8) null,Ssex char(4) null check(ssex='男' or ssex='女'),Sage int null,Sdept char(10) null)
//有效用例insert into Student values('19130201','丁鹏','男',20,'Software');

//无效用例insert into Student values('19130202','王韵婷','1',20,'Software');

use Students;create table SC(Sno char(8) not null,Cno int null,Grade int null check(Grade >=0 and Grade<=100),//或采用constraint ck_sc_grade check(Grade >=0 and Grade<=100))
//有效用例insert into SC values('19130201',1,99);

//无效用例insert into SC values('19130202',1,199);

use Students;create table Student(Sno char(8) not null primary key,Sname char(15) null,Ssex char(4) null,Sage int null,Sdept char(10) null)insert into Student values('19130202','王韵婷','女',20,'Software')insert into Student values('19130203','尹嘉琪','男',18,'Software')insert into Student values('19130204','卢冬冬','男',20,'Software')insert into Student values('19130205','史逸凡','男',19,'Software')create table SC(Sno char(8) not null,Cno int null,Grade int null,)
alter table SC add constraint pk_sc_fk foreign key(Sno) references Student(Sno);
//有效用例insert into SC values('19130202',1,99);

//无效用例insert into SC values('19130201',1,89);

use Students;create table Student(Sno char(8) not null primary key,Sname char(15) null,Ssex char(4) null,Sage int null,Sdept char(10) null)
alter table Student add idcard char(18) null;alter table Student add constraint pk_id_ck check(len(idcard)=18 and((CAST(right(idcard,1) AS INT )%2=1 and Ssex ='男') or (CAST(right(idcard,1) AS INT )%2=0 and Ssex ='女')));
//有效用例insert into Student values('19130202','王韵婷','女',20,'Software','320682199509130002');

//无效用例insert into Student values('19130205','史逸凡','男',19,'Software','320682199509130004');

