DQL、DML、DDL、DCL

SQL语言的类型

DQL:数据查询语言 select

DML:数据操作语言 insert delete update select

DDL:数据定义语言 create/drop / truncate / alter table

DCL:数据控制语言 grantrevoke
DML:

增加

insert into 表名(字段名123……) values (值123……);
1.全部插入,可以省略字段名
2.SQL99标准可以省(SQL92不能省)

动态输入值 & 占位符

insert into emp(EMPNO,ENAME,JOB) values (&empno,&ename,&job);

insert into emp(EMPNO,ENAME,&otherName) values (&empno,&ename,&otherValues);

批量插入
	1.创建新表(批量插入之前不存在)
		emp ---> myEmp;
		create table myEmp as select * from emp;
		create table myEmp2 as select ENAME,SAL from emp;
		create table myEmp3 as select ENAME,SAL from emp where sal < 3000;
		
		可以快速创建表结构
		create table myEmp4 as select * from emp where 1 = 0;
		
	2.在旧表里面插入(已存在表)
		insert into myEmp4 ( empno , ename , sal) select empno , ename, sal from emp;
		
	3.begin ...end /
		begin 
			insert ...
			insert ...
		end;
		/
		
	   	   begin
		2  insert into emp
		3  values (6666,'LISI','MANAGER',7788,'19-9月 -88',9998,1000,10);
		4  insert into emp
		5  values (2323,'WAWU','MANAGER',7788,'19-9月 -88',9998,1000,10);
		6  end;
		7  /
		
		
		海量数据:数据泵 \ SQL Loader \外部表
		

删除 delete
	delete from emp;
	
delete from emp;
撤回: rollback

delete from emp + where 语句;

全表删除
1.	delete from 表名 ; 可以回退
	truncate table 表名; 不可回退
		原因:CRUD 属于DML语句,truncate属于DML,不能回退
2.测试执行时间
	打开执行时间 set timing on;
	关闭:set timing off;
	对于少量数据:delete效率高;一行一行删除
	对于海量数据:truncate效率高;先删除表,在创建一个空表
	
3.delete 支持闪回,truncate 不支持闪回
4.delete 不会释放空间,truncate 会式释放空间
5.delete 会产生碎片,truncate不会
	碎片太多,需要整理碎片
		a.alter table 表明 move;
		b.导入导出
		
修改update

update table_name set 字段名1 = 字段名1 , 字段名2 = 字段名2 …… where ……

update emp set ename= 'x' , job = 'y';
rollback;

update emp set ename= 'x' , job = 'y' where ENAME = 'LMHao';



DDL : create/drop/truncate/alter

创建表:
SQL> create table mytable6(
  2  id number,
  3  name varchar(10),
  4  age number);
	注意事项:
		1.权限和空间问题
		2.表名规定:
			必须以字母开头;
			表名只能包含:大小写字母、数字、_、$、#;
			长度:1-30字符;
			不能与数据库中的其他表重名
			不能与保留字重名:查看保留字:select * from v$reserved_words order by keyword asc;

修改表:
	追加新列
		alter table  mytable6 add myother varchar2(10);
	修改列
		修改长度
			alter table mytable6 modify myother varchar2(20);
		修改列的类型
			alter table mytable6 modify myother number;
		注意: blob/clob不能修改 -->先删除,再追加
		删除列:
			 alter table mytable6 drop column myother;
		重命名列
			alter table mytable6 rename column Id to Did;
		删除表
			drop table mytable6;
		查看数据库中的所有表
			select * from tab;
		清空回收站
			purge recyclebin;
		查看回收站
			show recyclebin;
		还原回收站
			闪回技术
		彻底删除:删表,并清空
			drop table test02 purge;
清屏指令:DOS窗口进 :clear SCR;

设置某个字段的宽度:

字符:

​	col keyword for a10;

数字

​	col length for 999999;
多表连接查询

1.交叉连接(笛卡尔积):所有情况的组合
	select * from emp,dept ; (没意义)
2.内连接
	多张表通过相同字段进行匹配,只显示匹配成功的
	a. 等值连接
		select * from emp e,dept d where e.deptno = d.deptno;
    b.
    	select * from emp e inner join dept d on e.deptno = d.deptno;
3.外连接
	左外连接:以左表为基准(左表数据全部显示),去匹配右表数据,如果匹配成功 则全部显示,匹配不成功,显示部分(无数据部分,NULL填充)
	a.
		select * from emp e left outer join dept d  on e.deptno = d.deptno;
	b.Oracle独有
		select * from emp e,dept d where e.deptno = d.deptno(+);
		
	右外连接:以右表为基准(右表数据全部显示),去匹配左表数据,如果匹配成功 则全部显示,匹配不成功,显示部分(无数据部分,NULL填充)
	a.
		select * from emp e right outer join dept d  on e.deptno = d.deptno;
	b.Oracle独有
		select * from emp e,dept d where e.deptno(+) = d.deptno;
	
	全外连接: = 左外 + 右外  - 去重
	
	自连接 :将一张表 通过别名 “视为” 不同的表
		必须费性能
		优化:
			层次连接 : 效率高,难度大
					SQL> select level,empno,ename,mgr from emp
 				 2  connect by prior empno=mgr
 				 3  start with mgr is null
 				 4  order by level;
	
	查询员工姓名以及该员工的领导姓名
	select e.ename,b.ename from emp e,emp b where e.mgr=b.empno;
		

子查询:
	1.子查询可以出现的位置:whereselecthavingfrom;不能写在group  by 后面
		a.select 单行列(常量列)
		b.having
			查询最低工资比10号部门的最低工资高的部门编号
				select deptno,min(sal) from emp  group by deptno  having min(sal) > (select min(sal) from emp where deptno = 10);
		c.from: 相当于修改了表结构(临时表)
			select * from (select ename , sal , sal *12 from emp);
	2.主查询和子查询可以是,也可以不是同一张表
		a.查询销售部的员工信息
			select * from emp where deptno =(select deptno from dept where dname = 'SALES');
		
	3.子查询可以用 单行操作符 ( < > ) ,多行操作符( inany : 只要有一个
		all : 全部都得满足
    	a.查询销售部,财务部的员工信息
    		select * from emp where deptno in (select deptno from dept where dname = 'SALES'  or dname = 'ACCOUNTING');
    	b.查询工资比30部门中 任何其中一个员工高的员工信息
    		select * from emp where sal > (select min(sal) from emp where deptno = 30) ;
    		select * from emp where sal > any(select sal from emp where deptno = 30) ;
    4. 子查询中的null
   		a.
    		select * from  emp where mgr in (7566,7698,null);
    		select * from  emp where mgr in (7566,7698);
    	b.
    		select * from  emp where mgr not in (7566,7698);
    		select * from  emp where mgr not in (7566,7698,null);		NUll自身特性,!=NULL 无法查询任何数据
    		用 is null , is not  null
    	
  	 	c.查询不是领导的员工信息(子查询中排除NULLselect * from emp where empno not in(select mgr from emp where mgr is not null)
    5.一般不在子查询中排序
    	top-n排序
    	
伪列:不存在任何一张表,但是会被所有的表共享
rownum : 逻辑序列 1 2 3 4 5 6 7……	逻辑伪列
rowid :物理序列(18)  真实存在的位置	物理伪列	前6位 数据对象编号 依次往后数三位:数据文件	依次后		6位:数据块编号	依次往后3位 :行号
rounum : 不同sql语句执行时,rownum的值不一致
			相同的sql语句执行时,rownum的值不变(在第一次查询时,产生的rounum,之后保持不变)
		
		a.查询工资最高的三名员工信息
			select ename,sal from emp order by SAL desc; 
			select ename,sal from emp where rownum <= 3 order by SAL desc;(错误答案)(不同sql语句执行时,rownum的值不一致)
			
			top-n 前n个数据
			select rownum,... from (select * from xxx order by ...) where rownum <= n;
			
			select ename,sal from (select * from emp order by SAL desc) where rownum <= 3 ;(正确)
		
		b.rownum\rowid 删除重复数据
			rowid 根据插入的数据依次递增
			
			思路:根据编号分组(将重复的编号放到一组)然后在每组中只保留一个
				保留 rowid 最小或最大的那个
			delete from mystudent where rowid not in ( select stuno ,min(rowid) from mystudent group by stuno); 
			
			
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值