黑马程序员 Oracle约束、索引、权限


约束:
(1)check约束
eg:check(price>0)
(2)唯一约束
eg:email varchar(50) unique,
(3)默认约束
eg:sex char(2) default '男' check(sex in('男','女'))
(4)主键约束
eg:customerId char(8) primary key,
(5)外键约束
eg:customerId char(8) references customer(customerid),
增加约束:
(1)增加not null约束
alter table goods modify goodsName not null;
(2)增加唯一约束
alter table customer add constraint cardunique unique(cardid);
(3)增加check约束
alter table customer add constraint addresscheck check(address in('东城','西城'));
删除约束:
alter table 表名 drop constraint 约束名称;
(1)删除主键
alter table 表名 drop primary key;
如果两张表存在主从关系,那么在删除主表的主键约束时,必须带上cascade选项
alter table 表名 drop primary key cascade;
显示约束信息:
sql>select constraint_name,constraint_type,status,validated from user_constraints where table_name='表名';
显示列约束:
sql>select colum_name,position from usr_cons_clumns where constrain_name='约束名';
列级定义:
在定义列的同时定义约束。
sql>create table department(
dept_id number(2) constraint pk_department primery key,
name varchar2(12),
loc varchar2(12)
):
表级定义:
在定义了所有列后,再定义约束,not null只能在列级上定义。
sql>create table employee(
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 department(dept_id)
):
索引:
(1)单列索引
create index 索引名 on 表名(列名);
(2)复合索引
create index emp_idx1 on emp(ename,job);
显示索引:
sql>select index_name,index_type from user_indexs where table_name='表名';
显示索引列:
select table_name,column_name from user_ind_comumns where index_name='IND-ENAME';
权限:
(1)dba用户(sys,system)可以将任何对象上的对象权限授予其他用户;
(2)希望monkey用户只可以修改scott.emp的表的sal字段
sql>grant update on emp(sal) to monkey;
(3)希望monkey只可查询scott.emp表的ename,sal数据
sql>grant seelct on emp(ename,sal) to monkey ;
(4)授予alter权限
--如果black用户要修改scott.emp表的结构,则要授予alter对象权限。
sql>conn scott/tiger;
sql>grant alter on emp to blake;
(5)授予index权限
--如果想在别的方案上建立索引,则必须具有index权限
sql>conn scott/tiger;
sql>grant index on scott.emp blake;
回收对象权限(级联回收):
如:
scott------------>blake----------->jones
select on emp select on emp select on emp;
对象权限级联回收,系统权限不级联回收。
sql>conn scott/tiger@accp;
sql>revoke select on emp from blake;
回收系统权限(不级联回收):
如:
system------------>ken------------->tom
sql>revoken create session from ken;tom还能登录;
角色:
(1)预定义角色
常用的是:connect,resource,dba
(2)自定义角色
(1)建立角色(不验证)
sql>create role 角色名 not identified;
(2)建立角色(数据库验证
sql>create role 角色名 identified by 密码;
给角色授权:
sql>conn system/manager;
sql>grant create session to 角色名 with admin iption;
sq>conn scott/tiger@myoral;
sql>grant insert,update delete on scott.emp to 角色名;
给用户授角色:
sql>conn system/manager;
sql>grant 角色名 to 用户名 with admin opton;
删除角色:
sql>conn system/manager;
sql>drop role 角色名;
显示所有角色:
sql>select * form dba_roles;

 

posted @ 2013-05-14 22:30  xiewen3410  阅读(114)  评论(0编辑  收藏  举报