视图(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;