您的位置:首页 > 数据库

SQL基础学习笔记(二)—增删改

2015-02-03 18:08 579 查看

向表中插入一条数据
create table emp1
as
select employee_id , last_name , hire_date , salary from employees
where 1 = 2
需要注意的是,插入的数据,必须和表中数据种类,一一对应
insert into emp1
values(1001,'abc',to_date('1998-12-11','yyyy-mm-dd'),10000)

如果salary是null

insert into emp1
values(1002,'c',to_date('1999-01-11','yyyy-mm-dd'),null)注意,salary不写,是不可以的
也可以是指定指定要输入的值,注意(employee_id,last_name,hire_date),这个顺序是可以改变的,但是value,

必须与(employee_id,last_name,hire_date)一一对应
insert into emp1(employee_id,last_name,hire_date)
values(1004,'c1',to_date('2999-05-11','yyyy-mm-dd'))


这样salary就可以不写了

有一点必须注意,只有可为空的值,才能不赋值

Name        Type         Nullable Default Comments 

----------- ------------ -------- ------- -------- 

EMPLOYEE_ID NUMBER(6)    Y                         

LAST_NAME   VARCHAR2(25)                           

HIRE_DATE   DATE                                   

SALARY      NUMBER(8,2)  Y   

这个表last_name ,hire_date,就不能为空

从其他表中拷贝数据

--把employees表中80号部门的,下述信息导入新表
insert into emp1(employee_id , hire_date , last_name , salary)
select employee_id  , hire_date , last_name , salary
from employees
where department_id = 80
创建脚本(了解)
insert into emp1(employee_id , last_name , salary ,hire_date)
values(&id,'&name',&salary,'&hire_date')

更新数据

SQL> update emp1
2 set
3 salary = 12000
4 where employee_id = 1005;//注意不写where会把所有的数据都改掉

(commit;保存)


如果不小心都改了,可以使用rollback;但是commit后的数据,是无法回滚的

--更新114号员工的工作和工资使其与205员工相同
create table empployees1
as
select * from employees

update empployees1
set job_id = (
select job_id
from empployees1
where employee_id = 205
),salary = (
select salary
from empployees1
where employee_id = 205
)
where employee_id = 114;


--调整与employee_id 为200的员工job_id 相同的员工的department_id为employee_id为100的员工的department_id
update empployees1
set department_id = (
select department_id
from empployees1
where employee_id = 100
)
where job_id = (
select job_id
from empployees1
where employee_id = 200
)


删除数据
delete from...where....

从emppployees1表中删除部门名称中含public字符的部门id


delete from empployees1
where department_id = (
select department_id
from departments
where department_name like '%Public%'
)
注意delete操作的完整性错误

删除一个含有员工的department_id,是不可以的,因为employees表不知道把这些人

怎么办

增:

<pre name="code" class="sql">(1)insert into...values(...)

(2)insert into... select ..from .. where ...




改:
update ..set ..where ..


删:
delete from ..where ..


事务:


一组逻辑操作单元,使数据从一种状态变换到另一种状态。

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

一个或多个DML 语句

一个 DDL(Data Definition Language 数据定义语言) 语句

一个 DCL(Data Control Language  数据控制语言) 语句

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

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

COMMIT 或 ROLLBACK 语句




DDL 语句(自动提交)

rollback,回滚的是最后一次的commit

提交或回滚前的数据状态

改变前的数据状态是可以恢复的

执行 DML 操作的用户可以通过 SELECT 语句查询之前的修正

其他用户不能看到当前用户所做的改变,直到当前用户结束事务。

DML语句所涉及到的行被锁定, 其他用户不能操作。

这个类似于多线程操作共享数据,当一个线程在操作改数据时,如果它不释放锁,其他线程是无法操作这个数据的

scott用户正在update表,没有commit

system用户

select * from scott.employees for update;

就无法查询,处于等待状态,等到scott用户commit了,就可以查询了

这时scott用户还想继续修改表,就不行了,因为要等system用户commit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: