【数据库实验】数据库、表、模式的SQL语句操作

1、数据库的建立、删除和修改操作

(1)使用SQL语句创建数据库EDUC,并进行如下设置:数据库文件和日志文件的逻辑名称分别为:Student_data和Student_log;数据文件的物理文件名为‘C:\DATABASE\Student_data.MDF’;数据文件的初始大小为5MB,文件增长量为1MB;日志文件的增长方式初始大小为2MB,文件增长量为10%;日志文件的物理文件名为‘C:\DATABASE\Student_log.LDF’。

create database EDUC on
primary
	(NAME=Student_data,
	 FILENAME='C:\database\Student_data.mdf',
	 SIZE=5MB,
	 FILEGROWTH=1MB)
log on
	(NAME=Student_log,
	 FILENAME='C:\database\Student_log.ldf',
	 SIZE=2MB,
	 FILEGROWTH=10%)
go

(2)使用SQL语句在数据库EDUC添加一个数据文件,逻辑名称:Student_data1,物理文件名为‘C:\DATABASE\Student_data1.NDF’文件夹中,数据文件的初始大小为2MB,文件增长量为10%。

alter database EDUC
add file
(
	NAME=Student_data1,
	FILENAME='C:\database\Student_data1.ndf',
	SIZE=2MB,
	FILEGROWTH=10%
)
go

(3)使用SQL语句修改数据文件"Student_data1",初始大小为5MB,文件增长量为2MB,最大值为1GB。

alter database EDUC
modify file
(
	name=Student_data1,
	size=5mb,
	filegrowth=2mb,
	maxsize=1gb
)
go

(4)使用SQL语句将新添加数据文件"Student_data1"删除。

alter database EDUC
remove file Student_data1
go

2、数据表的建立、删除和修改操作

(1)使用SQL语句在EDUC数据库中创建三个基本表(表名为“student”、“course”和“sc”)。
在这里插入图片描述

create table student
(
	sno char(9) primary key not null,
	sname char(20) not null,
	ssex char(2) check (ssex in ('男','女')),
	sage smallint,
	sdept char(20)
);
create table course
(
	cno char(4) primary key not null,
	cname char(40) not null,
	cpno char(4),
	ccredit smallint,
	foreign key (cpno) references course (cno)
);
create table sc
(
	sno char(9) not null,
	cno char(4) not null,
	grade smallint check (grade>=0 and grade<=100),
	primary key (sno,cno)
);
go

(2)使用SQL语句将student表中“sname”列的数据类型为CHAR(40)。

alter table student alter column sname char(40) not null;
go

(3)使用SQL语句在student表中添加新列,列名为“高考成绩”,数据类型为int。

alter table student add 高考成绩 int;
go

(4)使用SQL语句删除“高考成绩”列。

alter table student drop column 高考成绩;
go

(5)使用SQL语句设置student中的“sage”为not null

alter table student alter column sage smallint not null;
go

(6)使用SQL语句设置course中的“ccredit”的取值范围为1-3

alter table course add constraint ccredit check (ccredit>=1 and ccredit<=3);
go

3、模式的建立、删除和修改操作

(1)使用SQL语句:在EDUC数据库下,为guest用户创建ST模式,在该模式下面创建一张STUDENT表(列信息如上面的student表)。

create schema ST authorization guest;
go
create table ST.student
(
	sno char(9) primary key not null,
	sname char(20) not null,
	ssex char(2) check (ssex in ('男','女')),
	sage smallint,
	sdept char(20)
);
go

(2)使用SQL语句删除ST模式,若不能删除,为什么?如何才能删除?

drop table ST.student;
drop schema ST;
go

猜你喜欢

转载自blog.csdn.net/mc_dudu233/article/details/143139339