Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.

Similar presentations


Presentation on theme: "Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I."— Presentation transcript:

1 Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I

2 Outlines 1) Relational Database 2) Relational Data Model 3) Relational Database Constraints and Relational Database Schemas 4) Update Operations on Relations 5) Relational Algebra

3 Relational Database  Definition (by C.J. Date) “ A relational database is a database that is perceived by its users as a collection of tables. It is not just a database in which the data is physically stored as tables. ” Note: Relation is a mathematical term for a table.

4 Relational Data Model  The relational model of data is based on the concept of a Relation.  A relation is a mathematical concept based on the ideas of sets.  The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.  We review the essentials of the relational approach in this chapter.  Concepts

5 Relational Data Model RELATION: A table of values  What is “Relation” ?  Each column typically is called by its column name or column header or attribute name.  Sometimes row-ids or sequential numbers are assigned to identify the rows in the table.  Each row has a value of an item or set of items that uniquely identifies that row in the table.  Each row represents a fact that corresponds to a real-world entity or relationship.  A relation may alternately be thought of as a set of columns.  A relation may be thought of as a set of rows.

6 Relational Data Model  A relation is a named, 2-dimentional table of data. Not all tables qualify as relation.  Relation properties  Every relation has a unique name.  Each column has a unique name.  No 2 rows are identical.  Ordering of rows is not significant.  Ordering of columns is not significant.  All attribute values are atomic. (There always exists precisely one value, never a set of values)  Column values are of the same kind.

7 Relational Data Model  For Example: CUSTOMER (Cust-id, Cust-name, Address, Phone#)  Relation schema R (A 1, A 2,.....A n ) R is the name of relation. Degree of relation is the numbers of attributes of its relation schema. Relation schema R is defined over attributes A 1, A 2,.....A n CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values. Degree of relation is 4.

8 Relational Data Model  For Example: Cust-id : The set of valid 6-digit numbers. Cust-name : The set of customer names. Address : The set of home address where customers live. Phone# : The set of 10-digit phone numbers valid in Thailand.  Domain “ A domain D is a set of atomic values, all of the same type. ” All values in a column come from the same domain.

9 Relational Data Model  A domain has a logical definition. e.g., “USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S.  A domain may have a data-type or a format defined for it. e.g., The USA_phone_numbers may have a format: (ddd)-ddd-dddd where each d is a decimal digit. e.g., Dates have various formats such as monthname, date, year or yyyy-mm-dd, or dd mm,yyyy etc.  An attribute designates the role played by the domain. e.g., The domain Date may be used to define attributes “Invoice-date” and “Payment-date”.  Domain (Continued)

10 Relational Data Model  Each attribute in the model should be assigned domain information which includes: - Data type - Length - Data format (e.g., Date format is dd/mm/yy) - Range - Constraints (special restrictions on allowable values) - Null support - Default value (if any)  Domain (Continued)

11 Relational Data Model  A relation instance, r, of the relation schema  Relation instance R (A 1, A 2,.....A n ), denoted by r(R) is a mathematical relation of degree n on the domains dom(A 1 ), dom(A 2 ),…, dom(A n ), which is the subset of the Cartesian Product of the domains that define R. R is also called the intension of a relation. r is also called the extension of a relation.

12 Relational Data Model  A tuple is an ordered set of values.  Tuple  Columns in a table are also called attributes of the relation.  A relation may be regarded as a set of tuples (rows).  is a tuple belonging to the CUSTOMER relation.  Each row in the CUSTOMER table may be referred to as a tuple in the table and would consist of four values.  Each value is derived from an appropriate domain.

13 Relational Data Model We refer to component values of a tuple t by t[A i ] = v i (the value of attribute A i for tuple t). Similarly, t[A u, A v,..., A w ] refers to the subtuple of t containing the values of attributes A u, A v,..., A w, respectively.  Tuple (Continued)

14 Relational Data Model  Let S 1 = {0,1}  Let S 2 = {a,b,c}  Let  Then for example: is one possible ‘state’ or ‘population’ or ‘extension’ r of the relation R, defined over domains S 1 and S 2. It has three tuples.

15 Relational Data Model  Terminology Formal TermsInformal Terms RelationTable Attribute/DomainColumn TupleRow DomainValues in a column or pool of legal values Schema of a relationTable definition ExtensionPopulated table DegreeNo. of columns CardinalityNo. of rows Primary keyUnique identifier

16 Relational Data Model  Example

17 Relational Data Model  Key fields  Keys are special fields that serve two main purposes: - Primary keys are unique identifiers of the relation. Examples can use SSN as a primary key. This is how we can guarantee that all rows are unique. - Foreign key is a column or columns whose values are the same as a primary key of another table.  Keys can be simple (a single field) or composite (more than one field)  Keys usually are used as indexes to speed up the response to user queries.

18 Relational Constraints  There are three main types of constraints: “ Constraints are conditions that must hold on all valid relation instances. ” (Elmasri&Navathe, 2000)  Key constraints  Entity integrity constraints  Referential integrity constraints  What are relational constraints? “ Restrictions on data that can be specified on a relational database schema. ” (Date, 2000)

19 Relational Constraints  Key constraints  Superkey of R: A set of attributes SK of R such that no two tuples in any valid relation instance r(R) will have the same value for SK. That is, for any distinct tuples t 1 and t 2 in r(R), t 1 [SK]  t 2 [SK].  Key of R: A "minimal" superkey; that is, a superkey K such that removal of any attribute from K results in a set of attributes that is not a superkey.  Example: The CAR relation schema: CAR(State, Reg#, SerialNo, Make, Model, Year) has two keys Key1 = {State, Reg#}, Key2 = {SerialNo}, which are also superkeys. {SerialNo, Make} is a superkey but not a key.

20 Relational Constraints  Key constraints (Continued) The CAR relation with two candidate keys: LicenseNumber and EngineSerialNumber.

21 Relational Constraints  Key constraints (Continued)  A relation schema may have more than 1 key. Each of the keys is called a candidate key. One of the candidate keys are designated as the primary key of the relation.  If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined.

22 Relational Constraints  Entity integrity constraints  Relational Database Schema: A set S of relation schemas that belong to the same database. S is the name of the database. S = {R 1, R 2,..., R n }  Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R). This is because primary key values are used to identify the individual tuples. t[PK]  null for any tuple t in r(R)  Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key.

23 Relational Constraints  Entity integrity constraints (Continued)  A Null is created by making no entry at all, so a null denotes the absence of a value. - The value does not exist - The value exists, but it is not known. - The value is unknown, or it is not applicable.  A null can have any of the following meanings:

24 Relational Constraints  Referential integrity constraints  A constraint involving two relations (the previous constraints involve a single relation).  Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation.  Tuples in the referencing relation R 1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R 2. A tuple t 1 in R 1 is said to reference a tuple t 2 in R 2 if t 1 [FK] = t 2 [PK].  A referential integrity constraint can be displayed in a relational database schema as a directed arc from R 1. FK to R 2.

25 Relational Constraints  Referential integrity constraints (Continued)  Statement of the constraint The value in the foreign key column (or columns) FK of the the referencing relation R 1 can be either: (1) a value of an existing primary key value of the corresponding primary key PK in the referenced relation R 2, or.. (2) a null. In case (2), the FK in R 1 should not be a part of its own primary key.

26 Relational Constraints  Referential integrity constraints displayed on the COMPANY relational database schema

27 Relational Constraints Key constraints (columns) Entity integrity constraints (rows) Referential integrity constraints (between tables)  Summary of relational constraints

28 Update Operations on Relations  UPDATE operations consist of: INSERT a tuple DELETE a tuple MODIFY a tuple  Integrity constraints should not be violated by the update operations.  Several update operations may have to be grouped together.  Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints.

29 Update Operations on Relations  In case of integrity violation, several actions can be taken:  Cancel the operation that causes the violation (REJECT option)  Perform the operation but inform the user of the violation  Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option)  Execute a user-specified error-correction routine e

30 Update Operations on Relations  Insert operation  Insert into EMPLOYEE. This insertion violates the entity integrity constraint (null for the primary key SSN), so it is rejected.  Insert into EMPLOYEE. This insertion satisfies all constraints, so it is acceptable.

31 Update Operations on Relations  Delete operation  Delete the WORKS_ON tuple with ESSN = ‘999887777’ and PNO = 10. This deletion is acceptable.  Delete the EMPLOYEE tuple with SSN = ‘999887777’ This deletion is not acceptable, because tuples in WORKS_ON refer to this tuple. Hence, if the tuple is deleted, referential integrity violations will result.

32 Update Operations on Relations  Modify/Update operation  Update the SALARY of the EMPLOYEE tuple with SSN = ‘999887777’ to 28000. This update is acceptable.  Update the SSN of the EMPLOYEE tuple with SSN = ‘999887777’ to ‘987654321’. This update is unacceptable, because it violates primary key and referential integrity constraints.

33 Relational Algebra  Relational algebra is a set of operations to manipulate data and enable the user to specify basic retrieval requests.  The result of a retrieval is a new relation, which may have been formed from one or more relations.  Three groups of relational algebra operations:  Unary relational operations  Binary relational operations  Set theoretic operations

34 Relational Algebra  Unary relational operations:  The SELECT operation  The PROJECT operation


Download ppt "Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I."

Similar presentations


Ads by Google