逝者如斯夫,不舍昼夜

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

  1. --约束
  2.  
  3. 与表一起使用
  4. 约束不合理的数据,使其不能进入表中?
  5.  
  6. insert into stu values ('','李小龙','一班','该学生成天练武,不爱学习','不知道念什么中学');
  7.  
  8. 约束的类型
  9.  
  10. 约束的种类(重要)?
  11. 1、主键约束?
  12. 表中能够体现一行记录的唯一标识,这样的列就是主键?
  13. 特性:?
  14. 一张表中只能有一个主键约束?
  15. 主键约束可能作用在不止一个列上(联合主键)
  16.  
  17. 主键约束列中的值不能重复?
  18. 主键约束列中的值不允许出现nul
  19.  
  20. 如何创建主键?
  21. 关键字?primary?key?
  22. 创建表的时候直接创建主键?
  23. 直接在列上指定主键
  24.  
  25. create table haha(
  26. hahaid number primary key,
  27. hahaname varchar2(16),
  28. hahadate date
  29. );
  30.  
  31. select * from haha ;
  32. --–注意:不能指定联合主键
  33. 建表语句后在指定主键
  34.  
  35. drop table haha;
  36. create table haha(
  37. hahaid number,
  38. hahaname varchar2(16),
  39. hahadate date,
  40. constraint pk_haha_id primary key(hahaid)
  41. );
  42. --做联合主键
  43.  
  44. create table haha(
  45. hahaid number,
  46. hahaname varchar2(16),
  47. hahadate date,
  48. constraint pk_haha_id primary key(hahaid,hahaname)
  49. );
  50.  
  51. 创建表之后追加主键
  52.  
  53. 主键创建的规范:?
  54. 一般来说,做为主键的列,应该没有任何的实际意义。?
  55. 一个列做为主键后,不应该有任何其他的意义。
  56.  
  57. 外键约束?
  58. 关键字:foreign?key?
  59. 一个表的某一个列关联另一个表的主键或者唯一键,?
  60. 这个列就是一个外键?empdeptno?
  61. 创建一个外键
  62.  
  63. create table haha(
  64. hid number,
  65. hname varchar2(32),
  66. deptno number,
  67. constraint fk_haha_deptno foreign key(deptno)
  68. references dept(deptno)
  69. );
  70. --追加主键
  71.  
  72. create table haha(
  73. hid number,
  74. hname varchar2(32),
  75. deptno number
  76. );
  77. alter table haha
  78. add constraint fk_haha_deptno foreign key(deptno)
  79. references dept(deptno);
  80.  
  81. --3 唯一约束
  82. 表中的某一个类,其录入的值不能互相重复
  83.  
  84. --unique
  85. alter table haha
  86. add constraint un_haha_hname unique(hname);
  87.  
  88. 主键约束和唯一约束的区别(重要)?
  89. 1)一张表中只能有一个主键,可以有多个唯一约束
  90.  
  91. create table xixi(
  92. xxid number,
  93. xname1 varchar2(16),
  94. xname2 varchar2(16),
  95. xname3 varchar2(16),
  96. constraint pk_xx_id primary key(xxid),
  97. constraint un_xx_x1 unique(xname1),
  98. constraint un_xx_x2 unique(xname2)
  99. );
  100.  
  101. 2)主键的值必须是非空,唯一约束可以为空且可以不止一个为空?
  102. 3)主键创建的索引是聚集索引,?
  103. 唯一约束创建索引是非聚集索引(可以不提)
  104.  
  105. --4、检查性约束?
  106. –关键字:check
  107.  
  108. alter table xixi
  109. add(salary number);
  110. alter table xixi
  111. add constraint ck_xixi_sal check(salary > 0
  112. and salary < 2000);
  113. create table xixi(
  114. xxid number,
  115. xname1 varchar2(16),
  116. xname2 varchar2(16),
  117. xname3 varchar2(16),
  118. salary number,
  119. constraint pk_xx_id primary key(xxid),
  120. constraint un_xx_x1 unique(xname1),
  121. constraint un_xx_x2 unique(xname2),
  122. constraint ck_xixi_sal check(salary > 0
  123. and salary < 2000)
  124. );
  125.  
  126. 5、非空约束?
  127. –只能在列上定义,不能利用constraint关键字声明?
  128. not?null
  129.  
  130. create table xixi(
  131. xxid number,
  132. xname1 varchar2(16),
  133. xname2 varchar2(16) not null,
  134. xname3 varchar2(16) not null,
  135. salary number,
  136. constraint pk_xx_id primary key(xxid),
  137. constraint un_xx_x1 unique(xname1),
  138. constraint un_xx_x2 unique(xname2),
  139. constraint ck_xixi_sal check(salary > 0
  140. and salary < 2000)
  141. );
  142.  
  143. 一个列上可以作用多个约束
  144. 启用和禁用约束?
  145. 默认的情况是约束启用状态?
  146. 禁用约束
  147.  
  148. alter table xixi disable constraint ck_xixi_sal;
  149.  
  150. alter table xixi enable constraint ck_xixi_sal;
  151.  
  152. –启用约束时,表中存在违反约束条件的数据时,约束无法启用
  153.  
  154. --rownum
  155. 伪劣
  156.  
  157. select rownum , e.* from emp e ;
  158.  
  159. 查询工资前三的人
  160.  
  161. select rownum, e.* from emp e where rownum <= 3 order by sal desc;
  162.  
  163. 错误?
  164. 查询排在第3到第5的人?根据月薪
  165. 1rownum表示的是排序之前的序号?
  166. 2rownumwhere中可以进行筛选,但是只能用<?<=?,?
  167. 其他的得不到结果
  168. 排前三
  169.  
  170. select rownum,t1.* from (select * from emp order by sal desc)t1 where rownum <=3;
  171.  
  172. --–查询排在第3到第5的人?根据月薪
  173.  
  174. select rownum,t1.* from (select * from emp order by sal desc)t1 where rownum <=5 minus select rownum,t1.*from
  175. (select * from emp order by sal desc)t1 where rownum <=2;
  176.  
  177. --分页显示
  178. select t2.* from (select rownum as haha, t1.*
  179. from (select * from emp order by sal desc) t1--排序
  180. ) t2--将rownum变成一个实际的列
  181. where haha between 3 and 5;
  182.  
  183. --rownumoracle下的伪列?MySQLlimit SQL server top
  184.  
  185. 子查询:
  186.  
  187. 什么是子查询:
  188.  
  189. 单行单列子查询
  190. 在一个查询中嵌套的查询被称为子查询
  191.  
  192. -- 问题一:
  193.  
  194. 查询和scott同一个部门的其他员工
  195.  
  196. select *from emp where deptno = (select deptno from emp where ename = 'SCOTT') and ename <> 'SCOTT';
  197.  
  198. 单行多列子查询(了解)
  199. 查询与scott同部门,同经理的其他员工
  200.  
  201. select * from emp e1 where (e1.deptno, e1.mgr) = (select e2.deptno, e2.mgr from emp e2 where e2.ename = 'SCOTT')
  202. and e1.ename <> 'SCOTT';
  203.  
  204. 多行子查询(多行单列子查询)
  205. 多行多列基本看不到
  206. 当子查询结果返回的不止一条记录,外部查询不能使用=,而是
  207. 使用in进行条件关联
  208. 练习:查询scottADAMS同一个部门的员工信息
  209. 注意:在多行子查询下 = 效率高还是in效率
  210. 当确定子查询结果只有一条时,外部使用=,效率高
  211. 当无法确定子查询结果的条目数,外部推荐使用in
  212. 为了保证正确
  213. 牺牲了效率保证了正确率
  214. --查询与scott奖金数不同的人
  215.  
  216. select * from emp where comm not in (select comm from emp where ename = 'SCOTT' or ename = 'ALLEN');
  217.  
  218. 重点
  219. not in
  220. 如果not in中的值出现null,则不会查询出任何结果
  221. 确保子查询结果中一定没有null
  222.  
  223. select * from emp
  224. where comm not in (select nvl(comm, 0) from emp
  225. where ename = 'SCOTT'
  226. or ename = 'ALLEN');
  227.  
  228. –子查询可以使用再哪些语句中(嵌套子查询)
  229. 1where(重要)
  230. 查询比scott月薪高的人
  231.  
  232. select * from emp
  233. where sal > (select sal from emp where ename = 'SCOTT');
  234.  
  235. --查询与scott不在同一个部门的人
  236.  
  237. select * from emp
  238. where deptno <> (select deptno from emp where ename = 'SCOTT');
  239.  
  240. from 重要
  241.  
  242. select * from emp e,(select deptno,dname from dept) d
  243. where e.deptno = d.deptno;
  244.  
  245. 多表连接
  246.  
  247. select * from emp, dept;
  248.  
  249. 笛卡尔积 
  250. 两个集合“相乘”(结合),等于两个集合中的每个元素互相组成 
  251. 一个新的元素。
  252.  
  253. --1等值连接
  254.  
  255. select * from emp, dept
  256. where emp.deptno = dept.deptno;
  257.  
  258. --2、不等值连接 
  259. 不使用等号进行表的连接 <= >= 也是不等值连接 
  260. 查询员工的姓名,月薪和工资级别 
  261. 姓名和月薪(EMP 工资级别(salgrade
  262.  
  263. select e.ename, e.sal, s.grade
  264. from emp e, salgrade s
  265. where e.sal >= s.losal and e.sal <= s.hisal;
  266.  
  267. --3、外连接 
  268. 查询员工姓名和其所在部门的名称,包括没部门的员工
  269.  
  270. select * from emp e, dept d
  271. where e.deptno = d.deptno or e.deptno is null;
  272.  
  273. 此方法不行,因为所有的null都出来
  274. 外连接(左外连接/右外连接) 
  275. 简称左连接 右连接
  276. 查询员工姓名和其所在部门的名称,包括没部门的员工 
  277. 左连接 oracle 下的连接
  278.  
  279. select * from emp e, dept d
  280. where e.deptno = d.deptno(+);
  281.  
  282. 右连接 
  283. 查询没有员工的部门
  284.  
  285. select * from emp e, dept d
  286. where e.deptno(+)= d.deptno
  287.  
  288. 多表外连接 
  289. 练习:查询员工的姓名(empolyees),部门名称(departments), 
  290. 工作城市(locations),没部门的员工也要显示出来 
  291. (+)在多表连接中的传递
  292.  
  293. select * from employees e,departments d, locations l
  294. where e.department_id = d.department_id(+)
  295. and d.location_id = l.location_id(+);
  296.  
  297. --4、自连接(难点) 
  298. 通常用于模拟层次关系的数据(行政体系,组织结构) 
  299. 查询员工姓名e1.ename和其负责人的名字e2.ename
  300.  
  301. select e1.ename as 员工姓名 , e2.ename as 负责人姓名
  302. from emp e1, emp e2
  303. where e1.mgr = e2.empno ;
  304.  
  305. --SQL99标准 (重点)
  306. joinon(…)
  307. A join B on(连接条件) where 筛选条件
  308.  
  309. 等值连接 
  310. 查询员工姓名和其部门的名称
  311.  
  312. select e.ename, d.dname
  313. from emp e join dept d on(e.deptno = d.deptno);
  314.  
  315. --查询月薪高于1000的员工姓名和其部门的名称
  316.  
  317. select e.ename, d.dname
  318. from emp e join dept d on(e.deptno = d.deptno)
  319. where e.sal > 1000;
  320.  
  321. 不等值连接 
  322. 查询员工的姓名,月薪和工资级别 
  323. 姓名和月薪(EMP 工资级别(salgrade
  324.  
  325. select e.ename, e.sal, s.grade
  326. from emp e join salgrade s
  327. on(e.sal between s.losal and s.hisal);
  328.  
  329. 自连接 
  330. 查询员工姓名和其经理的姓名
  331.  
  332. select e1.ename, e2.ename
  333. from emp e1 join emp e2 on(e1.mgr = e2.empno );
  334.  
  335. 外连接 
  336. 左连接(常用) 
  337. hr环境下,查询员工的姓名,部门名称,包含没有部门的员工
  338.  
  339. select e.first_name, d.department_name
  340. from employees e left join departments d
  341. on(e.department_id = d.department_id);
  342.  
  343. 右连接(意义不大,少用)
  344.  
  345. select e.first_name, d.department_name
  346. from employees e right join departments d
  347. on(e.department_id = d.department_id);
  348.  
  349. 全连接
  350.  
  351. select *
  352. from employees e, departments d
  353. where e.department_id(+) = d.department_id(+);
  354.  
  355. --错误的语句,oracle做不了全连接
  356. SQL99标准写法
  357.  
  358. select *
  359. from employees e full join departments d
  360. on(e.department_id = d.department_id);
  361.  
  362. --集合
  363.  
  364. unionunion all关键字 
  365. union 去掉重复的记录 
  366. union 默认按照第一列进行排序
  367.  
  368. select ename, sal from emp where sal > 1000
  369. union select ename, sal from emp where sal < 1500;
  370.  
  371. union all
  372. 不排序任何列,包含重复的记录
  373.  
  374. select ename, sal from emp where sal > 1000
  375. union all select ename, sal from emp where sal < 1500;
  376.  
  377. 交集 intersect 取交集
  378.  
  379. select ename, sal from emp where sal > 1000 intersect select ename, sal from emp where sal < 1500;
  380.  
  381. 相减
  382.  
  383. select ename, sal from emp where sal > 1000
  384. minus select ename, sal from emp where sal > 1500;
  385.  
  386. 交并集的操作重要条件:(重要) 
  387. 组成集合的多个SQL语句,其查询结果必须类型一致, 
  388. 而且应该意义相同,这些SQL语句应该有相同的结构
  389. 类型是一致,但是没有意义
  390.  
  391. select ename, sal from emp where deptno = 10
  392. union select ename, comm from emp
  393. where deptno = 10;
  394.  
  395. 类型不一致的,语法错误
  396.  
  397. select ename, sal from emp where deptno = 10
  398. union select ename, hiredate from emp where deptno = 10;
  399.  
  400. --不相同的结构,语法错误
  401. select ename, sal
  402. from emp where deptno = 10
  403. 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. Eclipse创建一个JAVA WEB项目

    继上一篇博客,Eclipse的Tomcat已经配置好了,现在我们开始创建web项目. 1.打开Eclipse,选择菜单栏的file>New>Dynamic Web Project 弹出窗口 ...

  2. 《Linux命令行与shell脚本编程大全》第十九章 初识sed和gawk

    这两个工具能够极大简化需要进行的数据处理任务. 19.1 文本处理 能轻松实现自动格式化.插入.修改或删除文本元素的简单命令行编辑. sed和gawk就具备上述功能 19.1.1 sed编辑器 被称为 ...

  3. vue.js官方文档 PDF

    链接:https://pan.baidu.com/s/1jHMBb5W 密码:gsks

  4. Docker 使用教程

    概括  Docker与传统虚拟机的区别 与传统虚拟机的区别  Docker的安装 的安装  Docker daemon , client , containerd  镜像与容器操作  容器运 ...

  5. 企业级分布式监控系统-Zabbix基础

    1.基础分部 1.1Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. 1.2监控系统架构 C/S架构 客户端/服务器端,这种架构适合规模较小,处于同一地域的环境 C/P/S 客户端/ ...

  6. 利用InfluxDB和Grafana搭建数据监测的仪表盘

    InfluxDB的介绍及安装 InfluxDB是支持持续写入的时序数据库,常用于监测系统所需要的数据的存储. 官网的详细安装步骤 https://docs.influxdata.com/influxd ...

  7. Scala入门系列(八):面向对象之trait

    基础知识 1 将trait作为接口使用 此时Trait就与Java中的接口非常类似,不过注意,在Scala中无论继承还是trait,统一都是extends关键字. Scala跟Java 8前一样不支持 ...

  8. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  9. Android ListView与RecycleView的对比使用

    ListView,就如其名,是用来显示列表的一种View,而RecycleView,是其的加强版,今天带来的是这两个几乎具有相同的功能的对比使用 先从ListView说起吧 ListView: 1.在 ...

  10. java 邮件发送的公共方法

    protected static String host = "true"; protected static String auth = "smtp.163.com&q ...