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 employeesSET 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;
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 employeesSET 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;
相关文章推荐
- 传统路径导出 VS 直接路径导出(oracle exp direct=y)
- oracle 基本语句练习(四) SQL1999 语法规范 cross join ,等值连接,不等值连接,自连接与外连接
- 在java-oracle中实现主键值的插入
- ORACLE公有/私有同义词
- troubleshooting root.sh problem for oracle 10g/11g
- oracle library cache pin、library cache lock原理分析。
- 如何来分析oracle 11g grid infrastructure root.sh 执行错误诊断?
- Oracle创建表空间、创建用户以及授权、查看权限
- oracle Redhat64 安装错误3
- Oracle Real Application Cluster
- oracle的分析函数over 及开窗函数
- ORACLE使用函数对二进制、十进制、十六进制数互相转换
- 《Oracle 11g R2 DBA操作指南》
- oracle分页查询sql语句
- Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)
- Oracle SQL Lesson (7) - 使用子查询
- Oracle SQL Lesson (6) - 使用Join进行联合查询
- oracle手动创建数据库
- oracle SQL竖表转横表
- perl连接oracle,DBI,DBD,Data-ShowTable安装