- 插入使用insert into
eg. 在dept表中加入一行
insert into dept(deptno, dname, loc)
values(50, 'PROGRAMMING', 'BALTIMORE')
練習(xí)1.1 向部門表新增一個(gè)部門,部門編號(hào)為50,部門名稱為HR,工作地點(diǎn)為SY。
insert into dept(deptno, dname, loc)
values(50, 'HR', 'SY')
1.2 向部門表新增一個(gè)部門,部門編號(hào)為60,部門名稱為MARKET。
insert into dept(deptno, dname, loc)
values(60, 'MARKET', null)
練習(xí)2 向員工表中新增一個(gè)員工,員工編號(hào)為8888,姓名為BOB,崗位為CLERK,經(jīng)理號(hào)為7788,入職日期為1985-03-03,薪資3000,獎(jiǎng)金和部門為空。
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno)
values(8888, 'BOB', 'CLERK', 7788, to_date('1985-03-03'), 3000, null, null)
練習(xí)3 使用create table emp_back as select * from emp where 1=0,創(chuàng)建emp_back表,拷貝下來(lái)即可。
把emp表中入職日期大于1982年1月1日之前的員工信息復(fù)制到emp_back表中。
create table manager as
select * from emp where 1=0;
insert into emp_back
(select * from emp where hiredate > '1982-01-01')
練習(xí)4.1 修改部門20的員工信息,把82年之后入職的員工入職日期向后調(diào)整10天。
update emp set hiredate=hiredate + 10
where hiredate >'1982-01-01' and deptno=20;
4.2 修改獎(jiǎng)金為null的員工,獎(jiǎng)金設(shè)置為0.
update emp set comm=0
where comm is null
4.3 修改工作地點(diǎn)在NEW YORK或CHICAGO的員工工資,工資增加500。
update emp set sal=sal+500
where (select loc
from dept where EMP.deptno=DEPT.deptno) in ('NEW YORK', 'CHICAGO')
練習(xí)5 在emp表中增加一個(gè)列dname,來(lái)儲(chǔ)存部門名稱。
使用相關(guān)子查詢更新dname列為正確的部門名稱。
alter table emp
add(dname varchar2(14));
update emp set dname =
(select dname from dept where EMP.deptno=DEPT.deptno)
練習(xí)6.1 刪除經(jīng)理編號(hào)為7566的員工記錄。
delete from emp2
where mgr=7566
6.2 刪除工作在NEW YORK的員工記錄。
delete from emp2
where (select loc from dept where DEPT.deptno=EMP2.deptno)='NEW YORK'
6.3 刪除工資大于所在部門平均工資的員工記錄。
delete from emp2
where sal>(select avg(sal) from emp2 e where emp2.deptno=e.deptno)
課后作業(yè)1. 根據(jù)如下語(yǔ)句創(chuàng)建學(xué)生表student和班級(jí)表class
create table student(
xh char(4),--學(xué)號(hào)
xm varchar2(10),--姓名
sex char(2),--性別
birthday date,--出生日期
sal number(7,2), --獎(jiǎng)學(xué)金
studentcid number(2) --學(xué)生班級(jí)號(hào)
) ;
Create table class (
classid number(2), --班級(jí)編號(hào)
cname varchar2(20),--班級(jí)名稱
ccount number(3) --班級(jí)人數(shù)
)
基于上述學(xué)生表和班級(jí)表,完成如下問(wèn)題:
- 添加三個(gè)班級(jí)信息為:
1,JAVA1班,null;
2,JAVA2班,null;
3,JAVA3班,null;
insert into class values(1, 'JAVA1班', null);
insert into class values(2, 'JAVA2班', null);
insert into class values(3, 'JAVA3班', null);
- 添加學(xué)生信息如下:
‘A001’,‘張三’,‘男’,‘1905-05-01’,100,1
insert into student values('A001', '張三','男','1905-05-01',100,1)
- 添加學(xué)生信息如下:
'A002','MIKE','男','1905-05-06',10
insert into student values('A002', 'MIKE','男','1905-05-06',10,1)
- 插入部分學(xué)生信息: 'A003','JOHN','女’
insert into student(XH,XM,SEX) values ('A003', 'JOHN', '女')
- 將A001學(xué)生性別修改為‘女’
update student set sex='女'
where xh='A001'
- 將A001學(xué)生信息修改如下:
性別為男,生日設(shè)置為1980-04-01
update student set sex='男' where xh='A001';
update student set birthday='1980-04-01' where xh='A001'
- 將生日為空的學(xué)生班級(jí)修改為Java3班
update student set studentcid=(
select classid
from class
where cname= 'JAVA3班' )
where birthday is null
2.使用如下語(yǔ)句,建立以下表
create table copy_emp
(empno number(4),
ename varchar2(20),
hiredate date default sysdate,
deptno number(2),
sal number(8,2))
- 在表copy_emp中插入數(shù)據(jù),要求sal字段插入空值,部門號(hào)50,參加工作時(shí)間為2000年1月1日,其他字段隨意
insert into copy_emp(sal, deptno, hiredate) values(null, 50, '2000-01-01')
- 在表copy_emp中插入數(shù)據(jù),要求把emp表中部門號(hào)為10號(hào)部門的員工信息插入
insert into copy_emp
(select empno,ename,hiredate,deptno,sal from emp where deptno=10)
- 修改copy_emp表中數(shù)據(jù),要求10號(hào)部門所有員工漲20%的工資
update copy_emp set sal=sal*1.2 where deptno=10
- 修改copy_emp表中sal為空的記錄,工資修改為平均工資
update copy_emp set sal=(select avg(sal) from copy_emp) where sal is null
- 把工資為平均工資的員工,工資修改為空
update copy_emp set sal=null where sal=
(select avg(sal) from copy_emp)
- 事務(wù)
多條語(yǔ)句同時(shí)運(yùn)行成功或失敗 - 事務(wù)前后加begin和end