美文网首页
第二章 中级SQL

第二章 中级SQL

作者: Akushu | 来源:发表于2017-06-05 10:13 被阅读0次

内连接:inner join, natural join 就是典型的内连接。内连接不保留未匹配元组的连接。

例如:student表中有一个ID为00000的学生,但没有选课所以,takes表中不会出现ID00000。因此natural join后的结果也就不会有ID00000。这就是内连接。

外连接:

left outer join:只保留出现在左外连接运算之前(左边)的关系中的元组。

right outer join:只保留出现在左外连接运算之前(右边)的关系中的元组。

 full outer join:保留出现在两个关系中的元组。

where和on的不同:where谓词返回一个true的条件后进行筛选。

select * from student natural left outer join takes where/on student.ID = takes.ID;

where的话,student.ID=takes.ID是一个条件,返回true。当takesID中没有00000时,自然就返回false,所以这条元组就被过滤了。on则不会过滤。

没有使用outer前缀的join则默认为inner join

视图定义(view):create view faculty as select ID, name, dept_name from instructor;

视图存储的是查询表达式而非执行结果。

对视图进行修改将会影响到逻辑层次的数据库。

事务有时候会违反完整性约束,所以通常用关键词initally deferred来声明约束是可延迟的,指明约束的检查被延迟到该事物结束时执行。

Mysql drop constraint:

alter table instructor drop foreign key instructor_ibfk_1; 通过show create table instructor知道foreign key

add constraint:

alter table instructor add foreign key instructor(dept_name) references department(dept_name) on delete cascade;

type & domain

create type 和 create domain,创建type用来检测一些赋值错误,如将美元赋值给英镑。

两者有明显的差别,具体的情况应该用得比较少。

数据库授权:

grant select on instructor to zachary;

revoke select on instructor to zachary; 收回

角色(role)的概念:create role instructor,角色的概念用来授权,授权给role,只要将需要这个权限的任务增加该role标示即可。

grant select on instructor to zachary with grant option:表示zachary可以将权限授予其他人

默认的权限收回是级联收回,即同时收回被收回用户授予给其他的人的权限。

所以授权最好是角色授权而不是用户授权。

习题:

4.1.d. select dept_name, count(ID) from department natural left outer join instructor group by dept_name;

4.2

mysql> select * from student natural join takes

-> union

-> select ID, name, dept_name, tot_cred, NULL, NULL, NULL, NULL, NULL from student s where not exists

-> (select ID from takes T where T.ID = S.ID)

-> ;

4.8

a. mysql> select ID, name, sec_id, semester, year, time_slot_id, count(distinct building, room_number)

-> from instructor natural join teaches natural join section group by (ID, name, sec_id, semester, year, time_slot_id)

-> having count(distinct building, room_number) > 1;

OLAP:

度量属性(measure attribute):这个属性度量了某个值,可以在其上进行聚集操作,比如sale的quantity属性。

维度属性(dimension attribute):定义度量属性以及度量属性的汇总可以在其上进行观察的各个维度。比如sale的item_name,size等。

相关文章

  • 第二章 中级SQL

    内连接:inner join, natural join 就是典型的内连接。内连接不保留未匹配元组的连接。 例如:...

  • 数据库基础Database2-中级SQL

    数据库基础Database2 三 中级SQL 中级SQL主要包含形式更复杂的SQL查询、视图定义、事务、完整性约束...

  • 第二章 InterSystems SQL基础

    第二章 InterSystems SQL基础 本章概述了InterSystems SQL的特性,特别是那些SQL标...

  • 中级会计职称导图——会二

    中级会计实务 第二章 存货

  • Chapter 4 Intermediate SQL

    中级SQL(Chapter 4) Join 普通连接的条件 根据on后面的条件将表A与B进行连接 Outer Jo...

  • Oracle基础(一)

    oracle数据库 前言 oracle sql 第一章 Selecting Rows 第二章 Sortin...

  • 周六自然醒

    今日任务 中级: 昨天没听完的视频到07 第二章结束粉皮书 做第一章 第二章 讲义:俩章过一遍(念一遍) 初级经...

  • 名词从句

    //本篇为《英语语法新思维中级教程——通悟语法》第二章“名词从句”的读书笔记。 English is import...

  • 第二章 SQL谓词的概述(二)

    第二章 SQL谓词的概述(二) 谓词和%SelectMode 所有谓词都使用逻辑(内部存储)数据值进行比较。但是,...

  • leetcode sql做题笔记二 中级篇

    中等难度一共14题 1.574.Winning Candidate join问题 找出出现频率最高的,然后连接表找...

网友评论

      本文标题:第二章 中级SQL

      本文链接:https://www.haomeiwen.com/subject/sroffxtx.html