软件:oracle 11g express edition,dbvisuaize
首先装好oracle 连接数据库 默认用户system 也可以自己在oracle入门或cmd里面新建用户(建议!),但用户建好之后就无需在创建数据库了,负责它会一直报错
1.开始创建表
student
属性名 | 数据类型 | 长度 | 可否为空 | 说明 |
student_id | int |
| 否 |
|
name | varchar | 20 | 否 |
|
sex | char | 2 | 否 | 默认值“男” |
age | int |
| 可 | 年龄在18-60之间 |
create table student(int not null,name varchar(20) not null,sex char(4) not null default '男',age int check(age<18 and age>60))
#oracle的编码为utf-8 所以一个汉字要占2-3个字节,这里把char(2)给为char(4)
创建表2
teacher
属性名 | 数据类型 | 长度 | 可否为空 | 说明 |
teacher_id | int |
| 否 | 主键 |
name | varchar | 20 | 否 |
|
sex | char | 2 | 否 |
|
age | int |
| 可 |
|
birthdate | datetime |
| 可 |
|
workdate | datetime |
| 可 | 工作时间必须大于生日 |
create table teacher(teacher_id int primary key not null,name varchar(20) not null,sex char(4) not null,age int,birthdate date,workdate date );
alter table teacher add constraint chk_time(workdate>birthdate)#增加约束
创建表
test
属性名 | 数据类型 | 长度 | 可否为空 | 说明 |
test_id | int |
| 否 | 主键 |
test_name | varchar | 20 | 可 |
|
teacher_id | int |
| 否 | 参照teacher表中的teacher_id |
create table test(test_id int primary key not null,test_name varchar(20),teacher_id int not null foreign key references teacher(teacher_id))
#这个语句会报错 不知道为什么 大家可以试一下,有答案可以告知一下吗?
所以我用了外键约束重新写了一下
create table test(test_id int primary key not null,test_name varchar(20),teacher_id int not null)
alter table test add constraint fk_test foreign key(teacher_id) references teacher(teacher_id)
创建表
test_score
属性名 | 数据类型 | 长度 | 可否为空 | 说明 |
student_id | int |
| 否 | 主键,student表中的student_id |
test_id | int |
| 否 | 主键,参照test表中的test_id |
score | int |
| 可 | 成绩值在0-100之间 |
create table test_score(student_id not null,test_id not nuill,score int check(score>0 and score<100)
alter table test_score add constraint fk_studnet foreign key(student_id) references student(student_id)
alter table test_score add constraint fk_test key(test_id) references test(test_id)