<<

Data and Its Structure

Data is actually stored as bits, but it is difficult to The Relational work with data at this level. It is convenient to data at different levels of abstraction. Chapter 3 Schema: Description of data at some abstraction level. Each level has its own schema. We will be concerned with three schemas: physical, conceptual, and external.

1 2

Physical Data Level Conceptual Data Level

Physical schema describes details of how data is Hides details. stored: tracks, cylinders, indices etc. In the , the conceptual schema presents Early applications worked at this level data as a set of tables. explicitly dealt with details. DBMS maps conceptual to physical schema automatically. Problem: Routines were hard-coded to deal with Physical schema can be changed without changing physical representation. application: Changes to difficult to make. DBMS would change mapping from conceptual to Application code becomes complex since it must deal physical transparently with details. This property is referred to as physical data independence Rapid implementation of new features impossible. 3 4

External Data Level Conceptual Data Level (con t) In the relational model, the external schema also presents data as a set of relations.

Conceptual view An external schema specifies a view of the data of data Application in terms of the conceptual level. It is tailored to the needs of a particular category of users. Portions of stored data should not be seen by some users. Students should not see their files in full. Physical view of data Faculty should not see billing data. DBMS Information that can be derived from stored data might be viewed as if it were stored. GPA not stored, but calculated when needed. 5 6

1

External Data Level (con t) Levels of Abstraction Application is written in terms of an external schema. billing A view is computed when accessed (not stored). payroll records Different external schemas can be provided to different View 1 View 2 View 3 External categories of users. schemas Translation from external to conceptual done automatically by DBMS at run time. Conceptual schema Conceptual schema can be changed without changing application: Physical schema Mapping from external to conceptual must be changed. Referred to as conceptual data independence. 7 8

Data Model Schema: description of data at some level Relational Model (e.g., tables, attributes, constraints, domains) A particular way of structuring data (using Model: tools and language for describing: relations) Conceptual and external schema Simple (DDL) Mathematically based Integrity constraints, domains (DDL) Expressions ( queries) can be analyzed by DBMS Operations on data Queries are transformed to equivalent expressions Data manipulation language (DML) automatically () Directives that influence the physical schema Optimizers have limits (=> programmer needs to know (affects performance, not semantics) how queries are evaluated and optimized) Storage definition language (SDL)

9 10

Relation Instance Instance (Example) Relation is a set of ordering immaterial Id Name Address Status No duplicates Cardinality of relation = number of tuples 11111111 John 123 Main freshman All tuples in a relation have the same structure; constructed from the same set of attributes 12345678 Mary 456 Cedar sophmore Attributes are named (ordering is immaterial) Value of an attribute is drawn from the attribute s domain 44433322 Art 77 So. 3rd senior There is also a special value (value unknown or undefined), which belongs to no domain 87654321 Pat 88 No. 4th sophmore Arity of relation = number of attributes Student 11 12

2 Relation Schema Relational

Relation name Finite set of relations Attribute names & domains Each relation consists of a schema and an Integrity constraints like instance The values of a particular attribute in all tuples = set of relation schemas are unique constraints among relations (inter-relational The values of a particular attribute in all tuples constraints) are greater than 0 Database instance = set of (corresponding) Default values relation instances

13 14

Database Schema (Example) Integrity Constraints

Student (Id: INT, Name: STRING, Address: STRING, Part of schema Status: STRING) Restriction on state (or of sequence of states) of Professor (Id: INT, Name: STRING, DeptId: DEPTS) data base Course (DeptId: DEPTS, CrsName: STRING, Enforced by DBMS CrsCode: COURSES) Intra-relational - involve only one relation Transcript (CrsCode: COURSES, StudId: INT, Part of relation schema e.g., all Ids are unique Grade: GRADES, Semester: SEMESTERS) Inter-relational - involve several relations Department(DeptId: DEPTS, Name: STRING) Part of relation schema or database schema

15 16

Constraint Checking Kinds of Integrity Constraints

Automatically checked by DBMS Static restricts legal states of database Protects database from errors Syntactic (structural) Enforces enterprise rules e.g., all values in a must be unique Semantic (involve meaning of attributes) e.g., cannot register for more than 18 credits Dynamic limitation on sequences of database states e.g., cannot raise salary by more than 5%

17 18

3 Key Constraint A key constraint is a sequence of attributes Key Constraint (cont d) A1, ,An (n=1 possible) of a relation schema, S, with the following property: - set of attributes containing key A relation instance s of S satisfies the key constraint iff at most one in s can contain a particular set of (Id, Name) is a superkey of Student values, a1, ,an, for the attributes A1, ,An Every relation has a key Minimality: no subset of A1, ,An is a key constraint Key Relation can have several keys: Set of attributes mentioned in a key constraint : Id in Student (can t be null) e.g., Id in Student, : (Name, Address) in Student e.g., (StudId, CrsCode, Semester) in Transcript It is minimal: no subset of a key is a key (Id, Name) is not a key of Student 19 20

Foreign Key Constraint Constraint : Item named in one relation must refer to tuples that describe that item in another (Example) Transcript (CrsCode) references Course(CrsCode ) Professor(DeptId) references Department(DeptId) A2 Attribute A1 is a foreign key of R1 referring to attribute A2 in R2, A1 v3 v1 v5 if whenever there is a value v of A1, there is a tuple of R2 in which A has value v, and A is a key of R2 v2 v1 2 2 v3 v6 This is a special case of referential integrity: A must be a candidate key 2 v4 v2 of R2 (e.g., CrsCode is a key of Course in the above) R2 Course null v7 If no row exists in R2 => violation of referential integrity v3 v4 Not all rows of R2 need to be referenced: relationship is not symmetric R1 R2 (e.g., some course might not be taught) Foreign key Value of a foreign key might not be specified (DeptId column of some Candidate key professor might be null)

21 22

Foreign Key (cont d) Foreign Key (cont d) Foreign key might consist of several columns

Names of the attrs A1 and A2 need not be the same. (CrsCode, Semester) of Transcript references With tables: (CrsCode, Semester) of Teaching Teaching(CrsCode: COURSES, Sem: SEMESTERS, ProfId: INT) Professor(Id: INT, Name: STRING, DeptId: DEPTS) R1(A1, An) references R2(B1, Bn) ProfId attribute of Teaching references Id attribute of Professor Ai and Bi must have same domains (although not R1 and R2 need not be distinct. necessarily the same names) Employee(Id:INT, MgrId:INT, .) B1, ,Bn must be a candidate key of R2 Employee(MgrId) references Employee(Id) Every manager is also an employee and hence has a unique row in Employee

23 24

4 Inclusion Dependency SQL Referential integrity constraint that is not a foreign key constraint Language for describing database schema Teaching(CrsCode, Semester) references and operations on tables Transcript(CrsCode, Semester) Data Definition Language (DDL): (no empty classes allowed) of SQL for describing schema Target attributes do not form a candidate key in Transcript (StudId missing) No simple enforcement mechanism for inclusion dependencies in SQL (requires assertions -- later)

25 26

Table Declaration

Tables Student ( Id: , SQL entity that corresponds to a relation Name: (20), Address: (50), An element of the database schema Status: 10) SQL-92 is currently the most supported ) standard but is now superseded by Id Name Address Status SQL:1999 and SQL:2003 101222333 John 10 Cedar St Freshman Database vendors generally deviate from 234567890 Mary 22 Main St Sophomore the standard, but eventually converge

27 Student 28

Primary/Candidate Keys Null

Course ( Problem: Not all information might be known when CrsCode (6), row is inserted (e.g., Grade might be missing from CrsName (20), Transcript) DeptId (4), A column might not be applicable for a particular Descr (100), row (e.g., MaidenName if row describes a male) (CrsCode), Solution: Use place holder null DeptId, CrsName) -- candidate key Not a value of any domain (although called null value) ) Indicates the absence of a value Not allowed in certain situations Comments start with 2 dashes Primary keys and columns constrained by

29 30

5 Default Value Semantic Constraints in SQL

-Value to be assigned if attribute value in a row Primary key and foreign key are examples is not specified of structural constraints Semantic constraints

Student ( Express the logic of the application at hand: Id , e.g., number of registered students maximum Name , enrollment Address (50), Status freshman , (Id) )

31 32

Semantic Constraints (cont d) Semantic Constraints (cont d) Used for application dependent conditions Example: limit attribute values Example: relate values of attributes in different columns Transcript ( StudId Employee ( CrsCode (6), Id , Semester (6), Name (20), Grade (1), Salary (Grade ( A , B , C , D , F )), MngrSalary (StudId > 0 StudId < 1000000000) ) ( MngrSalary > Salary) ) Each row in must satisfy 33 34

Constraints Problems Constraints Problems

Problem 1: Empty table always satisfies all Problem 2: Inter-relational constraints should be constraints (an idiosyncrasy of the SQL symmetric standard) Employee ( Employee ( Id , Id Name (20), Name (20), Salary , Salary MngrSalary MngrSalary , Manager) < Employee)) ) Employee)) ) Why should constraint be in Employee an not Manager? If Employee is empty, there are no rows on which to What if Employee is empty? evaluate the condition.

35 36

6 Assertion Assertion Element of schema (like table) Symmetrically specifies an inter-relational constraint KeepEmployeeSalariesDown Applies to entire database (not just the individual rows of a single table) * Employee E E.Salary > E.MngrSalary)) hence it works even if Employee is empty

DontFireEveryone Employee)

37 38

Assertions and Inclusion Dependency Domains Possible attribute values can be specified NoEmptyCourses Using a constraint or Teaching T Creating a new domain -- for each row T check Domain can be used in several declarations -- the following condition Domain is a schema element Transcript R Grades (1) Courses with T.CrsCode = R.CrsCode ( A , B , C , D , F )) no students T.Semester = R.Semester) Transcript ( ) ) ., Students in a Grade: Grades, particular course ) 39 40

Foreign Key Constraint Foreign Key Constraint

CrsCode Teaching ( ProfId CrsCode ProfId x CrsCode (6), Semester (6), x y (CrsCode, Semester), Course (CrsCode) Course, (ProfId) Professor (Id) ) Id Teaching

y Professor 41 42

7 Circularity in Foreign Key Constraint (cont d) Circularity in Foreign Key Constraint Problem 2: Insertion of row in A requires prior

A1 A2 A3 B1 B2 B3 existence of row in B and vice versa Solution: use appropriate constraint checking mode: A y x x y B IMMEDIATE checking DEFERRED checking candidate key: A1 candidate key: B1 foreign key: A3 references B(B1) foreign key: B3 references A(A1)

Problem 1: Creation of A requires existence of B and vice versa Solution: A ( ) -- no foreign key B ( ) -- include foreign key A cons

(A3) B (B1) 43 44

Handling Foreign Key Violations Reactive Constraints Insertion into A: Reject if no row exists in Constraints enable DBMS to recognize a bad state B containing foreign key of inserted row and reject the statement or transaction that creates it Deletion from B: More generally, it would be nice to have a B mechanism that allows a user to specify how to : Reject if row(s) in A references react to a violation of a constraint row to be deleted (default response) SQL-92 provides a limited form of such a reactive mechanism for foreign key violations A x B x ? Request to 45 row rejected 46

Handling Foreign Key Violations Handling Foreign Key Violations (cont d) (cont d)

Deletion from B (cont d): Deletion from B (cont d): : Set value of foreign key in : Set value of foreign key in referencing row(s) in A to null referencing row(s) in A to default value (y) which must exist in B

y B A B A null y x x

Row Row deleted deleted 47 48

8 Handling Foreign Key Violations Handling Foreign Key Violations (cont d) (cont d) Update (change) foreign key in A: Reject if no row exists in B containing new foreign key Deletion from B (cont d): Update candidate key in B (to z) same actions as : Delete referencing row(s) in A as well with deletion: : Reject if row(s) in A references row to be updated (default response) A B : Set value of foreign key to null x x : Set value of foreign key to default Cascading when : Propagate z to foreign key key in B changed from x to z

A z B 49 z 50

Handling Foreign Key Violations Specifying Actions (cont d) Teaching ( The action taken to repair the violation of a ProfId , foreign key constraint in A may cause a CrsCode (6), Semester (6), violation of a foreign key constraint in C (CrsCode, Semester), The action specified in C controls how that violation is handled; (ProfId) Professor (Id) If the entire chain of violations cannot be resolved, the initial deletion from B is rejected. (CrsCode) Course (CrsCode)

y ) y x x

C A B 51 52

Triggers Views A more general mechanism for handling Schema element events Part of external schema Not in SQL-92, but is in SQL:1999 Trigger is a schema element (like table, A virtual table constructed from actual tables on assertion, ) the fly Can be accessed in queries like any other table

CrsChange Not materialized, constructed when accessed CrsCode, Semester Transcript Similar to a subroutine in ordinary programming Grade

53 54

9 Views - Examples Modifying the Schema Part of external schema suitable for use in Bursar s office:

CoursesTaken (StudId, CrsCode, Semester) Student T.StudId, T.CrsCode, T.Semester Gpa 0 Transcript T Student GpaRange Part of external schema suitable for student with Id 123456789: (Gpa >= 0 AND Gpa <= 4)

CoursesITook (CrsCode, Semester, Grade) Transcript T.CrsCode, T.Semester, T.Grade Cons -- constraint names are useful Transcript T T.StudId = 123456789 Employee

DontFireEveryone 55 56

Access Control Controlling Authorization in SQL access_list table might contain sensitive information user_list Access has to be limited: access modes: User Users have to be identified authentication name Generally done with passwords (Grade) Transcript prof_smith Each user must be limited to modes of access Only the Grade column can be updated by prof_smith appropriate to that user - authorization Transcript joe SQL:92 provides tools for specifying an Individual columns cannot be specified for access (in the authorization policy but does not support SQL standard) all columns of Transcript can be read But access control to individual columns can be simulated authentication (vendor specific) through views (next)

57 58

Controlling Authorization in Authorization Mode SQL Using Views Foreign key constraint enforces relationship between tables that can be exploited to access Control access: can enable perpetrator prevent deletion view of rows user_list DontDismissMe ( Id CoursesTaken joe (Id) Student ) Thus views can be used to simulate access control to individual columns of a table Reveal information: successful insertion into DontDissmissMe means a row with foreign key value exists in Student

DontDismissMe ( 111111111 ) 59 60

10 REFERENCE Access mode (cont d)

GRANT REFERENCES ON Student TO joe

61

11 This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.