简单介绍一下索引 约束 角色一些简单的SQL

维护数据库的完整性
  一般有三种 约束 ,触发器 ,应用程序(过程,函数),其中约束具有较高的性能
约束

   包括 not null,unique,primary key, foreign key ,check 共五种   

    unique  不能重复,但可以为空

   一个表可以有一个主键,但可以有多个unique
create  table goods
(
   goodsId char(8)  primary key,
   goodName 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
(
  customId  char(8) references customer(customerId),
  goodId   char(8)  references  goods(goodsId),
  nums number(10) check(nums between 1 and 30)
)
 
alter table goods modify goodname not null;
alter table customer add constraint customer_indentity unique(cardId);
alter table customer add constraint address_check check (address in ('东城','西城'));

alter table customer  drop primary key cascade;


--显示约束信息

select * from user_constraints  t where t.TABLE_NAME ='GOODS';
select * from  user_cons_columns  t  where t.CONSTRAINT_NAME ='SYS_C0011152';

--修改表的约束

alter table goods modify goodname not null;
alter table customer add constraint customer_indentity unique(cardId);
alter table customer add constraint address_check check (address in ('东城','西城'));
alter table customer  drop primary key cascade;

--显示约束信息

select * from user_constraints  t where t.TABLE_NAME ='GOODS';
select * from  user_cons_columns  t  where t.CONSTRAINT_NAME ='SYS_C0011152';


---行级别约束定义

create  table department 
(
  departmentId   number(10)  constraint pk_depart primary key ,
  name  varchar2(50),
  loc varchar2(50)
)
---表级别约束定义
create table employee
(
  empId  number(10),
  departId number(10),
  ename varchar(50),
  
  constraint employee_pk    primary key (empId),
  constraint employee_fk   foreign  key(departId) 
  references  department(departmentId)
)

-- 单索引
create index emp_name_index on employee(ename);
--复合索引

create    index  emp_name_category  on (name,catagory);


--使用原则

--1.在大表上建立数据
 
--2.不能再经where 字句和连接条件经常出现的列

--3.索引的层次不能超过四层 

索引的缺点
1.建立索引,系统要占用大约为表1.2倍的硬盘和内存坑技安来保存索引。
2.更新数据的时候,系统必须要有额外的时间来同时对索引进行更新,以维持数据和索引的一致性
实践证明 ,不恰当的索引不但于事无补,反而会降低系统的性能,因为大量的缩影在进行插入,修改和删除操作时 比没有索引时 要花费更多的时间

比如在如下字段上建立索引应该是不恰当的
1.很少或从不应用的字段
2,,逻辑型的字段如男或女(是或否)等,综上所述提高查询效率是以消耗一定的系统资源为代价的,索引不能盲目的建立,这个是考验DBA 是否优秀的
一个重要标准


索引的分类
  唯一性可以分为唯一索引和非唯一索引
  
    
select *   from user_indexes  t where t.TABLE_NAME='EMPLOYEE';
select * from user_ind_columns  t where t.INDEX_NAME='EMP_NAME_INDEX'

数据库
   包括系统权限和对象权限
   
 
 查看所有权限(sysdba(
 select * from dba_roles;
      
select * from dba_roles;
create user  ken identified by ken;

grant create session,create table ,create view to ken with admin option;
create user tom identified by  tom;

revoke create view from ken;

grant create session to ken with admin option;
grant  create view to ken;
select * from dba_tab_privs;

 对象权限
   授予对象
      
create user monkey identified by monkey;
grant create session to monkey;
---scott 用户  
grant select on emp to monkey;
grant select on emp to monkey;
grant update on emp to monkey;
grant delete on emp to monkey;
grant insert on emp to monkey;

grant alter on emp to monkey;

grant execute on dbms_transation to monkey;

---把所有用户权限付给 monkey

grant all on emp to monkey;

只能获得 某一列的值
grant  all on emp(ename,sal) to monkey

系统权限 --》 a->b->c   A  B 回收b  ,c  有权限
对象权限  --》;a->b-》c  B 回收b  ,c  没权限

grant select on emp to monkey with grant option;
revoke   select on emp from monkey

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章