实验一 数据库定义实验

实验一  数据库定义实验
实验一  数据库定义实验

实验一数据库定义实验

(一)实验目的

掌握在SQL Server Query Analyzer中利用CREATE、DROP、ALTER等SQL语句创建和删除数据库,创建、删除、更新基本表。(二)实验器材

Win7平台+ SQL Server 2008系统。

(三)实验内容

1、利用查询分析器创建学生课程数据库(Educ),保存在E:盘学生学号命名的文件夹下。(注意观察数据库文件的后缀名)

create database Educ

on primary(

NAME=Educ1_DATA,

Filename='E:\DB\Educ1_data.MDF',

size=3Mb,

MAXSIZE=100MB,

Filegrowth=10%)

log on (

NAME=Educ1_log,

Filename='E:\DB\Educ1_data.ldf',

size=2Mb,

Filegrowth=1MB)

2、利用查询分析器在Educ数据库中,创建基本表:学生表(Student)、课程表(Course)、教师表(Teacher)、选课表(SC)以及任课表(TC),同时根据表的要求完成相应的约束定义。各表的结构见实验教程P33表3.3(出生日期数据类型改为Date)、3.5、3.6、3.7、3.8;

use Educ

go

create table Student(

Sno char(11)primary key,

Sname char(10)not null,

Ssex char(2)not null,

Sbirthday Date,

Saddress char(10),

Sgrade smallint,

Smajor char(10),

Sdept char(2),

)

go

create table Course(

Cno char(4)primary key,

Cname char(20)not null,

Cpno char(4),

Ctype char(8),

Chour smallint,

Ccredit decimal(3,1),

Cterm smallint,

Csbstract varchar(200),

)

go

create table Teacher(

TID char(8)primary key,

Tname char(10)not null,

Tsex char(2),

Tposition char(10),

Tbirthday date,

Tworkdate date,

Tdept char(2),

)

go

create table SC(

Sno char(11),

Cno char(4),

Grede decimal(5,1),

primary key(Sno,Cno),

foreign key(Sno)references Student(Sno),

foreign key(Cno)references Course(Cno),

)

go

create table TC(

TID char(8),

Cno char(4),

Classroom char(12),

primary key(TID,Cno),

foreign key (TID)references Teacher(TID),

foreign key (Cno)references Course(Cno),

)

3、保存脚本文件。分别以Student.SQL、Course.SQL、Teacher.SQL、

SC.SQL、TC.SQL保存全部存脚本文件在E:学号文件夹;

4、修改及删除表结构

(1)在Teacher表中增加“籍贯”属性:TADDRESS CHAR(10);use Educ

go

alter table Teacher ADD Taddress char(10);

(2)将Student表中的Sdept属性长度更改为4;

use Educ

go

alter table Student alter column Sdept char(4);

(3)删除Teacher表中增加的属性“籍贯”:TADDRESS;

alter table Teacher drop column Taddress;

(4)逐个删除tudent表、Course表、Teacher表、SC表和TC 表;

drop table Student cascade;

go

drop table Course cascade;

go

drop table Teacher cascade;

go

drop table SC cascade;

go

drop table TC cascade;

(5)还原数据表。打开前面保存的SQL脚本文件(Student.SQL、Course.SQL、Teacher.SQL、SC.SQL、TC.SQL),并依次执行,重新生成学生表(Student)、课程表(Course)、教师表(Teacher)、选课表(SC)以及任课表(TC)。

5、分析并完成3.6思题:

6、完成P35 -3.7拓展训练。

相关主题
相关文档
最新文档