Download presentation
Presentation is loading. Please wait.
Published byStanley Potter Modified over 8 years ago
1
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Relational State Assertions These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see http://www.openlineconsult.com/db
2
2 © Ellis Cohen 2001-2008 Overview of Lecture Relational State Assertions Assertions with EXISTS Comparing Vector Relations and Scalar Values Comparing Relations Uniqueness Assertions
3
3 © Ellis Cohen 2001-2008 Relational State Assertions
4
4 © Ellis Cohen 2001-2008 Relational Mapping Conceptual ER Model (e.g. UML/Crow Magnum) Conceptual State Constraints (refers to ER Model) Relational Schema Relational State Constraints (refers to Relational Schema) Conceptual Model Relational Model
5
5 © Ellis Cohen 2001-2008 Relational State Constraints Check Constraints (CHECK, NOT NULL) Ensures that each tuple in a table satisfies a condition Uniqueness Constraints (UNIQUE, PRIMARY KEY) Ensures that all values in a column are unique Foreign Key Constraints (REFERENCES) Ensures that a value in one column (or group of columns) matches a value in a referenced unique column (or group of columns) Relational State Assertions (ASSERTION) Specifies an arbitrary condition (possibly involving multiple tables) that should remain satisfied
6
6 © Ellis Cohen 2001-2008 Relational State Assertions in SQL A relational state assertion is a kind of relational state constraint expressed as an arbitrary query- based boolean condition applied to the relational model Example: There are at least 5 clerks in department 10 (SELECT count(*) FROM Emps WHERE job = 'CLERK' AND deptno = 10) >= 5 Conceptual State Constraint Corresponding Relational State Assertion:
7
7 © Ellis Cohen 2001-2008 CREATE ASSERTION CREATE ASSERTION assertion-name CHECK( boolean-expr ) CREATE ASSERTION LotsOfClerks CHECK( (SELECT count(*) FROM Emps WHERE job = 'CLERK' AND deptno = 10) >= 5) is part of the SQL-92 standard, but is not currently supported by commercial databases. So, we will have to find other ways to enforce state assertions
8
8 © Ellis Cohen 2001-2008 Enforcing State Assertions There are two ways to enforce state assertions that can't be directly supported in SQL Database Enforcement: Use triggers (in the data-tier) to enforce assertions Application Enforcement: Enforce assertions through code implemented in user operations (in the middle-tier) or stored DB operations (in the data-tier)
9
9 © Ellis Cohen 2001-2008 Writing Assertions Even though most databases do not support CREATE ASSERTION, it is still useful to write state assertions for conceptual state constraints that can't be implemented using built-in SQL constraints. Why write state assertions? To take ambiguously stated conceptual constraints and generate something clear and unambiguous (though harder to read) To take a step that will ultimately make it easier to code the constraint's implementation.
10
10 © Ellis Cohen 2001-2008 SQL with State Assertions CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) not null ) CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30) not null, streetvarchar(40), cityvarchar(20), statechar(2) check (state <> 'CA'), zipchar(5), deptnonumber(3) references Depts, mgrnumber(4) references Emps, check (empno <> mgr) ) CREATE ASSERTION OneBiggie CHECK( (SELECT count(*) FROM Emps WHERE sal > 4500) = 1 ) What does it mean?
11
11 © Ellis Cohen 2001-2008 OneBiggie OneBiggie: (SELECT count(*) FROM Emps WHERE sal > 4500) = 1 There is exactly one employee who makes more than 4500
12
12 © Ellis Cohen 2001-2008 SQL Value Coercion (SELECT count(*) FROM Emps WHERE deptno = 10 AND job = 'CLERK') is actually a scalar result set (a.k.a. a scalar relation): a relation with a single attribute and a single tuple (SELECT count(*) FROM Emps WHERE deptno = 10 AND job = 'CLERK') ≥ 5 8 How can we compare the relation to 5? 8 ≥ 5 SQL automatically coerces scalar relations to values when necessary 8 ≥ 5
13
13 © Ellis Cohen 2001-2008 A Distinct Count Problem What's the meaning of (SELECT count(DISTINCT deptno) FROM Emps WHERE job = 'SALESMAN') < 3
14
14 © Ellis Cohen 2001-2008 SQL Assertion Answer No more than 2 departments have salesmen (SELECT count(DISTINCT deptno) FROM Emps WHERE job = 'SALESMAN') < 3 Note that this does NOT indicate the number of salesman, just the # of depts that have (1 or more) salesmen
15
15 © Ellis Cohen 2001-2008 Comparing Counts Depts( deptno, dname, loc ) Emps( empno, ename, street, city, state, zip, deptno, mgr ) check( empno <> mgr) deptno references Depts DeptEmps: (SELECT count(DISTINCT deptno) FROM Emps) >= (SELECT count(*) FROM Depts) What does DeptEmps mean?
16
16 © Ellis Cohen 2001-2008 Mandatory Parent Participation (SELECT count(DISTINCT deptno) FROM Emps) >= (SELECT count(*) FROM Depts) The # of depts with employees >= the # of depts (Assuming referential integrity ensures that every employee department is in Depts) Every department has at least one employee DeptEmployee works for deptno dname empno ename addr
17
17 © Ellis Cohen 2001-2008 Assertions with EXISTS
18
18 © Ellis Cohen 2001-2008 Existence Problem Consider the constraint There is at least one employee whose (weekly) salary is more than $4000 What's the corresponding assertion?
19
19 © Ellis Cohen 2001-2008 Existence Solution (SELECT count(*) FROM Emps WHERE sal > 4000) > 0 There is at least one employee whose (weekly) salary is more than $4000
20
20 © Ellis Cohen 2001-2008 Using EXISTS The company has at least one analyst (SELECT count(*) FROM Emps WHERE job = 'ANALYST') > 0 can also be written using the built-in SQL function EXISTS EXISTS( SELECT * FROM Emps WHERE job = 'ANALYST' ) which is true if evaluation of the SELECT results in at least one tuple
21
21 © Ellis Cohen 2001-2008 Using NOT EXISTS The company has no peons (SELECT count(*) FROM Emps WHERE job = 'PEON') = 0 can also be written using NOT along with the EXISTS function NOT EXISTS( SELECT * FROM Emps WHERE job = 'PEON' ) which is true if evaluation of the SELECT results in zero tuples
22
22 © Ellis Cohen 2001-2008 NOT EXISTS & CHECK NOT EXISTS used with a simple SELECT can be checked a tuple at a time It corresponds to a CHECK constraint! NOT EXISTS( SELECT * FROM Emps WHERE job = 'PEON' ) CHECK( job != 'PEON )
23
23 © Ellis Cohen 2001-2008 Simple NOT EXISTS Problem Consider the assertion NOT EXISTS( SELECT * FROM Emps WHERE job = 'DEPTMGR' AND sal < 1500 ) Rewrite this as a CHECK constraint
24
24 © Ellis Cohen 2001-2008 Simple NOT EXISTS Solution NOT EXISTS( SELECT * FROM Emps WHERE job = 'DEPTMGR' AND sal < 1500 ) CHECK( job != 'DEPTMGR' OR sal >= 1500 )
25
25 © Ellis Cohen 2001-2008 Complex NOT EXISTS NOT EXISTS( SELECT deptno FROM Emps WHERE job = 'DEPTMGR' GROUP BY deptno HAVING count(*) > 1 ) What does this mean? Can it be turned into a CHECK constraint?
26
26 © Ellis Cohen 2001-2008 Complex NOT EXISTS Solution No dept has more than 1 dept manager Cannot be expressed as a CHECK constraint NOT EXISTS( SELECT deptno FROM Emps WHERE job = 'DEPTMGR' GROUP BY deptno HAVING count(*) > 1 )
27
27 © Ellis Cohen 2001-2008 Comparing Vector Relations & Scalar Values
28
28 © Ellis Cohen 2001-2008 Assertion Question Suppose we want to assert that Every dept manager makes 1500 or more (assuming every employee has a job & sal) Can we write (SELECT sal FROM Emps WHERE job = 'DEPTMGR') >= 1500
29
29 © Ellis Cohen 2001-2008 Cannot Coerce Vector Relations 2975 2850 2450 sal 2975 2850 2450 sal ≥ 1500 Can't compare or coerce vector relations to values This is a vector relation: a relation with a single attribute and possibly multiple rows SELECT sal FROM Emps WHERE job = 'DEPTMGR'
30
30 © Ellis Cohen 2001-2008 Comparing Vectors to Values In comparing a vector relation to a scalar, there are (at least) two reasonable comparisons you might want to make 2975 2850 2450 sal ? ≥ 1500 1.Are all of the sal values 1500 or more 2.Is there any sal value that is 1500 or more Rather than choosing one of these as the meaning of ≥ we augment the comparison to explicitly specify one of them
31
31 © Ellis Cohen 2001-2008 ALL Comparisons 2975 2850 2450 sal Are all of the salaries ≥ 1500? (SELECT sal FROM Emps WHERE job = 'DEPTMGR') ALL >= 1500 – NOT STANDARD SQL, but we'll use it informally when writing assertions 1500 <= ALL (SELECT sal FROM Emps WHERE job = 'DEPTMGR') – STANDARD SQL ! How else could this be written in SQL?
32
32 © Ellis Cohen 2001-2008 ALL SQL Alternatives (SELECT sal FROM Emps WHERE job = 'DEPTMGR') ALL >= 1500 All deptmgr salaries ≥ 1500 1500 <= ALL (SELECT sal FROM Emps WHERE job = 'DEPTMGR') NOT EXISTS( SELECT * FROM Emps WHERE job = 'DEPTMGR' AND sal < 1500 ) (SELECT min(sal) FROM Emps WHERE job = 'DEPTMGR') ≥ 1500
33
33 © Ellis Cohen 2001-2008 ANY Comparisons 2975 2850 2450 sal Are any of the salaries ≥ 1500? (SELECT sal FROM Emps WHERE job = 'DEPTMGR') ANY >= 1500 – NOT STANDARD SQL, but we'll use it informally when writing assertions 1500 <= ANY (SELECT sal FROM Emps WHERE job = 'DEPTMGR') – STANDARD SQL ! How else could this be written in SQL?
34
34 © Ellis Cohen 2001-2008 ANY SQL Alternatives (SELECT sal FROM Emps WHERE job = 'DEPTMGR') ANY >= 1500 There are some deptmgr salaries ≥ 1500 1500 <= ANY (SELECT sal FROM Emps WHERE job = 'DEPTMGR') EXISTS( SELECT * FROM Emps WHERE job = 'DEPTMGR' AND sal >= 1500 ) (SELECT max(sal) FROM Emps WHERE job = 'DEPTMGR') ≥ 1500
35
35 © Ellis Cohen 2001-2008 Vector Comparison Problems What's the meaning of (SELECT count(*) FROM Emps WHERE sal > 3000 GROUP BY deptno) ALL <= 1 (SELECT count(DISTINCT job) FROM Emps GROUP BY deptno) ALL <= 5
36
36 © Ellis Cohen 2001-2008 Vector Comparison Answer (SELECT count(DISTINCT job) FROM Emps GROUP BY deptno) ALL <= 5 No department has more than 5 different kinds of jobs (SELECT count(*) FROM Emps WHERE sal > 3000 GROUP BY deptno) ALL <= 1 No department has more than 1 employee who makes > 3000 (Some depts might not have any such employee)
37
37 © Ellis Cohen 2001-2008 Comparing Relations
38
38 © Ellis Cohen 2001-2008 Mandatory Parent Participation Question 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…10 Emps Does every department have an employee?
39
39 © Ellis Cohen 2001-2008 Mandatory Parent Participation Testing 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…10 Emps 30 10 50 deptno Known Departments SELECT deptno FROM Depts deptno 30 10 Departments that have employees SELECT DISTINCT deptno FROM Emps ≠
40
40 © Ellis Cohen 2001-2008 Relation Equality R1 = R2 Two relationships are equal if They have the same attribute names (which are unordered) They have identical collections of tuples (which are unordered) If R1 has duplicate copies of a tuple, then R2 has the same number of copies of the same tuple (and vice versa) NOT Standard SQL, but commonly used informally for writing relational assertions
41
41 © Ellis Cohen 2001-2008 Specifying Mandatory Parent Participation (SELECT DISTINCT deptno FROM Emps WHERE deptno IS NOT NULL) =(SELECT deptno FROM Depts) DeptEmployee employs (SELECT DISTINCT deptno FROM Emps) =(SELECT deptno FROM Depts) DeptEmployee employs
42
42 © Ellis Cohen 2001-2008 Cardinality Constraints (SELECT deptno FROM Emps WHERE deptno IS NOT NULL GROUP BY deptno HAVING count(*) BETWEEN 2 AND 5) =(SELECT deptno FROM Depts) DeptEmployee employs 2..5
43
43 © Ellis Cohen 2001-2008 Relation Comparisons R1 R2 (or equivalently R2 R1) They have the same attribute names, and every tuple that is in R1 is also in R2 (including at least as many copies of each duplicate) [OK to type this as R1 <= R2] R1 = R2 (R1 R2) and (R2 R1) R1 and R2 have the same tuples (and the number of duplicates match) R1 != R2 not (R1 = R2) R1 R2 (or equivalently R2 R1) (R1 R2) and (R1 != R2) [can type R1 < R2] The Relation Comparison Operators are NOT part of standard SQL
44
44 © Ellis Cohen 2001-2008 State Assertion Exercise (SELECT DISTINCT depnto FROM Emps) (SELECT deptno FROM Depts) Assuming that every employee is assigned to a department, What's the meaning of this state assertions?
45
45 © Ellis Cohen 2001-2008 State Assertion Answer 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7844TURNER…30 7212LAVICH…30 7698BLAKE…10 7986STERN…10 Emps Every dept # in Emps (assuming no nulls) is also included in Depts SELECT DISTINCT depnto FROM Emps 30 10 30 10 50 SELECT deptno FROM Depts
46
46 © Ellis Cohen 2001-2008 Referential Integrity Constraints Foreign Key Constraint (SELECT DISTINCT depnto FROM Emps WHERE deptno IS NOT NULL) (SELECT deptno FROM Depts) Referential Integrity Constraint (SELECT DISTINCT hiredate FROM Emps WHERE hiredate IS NOT NULL) (SELECT DISTINCT startdate FROM Projs)
47
47 © Ellis Cohen 2001-2008 Comparing Assertions = (SELECT deptno FROM Depts) = (SELECT DISTINCT depnto FROM Emps WHERE job = 'CLERK') (SELECT deptno FROM Depts) (SELECT depnto FROM Emps WHERE job = 'CLERK') Assuming that every employee is assigned to a department, What's the difference between these two state assertions?
48
48 © Ellis Cohen 2001-2008 Answer Comparing Assertions Every department has a clerk (Assuming every employee in Emps is assigned to a department) = (SELECT deptno FROM Depts) = (SELECT DISTINCT depnto FROM Emps WHERE job = 'CLERK') (SELECT deptno FROM Depts) (SELECT depnto FROM Emps WHERE job = 'CLERK') Rewrite the assertions if employees can be unassigned Both have the same meaning!
49
49 © Ellis Cohen 2001-2008 Answer with Unassigned Employees Every department has a clerk = (SELECT deptno FROM Depts) = (SELECT DISTINCT depnto FROM Emps WHERE job = 'CLERK' AND deptno IS NOT NULL) (SELECT deptno FROM Depts) (SELECT depnto FROM Emps WHERE job = 'CLERK')
50
50 © Ellis Cohen 2001-2008 Implementing Relational Comparison If A and B are both tables and –they have the same attributes –they do not have any duplicates tuples –no tuples have NULL values Then A B is equivalent to (SELECT count(*) FROM (A NATURAL JOIN B) = (SELECT count(*) FROM A)
51
51 © Ellis Cohen 2001-2008 Uniqueness Assertions
52
52 © Ellis Cohen 2001-2008 Uniqueness Exercise Assuming every employee is assigned to a department What's the meaning of (SELECT DISTINCT deptno FROM Emps WHERE sal > 3000) = (SELECT deptno FROM Emps WHERE sal > 3000)
53
53 © Ellis Cohen 2001-2008 Unique Relation Comparison (SELECT DISTINCT deptno FROM Emps WHERE sal > 3000) = (SELECT deptno FROM Emps WHERE sal > 3000) -- assuming every employee assigned The depts of employees w sal > 3000 = The distinct depts of employees w sal > 3000 No dept has more than one employee with a salary > 3000
54
54 © Ellis Cohen 2001-2008 Using the UNIQUE Function UNIQUE is standard SQL, but not widely implemented (not in Oracle) When writing assertions, use UNIQUE UNIQUE(T) means (SELECT DISTINCT * FROM T) = (SELECT * FROM T) That is, T does not have duplicate tuples How would you use UNIQUE to assert: No department has more than one employee who makes > 3000?
55
55 © Ellis Cohen 2001-2008 Solution with Unique UNIQUE( SELECT deptno FROM Emps WHERE sal > 3000 ) -- assumes every employee assigned to a department UNIQUE( SELECT deptno FROM Emps WHERE sal > 3000 AND deptno IS NOT NULL ) -- no assumption needed No department has more than one employee who makes > 3000
56
56 © Ellis Cohen 2001-2008 Alternate Solutions (SELECT count(*) FROM Emps WHERE sal > 3000 GROUP BY deptno) ALL = 1 NOT EXISTS( SELECT deptno FROM Emps WHERE sal > 3000 GROUP BY deptno HAVING count(*) > 1 ) (SELECT DISTINCT deptno FROM Emps WHERE sal > 3000 AND deptno IS NOT NULL) = (SELECT deptno FROM Emps WHERE sal > 3000 AND deptno IS NOT NULL) -- uses relational comparison (non-standard) No department has more than one employee who makes > 3000
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.