10. DML in Oracle

10. DML in Oracle

DML: Database Operation Language

Add, delete, modify

In actual projects, the most used is the read operation, but inserting data and deleting data are equally important, while modification operations are relatively rare;

Insert operation

​ Insertion of tuple values

​ Insertion of query results

Way of insertion
-- insert into tablename values(val1,val2,...) 如果表名之后没有列,只能将所有的列都插入
-- insert into tablename values(val1,val2,...) 可以指定向哪些列中插入数据

insert into emp values(2222,'haha','clerk',7902,to_date('2019-11-2','YYYY-MM-dd'),1000,500,10);

select * from emp;
-- 向部分列插入数据的时候,不是向那个列插就插入的,要遵循创建表的时候定义的规范
insert into emp(empno,ename) values(3333,'wangwu');

-- 创建表的其他方式
-- 复制表同时复制表数据,不会复制约束
create table emp2 as select * from emp;
-- 复制表结构但是不复制表数据,不会复制约束
create table emp3 as select * from emp where 1 = 2;

-- 如果有一个集合的数据,把集合中的所有数据都挨条插入的话,效率如何?一般在实际的操作中,很少一条条插入,更多的是批量插入

-- 查询结果的插入
insert into tablename (select * from emp where ename like 'nba');

Delete operation

​ delete from tablename where condition;

-- 删除满足条件的数据
delete from emp2 where deptno = 10; 

​ delete from tablename; delete the data of the entire table

​ truncate: Unlike delete, delete goes through transactions when deleting, while truncate does not go through transactions. Once deleted, it is permanently deleted and does not have a rollback operation;

​ truncate table tablename;

Update operation

​ update tablename set c1= v1,c2=v2…where codition; The lesson can update one column or multiple columns;

Foresight

​ Addition, deletion and modification are common operations of the database. When performing operations, transaction guarantees are required. That is to say, the commit operation needs to be completed every time the SQL statement is executed in pl/sql. The transaction becomes very critical:

​ The main purpose is for data consistency,

​ If the same piece of data can only be accessed by one person at the same time, there will be no problem of data access disorder; but now the project is more concurrent access, and concurrent access also brings data insecurity; also It is a problem of inconsistency;

​ If you want to ensure data security, the most important way is to lock, mvcc;

​ The extension of the transaction:

​ The most basic database transaction

​ Declarative transaction

​ Distributed transaction

​ In order to improve efficiency, it is possible that multiple operations will be executed in the same transaction, then it may be partially successful and partially failed. Based on this situation, transaction control is required

​ select * from where xxx for update;

​ select * from t where xxx lock in share mode;

If the situation of the transaction is not guaranteed, it will cause dirty reading, non-repeatable reading, phantom reading...

Guess you like

Origin blog.csdn.net/Bruce_Zhang0828/article/details/113110246