本文共 1836 字,大约阅读时间需要 6 分钟。
在 studentdb 中,我们需要创建两个主要架构:Production 和 Person。以下是创建过程的详细步骤:
创建 Production 架构
create schema Production;
创建 Person 架构
create login st with password='suntao123';create user st for login st;grant create table to st;
create schema Person AUTHORIZATION st;
测试架构权限
create table Person.t1(id int, name char(10));-- 成功
按照要求对各表进行结构修改:
修改 course 表
cname 列的数据类型改为 varchar(40):alter table course alter column cname varchar(40);
在 student 表中添加新列
birthday 列,类型为 datetime,默认值为空:alter table student add birthday datetime default null;
修改 sc 表约束
alter table sc drop constraint CK__sc__grade__2A4B4B5E;
grade 不超过 150:alter table sc add constraint grade_new check (grade <= 150);
为 student 表添加缺省约束
ssex 列设置缺省值为 '男':alter table student add constraint ssex__new default '男' for ssex;
为 student 表添加检查约束
sdept 只能是 'CS'、'MA' 或 'IS':alter table student add constraint ck_student check (sdept in ('CS','MA','IS'));为 student 表添加唯一性约束
sname 列添加唯一性约束:alter table student add unique (sname);
为 SC 表添加外键约束
sc 表中的 sno 外键依赖于 student 表中的 fk_S_c 约束:alter table sc add constraint fk_S_c foreign key (sno) references student(sno);
禁用 Student 表的 sdept 检查约束
nocheck 关键字禁用约束:alter table student nocheck constraint ck_student;
为提高查询效率,创建以下索引:
在 student 表的 sname 列上建立普通降序索引
create unique index sname on student(sname desc);
在 course 表的 cname 列上建立唯一索引
create unique index cname on course(cname);
在 sc 表的 sno 列上建立聚集索引
create clustered index index_sno on sc(sno);
在 SPJ 表的 sno、pno 和 jno 列上建立普通索引
use spjdb;create unique index sno on SPJ(sno asc, pno asc, jno desc);
以上步骤完成后,数据库架构和数据表结构将得到有效优化,各项约束和索引也将正常生效。
转载地址:http://nxh.baihongyu.com/