您的位置:首页 > 数据库 > Oracle

Oracle SQL Lesson (9) - 操作数据(增删改)

2013-11-03 16:07 471 查看
使用INSERT语句
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);

INSERT INTO departments(department_id,
department_name, manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
字符和日期需要用单引号

INSERT INTO employees
VALUES (114,
'Den', 'Raphealy',
'DRAPHEAL', '515.127.4561',
TO_DATE('FEB 3, 1999', 'MON DD, YYYY'),
'SA_REP', 11000, 0.2, 100, 60);
insert into d values(50, 'SHC','America');
insert into d values(60, 'IT','China');

插入默认值或空值
INSERT INTO departments (department_id,
department_name)
VALUES (30, 'Purchasing');

INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);

insert.sql
INSERT INTO d VALUES(&dno, '&dname','&loc');
@/u01/app/oracle/insert.sql

使用子查询插入
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';
不使用values子句
drop table e purge;
create table e as select * from emp where 1=2;
insert into e select * from emp where deptno = 10;
insert into e select * from emp where deptno = 20;
insert into e (empno,ename,sal,deptno) select empno,ename,sal,deptno from emp where deptno=30;
alter table e modify job default 'WWW';

使用UPDATE语句
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

UPDATE employees
SET department_id = 50
WHERE employee_id = 113;

UPDATE copy_emp
SET department_id = 110;
Specify SET column_name= NULL to update a column value to NULL.

UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);

使用DELETE(DML)语句和Truncate(DDL)语句
DELETE [FROM] table
[WHERE condition];

create table emp_copy as select * from employees;

DELETE FROM emp_copy
WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name
LIKE '%Public%');

TRUNCATE TABLE table_name;
TRUNCATE TABLE copy_emp;
自动提交,不能回滚。不能加where子句。产生日志少,几乎不产生undo数据,速度快。会释放空间,delete后不会释放空间。
conn / as sysdba;
grant select on v_$database to scott;
grant select on dba_objects to scott;

conn scott/tiger;
create table big_t as select * from dba_objects;
exec dbms_status.gather_table_stats('scott','big_t');
select blocks,num_rows from user_tables where table_name='BIG_T';

delete big_t;
commit;
exec dbms_status.gather_table_stats('scott','big_t');
select blocks,num_rows from user_tables where table_name='BIG_T';

drop table big_t purge;
create table big_t as select * from dba_objects;
exec dbms_status.gather_table_stats('scott','big_t');
select blocks,num_rows from user_tables where table_name='BIG_T';

truncate table big_t;
exec dbms_status.gather_table_stats('scott','big_t');
select blocks,num_rows from user_tables where table_name='BIG_T';

事务控制
DML statements that constitute one consistent change to the data
One DDL statement
One data control language (DCL) statement

Begin when the first DML SQL statement is executed.
End with one of the following events:
-A COMMIT or ROLLBACK statement is issued.
-A DDL or DCL statement executes (automatic commit).
-The user exits SQL Developer or SQL*Plus. (Commit)
-The system crashes.(RollBack)
commit之后,其他的会话才能看到所做的更改。

session 1:
insert into d select * from d;
session 2:
select * from d;

session 1:
delete d where rownumber<=4;
session 2:
select * from d;

session 1:
create table e as select * from emp;
session 2:
select * from d;

session 1:
exit;
session 2:
select * from d;

session 1:
insert into d select * from d;
session 2:
ps -a | grep 'ora'
kill -9 4181
startup
select * from d;

使用savepoint,将一个事务分为多个部分
insert into d values(50, 'edu','china');
update d set loc='china' where deptno=40;
delete d where deptno=10;
rollback;
select * from d;

insert into d values(50, 'edu','china');
savepooint s1;
update d set loc='china' where deptno=40;
savepoint s2;
delete d where deptno=10;
rollback to savepoint s2;
select * from d;

rollback;
select * from d;

语句级别回滚
错误的语句只会回滚该语句,不会回滚之前的语句所做的更改。

读一致性
user a:
UPDATE employees SET salary = 7000
WHERE last_name = 'Grant';
user b:
SELECT * FROM userA.employees;

select * from emp for update;
SELECT employee_id, salary, commission_pct, job_id
FROM employees
WHERE job_id = 'SA_REP'
FOR UPDATE
ORDER BY employee_id;

使用FOR UPDATE OF column_name 来确定锁定制定的行
SELECT e.employee_id, e.salary, e.commission_pct
FROM employees e JOIN departments d
USING (department_id)
WHERE job_id = 'ST_CLERK‘
AND location_id = 1500
FOR UPDATE OF e.salary
ORDER BY e.employee_id;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: