SQL基础之DML数据处理(十三)

数据操作语言

DML可以在下列条件下执行:

——向表中插入数据

——修改现存数据

——删除现存数据

事务是由完成若干项工作的DML语句组成的


使用插入语句向表中插入数据

插入表[[…]列(列)]

值(价值[…],价值);

使用这种语法一次只能向表中插入一条数据


为每一列添加一个新值

<李>

按列的默认顺序列出各个列的值

<李>

在插入子句中随意列出列名和他们的值

<李>

字符和日期型数据应包含在单引号中

插入部门(department_id, department_name, manager_id location_id)值(304年,“系统控制”,202年,1900年),


隐式方式:在列名表中省略该列的值。

插入部门(department_id department_name)值(30 '购买');


显示方式:在值子句中指定空值。

插入部门值(100年,“金融”,null, null);


SYSDATE函数记录当前系统的日期和时间。

插入员工(employee_id,

first_name、last_name、

邮件,phone_number。

hire_date, job_id,薪水,

commission_pct, manager_id,

department_id)

值(113,

“路易”、“Popp来说”,

“LPOPP”、“515.124.4567”,

SYSDATE, AC_ACCOUNT, 6900,

null, 205年,110年),


添加新员工

插入员工价值观(114,

‘窝’,‘Raphealy’,

“DRAPHEAL”、“515.127.4561”,

TO_DATE(1999年2月3日,我的弟弟,YYYY),

“SA_REP”, 11000年为0.2,100年60);


创建脚本

SQL语在句中使用,变量指定列值。

,变量放在值子句中。

插入部门(department_id、department_name location_id)

值(和department_id,及department_name,及位置),


不必书写值子句。

子查询中的值列表应于插入子句中的列名对应。

插入sales_reps (id、名称、工资、commission_pct)

选择employee_id last_name、工资、commission_pct

从员工那里job_id像%代表%的;


使用更新语句更新数据,可以一次更新多条数据(如果有需求)。

设置更新表列=值(列=值,…)

[,,条件];


使用,子句指定需要更新的数据:

更新员工设置department_id=50

employee_id=113;


更新,copy_emp

设置department_id=110;

指定column_name=NULL更新一列的值为NULL。


更新 113号员工的工作和工资使其与 205号员工相同

update employees

set job_id=(select job_id

from employees

where employee_id=205),

salary=(select salary

from employees

where employee_id=205)

where employee_id=113;


使用UPDATE 子查询,更新为基于另一张表中的数据

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 [from] table [where condition];


使用WHERE 子句指定删除的记录

delete from departments  where department_name='finance';


delete from copy_emp;


基于另一张表删除数据

delete from employees 

where department_id=(select department_id

from departments

where department_name

like '%public%');


数据定义语言 (DDL) ,不是DML语句,不能使用撤销

语法:

TRUNCATE TABLE table_name;

示例:

TRUNCATE TABLE copy_emp;


数据库事务由以下的部分组成:

一个或多个DML 语句

一个 DDL 语句

一个 DCL 语句


以第一个 DML 语句的执行作为开始

以下面的其中之一作为结束:

– COMMIT 或 ROLLBACK 语句

– DDL 或 DCL 语句(自动提交)

– SQL Developer or SQL*Plus用户退出

– 系统崩溃


COMMIT 和ROLLBACK 语句的优点

使用COMMIT 和 ROLLBACK语句,我们可以:

  • 确保数据完整性。

  • 数据改变被提交之前预览。

  • 将逻辑上相关的操作分组。

SQL 基础之DML 数据处理(十三)