




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1,Chapter 6: Integrity and Security,Domain Constraints Referential Integrity Assertions断言 Triggers Security Authorization Authorization in SQL,2,Domain Constraints,Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result
2、 in a loss of data consistency. Domain constraints are the most elementary form of integrity constraint. They test values inserted in the database, and test queries to ensure that the comparisons make sense. New domains can be created from existing data types E.g. create domain Dollars numeric(12, 2
3、) create domain Pounds numeric(12,2) We cannot assign or compare a value of type Dollars to a value of type Pounds.把一个域中的值转换到另一个域中。 However, we can convert type as below (cast r.A as Pounds) (Should also multiply by the dollar-to-pound conversion-rate,3,Domain Constraints (Cont.,The check clause in
4、SQL-92 permits domains to be restricted: Use check clause to ensure that an hourly-wage domain allows only values greater than a specified value. create domain hourly-wage numeric(5,2)constraint value-test 约束名 check(value = 4.00) The domain has a constraint that ensures that the hourly-wage is great
5、er than 4.00 The clause constraint value-test is optional; useful to indicate which constraint an update violated,4,Can have complex conditions in domain check create domain AccountType char(10) constraint account-type-test check (value in (Checking, Saving) 在deposit关系中指定约束: check (branch-name in (s
6、elect branch-name from branch) 这个条件必须不仅仅是在deposit中插入或修改一个元组的时候检测,而且在关系branch中的元组被删除或修改时也要检测,5,drop domain hourly-wage alter domain hourly-wage drop constraint value-test alter domain hourly-wage add constraint value-test check(value = 4.00,6,Referential Integrity,Ensures that a value that appears in
7、 one relation for a given set of attributes also appears for a certain set of attributes in another relation. Example: If “Perryridge” is a branch name appearing in one of the tuples in the account relation, then there exists a tuple in the branch relation for branch “Perryridge”. Dangling tuples 悬挂
8、元组 Formal Definition Let r1(R1) and r2(R2) be relations with primary keys K1 and K2 respectively. The subset of R2 is a foreign key referencing K1 in relation r1, if for every t2 in r2 there must be a tuple t1 in r1 such that t1K1 = t2. Referential integrity constraint 参照完整性约束 also called subset dep
9、endency 子集依赖 since its can be written as (r2) K1 (r1,7,参照完整性示例,主码,外码,不是Student的主码,但是Department的主码。 Student中Sdept中的值都取自Department表中相应Sdept的值。 指定外码时,列名不一定要求相同,Student,Department,8,Referential Integrity in the E-R Model,Consider relationship set R between entity sets E1 and E2. The relational schema fo
10、r R includes the primary keys K1 of E1 and K2 of E2.Then K1 and K2 form foreign keys on the relational schemas for E1 and E2 respectively. Weak entity sets are also a source of referential integrity constraints. For the relation schema for a weak entity set must include the primary key of the entity
11、 set on which it depends,9,Checking Referential Integrity on Database Modification,The following tests must be made in order to preserve the following referential integrity constraint: (r2) K (r1) Insert. If a tuple t2 is inserted into r2外键表, the system must ensure that there is a tuple t1 in r1 主键表
12、such that t1K = t2. That is t2 K (r1) Delete. If a tuple, t1 is deleted from r1, the system must compute the set of tuples in r2 that reference t1: = t1K (r2) If this set is not empty, either the delete command is rejected as an error, or the tuples that reference t1 must themselves be deleted (casc
13、ading deletions are possible,10,参照完整性示例,(r2,r2,r1,K (r1,t2,insert,= t1K (r2,delete,11,Database Modification (Cont.,Update. There are two cases: If a tuple t2 is updated in relation r2外键表and the update modifies values for foreign key , then a test similar to the insert case is made. Let t2 denote the
14、 new value of tuple t2. The system must ensure that t2 K(r1) If a tuple t1 is updated in r1主键表, and the update modifies values for the primary key (K), then a test similar to the delete case is made. The system must compute = t1K (r2) using the old value of t1 (the value before the update is applied
15、). If this set is not empty, the update may be rejected as an error, or the update may be cascaded to the tuples in the set, or the tuples in the set may be deleted,12,Referential Integrity in SQL,Primary,candidate keys and foreign keys can be specified as part of the SQL create table statement: The
16、 primary key clause of the create table statement includes a list of the attributes that comprise the primary key. The unique key clause of the create table statement includes a list of the attributes that comprise a candidate key. The foreign key clause of the create table statement includes both a
17、 list of the attributes that comprise the foreign key and the name of the relation referenced by the foreign key,13,Referential Integrity in SQL Example,create table customer(customer-namechar(20),customer-streetchar(30),customer-citychar(30),primary key (customer-name) create table branch(branch-na
18、mechar(15),branch-citychar(30),assetsinteger,primary key (branch-name,14,Referential Integrity in SQL Example (Cont.,create table account(account-numberchar(10),branch-namechar(15),balanceinteger,primary key (account-number), foreign key (branch-name) references branch) create table depositor(custom
19、er-namechar(20),account-numberchar(10),primary key (customer-name, account-number),foreign key (account-number) references account,foreign key (customer-name) references customer,15,Cascading Actions in SQL,create table account (. . .foreign key(branch-name) references branch on delete cascade/set n
20、ull/set default on update cascade /set null/set default . . . ) restrict. 无关系依赖它,才可以删除或修改,否则拒绝。On短语不写表示为此方式。 Cascade set null/set default 删除或修改主键表时,将外键表中所有与主键表中被删除或修改的主键值对应的外键值置为null或default。 Cascading updates are similar. If there is a chain of foreign-key dependencies across multiple relations, wi
21、th on delete cascade specified for each dependency, a deletion or update at one end of the chain can propagate传播 across the entire chain,16,Cascading Actions in SQL (Cont.,If a cascading update or delete causes a constraint violation违反 that cannot be handled by a further cascading operation, the sys
22、tem aborts the transaction. As a result, all the changes caused by the transaction and its cascading actions are undone. Null values in foreign key attributes complicate SQL referential integrity semantics, and are best prevented using not null if any attribute of a foreign key is null, the tuple is
23、 defined to satisfy the foreign key constraint! Referential integrity is only checked at the end of a transaction Intermediate steps are allowed to violate referential integrity provided later steps remove the violation允许中间步骤违反约束,只要最终结果不违反即可。 Otherwise it would be impossible to create some database
24、states, e.g. insert two tuples whose foreign keys point to each other (e.g. spouse配偶 attribute of relation marriedperson,17,Assertions,An assertion is a predicate expressing a condition that we wish the database always to satisfy.断言约束:不必与特定的列绑定,可以理解为能应用于多个表的check约束,因此必须在表定义之外独立创建断言。 An assertion in
25、SQL takes the form create assertion check drop assertion When an assertion is made, the system tests it for validity, and tests it again on every update that may violate the assertion This testing may introduce a significant amount of overhead; hence assertions should be used with great care. Assert
26、ing for all X, P(X) is achieved in a round-about fashion using not exists X such that not P(X,18,Assertion Example,The sum of all loan amounts for each branch must be less than the sum of all account balances at the branch. create assertion sum-constraint check (not exists (select * from branch wher
27、e (select sum(amount) from loan where loan.branch-name = branch.branch-name) = (select sum(amount) from account where account.branch-name = branch.branch-name,19,Assertion Example,Every loan has at least one borrower who maintains an account with a minimum balance or $1000.00设最小余额即$1000 create asser
28、tion balance-constraint check (not exists ( select * from loan where not exists ( select * from borrower, depositor, account where loan.loan-number = borrower.loan-number and borrower.customer-name = depositor.customer-name and depositor.account-number = account.account-number and account.balance =
29、1000,20,Triggers,A trigger is a statement that is executed automatically by the system as a side effect(副作用) of a modification to the database.触发器是一种特殊的存储过程它不能被显式地调用而是在往表中插入记录更新记录或者删除记录时被自动地激活。所以触发器可以用来实现对表实施复杂的完整性约束。 To design a trigger mechanism, we must: Specify the conditions under which the tri
30、gger is to be executed. Specify the actions to be taken when the trigger executes. event-condition-action model 事件条件动作模型 Triggers introduced to SQL standard in SQL:1999, but supported even earlier using non-standard syntax by most databases. Suppose that instead of allowing negative account balances
31、, the bank deals with overdrafts by setting the account balance to zero不是将余额设成负值 creating a loan in the amount of the overdraft giving this loan a loan number identical to the account number of the overdrawn account,21,Steps,insert a new tuple s in the loan relation with,sbranch-name=tbranch-name,in
32、sert a new tuple u in the borrower relation with,sloan-number=taccount-number,samount= - tbalance,ucustomer-name=“Jones,uloan-number=taccount-number,set tbalance to 0,22,Trigger Example in SQL:1999,create trigger overdraft-trigger after update on account referencing new row as nrow for each rowwhen
33、nrow.balance 0begin atomicinsert into borrower (select customer-name, account-number from depositor where nrow.account-number = depositor.account-number); insert into loan values(nrow.account-number, nrow.branch-name, nrow.balance); update account set balance = 0where account.account-number = nrow.a
34、ccount-numberend,23,Triggering Events and Actions in SQL,Triggering event can be insert, delete or update Triggers on update can be restricted to specific attributes E.g. create trigger overdraft-trigger after update of balance on account Values of attributes before and after an update can be refere
35、nced referencing old row as : for deletes and updates referencing new row as : for inserts and updates Triggers can be activated before an event, which can serve as extra constraints. 避免非法更新的额外约束 E.g. convert blanks to null. create trigger setnull-trigger before update on rreferencing new row as nro
36、wfor each row when nrow.phone-number = set nrow.phone-number = null,24,Statement Level Triggers,Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a single transaction Use for each statement instead of for each row Use referencing o
37、ld table or referencing new table to refer to temporary tables containing the affected rows temporary tables cant be used in before triggers.Can be more efficient when dealing with SQL statements that update a large number of rows,25,External World Actions,We sometimes require external world actions
38、, such as re-ordering an item whose quantity in a warehouse has become small, or turning on an alarm light, to be triggered on a database update Triggers cannot be used to directly implement external-world actions, BUT Triggers can be used to record actions-to-be-taken in a separate table, and we ca
39、n have an external process that repeatedly scans the table and carries out external-world actions. E.g. Suppose a warehouse has the following tables inventory(item, level): How much of each item is in the warehouse minlevel(item, level) : What is the minimum desired level of each item reorder(item,
40、amount): What quantity should we re-order for each item at a time orders(item, amount) : The quantity of order to be placed for each item (read by external process,26,External World Actions (Cont.,create trigger reorder-trigger after update of level on inventory referencing old row as orow, new row
41、as nrow for each row when nrow.level (select level from minlevel where minlevel.item = orow.item) begin insert into orders (select item, amount from reorder where reorder.item = orow.item) end 注意,如果只检查更新后的新值是否小于最小值,就可能在商品已经被订购的时候错误地下另一个订购单,新值比最小值更小,旧值满足最小值要求,27,Triggers in MS-SQL Server,SQL Server为每
42、个触发器都创建了两个专用表Inserted表和Deleted表。这两个表由系统来维护它们存在于内存中而不是在数据库中。这两个表的结构总是与被该触发器作用的表的结构相同。触发器执行完成后与该触发器相关的这两个表也被删除。 Deleted表存放由于执行Delete或Update语句而要从表中删除的所有行。 Inserted表存放由于执行Insert或Update语句而要向表中插入的所有行。 SQL Server2000提供了两种触发器Instead of 和After 触发器。 Instead of触发器用于替代引起触发器执行的T-SQL语句。除表之外Instead of 触发器也可以用于视图用来
43、扩展视图可以支持的更新操作。 After触发器在一个Insert,Update或Deleted语句之后执行进行约束检查等动作都在After触发器被激活之前发生。After触发器只能用于表。 一个表或视图的每一个修改动作(insert,update和delete)都可以有一个instead of 触发器一个表的每个修改动作都可以有多个After触发器。 触发器的执行过程 如果一个Insertupdate或者delete语句违反了约束那幺After触发器不会执行因为对约束的检查是在After触发器被激动之前发生的。所以After触发器不能超越约束。 Instead of 触发器可以取代激发它的操作
44、来执行。它在Inserted表和Deleted表刚刚建立其它任何操作还没有发生时被执行。因为Instead of 触发器在约束之前执行所以它可以对约束进行一些预处理,28,Triggers in MS-SQL Server Syntax,create trigger overdraft-trigger on account-在什么表中创建触发器 for update -为什么事件触发。如果仅指定 FOR 关键字,则 AFTER 为 -默认值。 也就是说, FOR = AFTER。 as -事件触发后所要做的事情 if nrow.balance 0begin insert into borrow
45、er (select customer-name,account-number from depositor, inserted where inserted.account-number = depositor.account-number) insert into loan values (inserted.account-number, inserted.branch-name, inserted.balance) update account set balance = 0 from account, inserted where account.account-number = in
46、serted.account-numberend,29,Security,Security - protection from malicious attempts to steal or modify data. Database system level Authentication and authorization mechanisms to allow specific users access only to required data We concentrate on authorization in the rest of this chapter Operating sys
47、tem level Operating system super-users can do anything they want to the database! Good operating system level security is required. Network level: must use encryption to prevent Eavesdropping窃听 (unauthorized reading of messages) Masquerading冒充 (pretending to be an authorized user or sending messages
48、 supposedly from authorized users,30,Security (Cont.,Physical level Physical access to computers allows destruction of data by intruders; traditional lock-and-key security is needed Computers must also be protected from floods, fire, etc. More in Chapter 17 (Recovery) Human level Users must be scree
49、ned to ensure that an authorized users do not give access to intruders Users should be trained on password selection and secrecy,31,Authorization,Forms of authorization on parts of the database: Read authorization - allows reading, but not modification of data. Insert authorization - allows insertio
50、n of new data, but not modification of existing data. Update authorization - allows modification, but not deletion of data. Delete authorization - allows deletion of data,32,Authorization (Cont.,Forms of authorization to modify the database schema: Index authorization - allows creation and deletion
51、of indices. Resources authorization - allows creation of new relations. Alteration authorization - allows addition or deletion of attributes in a relation. Drop authorization - allows deletion of relations,33,Authorization and Views,Users can be given authorization on views, without being given any
52、authorization on the relations used in the view definition Ability of views to hide data serves both to simplify usage of the system and to enhance security by allowing users access only to data they need for their job A combination or relational-level security and view-level security can be used to
53、 limit a users access to precisely the data that user needs,34,View Example,Suppose a bank clerk needs to know the names of the customers of each branch, but is not authorized to see specific loan information. Approach: Deny direct access to the loan relation, but grant access to the view cust-loan,
54、 which consists only of the names of customers and the branches at which they have a loan. The cust-loan view is defined in SQL as follows: create view cust-loan as select branchname, customer-name from borrower, loan where borrower.loan-number = loan.loan-number,35,View Example (Cont.,The clerk is
55、authorized to see the result of the query: select *from cust-loan When the query processor translates the result into a query on the actual relations in the database, we obtain a query on borrower and loan. Authorization must be checked on the clerks query before query processing begins,36,Authoriza
56、tion on Views,Creation of view does not require resources authorization since no real relation is being created The creator of a view gets only those privileges that provide no additional authorization beyond that he already had. E.g. if creator of view cust-loan had only read authorization on borro
57、wer and loan, he gets only read authorization on cust-loan,37,Granting of Privileges,The passage of authorization from one user to another may be represented by an authorization graph.权限图 The nodes of this graph are the users. The root of the graph is the database administrator. Consider graph for u
58、pdate authorization on loan. An edge Ui Uj indicates that user Ui has granted update authorization on loan to Uj,38,Authorization Graph,Requirement: All edges in an authorization graph must be part of some path originating with the database administrator If DBA revokes grant from U1: Grant must be r
59、evoked from U4 since U1 no longer has authorization Grant must not be revoked from U5 since U5 has another authorization path from DBA through U2 Must prevent cycles of grants with no path from the root: DBA grants authorization to U7 U7 grants authorization to U8 U8 grants authorization to U7 DBA r
60、evokes authorization from U7 Must revoke grant from U7 to U8 and from U8 to U7 since there is no path from DBA to U7 or to U8 anymore,39,Attempt to Defeat Authorization Revocation,40,Security Specification in SQL,The grant statement is used to confer authorization grant on to is: a user-id public, w
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 团队项目管理模板跨部门协作任务分配表
- 2025-2030加油站便利店饮料品类结构优化指南
- 作业安全培训试题及答案解析
- 2025-2030制造业行业市场自动化进程与投资机会研究报告
- 2025-2030冷链预制菜微生物控制技术创新与保质期延长经济效益
- 香港证券期货从业资格考试卷1及答案解析
- 房建安全员上岗证题库及答案解析
- 基金从业考试上午场及答案解析
- 企业管理规范化保证承诺书(3篇)
- 2025-2030冷链物流温控技术突破与食品安全保障体系建设报告
- (外研版2019)高考英语一轮单元复习课件必修1 Unit 1A new start(含详解)
- 最新交管12123学法减分考试题库及答案大全
- 幼儿成长档案电子通用版
- 短视频:策划+拍摄+制作+运营课件(完整版)
- 首都师范大学本科生重修课程自学申请表
- 第四章路面施工.ppt
- mr9270s文件包中文说明书
- 机械制造技术基础-CA6140的传动系统分析
- HIV-1病毒载量测定及质量保证指南
- Wiley数据库使用方法(课堂PPT)
- 蚀刻液周知卡
评论
0/150
提交评论