oracle DML语句

DML语句

1、  插入数据

创建一个新表

create table new_cust as select * from customers

--使用insert语句添加行

/*

确定要插入的行所在的表

确定要插入哪些列,没有表示所有列

确定要插入的列的值列表

*/

1、插入时注意,主键约束和not null约束,外键约束

insert into customers(customers.customer_id,first_name,last_name,dob,phone) values

(8,'tang','long',date '1970-09-09','12345678');

INSERT INTO customers

  (customer_id, first_name, last_name, dob, phone)

VALUES

  (6, 'Fred', 'Brown', to_date('1970-1-1','yyyy-mm-dd'), '800-555-1215');

 

SELECT * FROM customers;

Not null约束

INSERT INTO customers

  (customer_id, first_name, last_name, dob, phone)

VALUES

  (7, '', 'Brown', to_date('1970-1-1','yyyy-mm-dd'), '800-555-1215');

SELECT * FROM customers;

2、忽略列的列表

--当为所有的列都提供值时可以忽略列的列表

--指定的值顺序必须与describe命名输出结果中显示的列顺序一致

INSERT INTO customers

VALUES

  (7, 'Jane', 'Green', DATE '1970-1-1', '800-555-1216');

 

select * from customers

3、为列指定空值

INSERT INTO customers

VALUES

  (8, 'Sophie', 'White', NULL, NULL);

4、插入默认值

INSERT  INTO  order_status(Order_Status_Id)

VALUES(3);

SELECT * FROM order_status;

5、在列值中使用单引号和双引号

INSERT INTO customers

VALUES(11, 'Kyle', 'O''Malley', '', NULL);

 

SELECT * FROM customers;

 

INSERT INTO products

  (product_id, product_type_id, name, description, price)

VALUES(13, 1, 'The "Greate" Gatsby', NULL, 12.99);

6、处理&

INSERT INTO products

  (product_id, product_type_id, name, description, price)

VALUES

  (14, 1, 'The "Greate" &&Gatsby', NULL, 12.99);

 

SELECT * FROM products;

7、从一个表向另外一个表复制行

select * from customers;

 

create table new_table as select * from customers;

 

delete from new_table;

 

select * from new_table;

 

insert into new_table select * from  customers;

 

 

INSERT INTO customers

  (customer_id, first_name, last_name)

  SELECT 13, first_name, last_name FROM customers WHERE customer_id = 1;

 

SELECT * FROM customers;

8、主键冲突

--主键冲突

INSERT INTO customers

  (customer_id, first_name, last_name, dob, phone)

VALUES

  (1, 'Fred', 'Brown', DATE '1970-1-1', '800-555-1215');

 

--not null约束

INSERT INTO customers

  (customer_id, first_name, last_name, dob, phone)

VALUES

  ('23', 'a', null, DATE '1970-1-1', '800-555-1215');

9、外键约束

SELECT * FROM products;

SELECT * FROM product_types;

 

INSERT INTO products

  (product_id, product_type_id, NAME, description, price)

VALUES

  (13, 6, 'Nike', 'US', 88.99);

10、替换变量

INSERT INTO products

  (product_id, product_type_id, NAME, description, price)

VALUES

  (&pid, &type_id, '&name', 'US', 88.99);

 

  select * from products

2、修改数据

--使用update语句修改行

/*

确定要修改的行所在的表

确定要修改哪些列及新的列值

确定要修改那些行where

*/

SELECT last_name

FROM  customers

WHERE customer_id = 2;

 

UPDATE customers

SET last_name = 'Red'

WHERE customer_id = 2;

 

select * from customers

--对多列进行更新

SELECT price,NAME

FROM products

WHERE price >= 10;

 

UPDATE products

   SET price = price * 1.2, NAME = lower(NAME)

 WHERE price >= 10;

--主键可以更新

UPDATE products SET product_id = 13 WHERE NAME = 'My Front Line';

 

select * from products where NAME = 'My Front Line';

修改雇员id为1的员工的薪水等于雇员id为2的薪水

update employees e set e.salary = (select t.salary from employees t where t.employee_id =2) where e.employee_id =1

3、删除数据

--使用delete语句删除行

DELETE FROM products WHERE product_id = 12;

--外键约束

DELETE FROM customers

WHERE customer_id = 1;

可以删除

DELETE FROM customers

WHERE customer_id = 5;

select * from purchases

                       

数据

  select * from all_sales

 

  delete from all_sales

 

  rollback

4、事务与锁

事务与锁是两个联系非常紧密的概念,它们保证了数据库的一致性。由于数据库是一个可以由多个用户共享的资源,因此当多个用户并发地存取数据时,就要保证数据的准确性。事务与锁就是来完成这个功能的。

事务在数据库中主要用于保证数据的一致性,防止出现错误数据。

在事务内的语句被作为一个整体,一个失败,全部失败。

事务在没有提交前可以回滚,一旦事务提交就不能再撤销。

什么是事务

事务就是一组包含一条或多条语句的逻辑单元,每个事务都是一个原子单位。

--数据库事务

--就是一组SQL语句,这组SQL语句是一个逻辑工作单元。

--事务的提交和回滚

Commit  永久性修改,提交事务

Rollback  把操作还原,回滚事务

INSERT INTO customers

VALUES

  (6, 'Fred', 'Green', DATE '1970-1-1', '800-555-1215');

COMMIT;

UPDATE customers SET first_name = 'Edward' WHERE customer_id = 1;

ROLLBACK;

--事务的开始和结束

--事务开始

1)连接到数据库,并执行一条DML语句

2)前一个事务结束后,又输入了另外一条DML语句

--事务结束

1)执行commit或rollback语句

2)执行一条DDL语句,会自动执行commit语句

3)执行一条DCL语句,如grant语句,会自动执行commit语句。

4)断开与数据库的连接。会根据是否正常推出commit或rollback

5)执行了一条DML语句,该语句失败了,会rollback

--保存点

--在事务的任何点都能够设置一个保存点,这样可以将修改回滚到保存点处。

select * from products

 

UPDATE products

SET price = price * 1.20

WHERE product_id = 1;

 

SAVEPOINT save1;

 

UPDATE products

SET price = price * 1.30

WHERE product_id = 2;

 

SELECT product_id,price

FROM products

WHERE product_id IN (1,2);

 

ROLLBACK TO SAVEPOINT save1;

 

SELECT product_id,price

FROM products

WHERE product_id IN (1,2);

--事务的ACID特性

/*

原子性(atomicity)事务必须成组地提交或回滚

一致性(consistency)事务必须确保数据库的状态一致,事务开始时,数据库的状态是一致的,结束时,状态也必须是一致的

隔离性(isolation)多个事务可以独立运行,而彼此之间不会产生影响

持久性(durability)一旦事务被提交之后,数据库的变化就会永久保留下来

*/

--并发事务

--多个用户同时对数据库进行交互,每个用户都可以同时运行自己的事务。

---------T1---------------------------------------T2----------------------

SELECT * FROM customers;                  SELECT * FROM customers;

--------------------------------------------------------------------------

INSERT INTO customers(

customer_id,first_name,last_name)

VALUES('7','John','Price');

--------------------------------------------------------------------------

UPDATE customers

SET last_name = 'Orange'

WHERE customer_id = 2;

--------------------------------------------------------------------------

SELECT * FROM customers;                  SELECT * FROM customers;

--------------------------------------------------------------------------

返回结果集中包含新插入的行和修改的行。    返回结果集中不包含事务T1所插入的

                                          行和修改后的结果。结果集还是原来的

--------------------------------------------------------------------------

COMMIT;

--------------------------------------------------------------------------

                                          SELECT * FROM customers;

--------------------------------------------------------------------------

                                          返回结果集中包含事务T1所插入的

                                          行和修改后的结果

数据库是一个庞大的多用户数据管理系统,在多用户的系统中,在同一时刻多个用户同时操作某相同资源的情况,时常发生。

利用锁可以消除多用户操作同一资源时可能出现的隐患。

排他锁:也叫写锁。事务对数据加了排他锁,那么其他事务将不能对该事务加任何锁,不能读取与访问。

共享锁:也叫读锁。该锁模式下的数据只能被读取。一个事务被加了共享锁后,其他事务不能再加排他锁,可以加共享锁。

锁是实现并发的主要手段,当事务提交后,会自动释放锁。

锁等待与死锁

锁等待的演示:

打开两个sql*plus窗口,分别对同一个表中的同一条记录进行操作,会出现锁等待。

第一个窗口执行

update customers c set c.first_name ='aaaa' where c.customer_id =6;

 

第二个窗口执行

update customers c set c.first_name ='bbbb' where c.customer_id =6;

会出现等待现象

只有第一个窗口commit;提交后,第二个窗口才行执行下去。

死锁:

死锁是锁等待的特例,通常发生在多个会话之间。

假设事务1要修改2个资源对象A,B

事务2也需要修改2个资源对象A,B

当事务1修改A时,锁定A,修改后,等待着修改B,同时修改A,B后,然后才提交事务。

事务2修改了B,等待修改A,这时A被事务1锁定。

最后出现了事务1等待事务2释放资源。事务2等待事务1释放资源,出现死锁的现象。

示例

事务1需要修改

update customers c set c.first_name ='tom' where c.customer_id =8;

update customers c set c.first_name ='john' where c.customer_id =9;

 

事务2需要修改

update customers c set c.first_name ='zhansan' where c.customer_id =8;

update customers c set c.first_name ='lisi' where c.customer_id =9;

步骤1:在第一个窗口执行

update customers c set c.first_name ='tom' where c.customer_id =8;

步骤2:在第二个窗口执行

update customers c set c.first_name ='lisi' where c.customer_id =9;

步骤3:在第一个窗口执行

update customers c set c.first_name ='john' where c.customer_id =9;

步骤4:在第二个窗口执行

update customers c set c.first_name ='zhansan' where c.customer_id =8;

会出现死锁的情况。

Oracle会自动检测死锁的情况,释放一个冲突锁,并把消息传递给对方事务。此时在第一个回话窗口中会出现下面的信息。

Oracle自动做出处理后,并重新回到锁等待的情况。

出现锁等待的情况,要尽快找出原因并进行处理,以免影响数据库性能。

--在命令行里导出用户的数据

exp  store/store@yd file =d:/back/store_back.dmp  owner =store log =d:/back/store_exp.log

---删除用户  

drop user store cascade;

--把我们备份的数据,再导入某个用户下面去

CREATE USER store2 identified  by store2;

GRANT connect, resource, dba TO store2;

--imp store/store@yd file =d:\back\store_back.dmp fromuser =store touser=store

--从一个用户里去导入到另一个用户里面去

 

imp system/123@yd file =d:\back\store_back.dmp fromuser =store touser=store2

CREATE USER store2 identified  by store2;

GRANT connect, resource, dba TO store2;

---往新用户导入部分表

imp system/123@yd file =d:\back\store_back.dmp fromuser =store touser=store2 

tables = employees, employees2, products, divisions, jobs, product_types

---导出部分表的命令

exp store/store@yd file =d:/back/table/store_back.dmp

tables= employees,employees2,customers  log =d:/back/table/store_exp.log

---导入部分表

imp store2/store2@yd tables= employees,employees2,customers  rows=y

file =d:/back/table/store_back.dmp

猜你喜欢

转载自www.cnblogs.com/fengxiangdong/p/10239366.html
今日推荐