視圖(view),稱爲虛表,在數據庫中不存在實體。java
視圖本質上是對物理表(基表)的一種數據保護。讓開發者或者用戶只能看到基表中的部分數據。數據庫
建立視圖的語法服務器
create or replace view 視圖名 as query
createorreplaceview v$empinfo as select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.deptno from emp e;
能夠向使用表同樣使用視圖session
1 -- 使用視圖 2 3 select * from v$empinfo; 4 5 -- 刪除視圖 6 7 drop view v$empinfo;
修改視圖中的數據oracle
添加數據數據庫設計
-- 向視圖添加數據 insertinto v$empinfo(empno,ename,job,mgr,hiredate,deptno) values(1090,'cai90','singer',7839,sysdate,30); select * from emp e;
經過視圖添加數據,數據最終添加到基本中,由於視圖是虛表。ide
視圖通常只是基表的部分數據,經過視圖向基表添加數據時,基本的數據只能添加一部分,此時若是基表會對未提供的字段置null。若是基表對未提供的字段要求不能爲null,這次添加會失敗。工具
insertinto v$empinfo(ename,job,mgr,hiredate,deptno) values('cai100','singer',7839,sysdate,30);
![]()
|
刪除數據開發工具
-- 【2】刪除數據 deletefrom v$empinfo where empno = 1090
更新數據優化
不能經過視圖更新視圖不存在的字段
update v$empinfo set comm = 20 where empno = 1080
只讀視圖
createorreplaceview v$empinfo as select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.deptno from emp e withreadonly;
insertinto v$empinfo(empno,ename,job,mgr,hiredate,deptno) values(1110,'cai100','singer',7839,sysdate,30);
![]()
|
1 -- 平均薪水的等級最低的部門,它的部門名稱是什麼 2 3 select vt3.deptno, d.dname 4 5 from (select * 6 7 from v$AvgSalGrade VT2 8 9 where VT2.grade = (selectmin(vt1.grade) from v$AvgSalGrade VT1)) VT3 10 11 join dept d 12 13 on vt3.deptno = d.deptno 14 15 16 17 18 19 -- 經過視圖優化 20 21 createorreplaceview v$AvgSalGrade as 22 23 select vt0.deptno,vt0.avgsal,sg.grade 24 25 from (select e.deptno,avg(e.sal) "AVGSAL" 26 27 from emp e 28 29 groupby e.deptno) VT0 join salgrade sg on vt0.avgsal between sg.losal and sg.hisal 30 31 withreadonly
第一個使用scott帳戶是須要解鎖
Alter user scott accountunlock;
此時scott若是對數據庫進行DDL操做是沒有權限的。把建立視圖、建立表的權限分配給soctt
注意:權限性操做都要以sysdba什麼來操做。
![]()
|
如何建立一個用戶並授予必定權限?
1 --建立用戶 2 3 Create user test01 identifiedby123 4 5 6 7 -- 查看是否建立成功 8 9 select * from dba_users 10 11 where username = 'TEST01'; 12 13 14 15 -- 受權登陸(會話)權限 16 17 Grant create session to test01; 18 19 20 21 -- 默認用戶沒有任何表,並且不具有操做其餘表的權限。 22 23 --select * from emp; 24 25 26 27 -- 受權soctt.emp全部權限(all)給test01 28 29 Grant all on scott.emp to test01; 30 31 -- 回收權限 32 33 Revoke all on scott.emp from test01; 34 35 36 37 -- 分配建立表的權限 38 39 Grant create table to test01; 40 41 42 43 -- 此時test01用戶能夠select,但不能insert數據 44 45 Grant unlimited tablespace to test01; 46 47 48 49 50 51 -- 修改用戶密碼 52 53 Alter user test01 identified by 1234; 54 55 56 57 -- 級聯刪除用戶 58 59 Drop user test01 cascade;
查看用戶權限
select * from user_sys_privs;
數據庫數據類型
number(x,y) |
數值型 |
最長是x位,y位小數 |
varchar2(maxlength) |
變長字符串 |
maxlength這個參數的上限是32767字節 |
char(max_length) |
定長字符串 |
最大2000字節 |
Date |
日期時間 |
只能精確到秒。 |
timestamp |
時間戳 |
精確到微秒 |
long |
長字符串 |
最大支持2GB |
其餘類型:
CLOB:最大長度4G -->大對象不多使用:若是存在大對象,通常的解決方案存入文件地址(地址爲程序所在應用服務器的相對路徑)。
BLOB:存二進制文件
注意:
在數據庫設計時,若是要存大文件(視頻,音頻等),必定不要用BLOB/CLOB,通用的解決方案都是文件的地址。
建立表的語法:
CREATE TABLE [schema.]table( column datatype [DEFAULT expr] , … );
建立一個學生表
T_STUINFO(sid,name,phone,gender,birthday,address)
Create table t_stuinfo( Sid number(4), Name varchar2(20), phone char(11), gender number(1), birthday date, address varchar2(100) )
開發工具生成
|
1 create table T_STUINFO 2 3 ( 4 5 sid NUMBER(4) not null, 6 7 name VARCHAR2(20) not null, 8 9 phone CHAR(11), 10 11 gender NUMBER(1) default 1 not null, 12 13 birthday DATE, 14 15 address VARCHAR2(100) 16 17 )
經過子查詢結果建立表
1 -- 經過其餘表結構建立表 2 3 Create table t_emp 4 5 as 6 7 select * from emp; 8 9 10 11 -- 只建立表的結構(複製表結構) 12 13 Create table t_emp2 14 15 as 16 17 select * from emp where1=2; 18 19 20 21 select * from t_emp; 22 23 select * from t_emp2;
1 -- 修改表操做 2 3 -- [1]給表添加字段 4 5 Alter table t_stuinfo add grade number(2) 6 7 8 9 -- [2]刪除表的字段 10 11 Alter table t_stuinfo drop column grade 12 13 14 15 -- [3] 修改表字段 16 17 Alter table t_stuinfo modify(address varchar2(150)) 18 19 20 21 -- [4]重命名 22 23 rename t_stuinfo to t_stuinfo2
語法
INSERT INTO table [(column [, column...])] VALUES (value [, value...]);
1 -- insert 2 3 Insert into t_emp2(empno,ename,job,mgr,hiredate,sal,comm,deptno) 4 5 values(1010,'cai10','singer',7938,sysdate,1000,1,10) 6 7 8 9 insert into t_emp2(empno,job,ename,mgr,hiredate,sal,comm,deptno) 10 11 values(1010,'singer','cai10',7938,sysdate,1000,1,10) 12 13 -- insert是事務操做,須要提交事務。 14 15 16 17 Insert into t_emp2 18 19 values(1020,'cai20','singer',7938,sysdate,2000,2,10)
update語法
UPDATE table SET column = value [, column = value] … [WHERE condition];
update t_emp2 set ename = 'cai22',sal = 2200 where empno = 1020
delete 語法
DELETE [FROM] table [WHERE condition];
-- delete Delete from t_emp2 where empno = 1010 --刪除表中的全部數據-沒有事務-速度快 Truncate table t_emp2;
序列是oracle專有的對象,它用來產生一個自動遞增的數列。
-- 建立序列 Create sequence seq_empno start with 1 increment by 1
1 -- 序列的使用 2 3 -- 序列中的下一個值,從定義(start with)的值開始 4 5 select seq_empno.nextval from dual; 6 7 -- 獲取序列的當前值 8 9 select seq_empno.currval from dual; 10 11 12 13 -- 序列的應用 14 15 select * from t_emp2; 16 17 insert into t_emp2 18 19 values(seq_empno.nextval,'cai10','singer',7938,sysdate,1000,1,10)
在數據庫開發設計表時,若是須要一個字段的值是自增的話,優先考慮序列。
事務(Transaction)是一個操做序列。這些操做要麼都作,要麼都不作,是一個不可分割的工做單位,是數據庫環境中的邏輯工做單位。
事務是爲了保證數據庫的完整性。
事務不能嵌套
在oracle中,沒有事務開始的語句。一個Transaction起始於一條DML(Insert、Update和Delete )語句,結束於如下的幾種狀況:
—用戶顯示執行Commit語句提交操做或Rollback語句回退。
—當執行DDL(Create、Alter、Drop)語句事務自動提交。
—用戶正常斷開鏈接時,Transaction自動提交。
—系統崩潰或斷電時事務自動回退。
1 -- beginTransaction(insert/update/delete) 2 3 Insert into t_emp2 4 5 values(6,'cai40','singer',7938,sysdate,4000,4,10); 6 7 insert into t_emp2 8 9 values(7,'cai50','singer',7938,sysdate,5000,5,10); 10 11 12 13 -- 【1】顯示的事務結束(endTransaction) 14 15 -- commit; 16 17 rollback;
-- beginTransaction(insert/update/delete) Insert into t_emp2 values(6,'cai40','singer',7938,sysdate,4000,4,10); insert into t_emp2 values(7,'cai50','singer',7938,sysdate,5000,5,10); --【2】隱式的事務結束 Create table abc( Sid number )
事務結合java代碼的格式
1 try{ 2 3 insert … 4 5 … 6 7 insert … 8 9 commit 10 11 }catch(Exception e){ 12 13 rollback 14 15 }finlly{ 16 17 關閉數據庫 18 19 }
1 -- beginTrans 2 3 Insert into t_emp2 values(9,'cai40','singer',7938,sysdate,4000,4,10); 4 5 Insert into t_emp2 values(10,'cai50','singer',7938,sysdate,5000,5,10); 6 7 select * from t_emp2; 8 9 10 11 save point sp1; 12 13 14 15 insert into t_emp2 values(11,'cai40','singer',7938,sysdate,4000,4,10); 16 17 insert into t_emp2 values(12,'cai50','singer',7938,sysdate,5000,5,10); 18 19 select * from t_emp2; 20 21 22 23 rollback to sp1; 24 25 26 27 commit;
save point 保持當前數據庫的狀態點。以便後續經過rollback回滾到指定狀態點。
1 try{ 2 3 insert … 4 5 insert … 6 7 save point sp1 8 9 10 11 insert … 12 13 insert … 14 15 save point sp2 16 17 18 19 commit 20 21 }catch(AException e){ 22 23 rollback 24 25 }catch(BException e){ 26 27 rollback to sp1 28 29 } 30 31 finlly{ 32 33 關閉數據庫 34 35 }
事務四大特徵:原子性,一致性,隔離性和持久性。
1. 原子性(Atomicity)
一個原子事務要麼完整執行,要麼乾脆不執行。這意味着,工做單元中的每項任務都必須正確執行。若是有任一任務執行失敗,則整個工做單元或事務就會被終止。即此前對 數據所做的任何修改都將被撤銷。若是全部任務都被成功執行,事務就會被提交,即 對數據所做的修改將會是永久性的。
2. 一致性(Consistency)
一致性表明了底層數據存儲的完整性。它必須由事務系統和應用開發人員共同來保證。事務系統經過保證事務的原子性,隔離性和持久性來知足這一要求; 應用開發人員則需 要保證數據庫有適當的約束(主鍵,引用完整性等),而且工做單元中所實現的業務邏輯不會致使數據的不一致(即,數據預期所表達的現實業務狀況不相一致)。例如,在一次轉 帳過程當中,從某一帳戶中扣除的金額必須與另外一帳戶中存入的金額相等。支付寶帳號100 你讀到餘額要取,有人向你轉100 可是事物沒提交(這時候你讀到的餘額應該是100, 而不是200)這種就是一致性
3. 隔離性(Isolation)
隔離性意味着事務必須在不干擾其餘進程或事務的前提下獨立執行。換言之,在事務或工做單元執行完畢以前,其所訪問的數據不能受系統其餘部分的影響。
4. 持久性(Durability)
持久性表示在某個事務的執行過程當中,對數據所做的全部改動都必須在事務成功結束前保存至某種物理存儲設備。這樣能夠保證,所做的修改在任何系統癱瘓時不至於丟 失。
當咱們建立表的時候,同時能夠指定所插入數據的一些規則,好比說某個字段不能爲空值,某個字段的值(好比年齡)不能小於零等等,這些規則稱爲約束。約束是在表上強制執行的數據校驗規則.
常見約束:
主鍵用於惟一標識一條記錄。主鍵值不可爲空,也不容許出現重複。
1 -- 建立表 2 3 -- 建立列級約束-顯式指定名稱,pk_sid 4 5 Create table t_stuInfo( 6 7 Sid number(4) constraint pk_sid primary key, 8 9 namevarchar2(20) 10 11 ) 12 13 14 15 -- 建立列級約束-沒式顯示指定名稱,系統隨機命名SYS_C.. 16 17 Create table t_stuInfo2( 18 19 Sid number(4) primary key, 20 21 namevarchar2(20) 22 23 )
![]()
|
表級約束:當多個列(字段)參與約束,能夠用表級約束。
1 -- 建立表,以表級約束 2 3 Create table t_stuInfo3( 4 5 Sid number(4), 6 7 phone char(11), 8 9 namevarchar2(20), 10 11 constraint pk_stuinfo primary key(phone,name) 12 13 ) 14 15 16 17 Create table t_stuInfo4( 18 19 Sid number(4), 20 21 phone char(11), 22 23 namevarchar2(20), 24 25 primary key(phone,name) 26 27 )
確保字段值不容許爲空
只能在列級定義
1 -- 建立列級約束-顯式指定名稱,pk_sid 2 3 Create table t_stuInfo5( 4 5 Sid number(4) primary key, 6 7 phone char(11) constraint nn_phone not null 8 9 ) 10 11 12 13 Create table t_stuInfo5( 14 15 Sid number(4) primary key, 16 17 phone char(11) not null 18 19 ) 20 21 22 23 -- 添加操做 24 25 Insert into t_stuinfo5(sid) 26 27 values(1000)
惟一性約束條件確保所在的字段或者字段組合不出現重複值
惟一性約束條件的字段容許出現空值
Oracle將爲惟一性約束條件建立對應的惟一性索引
1 Create table t_stuInfo6( 2 3 Sid number(4) primary key, 4 5 phone char(11) constraint uq_phone unique 6 7 ) 8 9 10 11 Create table t_stuInfo6( 12 13 Sid number(4) primary key, 14 15 phone char(11) unique 16 17 ) 18 19 20 21 createtable t_stuInfo6( 22 23 sid number(4) primary key, 24 25 phone char(11) unique not null 26 27 ) 28 29 30 31 Drop table t_stuInfo6; 32 33 create table t_stuInfo6( 34 35 sid number(4) primary key, 36 37 phone char(11), 38 39 constraint uq_phone unique(phone) 40 41 ) 42 43 44 45 Insert into t_stuinfo6(sid,phone) 46 47 values(1000,'18612340000') 48 49 insert into t_stuinfo6(sid,phone) 50 51 values(1001,'18612340000')
Check約束用於對一個屬性的值加以限制
1 Create table t_stuInfo7( 2 3 Sid number(4) primary key, 4 5 phone char(11) unique, 6 7 age number(3) check(age>0and age<100) 8 9 ) 10 11 12 13 Insert into t_stuInfo7 14 15 values(1000,'18612341234',-10)
1 create table emp3 2 3 ( id number(4) primary key, 4 5 age number(2) check(age > 0 and age < 100), 6 7 salary number(7,2), 8 9 sex char(1), 10 11 constraint salary_check check(salary > 0) 12 13 )
1 Create table t_stuInfo8( 2 3 Sid number(4) primary key, 4 5 phone char(11) unique, 6 7 tid number(4), 8 9 constraint fk_tid foreign key(tid) references t_teacher1(tid) 10 11 ) 12 13 14 15 Create table t_teacher1( 16 17 tid number(4) primary key, 18 19 name varchar2(20) not null 20 21 ) 22 23 24 25 Insert into t_teacher1 26 27 values(1,'alex'); 28 29 30 31 insert into t_stuInfo8 32 33 values(1000,'18612341234',1)
注:對於主表的刪除和修改主鍵值的操做,會對依賴關係產生影響,以刪除爲例:當要刪除主表的某個記錄(即刪除一個主鍵值,那麼對依賴的影響可採起下列3種作法:
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
[ON DELETE [CASCADE|SET NULL]] 如省略on短語,缺省爲第一中處理方式。
1 droptable t_stuinfo8; 2 3 create table t_stuInfo8( 4 5 sid number(4) primary key, 6 7 phone char(11) unique, 8 9 tid number(4), 10 11 constraint fk_tid foreign key(tid) references t_teacher1(tid) 12 13 ) 14 15 16 17 Create table t_stuInfo8( 18 19 Sid number(4) primary key, 20 21 phone char(11) unique, 22 23 tid number(4), 24 25 constraint fk_tid foreign key(tid) references t_teacher1(tid) on DELETE CASCADE 26 27 ) 28 29 30 31 Create table t_teacher1( 32 33 tid number(4) primary key, 34 35 namevarchar2(20) not null 36 37 ) 38 39 40 41 Insert into DELETE 42 43 values(1,'alex'); 44 45 46 47 insert into t_stuInfo8 48 49 values(1000,'18612341234',1) 50 51 52 53 select * from t_stuInfo8 54 55 delete from t_teacher1 where tid = 1;