博客
关于我
数据库原理实验报告(一)
阅读量:65 次
发布时间:2019-02-25

本文共 3721 字,大约阅读时间需要 12 分钟。

  • 答案在后面

一、在studentdb中创建架构Production和Person并比较区别。

create schema Production --架构命名不能以数字开头

create schema Person AUTHORIZATION st

注意: 在创建Person架构前需要使用下面的三条语句先在当前数据库中添加用户,并仅仅授予该用户建表的权限。

CREATE LOGIN st WITH PASSWORD=‘suntao123’

CREATE USER st FOR LOGIN st

GRANT create table to st

然后用户st以SQL SERVER身份验证方式登录服务器,尝试执行如下的SQL语句:

create table Person.t1(id int,name char(10)) --成功

create table Production.t1(id int,name char(10)) --失败,原因?

二、修改表结构,具体要求如下:

(1) 将表course的cname列的数据类型改为varchar(40).

(2) 为表student增加一个新列: birthday(出生日期), 类型为datetime, 默认为空值.

(3) 将表sc中的grade列的取值范围改为小于等于150的正数.

(4) 为Student表的“Sex”字段创建一个缺省约束,缺省值为’男’

(5)为“Sdept”字段创建一个检查约束,使得所在系必须是’CS’、’MA’或’IS’之一。

(6)为Student表的“Sname”字段增加一个唯一性约束

(7)为SC表建立外键,依赖于Student表的fk_S_c约束。

(8)禁止启用Student表的“Sdept”的CHECK约束ck_student。

三、分别建立以下索引(如果不能成功建立,请分析原因)

(1) 在student表的sname列上建立普通降序索引.

(2) 在course表的cname列上建立唯一索引.

(3) 在sc表的sno列上建立聚集索引.

(4) 在spj表的sno(升序), pno(升序)和jno(降序)三列上建立一个普通索引.

create database spjdb;create database studentdb;use studentdb;create table student(    sno   char(9) primary key not null,    sname char(10)            not null,    ssex  char(2),    sage  smallint,    sdept char(15),    check (sage >= 12));use studentdb;create table course(    cno     char(4) primary key not null ,    cname   char(20),    cpno    char(4),    ccredit smallint,);create table sc(    sno   char(9) not null ,    cno   char(4) not null ,    grade decimal(5,1),    foreign key (sno)references student(sno),    foreign key (cno)references course(cno),    check (grade between 0 and 100));use spjdb;create table S(    sno char(2) primary key not null ,    sname char(10) not null ,    status smallint,    city char(10),    check (status>0));create table P(    pno char(2) primary key not null ,    pname char(10) not null ,    color char(2),    weight smallint,    check (weight>0));create table J(    jno char(2) primary key not null ,    jname char(10) not null ,    city char(10));create table SPJ(    sno char(2) not null ,    pno char(2) not null ,    jno char(2) not null ,    qty smallint,    primary key (sno,pno,jno),    foreign key (sno)references S(sno),    foreign key (pno)references P(pno),    foreign key (jno)references J(jno),    check (qty>0));--use studentdb;--create schema Production;--CREATE LOGIN st WITH PASSWORD='本人mysql密码';--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));--修改表--(1) 将表course的cname列的数据类型改为varchar(40).--alter table course alter cname varchar(40); mysqlalter table course alter column cname varchar(40);--(2) 为表student增加一个新列: birthday(出生日期), 类型为datetime, 默认为空值.alter table student add birthday datetime default null;--(3) 将表sc中的grade列的取值范围改为小于等于150的正数.--删除表中原有约束alter table scdrop constraint CK__sc__grade__2A4B4B5E;--创建新约束alter table scadd constraint grade_new check (grade<=150);--(4) 为Student表的“Sex”字段创建一个缺省约束,缺省值为’男’alter table studentadd constraint ssex__new default '男' for ssex;--(5)为“Sdept”字段创建一个检查约束,使得所在系必须是’CS’、’MA’或’IS’之一。alter table studentadd constraint ck_student check (sdept in ('CS','MA','IS'));--(6)为Student表的“Sname”字段增加一个唯一性约束alter table studentadd unique (sname);--(7)为SC表建立外键,依赖于Student表的fk_S_c约束。alter table scadd constraint fk_S_c foreign key (sno) references student(sno);--(8)禁止启用Student表的“Sdept”的CHECK约束ck_student。alter table studentnocheck constraint ck_student;--6. 分别建立以下索引(如果不能成功建立,请分析原因)--(1) 在student表的sname列上建立普通降序索引.create unique index sname on student(sname desc );--(2) 在course表的cname列上建立唯一索引.create unique index cname on course(cname);--(3) 在sc表的sno列上建立聚集索引.create clustered index 索引名 on sc(sno);--(4) 在spj表的sno(升序), pno(升序)和jno(降序)三列上建立一个普通索引.use spjdb;create unique index sno on SPJ(sno asc,pno asc,jno desc);

转载地址:http://nxh.baihongyu.com/

你可能感兴趣的文章
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>