zoukankan      html  css  js  c++  java
  • sql修改约束语法练习

    --以系统管理员身份登录到SQL Server服务器,并使用T-SQL语句实现以下操作;
    --1. 将stu数据库中student表的sno定义为主键;
    alter table [student] add constraint PK_student primary key (sno)
    --2. 将数据库stu的表course的cno字段定义为主键,约束名称为cno_pk;
    alter table [course] add constraint cno_pk primary key (cno)
    --3. 为表course中的字段cname添加唯一值约束;
    alter table [course] add constraint UQ_course_name unique (cname)
    --alter table[course] drop constraint UQ_canme
    --4. 将数据库stu的表sc的sno及cno字段组合定义为主键,约束名称为sc_pk;
    alter table[sc] drop constraint PK_sc
    alter table[sc] add constraint PK_sc primary key(sno,cno)
    --5. 对于数据表sc的sno、cno字段定义为外码,使之与表student的主码sno及表course的主码cno对应,实现如下参照完整性:
    alter table[sc]
    add constraint FK_sc_sno foreign key(sno) references student(sno)
    alter table[sc]
    add constraint FK_sc_cno foreign key(cno) references course(cno)
    --1) 删除student表中记录的同时删除sc表中与该记录sno字段值相同的记录;
    create trigger s_c
    on student after delete
    as
    begin
    declare @preid char(9);
    select @preid=sno from deleted;
    delete sc where sno=@preid
    end;
    --2) 修改student表某记录的sno时,若sc表中与该字段值对应的有若干条记录,则拒绝修改;
    create trigger modify_sno_rule
    on student for insert
    as
    begin
    declare @m_id char(9);
    declare @num int;
    select @m_id=sno from inserted;

    end;
    --3) 修改course表cno字段值时,该字段在sc表中的对应值也应修改;
    ----4) 删除course表一条记录时,若该字段在在sc表中存在,则删除该字段对应的记录;
    --5) 向sc表添加记录时,如果该记录的sno字段的值在student中不存在,则拒绝插入;
    --6. 定义check约束,要求学生学号sno必须为9位数字字符,且不能以0开头,第二三位皆为0;
    alter table [student] add constraint CK_sno1 check(sno like'[1-9][0][0][0-9][0-9][0-9][0-9][0-9][0-9]')
    --7. 定义stu数据库中student表中学生年龄值在16-25范围内;
    alter table student add constraint CK_age check(sage between 16 and 25)
    --8. 定义stu数据库中student表中学生姓名长度在2-8之间;
    alter table student add constraint CK_name_length check(sname like'__'|'___'|'____'|'_____'|'______'|'_______'|'________')
    --9. 定义stu数据库中student表中学生性别列中只能输入“男”或“女”;
    alter table student add constraint CK_sex check(ssex like'男'|'女')
    --10. 定义stu数据库student表中学生年龄值默认值为20;
    alter table student add constraint DF_age default(20) for Sage
    --11. 修改student表学生的年龄值约束可以为15-30范围内;
    alter table student drop constraint CK_age
    alter table student add constraint CK_age check(sage between 15 and 30)
    --12. 删除上述唯一值约束、外键约束及check约束;
    alter table [course] drop constraint UQ_course_name
    alter table [student] drop constraint CK_sno1
    alter table student drop constraint CK_age
    alter table student drop constraint CK_name_length
    --13.向sc表中插入或修改一条记录时,通过触发器检查记录学号字段的值在student表中是否存在,
    --同时还要检查课程号的值是否存在若不存在,则取消插入或修改,否则插入成功;执行对sc的插入、修改操作,验证触发器的执行。
    create trigger logic1 on student
    for insert or update

    --14.设计一更新触发器,当course表中的cno列修改时,激活该触发器同时更新sc表中的记录。
    --15.设计一触发器,约束数据库系统课程的课容量为80。
    --16.选做题:设计实例,验证after触发器与instead of触发器的异同。

  • 相关阅读:
    多点触控版的驱动安装
    模板类的继承
    c++大整数运算
    RHEL6单用户模式切换
    Dlna相关开源项目收集整理
    给我的当前RHEL6新增一块硬盘!
    交叉编译libconv
    OSCam学习记录
    Android中的应用!!!!
    Elf文件格式学习笔记
  • 原文地址:https://www.cnblogs.com/linkzijun/p/4956243.html
Copyright © 2011-2022 走看看