oracle數據庫開發參考

sql語法

  • 1.完整性約束條件

    • 列級完整性約束
    	NOT NULL
    
    • 表級列級均可用
    unique,check,primary key,foreign key
    
  • 2.表操作

    • 創建表
    create [[global]temporory|table|schema.]tablename
    (
    	<列名> <數據類型> [列級完整性約束條件],
    	...,
    	[表級完整性約束條件]
    )
    [on commit {delete|preserve}rows]
    [organizition {heap|index|external...}] -- 表類型
    [partition by...] --分區及子分區信息
    [tablespace 表空間名]
    [logging|nologging] --是否保存重做日誌
    [compress|nocompress] --是否壓縮
    
    • 增加列
    //括號有多個的時候使用
    alter table [schema.]tablename add (<列名> <數據類型> [default...])
    
    • 修改列
    alter table [schema.]tablename modify (<列名> [數據類型,長度,默認值...] )
    
    • 刪除列
    alter table [schema.]tablename drop (<列名>,<列名>) [cascade constraints]
    
    //爲了應對不影響高併發環境,使用unused標記字段不可用
    alter table [schema.]tablename set unused (<列名>,<列名>) [cascade constraints]
    
  • 3.索引

    • 創建索引
    create [unique]|[bitmap]index [schema.]index_name on [schema.]table_name([column1[ASC|DESC]...] | [express])
    [tablespace 表空間]
    [pctfree n1] --爲insert預留的空間百分比
    [storage(initial n2)]
    [compress n3 | nocompress]
    [logging|nologging]
    [online]
    [compute statistics]
    [reverse | nosort]
    
    //demo
    create unique index tenantid_name_uk on hook(tenantId,name); --唯一索引
    create index tenantid_key on hook(tenantId);
    
    • 刪除索引
    drop index index_name;
    
  • 4.視圖

    • 創建視圖
    create [or replace | force] view view_name
    [(column1, column2...)]
    as select ... from ... where ...
    [with check option][constraint 約束名]
    [with read only]
    
    • 刪除視圖
    drop view view_name;
    
  • 5.數據操作

    • 複製表插入記錄
    create table table_name as select ....;
    
    • 使用視圖
    //插入數據
    insert into v_view. --需要主表存在此信息
    //update
    update v_view set ...
    //刪除
    delete from v_view
    
    • insert 數據
    insert into [user.]table [@db_link] (column1...) values (express...);
    //批量更新
    insert into table(...) select .... --select語法
    
    • update 數據
    update table_name set {column1=12, (column2[, column3]) = (select query)} [where ...]
    
    • 刪除 數據
    delete from table [where ...]
    
    truncate table table_name [reuse storage | drop storage]
    
    • 授權
    grant <權限...> [on <對象類型> <對象>] [to <用戶...>] [with grant option(可以分配給其他用戶)]
    
    // demo
    grant select on table table_name to user1;
    grant all privileges on table table_name to user2;
    
    • 回收權限
    revoke <權限> [on <對象類型> 對象] from <用戶...>
    
    • 內置函數
    //子符類函數
    ASCII(c1)	--c1是字符串,返回第一個字母的ASCII
    chr(i) --返回i對應的ascii,i是整數
    concat(c1,c2) --把c2連接到c1後面。
    initcap(c1)	--c1的每個單詞的首字母大寫,其餘字母小寫。
    instr(c1,c2,i,j) --返回從c1的第i個字符開始,c2出現第j次的位置號。
    length(c1) --返回c1的長度
    lower(c1) --返回c1的小寫字母
    ltrim(c1,c2) --將c1最左邊的字符去掉,使其第一個字符不在c2中。
    replace(c1,c2,c3) --用c3替換c2在c1的位置。
    substr(c1,i[,j]) --從第i個位置開始,返回j個長度的字符串。
    

    數學函數 在這裏插入圖片描述 在這裏插入圖片描述
    日期類函數
    在這裏插入圖片描述
    轉換類函數
    在這裏插入圖片描述
    聚集類函數
    在這裏插入圖片描述
    在這裏插入圖片描述

表空間

  • 在空間充足下的管理 (建議做好空間評估,減少資源浪費)
    1.通過數據字典監視
select * from user_free_space;
select * from dba_free_space;

2.增加數據文件

alter tablespace t_space add datafile '/data/info.dbf' size 2M;
  • 空間不足的管理
    1.增加system表空間的數據文件大小
alter database orcl datafile '/data/info.dbf' resize 2M;

在這裏插入圖片描述
在這裏插入圖片描述

2.創建新表空間

create tablespace t_sp
datafile '/data/info.dbf' size 2M
default storage(
	inital 2M
	next 2M
	minextents 2
	maxextents 10
	pctincrease 20
)
online;

在這裏插入圖片描述
3.動態增加表空間

alter tablespace t_sp 
add datafile '/data/info.dbf' size 5M, '/data/info2.dbf' size 10M;

在這裏插入圖片描述
在這裏插入圖片描述

實戰

  • 1.創建表空間
create tablespace webapp
datafile '/data/oracleData' size 50M
default storage(
	inital 500K
	next 500K
	minextents 1
	maxextents unlimited
	pctincrease 0
)
online;
  • 2.創建用戶
create user 用戶名 identified by 密碼 default tablespace space_data(表空間名稱) temporary tablespace space_temp(臨時表空間名稱);

//授權
grant connect,dba to 用戶名;

//修改限額
ALTER USER "用戶名" QUOTA UNLIMITED ON SPACE_DATA(表空間名稱);

//刪除用戶及數據
drop user user_name cascade;
  • 3.創建表
create table 表名稱 (
id  varchar2(50) primary key ,
name char(200) not null,
phone number(11) unique,
class carchar(10), 
foreign key (name)
)
tablespace USERS ----表放在USERS表空間
pctfree 10 ----保留10%空間給更新該塊數據使用
initrans 1 -----初始化事物槽的個數
maxtrans 255 ----最大事務槽的個數
storage ----存儲參數
(
initial 64K ---區段一次擴展64k
next 1M
minextents 1 ---最小區段數
maxextents unlimited --最大區段無限制
)

1、添加主鍵約束(將stuNo作爲主鍵)

alter table stuInfo add constraint PK_stuNo primary key (stuNo)

2、添加外鍵約束 (主表stuInfo和從表stuMarks建立關係,關聯字段stuNo)
alter table stuInfo 
add constraint FK_stuNo foreign key(stuNo) references stuinfo(stuNo)

3、添加唯一約束(身份證號唯一)
alter table stuInfo
add constraint UQ_stuID unique(stuID)

4、添加默認約束(如果地址不填 默認爲“地址不詳”)
alter table stuInfo
add constraint DF_stuAddress default (‘地址不詳’) for stuAddress

5、添加檢查約束 (對年齡加以限定 15-40歲之間)
alter table stuInfo
add constraint CK_stuAge check (stuAge between 15 and 40)

6、添加表註釋:學生信息表

comment on table STUINFO 
is '學生信息表';

7、添加列名稱:學號

comment on column STUINFO.stuid 
is '學號';
comment on column STUINFO.stuname
is '學生姓名';
  • 4.創建序列
CREATE SEQUENCE student_seq 	-- student_seq:序列名稱
     INCREMENT BY 1 -- 每次增加1個
     START WITH 1 --從1開始計數
     NOMAXVALUE -- 不設置最大值
     NOCYCLE --直累加,不循環
     NOCACHE --不建立緩衝區
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章