Oracle建表
程序员文章站
2022-05-28 22:14:42
Oracle建表参考网址:http://www.oraclejsq.com/getOracle_jcjc.do?nodeid=010100139 ......
oracle建表
参考网址:http://www.oraclejsq.com/getoracle_jcjc.do?nodeid=010100139
-- create table
create table student.stuinfo
(
stuid varchar2(10) not null,--学号:'s'+班号(6位数)+学生序号(3位数)(1)
stuname varchar2(50) not null,--学生姓名
sex char(1) not null,--性别
age number(2) not null,--年龄
classno varchar2(6) not null,--班号:年级(4位数)+班级序号(2位数)
stuaddress varchar2(100) default '地址未录入',--地址 (2)
grade char(4) not null,--年级
enroldate date,--入学时间
idnumber varchar2(18) default '身份证未采集' not null--身份证
)
tablespace users --(3)
storage
(
initial 64k
minextents 1
maxextents unlimited
);
-- add comments to the table
comment on table student.stuinfo --(4)
is '学生信息表';
-- add comments to the columns
comment on column student.stuinfo.stuid -- (5)
is '学号';
comment on column student.stuinfo.stuname
is '学生姓名';
comment on column student.stuinfo.sex
is '学生性别';
comment on column student.stuinfo.age
is '学生年龄';
comment on column student.stuinfo.classno
is '学生班级号';
comment on column student.stuinfo.stuaddress
is '学生住址';
comment on column student.stuinfo.grade
is '年级';
comment on column student.stuinfo.enroldate
is '入学时间';
comment on column student.stuinfo.idnumber
is '身份证号';
-- create/recreate primary, unique and foreign key constraints
alter table student.stuinfo
add constraint pk_stuinfo_stuid primary key (stuid);
--把stuid单做主键,主键字段的数据必须是唯一性的(学号是唯一的)
-- create/recreate check constraints
alter table student.stuinfo
add constraint ch_stuinfo_age
check (age>0 and age<=50);--给字段年龄age添加约束,学生的年龄只能0-50岁之内的
alter table student.stuinfo
add constraint ch_stuinfo_sex
check (sex='1' or sex='2');
alter table student.stuinfo
add constraint ch_stuinfo_grade
check (grade>='1900' and grade<='2999');