第8章作业

#第8章
#【例8.1】在stusys数据库中创建course1表,以列级完整性约束方式定义主键。
use stusys;
create table course1
 (
  cno char(4) not null primary key,
        cname char(16) not null,
        credit tinyint null
    );
#【例8.2】在stusys数据库中创建course2表,以表级完整性约束方式定义主键。
create table course2
 (
  cno char(4) not null,
        cname char(16) not null,
        credit tinyint null,
        primary key(cno)
    );
#【例8.3】在stusys数据库中创建course3表,以表级完整性约束方式定义主键,并指定主键约束名称。
create table course3
 (
  cno char(4) not null,
        cname char(16) not null,
        credit tinyint null,
        constraint PK_course3 primary key (cno)
    );
#【例8.4】 删除例8.3创建的在course3表上的主键约束。
alter table course3
 drop primary key;
#【例8.5】重新在course3表上定义主键约束。
alter table course3
 add constraint PK_course3 primary key(cno);
#【例8.6】在stusys数据库中创建course4表,以列级完整性约束方式定义唯一性约束。
create table course4
(
 cno char(4) not null primary key,
    cname char(16) not null unique,
    credit tinyint null
);
#【例8.7】在stusys数据库中创建course5表,以表级完整性约束方式定义唯一性约束。
create table course5
(
 cno char(4) not null primary key,
    cname char(16) not null,
    credit tinyint null,
    constraint uk_course5 unique(cname)
);
#【例8.8】 删除例8.7在course5表创建的唯一性约束。
alter table course5
drop index uk_course5;
#【例8.9】重新在course5表上定义唯一性约束。
alter table course5
add constraint uk_course5 unique(cname);
#【例8.10】创建score1表,在cno列以列级完整性约束方式定义外键。
create table score1
(
 sno char(6) not null,
    cno char(4) not null references course1(cno),
    grade tinyint null,
    primary key(sno,cno)
);
#【例8.11】创建score2表,在cno列以表级完整性约束方式定义外键,并定义相应的参照动作。
create table score2
 (
  sno char(6) not null,
        cno char(4) not null,
        grade tinyint null,
        constraint FK_score2 foreign key(cno) references course2(cno)
    );
#【例8.12】删除例8.11在score2表上定义的外键约束。
alter table score2
drop foreign key fk_score2;
#【例8.13】 重新在score2表上定义外键约束。
alter table score2
add constraint fk_score2 foreign key(cno) references course2(cno);
#【例8.14】在stusys数据库中创建表score3,在grade列以列级完整性约束方式定义检查约束。

create table score3
(
 sno char(6) not null,
    cno char(4) not null,
    grade tinyint null check(grade >=0 and grade<=100),
    primary key(sno,cno)
);
#【例8.15】在stusys数据库中创建表score4,在grade列以表级完整性约束方式定义检查约束。
create table score4
(
 sno char(6) not null,
    cno char(4) not null,
    grade tinyint null,
    primary key(sno,cno),
    constraint ck_score4 check(grade >=0 and grade<=100)
);
#【例8.16】删除例8.15在score4表上定义的检查约束。
alter table score4
drop check ck_score4;
#【例8.17】重新在score4表上定义检查约束。
alter table score4
add constraint ck_score4 check(grade >=0 and grade<=100);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值