No bar may charge an avg. of more than $5 for beer.
1 2 3 4 5 6
CREATEASSERTION NoRipoffBars CHECK (NOTEXISTS( SELECT bar FROM Sells GROUPBY bar HAVING5.0 < AVG(price)));
There cannot be more bars than drinkers.
1 2 3
CREATEASSERTION FewBar CHECK( (SELECTCOUNT(*) FROM Bars) <= (SELECTCOUNT(*) FROM Drinkers));
Trigger
•Whenever a new tuple is inserted into Sells:
–If the beer mentioned is not in Beers, then insert it (with a null manufacturer).
1 2 3 4 5 6 7
CREATETRIGGER BeerTrig AFTERINSERTON Sells -- INSERT can be DELETE or UPDATE OF <attr>. FOREACHROW--FOR EACH ROW can be omitted WHEN(:new.beer NOTIN (SELECTnameFROM Beers)) BEGIN INSERTINTO Beers(name) VALUES(:new.beer); END;
存储过程
一组为了完成特定功能的Transact-SQL语句集合。
域完整性约束
sql sever的三种机制:缺省值、规则、用户定义的数据类型
局部变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14
DECLARE @avg_budget int,@extra_budget int SET @extra_budget=15000 SELECT @avg_budget=AVG(budget) FROMproject IF (SELECT budget FROMproject WHERE projectno='p1')<@avg_budget BEGIN UPDATEproject SET budget=budget+@extra_budget WHERE projectno='p1' PRINT 'Budget for p1 increased by 15000' END ELSE PRINT 'Budget for p1 unchanged'
视图
1 2 3 4
CREATEVIEW v_count(projectno,countproject) ASSELECT projectno,COUNT(*) FROM workson GROUPBY projectno;
执行CREATE VIEW语句只是把视图的定义存入数据字典,并不执行SELECT语句
SQL Authorization
授权
1 2 3 4 5 6 7 8 9 10 11
GRANT <listofprivileges> ON <relation or other object> TO <listof authorization ID’s>; -- 如果想要接受者可以传递特权,使用: WITHGRANTOPTION
GRANTSELECT, UPDATE(price) ON Sells TO sally; -- Sally has the right to issue any query on Sells and can update the price component only GRANTUPDATEON Sells TO sally WITHGRANTOPTION; -- Sally can not only update any attribute of Sells, but can grant to others the privilege UPDATE ON Sells.
收权
1 2 3 4
REVOKE <listofprivileges> ON <relation or other object> FROM <listof authorization ID’s>; --这些用户可能还是可以使用该特权,因为他们可能从其他地方获得授权
两种方式:RESTRICT, CASCADE
授权图
节点
user/privilege/option/isOwner
对于特权,不同属性上的操作是不同的节点,可传递和不可传递特权的用户是不同的节点;节点中AP表示A用户有P特权。P*表示P with grant option。P 表示P的源头,AP 表示A is the owner of the object on which P is a privilege.
边
X->Y,X用来授权Y
•When A grants P to B, we draw an edge from AP * or AP ** to BP.
–Or to BP * if the grant is with grant option.
•If A grants a subprivilege Q of P (say UPDATE(a) on R when P is UPDATE ON R) then the edge goes to BQ or BQ *, instead.
•Fundamental rule: user C has privilege Q as long as there is a path from XQ (the origin of privilege Q ) to CQ, CQ *, or CQ.
–Remember that XQ could be CQ.
•If A revokes P from B with the CASCADE option, delete the edge from AP to BP.
If A uses RESTRICT, and there is an edge from BP to anywhere, then reject the revocation and make no change to the graph.
•Having revised the edges, we must check that each node has a path from some ** node, representing ownership.
•Any node with no such path represents a revoked privilege and is deleted from the diagram.
例题
Initially, user A is the owner of relation R, and no other user holds privileges on R. The following are executed:
by A: GRANT UPDATE ON R TO B
by A: GRANT UPDATE(a) ON R TO C WITH GRANT OPTION
by C: GRANT UPDATE(a) ON R TO B WITH GRANT OPTION
by A: REVOKE UPDATE(a) ON R FROM C CASCADE
Which of the following best describes the status of B’s privileges on R?
(a) B can update any attribute of R except a but cannot grant that privilege.
(b) B has no privileges on R and cannot grant any.
(c) B can update any attribute of R except a, but can grant others the privilege to update R:a .
(d) B can perform any update on R but cannot grant that privilege.
Consider a database with relation R and users Alice, Bob, Carol, and Dave. Alice owns relation R. The following sequence of operations takes place:
Alice: GRANT SELECT ON R TO Bob WITH GRANT OPTION
Alice: GRANT SELECT ON R TO Carol WITH GRANT OPTION
Carol: GRANT SELECT ON R TO Bob WITH GRANT OPTION
Bob: GRANT SELECT ON R TO Dave WITH GRANT OPTION
Carol: GRANT SELECT ON R TO Dave
Dave: GRANT SELECT ON R TO Carol WITH GRANT OPTION
Alice: REVOKE SELECT ON R FROM Bob CASCADE
After these statements are executed, which of the following statements is true?
(a) Dave has the SELECT ON R privilege, but without the grant option.
(b) Dave has the SELECT ON R privilege with the grant option.
(c) Dave does not have the SELECT ON R privilege.
(d) Dave has the grant option for the SELECT ON R privilege, but does not have the privilege itself.
It is a truth universally acknowledged that mutual respect plays a key role in our daily life. Jst as an old saying goes, “Respect others and you will be respected”. Obviously, this proverb teaches us that being respected by others is derived from respecting others.
Why does mutual respect play an important part in our society? Reasons can be listed as follows. First and foremost, all men are created equal which was first written in the U.S. Declaration of Independence. In other words, being respected is the basic right of human being. Moreover, to respect others means understanding others’ situation and respecting others; rights. For me, I have never interrupted others when they are talking or doing their own work. I always keep in mind that without respect, it is hard for me to gain friendship and trust.
In conclusion, respecting others matters a lot in our daily life, which also shows one’s good parenting and quality. The more you respect others, the more you will be respected. Only in this way can we expect our society more harmonious.
select k.catid,count(t.catid) from (select catid fromCategory) k leftjoin (select catid fromProjectleftouterjoin Workson on Project.proid=Workson.proid where empid='10102')t on k.catid=t.catid groupby k.catid
从参考答案上学到另外一种方法:
将 where 换成left outer join… on …. and,用and代替where,在连接的时候过滤,该项会被设为空值,不会在一开始过滤掉需要的空值。另外要注意left outer join 的两张表的顺序
Currently, with studying abroad gains mounting popularity among people, there is a heated debate about whether to attend college at home or abroad. Opinions on this topic vary from person to person. Some see more benefits in studying at home while others claim that studying abroad is a more ideal choice as it’s more challenging.
Personally, I am a strong favorer of the latter view. Listed below are the reasons for my advice. First of all, attending college abroad provides an opportunity to broaden one’s experience and mind. You can acquire cross-cultural experiences and gain new perspectives on your chosen field of study. In addition, studying abroad helps you to polish your social skills; you can make friends with different people with different background. Thirdly, overseas studying is conductive to the formation of an independent, autonomous and tenacious personality, which will ultimately benefit the achievement of our life goals.
Just as an old saying goes: ”It is better to travel thousand miles than to read ten thousand books.” Then studying abroad can not only enable us to reap in our books, but also in our trips. And this is why attending college abroad is a preferable selection for me.
Notes
mounting [ˈmaʊntɪŋ] adj.上升的;增长的
conductive adj. 导电(或热等)的
Singapore is a conductive environment for studies and learningEnglish. 新加坡是个学习英语和搞研究的好环境。
Understanding China’s history and culture is conductive to know Chinese well. 了解中国的历史和文化有助于了解中国人民。
tenacious [təˈneɪʃəs] adj. 顽强的、坚持的
In spite of his illness, he clung(v.抓紧) tenaciously to his job.尽管有病在身,他仍顽强地坚持工作
The Ming Dynasty, which ruled China for 276 years, was depicted as one of the greatest eras characterized by good governance and stable society in human history. During this period, the blossom of handicraft industryaccelerated the process of market economy and urbanization. A great deal of commodities, wine and silk included, were available on the market. In the meantime, clocks and tobacco products, among many other foreign goods, were imported. Major commercial canters like Beijing, Nanjing, Yangzhou and Suzhou took shape successively. It was also in the Ming Dynasty that fleets headed by navigator Zheng He had made seven large scale expeditions to the Indian Ocean. What’s also noteworthy is that three of the Four Great Classical Novels of Chinese Literature were written in the Ming Dynasty.
Notes
governance [ˈɡʌvərnəns] n. 统治;管理方法
noteworthy [ˈnoʊtwɜːrði] adj. 值得注意的;显著的
the Four Great Classical Novels of Chinese Literature 四大名著
Whether to Major in Science or Humanities at College?
Nowadays, as the whole society place increasingly considerable value on education, the question of whether to major in science or humanities at college is not only a concern for students, but also a focal point for parents. Some believe that to dig into science is a better choice because it promises us a brighter future; other may hold the opposite view that humanity knowledge is the foundation of humanity quality.
As for me, both arguments are justified. However, I believe that the important thing is not about which subject is better, what matters most is people who will have to make the decision. In other words, we should not lay one-sided emphasis on the advantages of either subject; on the contrary, the students themselves, their interests and preference, pros and cons are supposed to be taken into consideration. For example, if a student is more adept at humanity where his real interests lie, then he should dedicate to the study on humanity.
Whether to major in science or humanity is a critical choice for every student because its result has a profound influence on personal career development and life style. Thus, we should figure out what we really care about so as not to put the cart before the horse.
notes
considerable [kənˈsɪdərəbl] adj. 相当多(或大、重要等)的
The project wasted a considerable amount of time and money.那项工程耗费了相当多的时间和资金。