oracle如何使用索引,oracle索引的建立与使用

oracle的索引是怎么使用的?

索引就像书的目录一样,是为了方便查询的。一般在数据表记录很多的时候建立索引,oracle的索引有哈希索引和聚簇索引

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的思南网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

Oracle使用(九)_表的创建/约束/索引

表创建标准语法:

CREATE TABLE [schema.]table

(column datatype [DEFAULT expr] , …);

--设计要求:建立一张用来存储学生信息的表,表中的字段包含了学生的学号、姓名、年龄、入学日期、年级、班级、email等信息,

--并且为grade指定了默认值为1,如果在插入数据时不指定grade得值,就代表是一年级的学生

--DML是不需要commit的,隐式事务

create table student

(

stu_id number(10),

name varchar2(20),

age number(2),

hiredate date,

grade varchar2(10) default 1,

classes varchar2(10),

email varchar2(50)

);

-- 注意日期格式要转换,不能是字符串,varchar2类型要用引号,否则出现类型匹配

--DML 需要收到commit

insert into student values(20211114,'zhangsan',22,to_date('2021-11-14','YYYY-MM-DD'),'2','1',' 123@qq.com ');

insert into student(stu_id,name,age,hiredate,classes,email) values(20211114,'zhangsan',22,to_date('2021-11-14','YYYY-MM-DD'),'1',' 1234@qq.com ');

select * from student;

-- 给表添加列,添加新列时不允许为not null,因为与旧值不兼容

alter table student add address varchar(100);

-- 删除列

alter table student drop column address;

--修改列

alter table student modify(email varchar2(100));

正规表设计使用power disinger

--表的重命名

rename student to stu;

-- 表删除

drop table stu;

**

在删除表的时候,经常会遇到多个表关联的情况(外键),多个表关联的时候不能随意删除,使用如下三种方式:

2.表的约束(constraint)

约束:创建表时,指定的插入数据的一些规则

约束是在表上强制执行的数据校验规则

Oracle 支持下面五类完整性约束:

1). NOT NULL 非空约束 ---- 插入数据时列值不能空

2). UNIQUE Key 唯一键约束 ----限定列唯一标识,唯一键的列一般被用作索引

3). PRIMARY KEY 主键约束 ----唯一且非空,一张表最好有主键,唯一标识一行记录

4). FOREIGN KEY 外键约束---多个表间的关联关系,一个表中的列值,依赖另一张表某主键或者唯一键

-- 插入部门编号为50的,部门表并没有编号为50的,报错

insert into emp(empno,ename,deptno) values(9999,'hehe',50);

5). CHECK 自定义检查约束---根据用户需求去限定某些列的值,使用check约束

-- 添加主键约束/not null约束/check约束/唯一键约束

create table student

(

stu_id number(10) primary key,

name varchar2(20) not null,

age number(3) check(age0 and age126),

hiredate date,

grade varchar2(10) default 1,

classes varchar2(10),

email varchar2(50) unique,

deptno number(2),

);

-- 添加外键约束

create table stu

(

stu_id number(10) primary key,

name varchar2(20) not null,

age number(3) check(age0 and age126),

hiredate date,

grade varchar2(10) default 1,

classes varchar2(10),

email varchar2(50) unique,

deptno number(2),

FOREIGN KEY(deptno) references dept(deptno)

);

-- 创建表时没添加外键约束 也可以修改 其中fk_0001为外键名称

alter table student add constraint fk_0001 foreign key(deptno) references dept(deptno);

索引创建有两种方式:

组合索引:多个列组成的索引

--索引:加快数据剪碎

create index i_ename on emp(ename);

--当创建某个字段索引后,查询某个字段会自动使用到索引

select * from emp where ename = 'SMITH';

--删除索引 索引名称也是唯一的

drop index i_ename;

一些概念:

回表:

覆盖索引

组合索引

最左匹配

oracle中怎么建立和使用索引

在程序中,oracle优化器在认为索引效率更高时,会自动调用索引。

也可以显式调用索引:

select

/*+index(A,索引名)*/

*

from

A

where

b=‘’,c='';

如何使用oracle中的索引

方法如下: Oracle中建立索引,会提高查询速度: create index 索引名 on 表名(列名); 例如: create index index_userid on tbl_detail(userid); 如何找数据库表的主键字段的名称? SELECT * FROM user_constraints WHERE CONSTRAINT_TYPE='P' a...


分享题目:oracle如何使用索引,oracle索引的建立与使用
转载注明:http://scyanting.com/article/dscjpcj.html