博客
关于我
数据库原理实验报告(一)
阅读量: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原理简介—7.redo日志的底层原理
查看>>
MySQL原理简介—8.MySQL并发事务处理
查看>>
MySQL原理简介—9.MySQL索引原理
查看>>
MySQL参数调优详解
查看>>
mysql参考触发条件_MySQL 5.0-触发器(参考)_mysql
查看>>
MySQL及navicat for mysql中文乱码
查看>>
MySqL双机热备份(二)--MysqL主-主复制实现
查看>>
MySQL各个版本区别及问题总结
查看>>
MySql各种查询
查看>>
mysql同主机下 复制一个数据库所有文件到另一个数据库
查看>>
mysql启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
mysql命令
查看>>
mysql命令==_mysql命令
查看>>
mysql命令和mysql的配置文件
查看>>
watch
查看>>