Oracle SQL Lesson (9) - 操作数据(增删改)
使用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;
Oracle SQL Lesson (9) - 操作数据(增删改)的更多相关文章
- Delphi - cxGrid连接Oracle数据库 实现数据的增删改查
cxGrid连接Oracle数据库 实现数据的增删改查 cxGrid连接Oracle数据库 1:通过OraSession连接数据库.OraDataSet实现OraSession和OraDataSour ...
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- SQL语法之初级增删改查
SQL语法之初级增删改查 1.增 1.1插入单行 INSERT INTO [表名](列名) VALUES(列值) 语法如下: INSERT INTO bsp_Nproductclass(guid,pi ...
- oracle初试、函数、增删改查、多表查询
安装oracle后的测试以及解锁账户 安装后打开命令行,输入 sqlplus 回车后会提示输入用户名,输入 sys或者system 回车后输入密码,密码为安装or ...
- oracle学习笔记系列------oracle 基本操作之表的增删改查
--创建一个表 CREATE TABLE employee_souvc( id ), name ), gender ), birth DATE, salary ,), job ), deptno ) ...
- Sql Server——数据的增删改
所谓数据的增删改就是在创建好数据库和表后向表中添加数据.删除表中的数据.更改表中的一些数据. 新增数据: 语法一: insert into 表名 values (数据内容) --这里需要 ...
- SQL学习之简单增删改查
SQL最常用的语句,就是增删改查: 增删改查的对象,分别是库(文件夹),表(文件),表的内容(表的记录): 一.创建一个基本的表 #create table Student_Info (Name VA ...
- Python 模拟SQL对文件进行增删改查
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__: Dalhhin # Python 3.5.2,Pycharm 2016.3.2 # 2 ...
- SQL简单语句(增删改查)
简单的SQL语句增删改查操作 说明: 在mysql里面亲测结果正确 用到的表(学生表:studnets) 1.创建一个学生表,(学号,姓名,性别,家庭住址) mysql> create t ...
随机推荐
- java基础---->java调用oracle存储过程(转)
存储过程是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.今天 ...
- haproxy配置监控redis主备切换(转)
环境前提: redis sentinel配置,三台主机,且配置运行良好 配置文件中添加: frontend ft_redis bind 0.0.0.0:6379 name re ...
- 14.4.4 Configuring the Memory Allocator for InnoDB InnoDB 配置内存分配器
14.4.4 Configuring the Memory Allocator for InnoDB InnoDB 配置内存分配器 当InnoDB 被开发, 内分配齐 提供了与操作系统和运行库往往缺乏 ...
- Android开发经验之—intent传递大数据
在Activity或者组件之前传递信息时,一般採用intent绑定bundle的方式传值,但在使用过程中须要注意的是不要用bundle传递大容量数据: 在做项目的过程中,须要将听写界面的听写结果信息传 ...
- CodeForces 13E. Holes 分块处理
正解是动态树,太难了,仅仅好分块处理水之.看了看status大概慢了一倍之多.. 分块算法大体就是在找一个折衷点,使得查询和改动的时间复杂度都不算太高,均为o(sqrt(n)),所以总的时间复 ...
- [C++]const修饰符
Date: 2014-1-1 Summary: const 修饰符笔记 Contents: 1.const 修饰符 声明一个常量数据类型 , 在编译时就确定数据类型 2.const 与 指针 一般情况 ...
- 专注UI——有用技术:模糊搜索
在如今的项目中.须要做模糊搜索,在曾经技术的基础上非常快得完毕了第一版.大家先看看第一版的效果,我们一会做评论: 0基础: 我们可能部分源代码(附件中会有所有源代码) <span style=& ...
- UNIX 网络编程之线程
概述: 实现并发服务器一般都是父进程accept一个连接,然后fork一个子进程,该子进程处理与该连接对端的客户之间的通信.但是fork是昂贵,耗资源和时间.而线程是轻量级线程,它的创建比进程的创建块 ...
- Linux 系统文件夹结构
登录后,在当前的命令窗口中输入命令: <span style="font-size:14px;">ls /</span> 你的终端上会出现例如以下的代码: ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...