设为首页 加入收藏

TOP

玩转oracle学习第五天(一)
2015-11-21 01:39:23 来源: 作者: 【 】 浏览:0
Tags:玩转 oracle 学习 第五
??

1.上节回顾
2.维护数据的完整性
3.管理索引
4.管理权限和角色


1.掌握维护oracle数据完整性的技巧
2.理解索引的概念,会建立索引
3.管理oracle的权限和角色

介绍:维护数据的完整性
数据完整性用于确保数据库数据遵从一定的商业和逻辑guize,
在oracle中,数据完整性可以使用约束,触发器,
应用程序(过程,函数)三种方式来实现,在这三种方法中,因为约束易于维护,
并且具有最好的性能,所以作为维护数据完整性的首选

约束:约束用于确保数据库数据满足特定的商业规则,
在oracle中,约束包括:not null,unique,primary key,
foreign key和check五种

not null(非空)
如果定义了not null,那么当插入数据时,必须为列提供数据

unique(唯一):
当定义了唯一约束后,该列值是不能重复的,但可以为null

primary key:

check:用于强制行数据必须满足一定的规则

商店售货系统表设计案例:
商品表:(goods)

商品号:goodsId
商品名:goodsName
单价:unitprice
商品类别:category
供应商:provider


客户表:(customer)

客户号:customerId
姓名:name
住址:address
电子邮件:email
性别:sex
身份证:cardId


购买:(purchase)

客户号:customerId
商品号:goodsId
购买数量:nums

请用sql语言完成下列功能:
1.建表,在定义中要求声明:
(1)建表
(2)客户的姓名不能为空
(3)单价必须大于0,购买数量必须在1到30之间
(4)电子邮件不能够重复
(5)客户的性别必须是男或者女,默认为男

create table goods(goodId char(8) primary key,
goodsName varchar2(30),
unitprice number(10,2) check(unitprice > 0),
category varchar2(8),
provider varchar2(30)
);

create table customer(customerId char(8) primary key,
name varchar2(50) not null,
address varchar2(50),
email varchar2(50) unique,
sex char(2) default '男' check(sex in ('男','女')),
cardId char(18)
);

create table purchase(customerId char(8) references customer(customerId), //外键
goodsId char(8) references goods(goodsId),
nums numbers(10) check (nums between 1 and 30)
);


商店售货系统表设计案例(2)
如果在建表时忘记建立必要的约束,则可以在建表后使用 alter table进行修改,


alter table goods modify goodsName not null;

alter table customer add constraint cardunique unique(cardId);//修改一个表,增加唯一性约束,并位唯一性取名字cardunique

alter table customer add constraint addresscheck check (address in ('东城','西城'));

删除约束:
alter table 表名 drop constraint 约束名称;

显示约束信息:
1.显示约束信息:
通过查询数据字典视图user_constraints,可以显示当前用户所有
的约束的信息
select constraint_name,constraint_type,status,validated from
user_constraints where table_name='表名';

2.显示约束列
通过查询数据字典视图user_cons_columns,可以显示约束所
对应的表列信息

select column_name,position from user_cons_columns where constraint_name='约束名';

3.当然也有更容易的方法,直接用PL/SQL developer查看即可


维护数据完整性 -表级定义和列级定义
列级定义:
列级定义是在定义列的同时定义约束:
create table department4
(
dept_id number(2) constraint pk_department primary key,
name varchar2(12),
ioc varchar2(12)
);

表级定义:
表级定义是在定义了所有列后,再定义约束,这里需要注意:
not null 约束只能在列级上定义
create table employee2
(
emp_id number(4),name varchar2 (15),dept_id number(2),
constraint pk_employee primary key (emp_id),
constraint fk_department foreign key (dept_id) references department4(dept_id)
);


管理索引:原理介绍
索引是用于加速数据存储的数据对象,合理的使用索引可以大大
降低i/o次数,从而提高数据访问性能,索引有很多种
为什么添加索引后,会加快查询速度呢?

视图是为了将数据进行分类,归档,然后得到数据的索引,通过
索引就可找到数据了
索引提高了数据的查找速度,但是索引并不是建的越多越好

索引分类:
单列索引:基于单个列所建的索引
create index nameIndex on customer(name);

复合索引:基于两列或是多个列的索引,在同一张表上可以有多个索引,但是要求
列的组合必须不同,比如:
create index emp_index1 on emp(ename,job);

create index emp_index1 on emp(job,ename);
是两个不同的索引

sql语句的执行时从右到左的进行扫描的

使用原则:
(1)在大表上建立索引
(2)在where子句或是连接条件上经常引用的列上建立索引
(3)索引的层次不要超过4层

索引的缺点:
1.建立索引,系统要占用大约为表的1.2倍的硬盘和内存空间来保存索引
2.更改数据的时候,系统必须要有额外的时间来同时对索引进行更新,以维持数据和索引的一致性

按照数据存储方式,可以分为B*树,通过查询数据字典视图dba_indexs和
user_indexa,可以显示索引的信息

管理权限和角色

oracle中的权限:
(1)系统权限:oracle提供了140多种系统权限
什么是系统权限?
操作数据库系统的权限
系统权限有哪些?
如何赋予系统权限?
授予系统权限一般是dba来完成的
create user ken identified by m123;
create user tom identified by m123;

conn system/manager as sysydba;
grant create session,create table to ken with admin option;(权限可以转移授权)

gra

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇oracle设定用户密码使用时间 下一篇mysql.sock的作用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: