[獨孤九劍]Oracle知識點梳理(一)表空間、用戶html
[獨孤九劍]Oracle知識點梳理(四)SQL語句之DML和DDLspa
[獨孤九劍]Oracle知識點梳理(五)數據庫經常使用對象之Table、Viewcode
[獨孤九劍]Oracle知識點梳理(六)數據庫經常使用對象之Procedure、function、Sequencehtm
[獨孤九劍]Oracle知識點梳理(七)數據庫經常使用對象之Cursor對象
[獨孤九劍]Oracle知識點梳理(八)常見Exception blog
[獨孤九劍]Oracle知識點梳理(九)數據庫經常使用對象之package字符串
[獨孤九劍]Oracle知識點梳理(十)%type與%rowtype及經常使用函數get
Oracle的經常使用字段類型有:
1 create table person 2 ( 3 ID number primary key, 4 name varchar2(32), 5 gender char default 1 6 ) 7 tablespace tablespaceName; --設置表空間,不加就默認爲當前用戶所在表空間 8 9 comment on table person is '人員表'; --添加備註 10 comment on column person.ID is '編號'; 11 comment on column person.name is '姓名'; 12 comment on column person.gender is '性別';
1 rename person to people; --修改表名 2 alter table person rename column name to xingming; --修改列名 3 alter table person add phonenumber varchar2(11);--添加列 4 alter table person add birthday date default sysdate not null; --添加列,設置默認值,不可爲空 5 alter table person drop column phonenumber; --刪除列 6 alter table person modify name varchar2(64); --修改列長度 7 alter table person modify gender char(2); --修改列精度 8 alter table person modify gender varchar2(2); --修改列的數據類型 9 alter table person modify gender default 2;--修改列的默認值
alter table person add constraint pk_person_id parmary key(id);--將id列設置爲主鍵
alter table person add constraint fk_person_gender foreign key(gender); --將gender列設置爲外鍵
alter table person add constraint ck_person_gender check (gender in (1,2));--檢查gender列的值必須在一、2中
alter table person modify name constraint not_null_person_name not null;--name列不能爲空
alter table person add constraint uq_emp_name unique(name); --設置name列惟一約束
alter table person modify birthday date default sysdate; --設置birthday列的默認值
1 --禁用約束 2 alter table person disable constraint uq_person_name;--禁用uq_person_name這個約束 3 --啓用約束 4 alter table person enable constraint un_person_name; 5 --延遲約束 6 alter table person drop constraint fk_person_gender; 7 alter table person add constraint fk_person_gender foreign key(gender) reference genderInfo(genderNo) deferrable initially deferred;
1 truncate table person where id>10; --清除表person中的數據,不可恢復 2 delete table person where id>10; --刪除表person中的數據,可恢復 3 drop table person; --刪除表
這裏泛指普通視圖view,Oracle還包含一種叫物化視圖(materialized view)的視圖。這裏暫不涉及。
視圖分爲簡單視圖( 基於單個基表,且不包含函數和數據分組操做 ) 和複雜視圖( 基於多個基表或視圖);簡單視圖能夠經過視圖修改數據. 這些修改包括插入數據. 更新數據和刪除數據. 可是對於複雜視圖來講, 經過視圖修改數據必須知足必定的規則.
在視圖定義中沒有設定READ ONLY 的前提下, 若是視圖包含了下面的內容, 那麼不能經過視圖刪除表中的數據:
[關於經過視圖修改數據的操做這裏不作介紹]
1 create or replace view v_person --建立或修改視圖 2 [columnName1,columnName2] --設置列名,也可在select中設置別名 3 as -- 注1 4 select a.f1,b.f2 from table1 a,table2 b where a.f0=b.f0;
注1:關於Oracl的is和as,
在存儲過程(PROCEDURE)和函數(FUNCTION)中沒有區別,
在視圖(VIEW)中只能用AS不能用IS,
在遊標(CURSOR)中只能用IS不能用AS。
使用union關鍵字進行合併查詢的時候,數據庫引擎會自動過濾掉結果集中的重複記錄。
使用union all關鍵字進行合併查詢的時候,數據庫引擎就不會過濾結果集中的重複數據,所以在執行效率上將union all比union要高上不少。
drop view v_person;