--constraint
--not null 非空约束
create table demo01(
empno number(4),--4是数字的个数,如果是number(4,2) 则表示4个数字,其中2个是小数如10.00;注意4是最大位数
ename varchar2(10) not null, --10是字节个数 ;注意:10是最大的个数
job varchar2(10)
)
insert into demo01 values(1234,' ','SALES');
select * from demo01;
--unique 唯一键
create table demo02(
empno number(4) unique,
ename varchar2(10),
job varchar2(8)
)
select * from demo02;
insert into demo02 values(1234,'zhangsan','SALES');
insert into demo02 values(1234,'lisi','SALES');
insert into demo02 values(null,'wangwu','SALES');--字段约束是唯一时属性值可以为空,且多个记录中的该属性值都可以为空
insert into demo02 values(null,'zhaoliu','SALES');
--非空&唯一
create table demo03(
empno number(4) not null unique,
ename varchar2(10),
job varchar2(8)
)
select * from demo03
insert into demo03 values(1234,'zhangsan','SALES');
insert into demo03 values(null,'lisi','SALES');
--自定义检查约束
create table demo04(
empno number(4),
ename varchar2(10),
job varchar2(8),
age number(3) check(age>0 and age<200),
gender varchar2(3) check(gender='男' or gender='女')
)
insert into demo04 values(1234,'zhangsan','SALES',56,'男')
insert into demo04 values(1234,'zhangsan','SALES',44,'详')--必须满足自定义异常中的条件
insert into demo04 values(1234,'zhangsan','SALES',44,null)--自定义异常可以为空
insert into demo04 values(1234,'zhangsan','SALES',null,'男')--自定义异常可以为空
select * from demo04 ;
--创建列时,为列创建约束且取名 =
create table demo05(
empno number(4),
ename varchar2(10),
job varchar2(8),
age number(3) constraint ck_age not null
)
insert into demo05 values(1234,'zhangsan','SALES',300);
select * from demo05;
--在添加完列后,还可以添加约束
--除了not null不可以
create table demo06(
empno number(4),
ename varchar2(10),
age number(3),
constraint uq_empno unique(empno),
constraint ck_age2 check(age>0 and age<200)
)
insert into demo06 values(1234,'zhangsan');
insert into demo06 values(1234,'lisi');
--主键约束
--为了保证该列的数据能够证明行记录在表中是唯一的
--主键约束从形式看,类同于(非空和唯一)约束
create table demo07(
empno number(4) primary key,
ename varchar2(10),
age number(3)
)
insert into demo07 values(1234,'zhangsan',18);
insert into demo07 values(1234,'lisi',18);
insert into demo07 values(null,'wangwu',20);
--在列之后添加
create table demo08(
empno number(4),
ename varchar2(10),
age number(3),
constraint pk_empno primary key(empno)
)
-- 一张表只有一个主键
create table demo09(
empno number(4) primary key,
ename varchar2(10) primary key,
age number(3)
--constraint pk_empno primary key(empno)
)
-- 一张表虽然只有一个主键,但可以选择多个列,作为联合主键
create table demo09(
empno number(4) ,
ename varchar2(10),
age number(3),
constraint pk_empno2 primary key(empno,ename)
)
insert into demo09 values(1234,'zhangsan',18);
insert into demo09 values(1234,'lisi',20);
insert into demo09 values(2345,'zhangsan',20);
insert into demo09 values(1234,null,18);
--外键foreign key
--外键用来描述两张表之间的关系,必须保证外键列的值必须为参考列的值中的一个
create table t_class(
classid number(2) primary key,
classname varchar2(20) not null,
classnum varchar2(30)
)
insert into t_class values(10, 'java班','教室6');
insert into t_class values(20, '大数据班','教室3');
insert into t_class values(30, 'python班','教室4');
delete from t_class where classid = 20;
create table t_student(
stuid number(8) primary key,
stuname varchar2(30) not null,
stuphone varchar2(11),
classid varchar2(2) references t_class(classnum)
)
insert into t_student values(11111111,'方子恒',110,10);
insert into t_student values(11111112,'马子明',120,20);
insert into t_student values(11111113,'王福禄',130,10);
select * from t_class;
select * from t_student;
--存在外键的表为从表,被依赖的表为主表
--外键不一定为主表的主键,也可以为主表的唯一键
--约束除了在建表时,可以添加约束外,还可以修改表时修改约束
alter table demo05 add constraint uq_demo05_ename unique(ename);
alter table demo05 drop constraint uq_demo07_ename
drop constraint --待办=====