美文网首页
第二章 中级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

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