数据库中定义表和建立完整性代码
2014-11-12 23:10
183 查看
--***************************************实验一******************************************
--**********************************建立表和定义完整性约束**************************
create database js
use js
drop table 院系
create table 院系( 编号 smallint Primary key,
名称 char(20) not null,
负责人 char(10),
办公地点 char(20),
constraint uniquea unique (名称)
)
create table 学生( 学号 char(8) Primary key,
院系 smallint,
姓名 char(10),
性别 char(2) check(性别 in('男','女')),
生源 char(6),
状态 char(4) check(状态 in('正常','留级','休学','退学')))
drop table 教师
create table 教师( 教师编号 char(8) Primary key,
院系 smallint foreign key references 院系(编号),
姓名 char(10),
性别 char(2) check(性别 in('男','女')),
职称 char(6) check(职称 in('教授','副教授','讲师','助教')),
专业 char(10))
drop table 课程
create table 课程( 课程编号 char(8)Primary key,
课程名称 char(20)not null,
责任教师 char(8)foreign key references 教师(教师编号),
学时 smallint not null,
课程性质 char(10) check(课程性质 in('公共基础','专业基础','专业选修','任意选修')))
drop table 选课
create table 选课( 学号 char(8)foreign key references 学生(学号) on delete cascade,
课程编号 char(8) foreign key references 课程(课程编号),
成绩 smallint check(成绩>=0 and 成绩<=100)default null,
primary key(学号,课程编号))
--实验要求
alter table 学生 add 平均成绩 smallint default null
alter table 课程 add constraint chk_xueshi check(学时%8=0)
alter table 课程 alter column 学时 smallint not null
alter table 院系 drop constraint uniquea
alter table 院系 alter column 名称 varchar(30)
alter table 院系 add constraint uniquea unique(名称)
alter table 教师 add 工资 numeric(5,2)
--**********************************建立表和定义完整性约束**************************
create database js
use js
drop table 院系
create table 院系( 编号 smallint Primary key,
名称 char(20) not null,
负责人 char(10),
办公地点 char(20),
constraint uniquea unique (名称)
)
create table 学生( 学号 char(8) Primary key,
院系 smallint,
姓名 char(10),
性别 char(2) check(性别 in('男','女')),
生源 char(6),
状态 char(4) check(状态 in('正常','留级','休学','退学')))
drop table 教师
create table 教师( 教师编号 char(8) Primary key,
院系 smallint foreign key references 院系(编号),
姓名 char(10),
性别 char(2) check(性别 in('男','女')),
职称 char(6) check(职称 in('教授','副教授','讲师','助教')),
专业 char(10))
drop table 课程
create table 课程( 课程编号 char(8)Primary key,
课程名称 char(20)not null,
责任教师 char(8)foreign key references 教师(教师编号),
学时 smallint not null,
课程性质 char(10) check(课程性质 in('公共基础','专业基础','专业选修','任意选修')))
drop table 选课
create table 选课( 学号 char(8)foreign key references 学生(学号) on delete cascade,
课程编号 char(8) foreign key references 课程(课程编号),
成绩 smallint check(成绩>=0 and 成绩<=100)default null,
primary key(学号,课程编号))
--实验要求
alter table 学生 add 平均成绩 smallint default null
alter table 课程 add constraint chk_xueshi check(学时%8=0)
alter table 课程 alter column 学时 smallint not null
alter table 院系 drop constraint uniquea
alter table 院系 alter column 名称 varchar(30)
alter table 院系 add constraint uniquea unique(名称)
alter table 教师 add 工资 numeric(5,2)
相关文章推荐
- 常用的在数据库中建立无限级树形菜单的asp.net代码
- 在SQL2008中如何建立数据库并定义文件流FILESTREAM?
- 韩顺平java从入门到精通第59讲,建立数据库代码
- JDBC建立数据库连接的代码
- IM长连接建立XMPP协议的使用(数据库的定义)
- 微软项目管理[EPM]数据库剖析1:如何取得全局项目有哪些自定义的大纲代码定义
- 学习Unity3D第二十天建立数据库和表的代码操作。
- 代码建立数据库
- 推荐制作精良的笔记软件Wire Note 应用键盘钩子技术获取即时数据 资料查阅的好工具 建立代码片段数据库
- 傻瓜式建立数据库,高效数据库操作代码的编写--android 推荐
- 报告论文:建立数据库 练习SQL语言进行数据定义、操作、控制
- SQL之代码建立数据库
- 数据库之代码编写以及表格的建立
- Powerbuilder 9.0 +MySQL Server 5.1 建立数据库连接的过程及代码
- 数据库中完整性约束的SQL定义几点小解
- 代码中添加事务控制 VS(数据库存储过程+事务) 保证数据的完整性与一致性
- 44. 普通对象建一个用户方法,提交时报:失败:建立业务逻辑对象失败:业务逻辑定义更新到数据库失败:ORA-00904: "DEFVERSION": 标识符无效
- 简单建立数据库连接池并包装查询代码
- 跟我一起学extjs5(28--加入模块和菜单定义[1建立数据库和表])