维护表的完整性
可以用约束,触发器,应用程序(过程,函数)三种方法实现
而约束作为首选
约束包括not null,nuique,primary key ,foreign key ,check
注意一个表只有一个主键,主键可以有多个列,不可为空。
age number(3) check (age > 0),
name varchar2(20) not null,
mother varchar2(20) nuique,
sex char(2) default '男' check(sex in('男','女')),
categoryId number reference categroy(categoryid),
unms check(sums between 1 and 66)
忘记了约束,可以建表后使用alter table ,但是not null约束需要用
modify选项,其他四种可以使用add 选项
alter table xxx modify name not null;
alter table xxx add constraint useridnuique unique(userid)
// userid 增加不重复约束
删除约束 alter table 表名 drop constraint 约束名
强制删除,主从关系删除 alter table 表名 dropprmary key cascade;
显示约束信息
select constraint_name ,constraint_type,status,validated form
user_constraints where table_name = '表名';
显示约束列
select column_name,position from user_cons_consmns
where constaint_name = '表名' ;
表级定义 列级定义
表定义,定义了之后,在定约束
create table a (id number constraint a_pk primary key,name varchar2(20))
create table b (id number,name varchar2(20),
constraint a_pk primary key(id))
建立索引为什么加了索引,i/o 性能会提高很多。索引不是随意建的,创建的不好反而相反影响性能。
创建索引
单列索引
create index nameindex on 表(name)
联合索引(复合索引)
create index nameindex on 表(字段1,字段2)
-- 注意顺序,一下帅选很多的字段放到后面,因为sql语句的扫描是从后往前扫描的