Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch off your Mobiles Phones or Change Profile to Silent Mode.

Similar presentations


Presentation on theme: "Switch off your Mobiles Phones or Change Profile to Silent Mode."— Presentation transcript:

1 Switch off your Mobiles Phones or Change Profile to Silent Mode

2 Relational Model and SQL

3 Three Parts of Relational Model Structural Part Type of structures (building blocks) from which database is constructed. Manipulative Part Operations which are used for retrieving and updating data in database. Integrity Part Rules that all valid databases must obey.

4 Relational Database Language Data Definition Language (DDL) Used to define database schema – structural part of relational model Language that allows creation and description of tables (relations) and their relationship within database. Result of compilation of DDL statements is set of relations stored in special files collectively called system catalog (data dictionary)

5 Relational Database Language Data Manipulation Language (DML) Used to manipulation data in database – manipulative part. Language that provides set of operations (Insert, Modify, Retrieve and Delete) to support basic data manipulation on data.

6 SQL vs Relational Algebra SQL Deals with both structural part (DDL) and manipulative part (DML) of relational model. Examples of DDL statements CREATE, ALTER, DROP, RENAME Examples of DML statements INSERT, UPDATE, DELETE, SELECT Relational Algebra Deals with manipulative part of relational model. Contains set of 8 operators.

7 SQL vs Relational Algebra Difference in power / function Difference in styles of expression SQLRelational Algebra Both DDL and DMLDML only SQLRelational Algebra Non procedural languageProcedural language Descriptive (specify what to retrieve) Prescriptive (specify how to retrieve) Problem specificationProblem solving procedure

8 Relational Algebra Collection of high level operators that operate on relations Traditional set operations Union, Intersection, Difference, Product Special relational operations Restriction, Projection, Join, Division

9 Definition of Algebra

10 Syntax of Relational Algebra RESTRICT relationa-name-1 WHERE condition [GIVING result-relational-name] PROJECT relationa-name-1 OVER attribute-list [GIVING result-relational- name] JOIN relationa-name-1 AND relationa- name-2 [OVER join-attribute-list] [GIVING result-relational-name]

11 Syntax of Relational Algebra DIVIDE dividend-relation-name BY divisor-relation-name [OVER common- attribute-list] [GIVING result-relational- name] relation-name-1 UNION relation-name-2 [GIVING result-relation-name] relation-name-1 PRODUCT relation- name-2 [GIVING result-relation-name] Relation-name-1 DIFFERENCE relation- name-2 [GIVING result-relation-name]

12 Syntax of Relational Algebra Relation-name-1 INTERSECT relation- name-2 [GIVING result-relation-name]

13 Example Database

14 Union UNION of two union-compatible relations A and B, A UNOIN B, is relation with same heading as each of A and B with body consisting of set of all tuples belonging to either A or B or both. UNION combines all rows from two relations, excluding duplicate rows. The relations must have the same attribute characteristics (the columns and domains must be identical) to be used in the UNION.

15 Union 3 rd Year Students UNION 2 nd Year Students

16 Intersection INTERSECTION of two union- compatible relations A and B, A INTERSECTION B, is relation with same heading as each of A and B with body consisting of set of all tuples beloging to both A and B. INTERSECTION yields only the rows that appear in both relations. You cannot use INTERSECTION if one of attributes is numeric and one is character-based.

17 Intersect Operator

18 Difference DIFFERENCE of two union-compatible relations A and B, A MINUS B (or A DIFFERENCE B), is relation with same heading as each of A and B with body consisting of set of all tuples belonging to A and not to B DIFFERENCE yields all rows in one relation that are not found in the other relation; that is, it subtracts one relation from the other.

19 Difference

20 Cartesian Product Cartesian Product of two product compatible relations A and B, A TIMES B or A PRODUCT B, is relation with heading that is coalescing of headings of A and B and with body consisting of set of all tuples t such that t is coalescing of tuple a belonging to A and tuple b belonging to B. Cardinality of results is product of cardinalities of A and B, and degree of result is sum of their degrees.

21 Cartesian Product

22

23

24 Restrict (Select) RESTRICT operator effectively yields a ‘horizontal’ subset of give relation, which is obtained by selecting all tuples that satisfied restriction condition defined by WHERE clause consisting of arbitrary Boolean combination of simple conditions. RESTRICT, also known as SELECT, yields values for all rows found in a relation that satisfy a given condition.

25 Restrict (Select)

26 Project PROJECTION operator yields all values for selected attributes. In other words, PROJECT yields a vertical subset of a relation.

27 Project PROJECT 3 rd Year Students OVER Student PROJECT Current-marks OVER course

28 Project PROJECT Current-marks OVER students, course

29 Divide Division of R by S, R DIVIDEBY S, is relation whose header part is A1, A2, …., An and where each tuple is value for R A such that values of R B occurring with this value for R A in tuples of R correspond to all values of S B in S.

30 Divide

31

32

33 Natural Join JOIN allows information to be combined from two or more relations. JOIN allows the use of independent relations linked by common attributes. Different types of JOIN: theta join where join condition is any of =, >, =, <= equi join where join condition is = natural join where join condition is = but only one join column is presented in result

34 Natural Join Natural Join of tables R and S (with union-compatible columns in common) over these columns will result in table with header derived by concatenating headers of R and S and whose tuples are concatenation of all those tuples in R and S where values in common columns to join operator.

35 Natural Join

36 Relational Integrity Rules & SQL Relational integrity is one of three parts of Relational Model. Data integrity in Relational Database is concerned with accuracy and validity of data in database. Relational Integrity is enforced via Integrity rules.

37 Relational Integrity Rules & SQL There are three main types of integrity specified in Relational Model: Domain Integrity Entity Integrity Referential Integrity These integrity rules can be specified and implemented by appropriate SQL statements.

38 Domain Integrity Range of values that a particular column can take is known as domain. Domain Integrity rule states that: Every attribute is required to satisfy constraint that its values are drawn from relevant domain.

39 Domain Integrity Example: The domain of gender has only two values (e.g. Male/Female). The domain of start date could be any valid date greater than 01/01/2006. The domain of salary may be restricted to the range 20,000 - 80,000. Question: How to implement domain integrity in SQL?

40 Entity Integrity It is concerned with Primary keys Entity Integrity rules states that: Every base relation must have a primary key and no attribute which forms part of that primary key can accept null values.

41 Primary Keys PRIMARY KEY for a table T1 is a column PK of T1 such that, at any given time, no two rows of T1 have same value for PK. An entity’s primary key is one or more of its attributes which together uniquely identify an occurrence of the entity.

42 Primary Keys STUDENT (Student-ID, Student-Name, Address) Student-idStudent-nameAddress S1001Tom12a Clover Street S1002AlexFlat B High Street S1003Brown218 Tulip Road STUDENT

43 Primary Keys FEATURES Can never take on a particular set of values in more than one occurrence of the entity. No primary key attribute can have a null value. Every attribute in the primary key is mandatory.

44 Implementation of Entity Integrity In Oracle using CREATE TABLE / ALTER TABLE statement: CREATE TABLE Customer (Customer_NoNumber(10) Primary Key,NameVarchar2(20),AddressVarchar2(20),Depot_NoNumber(10),Credit_LimitNumber(10));

45 Referential Integrity It is concerned with Foreign keys. Referential Integrity rules states that: Database must not contain any unmatched Foreign key values.

46 Foreign Key FOREIGN KEY is a column FK of some table T2 such that, at any given time, every (non NULL) value of FK in T2 is required to be equal to the value of the PRIMARY KEY PK in some row of some base table T1.

47 Foreign Key Lecturer-idLecturer-nameDepartmentSalary W01Emma GregComputing35,000 S01Wendy HolderComputing42,000 D01Amy KingAccounting27,000 J02Bob JonesComputing29,000 N01Bob WhalesAccounting36,500 J01Jack NelsonHistory41,500 LECTURER(Lecture-Id, Lecture-Name, Department, Salary) DepartmentLocation ComputingMoorgate AccountingStaples HistoryLewiston LECTURER-LOCATION(Department*, Location

48 Foreign Key Cust-idCust-nameCredit-StatusBusiness-type 1001SmithExcellentRetail 1910JonesGoodWholesale 2450BrownFairMultiple CUSTOMER Order-noDateCust-id* 10001/02/061910 10104/06/061001 10215/06/061910 ORDER Order-no*Item-noItem-nameQuality 10011Spade15 10092Shovel10 10111Spade12 10292Shovel50 ORDER-LINE

49 Foreign Key Feature: There is no requirement that a Foreign Key be a component of the Primary Key. Dept-code…… DEPARTMENT Empl-no……Dept-code*…… EMPLOYEE Feature: A foreign key will be composite if the primary key it matches is composite. Manufacturer-nameModel-name…… VEHICLE Vehicle-noManufacturer-name*Model-name*… VEHICLE-IN-STOCK

50 Foreign Key Feature: Many-To-Many relationships are always decomposed with Foreign keys making up a composite Primary Key. Supp-no…… SUPPLIER Supp-no*Part-no*…… SUPPLY Part-no…… PART

51 Foreign Key Feature: Note that T1 and T2 do not have to be distinct Empl-no……Mge-no*…… EMPLOYEE

52 Foreign Key Cust-idCust-nameCredit-StatusBusiness-type 1001SmithExcellentRetail 1910JonesGoodWholesale 2450BrownFairMultiple CUSTOMER Order-noDateCust-id* 10001/02/061910 10104/06/061001 10215/06/061910 ORDER Feature: There is no requirement that a given Primary Key value must appear in a corresponding Foreign Key.

53 Referential Integrity If a foreign key exists in a relation, either foreign key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null. CUSTOMER ORDER places

54 Referential Integrity Cust-idCust-nameCredit-StatusBusiness-type 1001SmithExcellentRetail 1910JonesGoodWholesale 2450BrownFairMultiple CUSTOMER(Cust-id, Cust-name, Credit-status, Business-type Order-noDateCust-id* 10001/02/061910 10104/06/061001 10215/06/061910 ORDER(Order-no, Date, Cust-id*)

55 Referential Integrity It ought to be impossible to delete Cust_id 1001 in Customer table and at same time leave Cust_id 1001 undisturbed in Order table. It ought to be impossible to update Cust_id value in Customer tuple (e.g. 1001 changed to 200) and at same time leave Order table unchanged. CUSTOMER ORDER places

56 Referential Integrity ON DELETE and ON UPDATE can have NO ACTION or CASCADE or SET NULL or SET DEFAULT RESTRICTED / NO ACTION Default option Operation is ‘restricted’ to the case where no such matching occurs. Attempt to delete (update) customer id 1001 fails.

57 Referential Integrity CASCADES Operation cascades to delete matches as well. Delete or Update customer id 1001 and orders 101. NULLIFIES Foreign key is set to null in all matches and record is deleted (updated). Used only when foreign key can accept null values Delete (update) record, set cust_id in orders 101 to null.

58 Referential Integrity DEFAULT This is alternative to SET NULL Foreign key is in all matches is set to some default value such as ‘9999’ to represent missing information Delete (update) record, set cust_id in orders 101 to ‘9999’.

59 Referential Integrity Implement Current standard of SQL “SQL/92” has included Referential Integrity Constraints, defined at data definition stage automatically. Subsequently, with each insert, update and delete operation, there is check that referential integrity rule is not iolated.

60 Referential Integrity Implement CREATE TABLE Customer (Cust_idNumber(4)NOT NULL,Cust_NameVarchar2(20)NOT NULL,Business_TypeVarchar2(20), PRIMARY KEY (Cust_id));

61 Referential Integrity Implement CREATE TABLE Order (Order_NoNumber(3)NOT NULL,Order_DateDateNOT NULL,Cust_idNumber(4), PRIMARY KEY (Order_No), FOREIGN KEY (Cust_id) REFERENCES Customer (Cust_id) ON DELETE CASCADE ON UPDATE CASCADE); Question: Can you create Order table before Customer table? Why?

62 Application Specific or User Defined Integrity Rules Main (general) Integrity rules Above three Relational Integrity rules Additional application specific or user defined integrity rules There could be (many) additional integrity rules which only apply to particular application / database.

63 Application Specific or User Defined Integrity Rules Question: Consider university student database; - what could be additional integrity rules? Answers Possible additional application specific rules could be: no undergraduate student is allowed to take a postgraduate module. no module can be taken unless its pre- requisite module has been taken. no student can be registered for module if maximum capacity has reached.

64 Any Questions?


Download ppt "Switch off your Mobiles Phones or Change Profile to Silent Mode."

Similar presentations


Ads by Google