oracle数据库SQL语句详解

--使用sys账号创建用户han并为授予不同的角色,并且可以授权给其他用户--
create user han identified by han
default tablespace users
temporary tablespace temp
profile default;
grant connect,resource,dba to han;

--创建用户,指定表空间和临时表空间
create user hl identified by hl
default tablespace users
temporary tablespace temp;

--为用户授予角色(角色其实就是各种系统权限的一个集合,方便授权)
--CONNECT角色:允许用户连接数据库
--RESOURCE角色:允许用户创建相关的数据库对象,如表、序列等
--dba 角色:拥有所有权限。
grant connect, dba, resource to hl
  with admin option;
---查看数据库系统的角色的种类
select * from session_roles;
-- 查看当前用户所拥有的角色--
select * from user_role_privs;
--查看数据库所有权限种类
select * from system_privilege_map;

---可以使用ALTER USER语句修改用户密码--
alter user hl identified by hl;
--在sqlPlus中可以通过password命令修改密码


--在Oracle数据库中,可以通过DBA_USERS查询出包含与用户和用户特征相关的信息--
SELECT username,user_id,password,default_tablespace FROM dba_users;



--------------------------用户权限管理 (分为系统权限和对象权限)----------------------------------------
/*
    常用系统权限--
    CREATE SESSION  连接到数据库
    CREATE SEQUENCE 创建序列,用来生成一系列的数值
    CREATE SYNONYM  创建同义词
    CREATE TABLE    在用户架构中创建表
    CREATE ANY TABLE    在任意架构中创建表
    DROP TABLE  从用户架构中删除表
    DROP ANY TABLE  在任意构架中删除表
    CREATE PROCEDURE    创建存储过程
    EXECUTE ANY PROCEDURE   在任意架构中执行存储过程
    CREATE USER 创建用户
    DROP USER   删除用户
    CREATE VIEW 创建视图
*/
--查看当前用户拥有系统权限情况--
select * from user_sys_privs order by privilege;

--受于系统权限 with admin option 表示可以授权给其他用户--
grant create table to hl with admin option;

grant execute any procedure to hl  with admin option;

--撤销权限,虽然撤销了当前用户的对应权限,但是其之前授予给其他用户的权限还在--
revoke connect from hl;
revoke dba from hl;
revoke resource hl;

--授予用户对象权限(包括insert,update,select,delete,execute),-with grant option 表示可以授权给其他用户-
grant insert, select on userInfo to hl with grant option;

grant update(userName) on userInfo to hl
  with grant option;

-------------------------表的相关信息查询----------------------------

--查询USER_TABLES视图获取表的相关信息--
SELECT table_name, tablespace_name, temporary FROM user_tables;

--可以通过查看user_tab_columns视图,查看表的列相关信息--
SELECT table_name,
       column_name,
       data_type,
       data_length,
       data_precision,
       data_scale
  FROM user_tab_columns;

-------------------------DDL(定义,修改,删除表)中的相关信息----------------------------
--创建表并添加约束----
create table userinfo2(
       id number(10) not null,
       username varchar2(10) not null,
       userAge number(3) ,
       userSex char(2)
);


--给相应的列添加约束条件---
--主键约束---
alter table userinfo2 add constraint user2_primary primary key (id); 
--自定义约束--
alter table userinfo2 add constraint user2_ageChk check (userAge>0 and userAge<100);
 --唯一约束---
alter table userinfo2 add constraint user2_nameUnique unique(username);

drop table userinfo2;

--使用RENAME语句对表进行重命名---
rename userInfo to userInfo1;
rename userInfo1 to userInfo;

--给表或列添加备注,可以帮助用户理解表或列的作用---
comment on table userInfo is '用户信息表(保存用户的相关信息)';
comment on column userInfo.id is '用户ID,用来唯一表示一个用户';

--以从user_tab_comments视图中查看表的备注信息--
--这里限制条件的字符串必须是大写字母,因为Oracle数据库列名、表名就是以大写形式存在的---
select * from user_tab_comments where table_name = 'USERINFO';
select * from user_col_comments where column_name = 'ID';

--根据已有数据表创建新的数据表 相当于表的克隆(可以克隆全部数据也可以是部分数据)--
create table userInfo4 as
  select userName, ID from userINfo;
--删除数据表--
drop table userInfo4;

--修改表的结构---
alter table userInfo add info varchar2(200);
alter table userInfo add CominTime date default sysdate not null;

--修改列名与列的数据类型及大小(在修改列的数据类型时必须将其值设置为空)
alter table userINfo rename column userid to id;
alter table userINfo rename column comintime to comeintime;
alter table userInfo modify username varchar2(10);
alter table userInfo modify info number(10);

--删除表中列---
alter table userInfo drop column info;
select * from userinfo order by id;

--在sqlplus中有效,查询视图为无效SQL---
describe table;
-------------------------DML(增删改insert,delete,update)----------------------------

--插入数据 TO_Date()方法可是实现日期的格式化插入
insert into userinfo
  ( username, usersex, comeintime)
values
  ( '李玖哲', '男', TO_DATE('2012-12-12', 'yyyy-mm-dd'));
--如果为表所有列都指定值,那么可以忽略列清单
insert into userinfo
values
  (null, '赵传', '男', '22', TO_DATE('2012-12-12', 'yyyy-mm-dd'), null,null,null);

--插入的字符串中可以包含引号
insert into userInfo
values
  (null,
   '胡彦斌2',
   '男',
   '22',
   TO_DATE('2012-12-12', 'yyyy-mm-dd'),
   '出鞘剑杀气荡',12394.02,300001);

delete from userInfo where username = '张信哲';

update userInfo set username = '张惠妹' where id = '7';

--清空表中的内容,但不删除表---
TRUNCATE TABLE userinfo3;
select * from userinfo3;
-- delete 和 truncate 两者都可以清空表中的数据,但存在区别
delete userinfo3;    --delete 是DML命令,删除的数据可以通过日志文件恢复
truncate table  userinfo3;     --truncate 是DDL命令,删除的数据不能够恢复,要慎用


-------------------------------各种运算符的使用-----------------------
--算术运算符(+-*/)
alter table userInfo add salary number(10, 2);
update userINfo set salary = 10000;
---用distinct关键字去除重复,distinct紧跟在select的后面
select distinct u.usersex from userinfo u; 

select (salary + 100) * 2 - 100 as "工资"  from userInfo where username = '张信哲';
--连接运算符,创建一个合并列
select username || salary as "姓名+工资" from userInfo;
select concat(username, salary) as "姓名+工资" from userINfo;

--Where子句中的and\or\in\not in\between and \like关键字的使用
select * from userInfo where username like '张%';
select *from userinfo u where u.username like '张%' and u.userage > 25;
select * from userinfo u where u.usersex in ('男');
select * from userinfo u where u.usersex not in ('女', '男');

--默认日期格式为DD-MON-yyyy Oracle数据库以内部数字格式存储日期,包括世纪、年、月、日、小时、分钟和秒
select *  from userinfo u where u.comeintime 
between to_date('2015-1-1', 'yyyy-mm-dd') and to_date('2016-1-1', 'yyyy-mm-dd');

--dual表常用在没有查询目标的SELECT语句块中,它只包含一行数据
--systimestamp 详细的当前系统时间 包括时区并精确到秒后6SELECT sysdate,systimestamp FROM dual;
SELECT TO_DATE('12-1月-12') - 3 FROM dual;

--IS NULL:匹配空值   IS NAN:匹配非数字值 (前面加上not 与之相反)
select * from userinfo u where u.userage is not nan;
select * from userinfo u where u.usersex  is not null;
select * from userinfo u where u.usersex  is  null;

--LIKE运算符 中的 '_'匹配一个任意字符 ; '%' 匹配(0-n)个任意字符
select u.* , u.rowid from userinfo u ;
select * from userinfo u where u.username like '__';
select u.* from userinfo u where u.username like '赵%';
select u.* from userinfo u where u.info like '%是%'

--如果要查询实际的下划线或百分号就需要使用ESCAPE选项区分通配符
--(ESCAPE选项告知数据库如何区分通配符和要匹配的字符--可以是任意字符)
select u.* from userinfo u where u.info like '%a%%' escape 'a';
select u.* from userinfo u where u.info like '%\_%' escape '\';

select * from userinfo u where u.id in (select t.userid from userInfo3 t);
-- order by 查询结果排序 desc升序 asc降序
select * from userinfo u order by u.id desc , u.comeintime asc for update;
--处理NULL值 NULL值被查询出来的时候没有显示信息,如何告知用户这是空字符串还是NULL,这可以通过NVL()函数来进行处理
select nvl(u.userage,-1) from userinfo u;
select nvl(u.info,'---此人什么都没有留下---') from userinfo u ;

----------------------------------函数的使用------------------------------------------------
/*单行函数只处理单个行,并且为每行返回一个结果
       1、字符函数:处理字符串
       2、数字函数:用于数学计算
       3、类型转换函数:数据类型转换
       4、日期函数:处理日期和时间  
       5、正则表达式函数:使用正则表达式搜索数据
*/

--INITCAP(x):将字母字符串转换为每个词首字母为大写,其他字母为小写
select u.username,initcap(u.username) from userinfo u for update;

--INSTR(x,  find_string  [,  start]   [,  occurrence]) 
--返回指定字符串find_string在x中数字位置。可以指定开始搜索的位置start,并提供该字符串出现的次数occurrence。
--start和occurrence默认为1,表示从字符串开始的位置开始搜索,并返回第一次出现的位置
select u.info,instr(u.info,'是',1,2) from userinfo u;--表示在u.info中从头查找第2个'是'出现的位置
select u.info,instr(u.info,'是',1) from userinfo u;

--LENGTH(x):返回表达式中的字符数
select u.info,length(u.info) from userinfo u;

--统计数字的位数时:如果小数点后不全为零就统计到最后一个不为零的位数,否则就只统计整数部分
select u.salary ,length(u.salary) from userinfo u;
select u.* ,u.rowid from userinfo u;
select u.comeintime ,length(u.comeintime) from userinfo u;--为什么日期类型的长度都是9位?

--LOWER(column|expression):将字母字符值转换为小写
-- UPPER(column|expression):将字母字符值转换为大写
select u.username,lower(u.username) as 小写, upper(u.username) as 大写 from userinfo u;

--LPAD(x,  width  [,  pad_string]):在字符串左侧填充pad_string字符,以使总字符宽度为width
--RPAD(x,  width  [,  pad_string]):在字符串右侧填充pad_string字符,以使总字符宽度为width
select u.username , lpad(u.username,6,'*') as 左填充 ,rpad(u.username,6,'*') as 由填充 from userinfo u;

/*LTRIM(x [,  trim_string]):
         从x字符串左侧去除所有的trim_string字符串,如果没有指定trim_string字符串,则默认为去除左侧空白字符
RTRIM(x [,  trim_string]):
        从x字符串右侧去除所有的trim_string字符串,如果没有指定trim_string字符串,则默认为去除右侧空白字符
--TRIM(trim_string FROM x):
        从x字符串两侧去除trim_string字符串
*/
select u.info ,length(u.info) ,ltrim(u.info,' ')as 左边,length(ltrim(u.info,' ')),
 rtrim(u.info,' ') as 右边,length(rtrim(u.info,' ')), 
 trim(' ' from u.info) as 左右两边 ,length(trim(' ' from u.info)) from userinfo u;
--上边例子为:去掉两边的空格

--NVL(x,  value):用于将一个NULL值转换为另外一个值。如果x是NULL值的话返回value值,否则返回x值本身
select u.usersex , nvl(u.usersex,'没填') from userinfo u;
--NVL2(x,  value1,  value2):如果x不为NULL值,返回value1,否则返回value2
select u.usersex , nvl2(u.usersex,'填了','没填') from userinfo u;
--REPLACE(x,  search_string,  replace_string):从字符串x中搜索search_string字符串,并使用replace_string字符串替换。
--并不会修改数据库中原始值。
select u.info ,replace(u.info,' ','*') from userinfo u ;--将字符串中的"空格"替换成*号

--神奇的读音匹配
SELECT * FROM userinfo u WHERE SOUNDEX(u.username) = SOUNDEX('han');

SELECT * FROM userinfo u WHERE   SOUNDEX(u.info) = SOUNDEX('是');--不能识别汉语读音

--SUBSTR(x,  start  [,  length]):返回字符串中的指定的字符,这些字符从字符串的第start个位置开始,长度为length个字符;
--如果start是负数,则从x字符串的末尾开始算起;如果length省略,则将返回一直到字符串末尾的所有字
select u.info, substr(u.info,0,10) from userinfo u;


------------------------------------数字函数------------------------------------
--ABS(value)    返回value的绝对值 
SELECT ABS(10),ABS(-10) FROM dual;

--CEIL(value)   返回大于或等于value的最小整数(向上取整)
SELECT CEIL(5.8),CEIL(-5.2) FROM dual;
--返回:6 和 -5--

--FLOOR(value)  返回小于或等于value的最大整数(向下取整)
SELECT FLOOR(5.8),FLOOR(-5.2) FROM dual;
--返回:5 和 -6

--POWER(value,n)    返回value的n次幂
SELECT POWER(2,1),POWER(2,3) FROM dual;
--返回:28

--MOD(m,n)  返回m和n取余数的结果
SELECT MOD(8,3),MOD(8,4)FROM dual;
--返回:20

--SQRT(value)   对value进行开方
SELECT SQRT(25),SQRT(5) FROM dual;
--返回:52.23606798

--TRUNC(value,n)    对value进行截断。如果n>0,保留n位小数;n<0,则保留-n位整数位;n=0,则去掉小数部分
SELECT TRUNC(5.75),TRUNC(5.75,1), TRUNC(115.75,-2) FROM dual;
--返回:55.7100

--ROUND(value[,n])  对value进行四舍五入,精确到小数点右侧的n位。如果n省略的话,相当于n=0的情况。
    SELECT ROUND(5.75),ROUND(5.75,1),ROUND(5.75,-1) FROM dual;
--返回:65.810

------------------------------------------------类型转换函数------------------------------------
--TO_CHAR(x [,  format]):将x转化为字符串。 format为转换的格式,可以为数字格式或日期格式
---9    数字位置(9的个数确定了显示的宽度); 0   显示前导0;    EEEE  科学计数法(格式必须指定4个E)
--$    浮动的美元符号;  L 浮动的当地货币符号
select u.salary ,to_char(u.salary,'$999,999.99') from userinfo u;
select u.salary ,to_char(u.salary,'L999,999.99') from userinfo u;
select u.salary ,to_char(u.salary,'999.99EEEE') from userinfo u;
select u.salary ,to_char(u.salary,'099999.99') from userinfo u;
--CAST(x  AS  type)将x转换为指定的兼容的数据库类型。
SELECT
 CAST(12345.67 AS VARCHAR2(10)),
 CAST('05-7月-07' AS DATE),
 CAST(12345.678 AS NUMBER(10,2))
FROM dual;

--TO_DATE(x [,format]):将x字符串转换为日期
--年 YYYY:4位数字的年,如:2008;    YY:2位数字的年,如:08
--月 MM:两位数字的月份,如:09 ;   MONTH:月份的全称的大写形式  MON:3位的月
--天 DD:月份中日的2位表示形式 ; DAY:大写的星期几; Day:表示小写的星期几 
--小时 HH24:24小时进制     HH:12小时进制
--分钟 :MI:2位的分钟数 
--秒 :SS:2位的秒数
select TO_DATE('2012-3-15 22:12:12','YYYY-MM-DD hh24:mi:ss') FROM dual;
select TO_DATE('2012-3月-12 22:12:12','YYYY-mon-dd hh24:mi:ss') FROM dual;

---------------------------------聚集函数 : 亦称分组函数、聚合函数-----------------------------------------------

--可以在聚集函数中使用DISTINCT关键字,排除重复值

--MAX(x)、MIN(x) 分别统计x的最大值和最小值
--COUNT(x)统计查询返回的行数 
--AVG(x):获取x的平均值

select count(u.username) as 总人数,sum(u.salary)as 总工资, 
avg(u.salary + 100)as 平均工资 , min(u.salary)as 最小值 , max(u.salary) 最大值 from userinfo u;

select u.*,u.rowid from userinfo u;

--如果是用来统计字符串,那么将会先对字母进行排序(a-z)然后给汉字按照读音的字母顺序(a-z)排序,--
--如果是日期的话,最大值会是最近的时间,最小值会是最早的时间--
select  min(u.username),max(u.username), min(u.comeintime),max(u.comeintime) from userinfo u;

--可以使用GROUP BY对多个列进行分组(不能使用聚集函数作为WHERE子句的筛选条件)--
select u.usersex,to_char(avg(u.salary),'999,999.99')as 平均工资,count(u.salary) as 人数 
from userinfo u group by u.usersex order by 人数 desc;

--可以使用HAVING子句过滤分组后的行语法:SELECT ...FROMWHERE   GROUP BY ... HAVING ... ORDER BY ...;---
--GROUP BY使用时可以不使用HAVING,但是使用HAVING时必须有GROUP BY--
select u.usersex,to_char(avg(u.salary),'999,999.99')as 平均工资,count(u.salary) as 人数 
from userinfo u group by u.usersex having avg(u.salary) > 20000 order by 人数 desc;

-------------------------------------------集合运算-----------------------
--集合运算包括:intersect(交集):返回两个查询的共有记录----
select * from userinfo where usersex is not null 
intersect 
select * from userInfo u where u.usersex = '男';
--union all(并集):返回各个查询的所有记录,包括重复记录---
select * from userinfo where usersex is not null 
union all 
select * from userInfo u where u.usersex = '男';

--union (并集):返回各个查询的所有记录,不包括重复记录 并进行排序处理--
select * from (select * from userinfo where usersex is null 
union  
select * from userInfo u where u.usersex = '男') t order by t.userage;

--minus (补集): 返回第一个查询检索的记录减去第二个查询检索出的记录剩余的记录--
select * from userInfo 
minus 
select * from userInfo u where u.usersex = '男';



---------------------------伪列使用(伪列在表中不储存,不可以进行增删改操作,但是可以查询)-------------------------------------
---rowid:代表每一行在文件中储存的物理地址,在插入一行数据的时候自动生成。
---rownum:代表查询结果集中的行号(即:序号),再查询的自动生成。

--有下面sql语句的结果可以看出:where rownum 
select * from (select rownum as myrownum ,s.* 
from (select u.* from userinfo u order by u.userage) s) 
where myrownum <7 and myrownum >3;


-----------------------------序列的使用----------------------
---生成序列号-----

create sequence MYSEQUENCE
minvalue 1
maxvalue 999999999999999999999999999
start with 30
increment by 1
cache 20
cycle;

---------------------------使用序列号,实现ID的自动增长------------------
--创建部门表----
create table department(
       dept_ID number(10) not null,
       dept_name varchar2(20) not null,
       dept_num number(10) not null,
       dept_Manager varchar2(10) not null
);
--设置主键约束-----
alter table department add constraint dept_primaryKey primary key(dept_ID);
--设置外键约束-----------
alter table department add constraint dept_foreignKey foreign key(dept_Manager);
alter table department add constraint dept_unique     unique (dept_num);
references  userinfo(username) on delete set null;



insert into department  values(MYSEQUENCE.NEXTVAL,'事业部',200001,'啊李丽');
insert into department  values(MYSEQUENCE.NEXTVAL,'技术部',100001,'韩磊');
insert into department  values(MYSEQUENCE.NEXTVAL,'人力资源部',300001,'韩磊');
insert into department  values(MYSEQUENCE.nextval,'市场部',400001,'张信哲');

select * from department;
select * from userinfo for update;
---为用户信息表添加部门编号列
alter table userinfo add  dept_num number(10) ;
---设置部门编号列的外外键约束
alter table userinfo add constraint user_deptforeignKey foreign key (dept_num) 
references department(dept_num) on delete set null;

----------------------------------------------触发器(实现ID自动增长)------------------------------------
--用触发器可以不必再插入时利用mysequence.nextval设置id的值,不设置或直接用null,然后由触发器自动设置。
create or replace trigger autoIncrement_UserId
before insert on userinfo for each row
begin 
select useridsequence.nextval into :new.id  from dual;
end;

/*参数说明:
(before:指定操作前出发。after:在指定操作后触发。instead of :指定创建替代触发器。指定事件不操作,而执行触发器本身的操作)
(触发事件:insert、delete、update)
(on 后面指定:触发器所在的表的名字--tablename)
(for each row:表示该触发器为行级触发器,如果DML语句影响多行时,触发器将针对每一行执行一次。若不指定表示为语句级触发器,
无论影响几行,触发器都只执行一次)
*/

--禁用和启用触发器
alter trigger autoIncrement_UserId disable;
alter trigger autoIncrement_UserId enable;
--禁用或启用一个表中的所有触发器
alter table userinfo disable all triggers;
alter table userinfo enable all triggers;
--删除触发器
drop trigger autoIncrement_UserId;


------------------------------------数据完整性--------------------------------------
/*数据完整性可分为:              
           域完整性(又称:列完整性。列的取值约束如:check约束、not null、数据类型约束、取值范围、长度约束等)
           实体完整性(又称:行完整性。约束如:primary 主键约束、 unique 唯一性约束)
           参照完整性(又称:引用完整性。)约束如:外键约束
  完整性约束就是通过限制列数据、行数据和表之间数据来保证数据完整性的有效方法。
  sql语句实现参考上面创建表过程中用到的约束条件
*/





-----------------------连接查询-------------------------

---内连接查询(inner join ... on) inner 可以省略,系统默认为inner连接查询
--对查询结果可以使用where语句过滤--
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u 
inner join department d on u.dept_num = d.dept_num where u.usersex ='男';


--完全外连接查询(full outer join ... on
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u 
full outer join department d on u.dept_num = d.dept_num;


--左外连接查询(left outer join ... on)结果表中除了包括满足条件的行外,好包括左表的所有行
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u 
left outer join department d on u.dept_num = d.dept_num;
--oracle 提供的方式 结果集等同与左外链接查询
--(+):Oracle 专用的联接符,在条件中出现在左边指右外联接,出现在右边指左外联接。
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u ,department d 
where u.dept_num = d.dept_num(+);


--右外连接查询(right outer join ... on)结果表中除了包括满足条件的行外,好包括右表的所有行
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u 
right outer join department d on u.dept_num = d.dept_num;
--相当于右外链接---
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u ,department d 
where u.dept_num(+) = d.dept_num;


--交叉连接查询(corss)----
---交叉连接实际上就是将两个表进行笛卡尔积运算,结果表的行数就是两个表行数之积;---
select u.username,u.usersex,d.dept_num,d.dept_name from userinfo u cross join department d ;


-----------------------视图操作------------------------

--通过建立视图可以屏蔽基表中的一些信息

---新建视图,修改视图都是下面的语句
---or replace: 如果视图已存在,替换旧视图
---with read only :代表该视图为只读视图,不能通过视图对基表进行增删改操作
--默认是可以通过视图对基表进行删改等操作的

create or replace  view userDept as
select u.id, u.username,d.dept_num,d.dept_name,d.dept_manager from userinfo u, department d 
where u.dept_num = d.dept_num
with read only;
---删除视图--
drop view userDept;

----可以直接利用视图进行查询-----
select * from userDept ;


-----------------------------------------索引----------------------------------------------

/*使用索引的优点:提高查询效率(Oracle 数据库会为表的主键和包含唯一约束的列自动创建索引。)
  索引提高查询效率的原理:在某列上创建索引后, 如果该列出现在查询条件中,
                         Oracle 会自动的引用该索引,先从索引表中查询出符合条件记录的 ROWID,由于 ROWID 是
                        记录的物理地址,因此可以根据 ROWID 快速的定位到具体的记录,表中的数据非常多时
                        引用索引带来的查询效率非常可观.
  使用索引的影响: 索引可以提高查询的效率,但是在数据增删改时需要更新索引,因此索引对增删改时会有负面影响。
*/
-- 创建索引(unique 唯一索引,bitmap 图索引)--
--还有sort/nosort:表示是否进行排序;compaess/nocompaess:对于复合索引是否进行压缩处理--
--reverse:指定以反序索引块的字节(不能与nosort同时出现)。
create unique index dept_nameIndex on department(dept_name) sort;

--修改索引名称
alter index dept_nameIndex rename to dept_nameIndex2;
--删除索引
drop index dept_nameIndex;


----------------------------------------事务处理------------------------------------------------------------------------

/*每个事务都遵循ACID原则————即:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)

原子性:原子性意味着每个事务都必须被认为是一个不可分割的单元。只有事务中的每个任务全部执行成功,才能认为该事务是成功的。
        否则代表事务失败,数据库中的数据将返回到事务中所有语句(即:任务)执行前的状态,以保证数据的整体性没有受到影响。
       (简单来讲就是:对于其数据修改,要么全都执行,要么全都不执行)从而保证数据的安全性。

一致性:事务在完成时,必须使所有的数据都保持一致状态。在相关数据库中,所有规则都必须应用于事务的修改,以保持所有数据的完整性。
        事务结束时,所有的内部数据结构(如 B 树索引或双向链表)都必须是正确的。某些维护一致性的责任由应用程序开发人员承担,
        他们必须确保应用程序已强制所有已知的完整性约束。例如,当开发用于转帐的应用程序时,应避免在转帐过程中任意移动小数点。 

隔离性:由并发事务所作的修改必须与任何其它并发事务所作的修改隔离。事务查看数据时数据所处的状态,要么是另一并发事务修改它之
        前的状态,要么是另一事务修改它之后的状态,事务不会查看中间状态的数据。这称为可串行性,因为它能够重新装载起始数据,
        并且重播一系列事务,以使数据结束时的状态与原始事务执行的状态相同。当事务可序列化时将获得最高的隔离级别。在此级别上,
        从一组可并行执行的事务获得的结果与通过连续运行每个事务所获得的结果相同。由于高度隔离会限制可并行执行的事务数,所以
        一些应用程序降低隔离级别以换取更大的吞吐量。  

持久性:事务完成之后,它对于系统的影响是永久性的。该修改即使出现致命的系统故障也将一直保持。 

注:在oracle中的事务是隐式开始的,不需要用户用显式语句来开始一个事务。当执行:DML语句是会自动开启事务。

*/
--自治事务的使用(即:事务中的事务,它独立于其父事务的提交或回滚)

declare 
        pragma autonomous_transaction;
begin
        insert into userinfo values
        (null,
         '王菲3',
         '女',
         '25',
         TO_DATE('2012-12-12', 'yyyy-mm-dd'),
         '匆匆那年',12394.02,300001);       
commit;
end;

数据库文件及相关资料下载地址http://pan.baidu.com/s/1eQ5391w

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值