任务7创建教务管理数据库系统

创建名为JWGL_DB的数据库,包含一个主数据文件和一个事务日志文件。主数据文件的逻辑名为JWGL_DB_DATA,操作系统文件名为JWGL_DB_DATA.MDF,初始容量大小为5MB,最大容量为20MB,文件的增长量为20%。事务日志文件的逻辑文件名为JWGL_DB_LOG,物理文件名为JWGL_DB_LOG.LDF,初始容量大小为5MB,最大容量为10MB,文件增长量为2MB,最大空间不受限制。数据文件与事务日志文件都放在H盘指定目录下

--(1)
Create DataBase JWGL_DB
On Primary(
Name = JWGL_DB_DATA,
FileName = 'H:\Sql Server\SQLDataBase\JWGL_DB_DATA.MDF',
Size = 5MB,
MaxSize = 20MB,
FileGrowth = 20%)

Log On
(Name=JWGL_DB_LOG,
FileName = 'H:\Sql Server\SQLDataBase\JWGL_DB_LOG.LDF',
Size = 5MB,
MaxSize = 10MB,
FileGrowth  =2MB
)
go

use  [JWGL_DB]
go
--(2)
Create Table 学校
(学校编号 int identity(1,1) Constraint [column_学校(学校编号)_pk] Primary key not null,
学校名称 varchar(30) not null,
)

Create Table 学院
(学院编号 int identity(1,1) Constraint [column_学院(学院编号)_pk] Primary Key not null,
学院名称 varchar(20) not null,
学校编号 int not null
Constraint [FK学院(学校编号)] Foreign Key
	References 学校(学校编号)
On Delete Cascade On Update Cascade
)

Create Table 专业
(专业编号 int identity(1,1) Constraint [column_专业(专业编号)_pk] Primary key not null,
专业名称 varchar(20) not null,
学院编号 int not null
Constraint [FK专业(学院编号)] Foreign Key
	References 学院(学院编号)
On Delete Cascade On Update Cascade
)

Create Table 班级
(
班级编号 int identity(1,1) Constraint [column_班级(班级编号)_pk] Primary Key not null,
班级名称 varchar(20) not null,
专业编号 int not null
Constraint [FK班级(专业编号)] Foreign Key
	References 专业(专业编号)
On Delete Cascade On Update Cascade
)

Create Table 学生
(
学号 char(12) Constraint [column_学生(学号)_pk] Primary Key not null,
姓名 varchar(20) not null,
性别 bit check(性别=0 or 性别=1) null ,
手机号 varchar(20) null,
家庭电话 varchar(20) null,
家庭地址 varchar(50) null,
备注 nvarchar(Max) null,
班级编号 int not null
Constraint [Fk学生(班级编号)] Foreign Key
	References 班级(班级编号)
On Delete Cascade On Update Cascade
)
use JWGL_DB
go

--2013-06-02-19:24
/*alter table 学生
add 注册日期 datetime default(getdate()) null*/


Create Table 教师
(
教师编号 int identity(1,1) Constraint [column_教师(教师编号)_pk] Primary Key not null,
姓名 varchar(20) not null,
性别 char(1) default('0') check(性别='0' or 性别='1'),
手机号码 varchar(20) not null
)

Create Table 课程
(
课程编号 int identity(1,1) constraint [column_课程(课程编号)_pk] Primary Key not null,
课程名称 varchar(50) not null,
学分 tinyint constraint ck_学分 check(学分>0 and 学分 <100),
开课学年 date constraint ck_开课学年  null,		--delete check(DateDiff(year,开课学年,getDate())=0) 
开课学期 tinyint constraint ck_开课学期 check(开课学期>0) null,
理论学时 tinyint constraint ck_理论学时 check(理论学时>0) null,
实践学时 tinyint constraint ck_实践学时 check(实践学时>0) null,
备注 nvarchar(Max) null,
课程类型 varchar(20) constraint ck_课程类型 check(课程类型 = '一体化' or 课程类型='纯理论' or 课程类型='纯实践') null,
核心课程 bit constraint ck_核心课程 check(核心课程 = 0 or 核心课程=1)  null,
课程性质 varchar(20) constraint ck_课程性质 check(课程性质 = '必修课' or 课程性质='选修课') null,
考核方式 varchar(20) constraint ck_考核方式 check(考核方式 = '考试' or 考核方式='考查') null,
课程归属 varchar(20) constraint ck_课程归属 check(课程归属 = '基本素质与能力课' or 课程归属='职业能力课') null,
)


--alter table 课程
--add column 专业编号 int not null Constraint [Fk课程(专业编号)] Foreign Key
--	References 专业(专业编号) On Delete Cascade On Update Cascade, 

/*alter table 课程
add 课程归属 varchar(20) constraint ck_课程归属 check(课程归属 = '基本素质与能力课' or 课程归属='职业能力课') null
*/
/********为多对多关系的实体类创建中间表***********/
Create Table 专业_课程
(专业编号 int not null Constraint [Fk专业_课程(专业编号)] Foreign Key
	References 专业(专业编号) On Delete Cascade On Update Cascade,			--此处应该引用
课程编号 int not null Constraint [Fk专业_课程(课程编号)] Foreign Key 
	References	课程(课程编号) On Delete Cascade On Update Cascade,
constraint column_专业编号_课程编号_pk Primary Key(专业编号,课程编号)
)

Create Table 学生_课程
(学号 char(12) not null Constraint [FK学生_课程(学号)] Foreign Key
	References 学生(学号) On Delete Cascade On Update Cascade,
课程编号 int not null Constraint [FK学生_课程(课程编号)] Foreign Key
	References 课程(课程编号) On Delete Cascade On Update Cascade,
课程成绩 tinyint constraint ck_课程成绩 check(课程成绩>=0 and 课程成绩<=100) null,
constraint column_学生编号_课程编号_pk Primary Key(学号, 课程编号)
)

Create Table 教师_课程
(
教师编号 int not null Constraint [FK教师_课程(教师编号)] Foreign Key
	References 教师(教师编号) On Delete Cascade On Update Cascade,
课程编号 int not null Constraint [FK教师_课程(课程编号)] Foreign Key
	References 课程(课程编号) On Delete Cascade On Update Cascade,
课程名称 varchar(20) not null,
Constraint column_教师编号_课程编号_pk Primary Key(教师编号,课程编号)
)
--(3)
/*********对多值属性处理***********/

/*Create Table 课程类型
(
	课程类型编号 int identity(1,1) Constraint column_课程类型编号_pk Primary Key not null,
	课程类型名称	char(12) not null,
	课程编号 int null Constraint [FK课程类型(课程编号)] Foreign Key 
	References 课程(课程编号)
)

Create Table 课程_课程类型
(
	课程类型编号 外键
	课程编号	外键
)

Create Table 核心课程
(
	核心课程编号 int identity(1,1) Constraint column_核心课程编号_pk Primary Key not null,
	核心课程名称	char(12) not null,
	课程编号 int null Constraint [FK核心课程(课程编号)] Foreign Key 
	References 课程(课程编号)
)

Create Table 课程性质
(
	课程性质编号 int identity(1,1) Constraint column_课程性质编号_pk Primary Key not null,
	课程性质名称	char(12) not null,
	课程编号 int null Constraint [FK课程性质(课程编号)] Foreign Key 
	References 课程(课程编号) 
)

/*Create Table 考核方式
(
	考核方式编号 int identity(1,1) Constraint column_考核方式编号_pk Primary Key not null,
	考核方式名称	char(12) not null,
	课程编号 int null Constraint [FK考核方式(课程编号)] Foreign Key 
	References 课程(课程编号)
)

Create Table 课程归属
(
	课程归属编号 int identity(1,1) Constraint column_课程归属编号_pk Primary Key not null,
	课程归属名称	char(12) not null,
	课程编号 int null Constraint [FK课程归属(课程编号)] Foreign Key 
	References 课程(课程编号)
)*/
--(4)Other SQK practice tests
select *        
from [sys].check_constraints

select name                       
from [sys].foreign_keys

/*
alter table 教师_课程
drop constraint [FK教师_课程(课程编号)]
alter table 教师_课程           
add Constraint [FK教师_课程(课程编号)] Foreign Key(课程编号) 
	References	课程(课程编号) On Delete Cascade On Update Cascade
*/
*/

alter table Table_1
add Constraint [FKTable_1(TypeId)] Foreign Key(TypeId)
	References Table_2 On Delete Cascade On Update Cascade

写于2013-06-02-19:24

发布了40 篇原创文章 · 获赞 0 · 访问量 1531

猜你喜欢

转载自blog.csdn.net/zhoumoon/article/details/104942138
今日推荐