oracle学习笔记(四) DML数据控制语言和TCL 事务控制语言

DML 数据管理语言 Data manage language

insert, update, delete以及select语句,不过,有人也把select单独出来,作为DQL 数据查询语言 data query language

insert,updatedelete都需要执行commit才能将数据真正写入到数据库中

select 查询

select语法:
	select 字段列表:empno,ename,job....
		from 表名1 [,table2] 
		where [条件:列名=值]       
		[group by 列名,列名2..]      分组统计查询
		[order by 列名1 asc/desc] [,列名2 asc/desc]: 
/*
where子名: 限定(筛选)查询结果,多个条件可使用and | or 连接多个查询条件 
order by子名:对结果排序;asc升序,desc降序,默认升序
distinct: 消除重复列
*/

补充:

--下面两个语句等同
select s.* from student s;
select * from student s;
--查询的结果多出一列address
select s.*,address from student s 
--需要知道某公司的员工的所有工作有哪些,就是使用distinct清除掉重复的
select distinct job from employee;
--取个别名 my job 别名如果有空格的就得用双引号,否则语法会有错误
select distinct job as "my job" from employee;
select distinct job as my_job from employee;
--求总记录数,使用count函数
select count(*) from employee;

高级查询之后补充联合查询..

insert 插入

insert语法1:
    INSERT INTO <table_name>[(column...list)] 
       VALUES(value...list) 
	   
insert语法2:
     INSERT INTO <table_name> [(cloumn_list)] 
        SELECT column_names FROM <other_table_name>;
--插入一套数据,该数据只有列名1和列名2有值,得对应
insert into $tablename$(列名1,列名2) values(列名1 值1,列名2 值2)
--默认不写,得写完,得对应
insert into $tablename$ values(列名1 值1,列名2 值2...)

insert into $tablename$.emp (几个列名) values(与列名保持一致) --字符串是单引号
commit; --不提交的话,放在数据缓存区中,只能在当前会话可以查询得到,其他会话Sention是查询不到的
roolback; --回滚

--把查询的结果插入到表中,列的顺序一一对应
insert into tablename
	select * from table2name;

update 修改(更新)

语法:
     UPDATE <table_name> SET  <字段名1>=值1, <字段名2>=值2,…….
        WHERE  <条件1> AND[OR]  <条件2>.......
update student set age=10 
	where num = 10; --如果可以用主键作条件那最好就是用主键去更新,and or 多重条件
	
update student set age = age+1
	where num = 10;

delete 删除

语法:
     DELETE [FROM] <table_name>  WHERE  <条件>
delete student where num = 10;

事务控制语言:TCL (commit, rollback, savepoint)

事务控制语言:TCL: Transaction Controll Language

  1. 事务是最小的工作单元,它作为一个整体进行操作, 此工作单元中的语句要么全部成功,要么全部失败不充许部分成功和部分失败
  2. 保证事务的整体成功或失败,称为事务控制
  3. 事务的四大特性ACID :
    原子性(Atomicity )、一致性(Consistency )、隔离性(Isolation)、持久性(Durability )
  4. 用于事务控制的语句有:
    • COMMIT - 提交并结束事务处理
    • ROLLBACK - 撤销事务中已完成的工作
    • SAVEPOINT – 标记事务中可以回滚的点
update student where num=10;
savepoint s1;
insert into student where num=11;
rollback s1; --回退到了s1,也就是之后的插入数据操作撤销了
commit;
posted @ 2019-04-20 15:52  Stars-one  阅读(794)  评论(0编辑  收藏  举报