ACCP7.0S2优化myschool数据库设计第二章上机练习1-7

use master
go
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'xp_cmdshell',1
go
reconfigure
go
exec xp_cmdshell 'mkdir d:\project'
go
if exists(select * from sysdatabases where name='myschool')
drop database myschool
create database myschool
on primary(name='myschool_data',filename='d:\project\myschool.mdf',size=10,filegrowth=20%)
log on(name='myschool_log',filename='d:\project\myschool.ldf',size=3,maxsize=20,filegrowth=1)
use myschool
go
if exists(select * from sysobjects where name='subject')
drop table subject
create table subject(
subjectno int identity(1,1) not null,
subjectname nvarchar(50),
classhour int,
gradeid int)
if exists(select * from sysobjects where name='result')
drop table result
create table result(
studentno int not null,
subjectno int not null,
examdate datetime not null,
studentresult int not null)
if exists(select * from sysobjects where name='student')
drop table student
create table student(
studentno int not null,
loginpwd nvarchar(50) not null,
studentname nvarchar(50) not null,
sex bit not null,
gradeid int not null,
phone varchar(50),
address nvarchar(255),
borndate datetime not null,
email varchar(50),
id varchar(18) not null)
if exists(select * from sysobjects where name='grade')
drop table grade
create table grade(
gradeid int not null,
gradename nvarchar(50) not null)
alter table grade
add constraint pk_gradeid primary key(gradeid)
alter table student
add constraint pk_studentno1 primary key(studentno)
alter table student
add constraint uq_id unique(id)
alter table student
add constraint ck_borndate check(borndate>='1980-01-01')
alter table student
add constraint df_address default('地址不详') for address
alter table student
add constraint fk_studentno1 foreign key(studentno) references student(studentno)
alter table subject
add constraint pk_subjectno primary key(subjectno)
alter table subject
add constraint ck_subjectname check(subjectname!=null)
alter table subject
add constraint ck_classhour check(classhour>=0)
alter table subject
add constraint fk_gradeid foreign key(gradeid) references grade(gradeid)
alter table result
add constraint pk_studentno2 primary key(studentno,subjectno,examdate)
alter table result
add constraint df_examdate default(getdate()) for examdate
alter table result
add constraint ck_studentresult check(studentresult<=100 or studentresult>=0)
alter table result
add constraint fk_studentno2 foreign key(studentno) references student(studentno)
alter table result
add constraint fk_studentno3 foreign key(subjectno) references subject(subjectno)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值