逝者如斯夫,不舍昼夜

所有的SQL都经过测试,可粘贴,可复制,有问题请各位大神指出......

  --约束

 与表一起使用
约束不合理的数据,使其不能进入表中? insert into stu values ('','李小龙','一班','该学生成天练武,不爱学习','不知道念什么中学'); 约束的类型 约束的种类(重要)?
1、主键约束?
表中能够体现一行记录的唯一标识,这样的列就是主键?
特性:?
一张表中只能有一个主键约束?
主键约束可能作用在不止一个列上(联合主键) 主键约束列中的值不能重复?
主键约束列中的值不允许出现nul 如何创建主键?
关键字?primary?key?
创建表的时候直接创建主键?
直接在列上指定主键 create table haha(
hahaid number primary key,
hahaname varchar2(16),
hahadate date
); select * from haha ;
--–注意:不能指定联合主键
建表语句后在指定主键 drop table haha;
create table haha(
hahaid number,
hahaname varchar2(16),
hahadate date,
constraint pk_haha_id primary key(hahaid)
);
--做联合主键 create table haha(
hahaid number,
hahaname varchar2(16),
hahadate date,
constraint pk_haha_id primary key(hahaid,hahaname)
); 创建表之后追加主键 主键创建的规范:?
一般来说,做为主键的列,应该没有任何的实际意义。?
一个列做为主键后,不应该有任何其他的意义。 外键约束?
关键字:foreign?key?
一个表的某一个列关联另一个表的主键或者唯一键,?
这个列就是一个外键?emp中deptno?
创建一个外键 create table haha(
hid number,
hname varchar2(32),
deptno number,
constraint fk_haha_deptno foreign key(deptno)
references dept(deptno)
);
--追加主键 create table haha(
hid number,
hname varchar2(32),
deptno number
);
alter table haha
add constraint fk_haha_deptno foreign key(deptno)
references dept(deptno); --3 唯一约束
表中的某一个类,其录入的值不能互相重复 --unique
alter table haha
add constraint un_haha_hname unique(hname); 主键约束和唯一约束的区别(重要)?
1)一张表中只能有一个主键,可以有多个唯一约束 create table xixi(
xxid number,
xname1 varchar2(16),
xname2 varchar2(16),
xname3 varchar2(16),
constraint pk_xx_id primary key(xxid),
constraint un_xx_x1 unique(xname1),
constraint un_xx_x2 unique(xname2)
); 2)主键的值必须是非空,唯一约束可以为空且可以不止一个为空?
3)主键创建的索引是聚集索引,?
唯一约束创建索引是非聚集索引(可以不提) --4、检查性约束?
–关键字:check alter table xixi
add(salary number);
alter table xixi
add constraint ck_xixi_sal check(salary > 0
and salary < 2000);
create table xixi(
xxid number,
xname1 varchar2(16),
xname2 varchar2(16),
xname3 varchar2(16),
salary number,
constraint pk_xx_id primary key(xxid),
constraint un_xx_x1 unique(xname1),
constraint un_xx_x2 unique(xname2),
constraint ck_xixi_sal check(salary > 0
and salary < 2000)
); –5、非空约束?
–只能在列上定义,不能利用constraint关键字声明?
–not?null create table xixi(
xxid number,
xname1 varchar2(16),
xname2 varchar2(16) not null,
xname3 varchar2(16) not null,
salary number,
constraint pk_xx_id primary key(xxid),
constraint un_xx_x1 unique(xname1),
constraint un_xx_x2 unique(xname2),
constraint ck_xixi_sal check(salary > 0
and salary < 2000)
); 一个列上可以作用多个约束
启用和禁用约束?
默认的情况是约束启用状态?
禁用约束 alter table xixi disable constraint ck_xixi_sal; alter table xixi enable constraint ck_xixi_sal; –启用约束时,表中存在违反约束条件的数据时,约束无法启用 --rownum
伪劣 select rownum , e.* from emp e ; 查询工资前三的人 select rownum, e.* from emp e where rownum <= 3 order by sal desc; 错误?
查询排在第3到第5的人?根据月薪
1)rownum表示的是排序之前的序号?
2)rownum在where中可以进行筛选,但是只能用<?<=?,?
其他的得不到结果
排前三 select rownum,t1.* from (select * from emp order by sal desc)t1 where rownum <=3; --–查询排在第3到第5的人?根据月薪 select rownum,t1.* from (select * from emp order by sal desc)t1 where rownum <=5 minus select rownum,t1.*from
(select * from emp order by sal desc)t1 where rownum <=2; --分页显示
select t2.* from (select rownum as haha, t1.*
from (select * from emp order by sal desc) t1--排序
) t2--将rownum变成一个实际的列
where haha between 3 and 5; --rownum是oracle下的伪列?MySQL:limit ,SQL server top 子查询: 什么是子查询: 单行单列子查询
在一个查询中嵌套的查询被称为子查询 -- 问题一: 查询和scott同一个部门的其他员工 select *from emp where deptno = (select deptno from emp where ename = 'SCOTT') and ename <> 'SCOTT'; 单行多列子查询(了解)
查询与scott同部门,同经理的其他员工 select * from emp e1 where (e1.deptno, e1.mgr) = (select e2.deptno, e2.mgr from emp e2 where e2.ename = 'SCOTT')
and e1.ename <> 'SCOTT'; 多行子查询(多行单列子查询)
多行多列基本看不到
当子查询结果返回的不止一条记录,外部查询不能使用=,而是
使用in进行条件关联
练习:查询scott或ADAMS同一个部门的员工信息
注意:在多行子查询下 = 效率高还是in效率
当确定子查询结果只有一条时,外部使用=,效率高
当无法确定子查询结果的条目数,外部推荐使用in,
为了保证正确
牺牲了效率保证了正确率
--查询与scott奖金数不同的人 select * from emp where comm not in (select comm from emp where ename = 'SCOTT' or ename = 'ALLEN'); 重点
not in
如果not in中的值出现null,则不会查询出任何结果
确保子查询结果中一定没有null select * from emp
where comm not in (select nvl(comm, 0) from emp
where ename = 'SCOTT'
or ename = 'ALLEN'); –子查询可以使用再哪些语句中(嵌套子查询)
1、where(重要)
查询比scott月薪高的人 select * from emp
where sal > (select sal from emp where ename = 'SCOTT'); --查询与scott不在同一个部门的人 select * from emp
where deptno <> (select deptno from emp where ename = 'SCOTT'); from 重要 select * from emp e,(select deptno,dname from dept) d
where e.deptno = d.deptno; 多表连接 select * from emp, dept; 笛卡尔积 
两个集合“相乘”(结合),等于两个集合中的每个元素互相组成 
一个新的元素。 --1等值连接 select * from emp, dept
where emp.deptno = dept.deptno; --2、不等值连接 
不使用等号进行表的连接 <= >= 也是不等值连接 
查询员工的姓名,月薪和工资级别 
姓名和月薪(EMP) 工资级别(salgrade) select e.ename, e.sal, s.grade
from emp e, salgrade s
where e.sal >= s.losal and e.sal <= s.hisal; --3、外连接 
查询员工姓名和其所在部门的名称,包括没部门的员工 select * from emp e, dept d
where e.deptno = d.deptno or e.deptno is null; 此方法不行,因为所有的null都出来
外连接(左外连接/右外连接) 
简称左连接 右连接
查询员工姓名和其所在部门的名称,包括没部门的员工 
左连接 oracle 下的连接 select * from emp e, dept d
where e.deptno = d.deptno(+); 右连接 
查询没有员工的部门 select * from emp e, dept d
where e.deptno(+)= d.deptno 多表外连接 
练习:查询员工的姓名(empolyees),部门名称(departments), 
工作城市(locations),没部门的员工也要显示出来 
(+)在多表连接中的传递 select * from employees e,departments d, locations l
where e.department_id = d.department_id(+)
and d.location_id = l.location_id(+); --4、自连接(难点) 
通常用于模拟层次关系的数据(行政体系,组织结构) 
查询员工姓名e1.ename和其负责人的名字e2.ename select e1.ename as 员工姓名 , e2.ename as 负责人姓名
from emp e1, emp e2
where e1.mgr = e2.empno ; --SQL99标准 (重点)
join…on(…)
表A join 表B on(连接条件) where 筛选条件 等值连接 
查询员工姓名和其部门的名称 select e.ename, d.dname
from emp e join dept d on(e.deptno = d.deptno); --查询月薪高于1000的员工姓名和其部门的名称 select e.ename, d.dname
from emp e join dept d on(e.deptno = d.deptno)
where e.sal > 1000; 不等值连接 
查询员工的姓名,月薪和工资级别 
姓名和月薪(EMP) 工资级别(salgrade) select e.ename, e.sal, s.grade
from emp e join salgrade s
on(e.sal between s.losal and s.hisal); 自连接 
查询员工姓名和其经理的姓名 select e1.ename, e2.ename
from emp e1 join emp e2 on(e1.mgr = e2.empno ); 外连接 
左连接(常用) 
hr环境下,查询员工的姓名,部门名称,包含没有部门的员工 select e.first_name, d.department_name
from employees e left join departments d
on(e.department_id = d.department_id); 右连接(意义不大,少用) select e.first_name, d.department_name
from employees e right join departments d
on(e.department_id = d.department_id); 全连接 select *
from employees e, departments d
where e.department_id(+) = d.department_id(+); --错误的语句,oracle做不了全连接
SQL99标准写法 select *
from employees e full join departments d
on(e.department_id = d.department_id); --集合 union与union all关键字 
union 去掉重复的记录 
union 默认按照第一列进行排序 select ename, sal from emp where sal > 1000
union select ename, sal from emp where sal < 1500; union all
不排序任何列,包含重复的记录 select ename, sal from emp where sal > 1000
union all select ename, sal from emp where sal < 1500; 交集 intersect 取交集 select ename, sal from emp where sal > 1000 intersect select ename, sal from emp where sal < 1500; 相减 select ename, sal from emp where sal > 1000
minus select ename, sal from emp where sal > 1500; 交并集的操作重要条件:(重要) 
组成集合的多个SQL语句,其查询结果必须类型一致, 
而且应该意义相同,这些SQL语句应该有相同的结构
类型是一致,但是没有意义 select ename, sal from emp where deptno = 10
union select ename, comm from emp
where deptno = 10; 类型不一致的,语法错误 select ename, sal from emp where deptno = 10
union select ename, hiredate from emp where deptno = 10; --不相同的结构,语法错误
select ename, sal
from emp where deptno = 10
union select ename, sal, comm from emp where deptno = 10;

后续,会有视图和索引,以及存储过程的文章,客官们不要着急,耐心等待.........

Oracle初级——续续篇的更多相关文章

  1. 数据库之Oracle——初级

    世上岂无千里马,人中难得九方皋: 酒船鱼网归来是,花落故溪深一篙. 关于数据库的第一篇博客,这是我的第二次,人生第二春,什么也不想说,静静的开始吧,至于为什么写唐诗,请看第一篇文章! Oracle 初 ...

  2. oracle初级(续)

    有志者.事竟成,破釜沉舟,百二秦关终属楚: 苦心人.天不负,卧薪尝胆,三千越甲可吞吴. oracle基本简单的用法,之前的笔记稍作整理一下,希望对各位有用,如有问题可在下方留言,所有SQL都是经过or ...

  3. Oracle初级函数的使用

    --1.字符函数--UPPER(string|column) 可以将字符转成大写select upper('helloword') from dual;select upper(ename) from ...

  4. ORacle初级题

    一. 选择(每题1分,共15分) 1.在linux系统中,可以通过以下命令查看内核版本(). * A.who B.hostname C.uname -r D.release 2.登入linux系统后, ...

  5. Oracle初级入门 根据某字段重复只取一条记录,并计计算重复条数

    在平常开发中,去重复数据经常使用到,本人新手,接触Oracle也不久,开发中用到的小知识点,记录一下,老鸟可绕道,如果有写错的,请指正. 去重复记录可以使用distinct,当只查询一列数据时,可以轻 ...

  6. Oracle初级优化sql

    1.选择最有效率的表名顺序(只在基于规则的优化器中有效):ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理, ...

  7. Oracle初级第一天

    oracle卸载 运行regedit,删除HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ ...

  8. Oracle初级索引学习总结

    前言  索引是常见的数据库对象,建立索引的目的是为了提高记录的检索速度.它的设置好坏,使用是否得当,极大地影响数据库应用程序和Database的性能.虽然有许多资料讲索引的用法,DBA和Develop ...

  9. Oracle初级性能优化总结

    前言 关于对Oracle数据库查询性能优化的一个简要的总结. 从来数据库优化都是一项艰巨的任务.对于大数据量,访问频繁的系统,优化工作显得尤为重要.由于Oracle系统的灵活性.复杂性.性能问题的原因 ...

随机推荐

  1. vim操作命令

    一,命令模式下 文件顶部: gg 文件底部: G 删除当前行:dd 删除当前行,并进入INSERT模式: cc 取消删除:u

  2. 初识CC_MVPMatrix

    初识CC_MVPMatrix CC_MVPMatrix是一个mat4类型的uniform,在shader代码被编译之前,它由cocos2d-x框架插入进来的. bool GLProgram::comp ...

  3. 用echartsjs 实现散点图与table表格双向交互,以及实现echarts取自于table数据,和自定义echarts提示内容

    本人研究echarts已经有一段时间了,今天就分享几个关于echarts的小技巧.虽然看起来简单,但做起来却很繁琐,不过实用性倒是很好. 在一个大的页面中,左边为table表格,右边为echarts的 ...

  4. CNCC2017中的深度学习与跨媒体智能

    CNCC2017中的深度学习与跨媒体智能 转载请注明作者:梦里茶 目录 机器学习与跨媒体智能 传统方法与深度学习 图像分割 小数据集下的深度学习 语音前沿技术 生成模型 基于贝叶斯的视觉信息编解码 珠 ...

  5. 【html】01_html的介绍

    [HTML专修介绍] 定义: HTML(HypertextMarkup Language),超文本标记语言 如何理解: (意思就是超越了文本,还能兼容图片,视频,声音字节) 它的主要用处是什么? 就是 ...

  6. canvas画一个时钟

    效果图如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. 解决tomcat部署包错误

    Context namespace element 'annotation-config' and its parser class [org.springframework.context.anno ...

  8. sklearn.neighbors.kneighbors_graph的简单属性介绍

    connectivity = kneighbors_graph(data, n_neighbors=7, mode='distance', metric='minkowski', p=2, inclu ...

  9. 51Nod--1012最小公倍数

    1012 最小公倍数LCM 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 输入2个正整数A,B,求A与B的最小公倍数. Input 2个数A,B,中间用 ...

  10. tp的秘密

    入口文件index.php define('APP_DEBUG',True); 改为false* memory_get_usage 获取本套系统目前内存* tp框架中ThinkPHP\Library\ ...