一、索引
1.为了方便快速查找数据表中的记录,索引也是数据库中的一个对象,索引内部信息包含记录的rowid,相当于地址信息。
2.索引被创建后,由数据库自动维护。
--格式:
create [unique] index indexName on tableName(colName[,……])
--练习:创建索引,使用员工表中的部门号和员工编号
create index emp_index on emp(deptno,empno);
--当使用语句:
select * from emp order by deptno,empno;
--此时数据库中就使用了索引,进行快速排序。
drop index emp_index;
4.索引的优缺点
当数据表需要频繁进行DML操作时,此时数据库会自动维护索引,会增加开销,所以索引的使用需要看表的操作情况。
二、约束(constraint)
1.也叫完整性约束条件,数据表中的一些限制条件。当数据表中的数据发生变化必须遵循这些限制条件,不遵循,不能进行DML操作。
2.分类
(1)非空约束 not null 简称 NN
(2)唯一性约束 unique 简称 Uk
(3)主键约束 primary key 简称PK
(4)外键约束 foreign key 简称FK
(5)检查约束 check 简称CK
3.非空约束的建立
drop table temp_100;
create table temp_100(
name varchar2(20) not null,
age number(3) constraint temp_100_age_NN not null--列级约束
);
约束分列级约束和表级约束两种写法。
列级约束:就是在建表时写在列类型后面。
表级约束:就是在建表时写在最后一个字段后。
非空约束没有表级约束的写法。
--建表后修改非空约束:
--解除非空约束:
alter table temp_100 modify age number(3) null;
--添加非空约束:
alter table temp_100 modify age number(3) not null;
4.唯一性约束:unique
对字段进行此约束时,字段的值是唯一的,不能重复,可以为null.
建表时:
create table temp_101(
name varchar2(20) not null,
idcard varchar2(30) unique,--列级约束
);
--插入数据:
insert into temp_101 values('张三','123456789');
insert into temp_101 values('李四',null);
drop table temp_102;
create table temp_102(
name varchar2(20) not null,
idcard varchar2(30),
constraint t_idcard_uk unique(idcard)--表级约束写法
);
--建表后设置唯一性约束:
alter table temp_102 add constraint t_idcard_uk unique(idcard);
5.主键约束:(非空且唯一)
主键:是一张表中能给记录唯一标识符的字段。通常此字段除了作为唯一标识符,没有其他意义。
(1)一张表中只能有一个主键约束。其他约束没有个数限制。
(2)主键约束相当于非空约束和唯一性约束
(3)主键要选择表中尽可能没有数据逻辑的字段。
建表时(列级约束写法)
create table temp_103(
id number(4) primary key,
name varchar2(20)
);
insert into temp_103 values(1001,'张三');
insert into temp_103 values(1002,'李四');
建表时(表级约束写法)
create table temp_104(
id number(4),
name varchar2(20),
constraint t_id_pk primary key(id,name)
);
insert into temp_104 values(1001,'张三');
insert into temp_104 values(1001,'李四');
insert into temp_104 values(1002,null);
主键约束:可以对一个字段或多个字段进行约束,多个字段时,组合值:非空且唯一。
建表后增加主键约束:
create table temp_105(
id number(4),
name varchar2(20)
);
alter table temp_105 add constraint t_105_id_pk primary key(id);
6:外键约束:
两张表的两个字段存在关系或者一张表的两个字段的关系。其中表B的一个字段的值依赖表A一个字段中的某一个值或者可以为null.
那么:表A叫主表、父表,表B叫从表,子表。
建表时(列级约束写法):
create table temp_120(
empno number(4) primary key,
mgr number(4) references temp_120(empno)
);
建表时(行级约束写法):
create table temp_106(
empno number(4) primary key,
mgr number(4),
constraint te_mgr_fk foreign key(mgr) references temp_106(empno)
);
insert into temp_106 values(1001,null);
建表后追加外键约束:
create table temp_107(
id number(4),
mgr number(4)
);
alter table temp_107 add constraint t_107_mgr_fk foreign key(mgr) references temp_106(empno);
insert into temp_107 values(1001,1003);
7.检查约束:
对某一字段或某些字段做一些条件限制。必须满足条件的可以DML.
建表时:
create table temp_108(
name varchar2(20),
gender char(1) check(gender in('f','m'))
);
insert into temp_108 values ('张三','f');
create table temp_109(
name varchar2(20),
sal number(9,2) check(sal>1000 and sal<10000)
);
建表后追加检查性约束:
create table temp_110(
name varchar2(20),
gender char(3)
);
alter table temp_110 add constraint t_gender_ck check(gender ='女' or gender='男');
select * from emp where '1'='1';