oracle 入门操作

软件: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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值