一般有三種 約束 ,觸發器 ,應用程序(過程,函數),其中約束具有較高的性能
約束
包括 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