操纵数据(DML)
数据操纵语言:当添加、更新或者删除数据库中的数据时需要使用 DML 语句。DML依据的一个集合构成了一个被称为事务的逻辑单元。
当完成下面操作时,DML 语句被执行:
– 添加新行到表中
– 修改表中的行
– 删除表中的行
1 添加一个新行到表中
1.1INSERT 语句语法
用该语法一次只能插入一行
1.2 指定列添加
1.2.1 示例
向 departments 表中添加一条数据。 Department_Id=280
Department_name = Teaching
Manager_id=180
Location_id =2000
insert into departments(department_id,department_name,manager_id,loction_id)
values(280,'Teaching',180,2000);
只插入到临时表空间,需要使用commit;
1.3 完全列添加
1.3.1 示例
向 departments 表中添加一条数
据。 Department_Id=290
Department_name = Development
Manager_id=149
Location_id =2000
insert into departments(department_id, department_name, manager_id, location_id)
values(290, 'Development',149,2000);
select * from departments where
department_id = 290;
1.4 插入带空值的行
隐式方法: 省略字段列表中的列。
insert into departments(department_id,department_name)
values(300,'A');
显式方法: 在 VALUES 子句中指定 NULL 关键字。
insert into departments(department_id,department_name,m
anager_id,location_id)
values(310,'A',null,null);
1.5 插入日期值
1.5.1 示例
添加一个新的雇员:
insert into
employees(employee_id,first_name,last_name,
email,phone_number,hire_date,job_id,salary,
commission_pct,manager_id,department_id)
values(300,'Old','Lu','sdfsdfd','23423423',sysdate,'AD_PRES',23423,null,204,290);
使用默认日期格式:
insert into
employees(employee_id,first_name,last_name,
email,phone_number,hire_date,job_id,salary,
commission_pct,manager_id,department_id)
values(301,'Old','Lu','sdfsdddfd','23423423','01/3 月/2019','AD_PRES',23423,null,204,290);
指定日期格式:
insert into
employees(employee_id,first_name,last_name,
email,phone_number,hire_date,job_id,salary,
commission_pct,manager_id,department_id)
values(302,'Old','Lu','sdfsddfd','23423423',to_date('2019-03-01','yyyy-MM-dd'),'AD_PRES',23423,null,204,290);
1.6 从另一个表中复制行
• 用一个查询写 INSERT 语句
• 不用 VALUES 子句
• 在查询中列的数目要匹配 INSERT 子句中列的数目
1.6.1 创建表
create table EMP
(
id NUMBER not null,
name VARCHAR2(50),
salary NUMBER(8,2),
commission NUMBER(2,2)
)
1.6.2 示例
将 employees 表中的 job_id 中含有 REP 的工作岗位的数据插入到 emp 表中。
insert into
emp(id,name,salary,commission) select
employee_id,last_name,salary,commission_pct
from employees where job_id like '%REP%';
OR
insert into emp select
employee_id,last_name,salary,commission_pct
from employees where job_id like '%REP%';
1.7 使用默认值
在 INSERT 中的 DEFAULT
insert into emp(id,name) values(302,'Kevin');
insert into emp values(301,'OldLu',default,null);
2 改变表中的数据
2.1UPDATE 语句的语法
用 UPDATE 语句修改已存在的行
2.1.1 示例
更新 emp 表中 ID 为 170 的数据,将名字修改为 OldLu,薪水修改 20000。
update emp e set e.name ='OldLu',e.salary = 20000
where e.id = 170;
2.2 用查询更新列
2.2.1 示例
更新 emp 表中的雇员 165 的薪水,使其和雇员 156 相同。
update emp e set e.salary = (select salary
from emp where id = 156) where e.id = 165;
3 从表中删除行
3.1DELETE 语句
使用 DELETE 语句从表中删除已存在的行。
3.1.1 示例
删除 emp 表中 id 为 302 的雇员。
delete from emp where id = 302;
3.2 删除基于另一个表的行
3.2.1 示例
删除 emp 表中薪水与 IT_PROG 岗位相同的薪水。
delete from emp where salary in(select
distinct salary from employees where job_id
= 'IT_PROG');
3.3 删除表中的全部数据
如果在删除语句中没有给定任何条件,那么数据将会删除该表中的所有数据。
3.3.1 示例
删除表中的所有数据。
delete emp;
3.4 删除行: 完整性约束错误
不能删除包含主建的行,该主键被用做另一个表的外键;