Oracle学习笔记(九)
来源:互联网 发布:哈尔滨唯一网络 编辑:程序博客网 时间:2024/05/19 22:51
数据完整性实现方式:约束、触发器、应用程序(过程、函数)等
约束:not null,unique,primary key,foreign key,check五种。
not null:不能为空
unique:值不能重复,但可以为null
primary key:不能重复,而且不能为null(一张表最多只能有一个主键,但可以有多个unique)
foreign key:外键
check:用于强制行数据必须满足的条件
例子:
create table goods(
goodsId char(8) primary key,
goodsName varchar2(20) ,
unitprice number(7,2) check (unitprice>0),
category varchar2(20) ,
provider varchar2(30)
);
create table customer(
customerId char(8) primary key,
name varchar2(20) not null,
address varchar2(30),
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 number(5) check (nums between 1 and 30)
);
如果建表忘记了约束,可以增加约束,使用alter table命令。但是注意:
增加not null约束时,需要使用modify选项,而其他使用add选项。
alter table goods modify goodsName not null;
alter table customer add constraint cardunique unique(cardId);
alter table customer add constraint addresscheck check(address in('东城','西城','海淀','曹阳'));
删掉约束:
alter table 表名 drop constraint 约束名称;
在删除主键约束,必须带上cascade选项,如alter table 表名 drop primary key cascade;
1.显示约束信息:user_constraints
select constraint_name,constraint_type,status,validated from user_constraints where table_name='GOODS';
2.显示约束列:user_cons_columns
select column_name,position from user_cons_columns where constraint_name='约束名';
列级定义:在定义列的同时定义约束。
表级定义:定义了所有列后,再去定义约束。
//表级定义
create table student(
username varchar(20),
password char(6),
constraint pk_username primary key(username)
);
---------------------------------
索引,用于加速数据存储的数据对象。//不要随便建立索引
单列索引:create index 索引名 on 表名(列名);
复合索引:create index goods_idx on goods (goodsId,goodsName);
使用原则:
(1)在大表上建立索引才有意义
(2)在经常引用的列上建立索引
(3)索引的层次不要超过4层
-----------
用户和权限
create user ken identified by ken;
grant create session,create table to ken with admin option;//with admin option代表系统权限可以传递给其他用户
grant create view to ken;
revoke create view from ken;//回收创建视图的权限
注意:系统权限不是级联回收,而对象权限是级联回收。
grant select on emp to monkey;//用户ken将对emp表的搜索权限赋给monkey
select * from monkey.emp;
一次将所有权限交给monkey?
grant all on emp to monkey;
希望monkey只能修改ken.emp表的sal字段?
grant update on emp(sal) to monkey;
---
with grant option用于转授对象权限,但是该选项只能被授予用户,而不能授予角色。
自定义角色:
1.建立角色*(不验证):create role 角色名 not identified;
建立角色(数据库验证):create role 角色名 identified by yzh;//yzh是角色的密码
2.给角色授权:
grant create session to 角色名 with admin option;
grant all on emp to 角色名;
3.将角色授给用户:
create user heizi identified by m123;
grant 角色名 to heizi;
4.删除角色
drop role 角色名;
- Oracle学习笔记(九)
- Oracle学习笔记(九) 查询语句
- 学习笔记(九)
- oracle 学习笔记(九) update 和 delete
- 九、Oracle数据库之学习笔记---Oracle操作数据
- C++学习笔记(九)
- MySQL学习笔记(九)
- java学习笔记(九)
- Spring学习笔记(九)
- Hibernate学习笔记(九)
- Linux学习笔记(九)
- Linux学习笔记(九)
- Java学习笔记(九)
- Java学习笔记(九)
- JOS学习笔记(九)
- Lua学习笔记(九)
- Java学习笔记(九)
- python 学习笔记(九)
- 私有IP地址
- 一个简单的验证带验证提示例子
- 我的圣思园,我的梦
- 急!请大侠们指教!
- C++:构造函数和析构函数能否为虚函数
- Oracle学习笔记(九)
- 我来了
- 我来了
- Java中toArray的用法探究(java数组与list转换)
- 我来了
- sql datepart查询一周月年基本用法
- 解决windows vim乱码问题
- 加油
- 我一定要考上!