alter table 表名 add 列名 类型(长度)[约束&默认值]; alter table student add City varchar2(20) not null;
2.修改列
1 2 3 4 5
alter table 表名 modify 列名 类型 [约束 默认值]; --修改表中的列 alter table student modify City varchar(300) uniquenot null; alter table 表名 modify 列名 类型 null; --去掉列的非空约束 alter table 表名 modify 列名 类型 not null; --给列添加非空约束 alter table cjb modify xingming null
3.删除列
1 2
alter table 表名 dropcolumn 列名; --删除表中的列 alter table student dropcolumn sname
4.新的列定义默认值
1
alter table student modify city default'北京';
5.重命名列
1
alter table 表名 rename column 旧列名 to 新列名;
6.将表更改为只读状态
1 2
alter table 表名 READ ONLY; --改为只读状态 alter table 表名 READ WRITE; --改回可编辑模式
7.添加约束
1 2 3 4 5 6 7
alter table 表名 add 表级约束语法; --给表添加表级约束 --alter table 表名 add constraint 约束名 约束类型(列名) alter table bm2 add constraint bm2_zjys primary key(deptno); alter table emp add constraint FK_DEPTNO foreign key (deptno) references DEPT (DEPTNO); alter table emp add constraint CK_sal check sal (sal>800) --因非空约束不能使用表级约束语法,故添加非空约束需使用modify: alter table bm2 modify loc varchar2(20) not null;
8.删除约束
1 2
alter table 表名 dropconstraint 约束名; --删除一个约束 alter table bm2 dropconstraint BM2_ZJYS
insert 数据插入
格式一:指定列插入数据,使用前提是不被插入的列可为空
1 2
insert into bm3(deptno,loc) values(50,'七星区'); insert into bm3(deptno,dname,loc) values(60,'a','a')
格式二:不指定列插入,默认所有列都需要值,如遇到不插入数据的列则用null值填充
1
insert into bm3 values(70,'t',null);
格式三:只插入select查询结果集的数据,注意查询的结果集结构要保证和表结构一致
1 2 3
insert into bm3(deptno,dname) select deptno,dname from scott.dept insert into bm3 select20,'z','z'from dual insert into bm3 select deptno,ename,job from scott.emp
update 修改
1 2 3 4 5
update bm3 set deptno=80where deptno=60 update bm3 set dname='销售部门',loc='八星区'where deptno=80--修改多项数据,逗号隔开
--把WARD的工资改成和SMITH一样 update yg2 set sal=(select sal from yg2 where ename='SMITH') where ename='WARD'
delete 删除的是数据
delete、insert只能以行为单位进行操作
1 2 3 4 5 6 7 8
deletefrom yg2 where empno=7369 alter table yg2 dropcolumn deptno deletefrom yg2 where sal>(select sal from yg2 where empno=7499)
mergeinto 目标表 Using 备份表(源表) On (values=values) When matched thenupdate 和 delete Whennot matched theninsert
--用源表sc1对目标表sc进行更新 mergeinto sc --目标表(需要更新的表) using sc1 --源表(备份表) on(sc.sno=sc1.sno and sc.cno=sc1.cno) when matched then updateset sc.score=sc1.score--有值的时候去做更新 whennot matched then insertvalues(sc1.sno,sc1.cno,sc1.score)--没值的时候直接插入
mergeinto table1 using table2 on(table1.id=table2.id) when matched then updateset table1.setup_date=table2.setup_date updateset table1.mature_date=table2.mature_date whennot matched then insertvalues(table2.setup_date,table2.mature_date)
--1.将c002课程的成绩增加5分 update sc set score=score+5where cno='c002'
--2.将c001课程成绩小于80分的同学的成绩增加10分 update sc set score=score+10where cno='c001'and score<80
--3.增加一个学生:学号's013',姓名:'王麻子',年龄:28,性别:男 insert into student values('s013','王麻子',28,'男')
--4.创建一张和sc表相同的表,并将s001和s002学生的选课信息插入新表中 create table sc1 as select*from sc where sno='s001'or sno='s002'
--5.将所有c001课程成绩不及格的同学的分数改为60分 update sc set score=60where cno='c001'and score<60
--6.删除“s002”同学的“c001”课程的信息记录 deletefrom sc where sno='s002'and cno='c001' --重新插入“s002”同学的“c001”课程的信息记录 insert into sc values('s002','c001',80.90);
--7.用sc1去更新sc,当学生和选修课程相同的时候,把sc1的分数更新到sc,不同的时候就插入到sc mergeinto sc using sc1 on(sc.sno=sc1.sno and sc.cno=sc1.cno) when matched then updateset sc.score=sc1.score whennot matched then insertvalues(sc1.sno,sc1.cno,sc1.score) --操作符练习 --1.查询名字是BLAKE的人的编号,名字,工资 select empno,ename,sal from emp where ename='BLAKE'
--2.查询编号是7782的员工的编号,名字,工资,奖金 select empno,ename,sal,comm from emp where empno=7782
--3.查询职位是销售(SALESMAN)的人的名字,职位,入职日期 select ename,job,hiredate from emp where job='SALESMAN'
--4.查询部门是10的人的编号,名字,部门编号 select empno,ename,deptno from emp where deptno=10
--5.查询工资大于1500,并且小于2500的人的编号,名字,工资 select empno,ename,sal from emp where sal>1500and sal<2500
--6.查询工资小于2000的人的名字,工资,奖金 select ename,sal,comm from emp where sal<2000
--7.求工作是CLERK的或者工资小于2000员工姓名,工作,工资 select ename,job,sal from emp where job='CLERK'or sal<2000
--8.求工资小于800或者大于1500的员工姓名,工作,工资 select ename,job,sal from emp where sal<800or sal>1500
--9.求工作是CLERK,并且工资小于950或者大于1500的员工姓名,工作,工资 select ename,job,sal from emp where job='CLERK'and (sal<950or sal>1500)
--10.求10号部门工资大于1000和20号部门工资大于1500的员工姓名 select ename from emp where deptno=10and sal>1000or deptno=20and sal>1500 --11.求工资在1500到2000之间的员工姓名 select ename from emp where sal between1500and2000
--12.求部门编号是10号,20号,30号中任意一个部门的员工姓名 select ename from emp where deptno in(10,20,30)
--13.求姓名中包含'M'的员工姓名 select ename from emp where ename like'%M%'
--14.求员工姓名第二位是'M'的员工姓名 select ename from emp where ename like'_M%'
--15.求姓名中包含A和L的姓名 select ename from emp where ename like'%A%'and ename like'%L%'
--16.求姓名是五位的员工信息 select*from emp where ename like'_____'
--17.求姓名中第3位是%的员工姓名 select ename from emp where ename like'__\%%'escape'\\'
--18.求第一位是_,倒数第二位也是_的员工姓名 select ename from emp where ename like'\_%'and ename like'%\__'escape'\ '
--19.求以_开头,第三位也是下划线_,第六位是%的员工姓名 select ename from emp where ename like'\__\___\%%'escape'\ '
--20.求名字中不包含M的员工姓名 select ename from emp where ename notlike'%M%'