[关闭]
@rg070836rg 2016-07-03T14:49:47.000000Z 字数 2502 阅读 1639

数据库10.8实验

数据库实验


  1. 实验五:T-SQL(3)
  2. 1 实验目的
  3. 1 熟悉数据库的交互式SQL工具。
  4. 2 熟悉通过SQL对数据库进行操作。
  5. 3 完成作业的上机练习。
  6. 2 实验平台
  7. 利用SQLServer及其交互式工具SSMS来熟悉T-SQL
  8. 3 实验内容及要求
  9. 所有操作均要求用T-SQL命令完成。根据以下要求认真填写实验报告,记录所有的实验用例。
  10. 1. 给表studentsno增加检查长度为8位的约束并测试。
  11. 2. 给表studentssex的输入限定为男、女两个值并测试。
  12. 3. 给表scgrade的输入限定为0100并测试。
  13. 4. 给表sc的列增加外键约束并测试。
  14. 5. 给表student增加列idcard表示身份证号并限定输入长度为18位,且最后一位奇数表示男,偶数表示女,这个值必须与ssex一致,并请测试。

1 给表student列Sno增加检查长度为8位的约束并测试。

1.1 先建表

  1. use Students;
  2. create table Student
  3. (
  4. Sno char(8) not null primary key,
  5. Sname char(8) null,
  6. Ssex char(4) null,
  7. Sage int null,
  8. Sdept char(10) null
  9. )

1.2 增加约束条件

  1. alter table Student add constraint ck_tudent_ssex check (len(Sno)=8)

1.3 测试

  1. //有效用例
  2. insert into Student values('19130201','丁鹏','男',20,'Software')

有效用例

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

2.png-26.9kB

2 给表student列ssex的输入限定为男、女两个值并测试。

2.1 建表时,设定限制。

  1. use Students;
  2. create table Student
  3. (
  4. Sno char(8) not null primary key,
  5. Sname char(8) null,
  6. Ssex char(4) null check(ssex='男' or ssex='女'),
  7. Sage int null,
  8. Sdept char(10) null
  9. )

2.2 测试

  1. //有效用例
  2. insert into Student values('19130201','丁鹏','男',20,'Software');

3.png-25.6kB

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

4.png-27.7kB

3 给表sc列grade的输入限定为0到100并测试。

3.1 建表时,设定限制。

  1. use Students;
  2. create table SC
  3. (
  4. Sno char(8) not null,
  5. Cno int null,
  6. Grade int null check(Grade >=0 and Grade<=100),
  7. //或采用constraint ck_sc_grade check(Grade >=0 and Grade<=100)
  8. )

3.2 测试

  1. //有效用例
  2. insert into SC values('19130201',1,99);

5.png-25.6kB

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

6.png-27.3kB

4 给表sc的列增加外键约束并测试。

4.1 建表

  1. use Students;
  2. create table Student
  3. (
  4. Sno char(8) not null primary key,
  5. Sname char(15) null,
  6. Ssex char(4) null,
  7. Sage int null,
  8. Sdept char(10) null
  9. )
  10. insert into Student values('19130202','王韵婷','女',20,'Software')
  11. insert into Student values('19130203','尹嘉琪','男',18,'Software')
  12. insert into Student values('19130204','卢冬冬','男',20,'Software')
  13. insert into Student values('19130205','史逸凡','男',19,'Software')
  14. create table SC
  15. (
  16. Sno char(8) not null,
  17. Cno int null,
  18. Grade int null,
  19. )

4.2 增加外键

  1. alter table SC add constraint pk_sc_fk foreign key(Sno) references Student(Sno);

4.3 测试

  1. //有效用例
  2. insert into SC values('19130202',1,99);

7.png-25.6kB

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

8.png-27.3kB

5 给表student增加列idcard表示身份证号并限定输入长度为18位,且最后一位奇数表示男,偶数表示女,这个值必须与ssex一致,并请测试。

5.1 建表

  1. use Students;
  2. create table Student
  3. (
  4. Sno char(8) not null primary key,
  5. Sname char(15) null,
  6. Ssex char(4) null,
  7. Sage int null,
  8. Sdept char(10) null
  9. )

5.2 增加带约束条件的类

  1. alter table Student add idcard char(18) null;
  2. 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 ='女')));

5.3 测试

  1. //有效用例
  2. insert into Student values('19130202','王韵婷','女',20,'Software','320682199509130002');

9.png-27kB

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

10.png-29.1kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注