数据库原理实验报告(一)

  • 答案在后面

一、在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); mysql
alter 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 sc
drop constraint CK__sc__grade__2A4B4B5E;
--创建新约束
alter table sc
add constraint grade_new check (grade<=150);

--(4) 为Student表的“Sex”字段创建一个缺省约束,缺省值为’男’
alter table student
add constraint ssex__new default '男' for ssex;

--(5)为“Sdept”字段创建一个检查约束,使得所在系必须是’CS’、’MA’或’IS’之一。
alter table student
add constraint ck_student check (sdept in ('CS','MA','IS'));

--(6)为Student表的“Sname”字段增加一个唯一性约束
alter table student
add unique (sname);

--(7)为SC表建立外键,依赖于Student表的fk_S_c约束。
alter table sc
add constraint fk_S_c foreign key (sno) references student(sno);

--(8)禁止启用Student表的“Sdept”的CHECK约束ck_student。
alter table student
nocheck 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);
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值