Presentation is loading. Please wait.

Presentation is loading. Please wait.

An introduction to relational database RDB Sept. 20051.

Similar presentations


Presentation on theme: "An introduction to relational database RDB Sept. 20051."— Presentation transcript:

1 An introduction to relational database RDB Sept. 20051

2 2 What are behind the scene of RDBMS? Functional components –SQL language parser and processor –Query engine –Query optimizer –Storage management –Transaction Management (concurrency, recovery) –…

3 Sept. 20053 Outline: Relational Data Model Data Model Relational Data Model -relation schema, relations -database schema, database state -integrity constraints and updating Relational algebra (Won’t be discussed in detail, there will be a separate presentation on it) -select, project, join, cartesian product -set operations: union, intersection, difference, division

4 Sept. 20054 What is a model? In general, a model is a theoretical construct that represents complex information and processes by a set of variables and a set of logical and quantitative relationships between them. A model supports a set of operands and a set of functions (also called operators).

5 Sept. 20055 Other definition Models are simplified frameworks designed to represent the all individual complex processes which can be fit into the model. An abstract construct or idea that is a simplified view of reality. It must enable you to make testable predictions of what will happen under new circumstances. A stylized simplification of reality in which behavior is represented by variables and assumptions about how they are determined and interact. Models enable one to think consistently and logically about complex issues, to work out how changes in a modeled system matter, and (sometimes) to response to user requirements.

6 Sept. 20056 A DBMS allows a user to define the data to be stored in terms of a data model, which is a collection of high-level (abstract level) data description constructs that hide many low level storage details.

7 Sept. 20057 A database model is a theory or specification describing how a database is structured and used. Several such models have been suggested: –Hierarchical model –Network model –Relational model –Object model –Object-Relational model –…

8 Sept. 20058 A data model is a model that is specifically designed for data. It is a collection of descriptions of data structures and their contained fields, together with the operations or functions that manipulate them.

9 Sept. 20059 A data model should support Structure: defines how data are organized (hierarchical, network, relational, object- oriented). Integrity: provides a language for the definition of rules that restrict which instances of the defined structure are allowed. Manipulation: provides a language in which updates of the data can be expressed. Querying: provides a language in which the data can be queried.

10 Sept. 200510 A description of data in terms of a data model is called a schema. Data themselves in terms of a data model are called instances.

11 Sept. 200511 Three-schema architecture External view External view logical schema Internal schema Physical storage structures and details Describes the whole database for all users A specific user or groups view of the database

12 Sept. 200512 Data independence Ability to change the database at one level with no impact to the next higher level physical data independence - the ability to change the physical schema without affecting the conceptual schema typical example: add a new index logical data independence - the ability to change the conceptual schema without affecting existing external views or application programs typical example: add an attribute

13 Sept. 200513 Relational Data Model Most database management systems today are based on the relational data model, which is dominant in marketplace. Relational data model was first introduced in 1970 by Ted Codd (IBM).

14 Sept. 200514 The fundamental assumption of the relational model is that all data are represented as mathematical relations, i.e., a subset of the Cartesian product of n sets. In the mathematical model, reasoning about such data is done in two-valued predicate logic meaning there are two possible evaluations for each proposition: either true or false. Data are operated upon by means of a relational calculus and algebra. If NULL is considered, three values (true, false, and NULLs).

15 15 How the Programmer Sees the RDBMS Tables: Still implemented as files, but behind the scenes can be quite complex Students:Takes: Courses: “data independence” = separate logical view from physical implementation

16 Sept. 200516 A Relation is a Table namemanf WinterbrewPete’s Bud LiteAnheuser-Busch Beers Attributes (column headers) Tuples (rows)

17 Sept. 200517 Relation schema vs. relation instances The main construct for representing data in the relational model is a relation. A relation consists of a relation schema and relation instances. Intuitively, the relation instance is a table, and the relation schema describes the column heads for the table.

18 Sept. 200518 Relational Model Each relation is a table with rows and columns, which can be thought of as a set of records, each of which has the same number (and type) of fields. A relational database is a collection of one or more relations (tables). The idea is very simple and elegant.

19 Sept. 200519 Schema In the relational model, the schema for a relation specifies its name, the name of each field (or attribute or column), and the domain (type) of each field. Also constraints, as we will see later.

20 Sept. 200520 A relation schema R, denoted by R(A 1, …, A n ), is made up of a relation name R and a list of attributes A 1, …, A n. A relation r(R) is a mathematical relation of degree n on the domains dom(A 1 ), dom(A 2 ), … dom(A n ), which is a subset of the Cartesian product of the domains that define R: r(R)  (dom(A 1 )  (dom(A 2 )  …  (dom(A n )) formal termsinformal relationtable tuplerow attributecolumn header domaindata type describing column values

21 Sept. 200521 Cartesian product Emp(SSN, name, sex)  JDJD mfmf   JDJD  J), (1, D), (2, J), (2, D), (3, J), (3, D)}

22 Sept. 200522   JDJD  J, m), (1, D, m), (2, J, m), (2, D, m), (3, J, m), (3, D, m), (  J, f), (1, D, f), (2, J, f), (2, D, f), (3, J, f), (3, D, f)} Cartesian product  mfmf Emp(SSN, name, sex) 1 J m 2 D f

23 Sept. 200523 Domain A domain is a set of atomic values from which values can be drawn examples -social security numbers: set of valid 9-digit social insurance numbers -names: set of names of persons -grade point average: possible values of computed grade point averages; each must be a real number between 0 and 4.5.

24 Sept. 200524 Domain In many systems one specifies a data type (e.g. integer, date, string(20), …) and writes supporting application code to enforce any specific constraints (e.g. a SIN must be a 9-digit number). Domain = proper Data type + proper check constraints (you can think so and we will discuss why) Attribute An attribute A i is a name given to the role a domain plays in a relation schema R. Relation (or Relation State) A relation, or relation state, r of the relation schema R(A 1, A 2, … A n ) is a set of n-tuples r={t 1, t 2, … t m }, where each n-tuple is an ordered list of n values t i = (i = 1, …, m).

25 Sept. 200525 Relation Schema example EMPLOYEE(Name, SSN, HomePhone, Address, OfficePhone, …) EMPLOYEE Relation example: EMPLOYEENameSSNHomePhoneAddress Benjamin Bayer 305-61-2435 373-1616 2918 Bluebonnet Lane... Katherine Ashly 381-62-1245 375-4409 125 Kirby Road... Dick Davidson 422-11-2320 null 3452 Elgin Road...

26 Sept. 200526 Some characteristics of relations Unordered attributes from schema point of view, but when instances are considered, values order should match attribute order. no ordering of tuples each value in a tuple is atomic no composite values separate relation tuples for multivalued attributes some attributes may be null no value value missing/unknown a relation is an assertion e.g. a employee entity has a Name, SSN, HomePhone, etc each tuple is a fact or a particular instance some relations store facts about relationships

27 Sept. 200527 employee worksproject jones smith A Z B C D jonesz A C smithz B D

28 Sept. 200528 Relational Database a relational database schema S is a set of relation schemas S = {R 1, R 2,...} and a set of integrity constraints IC. A relational database state DB of S is a set of relation states DB={r(R 1 ), r(R 2 ),...}

29 Sept. 200529 fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation Pname, pnumber, plocation, dnum Essn pno, hours Essn, dependentname, sex, bdate, relationship EMPLOYEE WORKS ON PROJECT DEPENDENT DEPARTMENT DEPT _LOCATIONS A database schema:

30 Sept. 200530 fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation EMPLOYEE DEPARTMENT DEPT _LOCATIONS A database state: JohnBSmith123489 1965-01-09 731 FondrenM400003434885 FranklinTWong239979 1955-01-10 638 VossM500003434885 Research51988-05-22 5Houston 343488 6Stafford r(EMPLOYEE) r(DEPARTMENT) r(DEPT_LOCATION)

31 Sept. 200531 Integrity Constraints any database will have some number of constraints that must be applied to ensure correct data (valid states) 1. domain constraints a domain is a restriction on the set of valid values domain constraints specify that the value of each attribute A must be an atomic value from the domain dom(A). 2. key constraints a superkey is any combination of attributes that uniquely identify a tuple: t 1 [superkey]  t 2 [superkey]. -Example: (in Employee) a key is superkey that has a minimal set of attributes -Example: (in Employee)

32 Sept. 200532 Integrity Constraints If a relation schema has more than one key, each of them is called a candidate key. one candidate key is chosen as the primary key (PK) foreign key (FK) is defined as follows: i) Consider two relation schemas R 1 and R 2 ; ii) The attributes in FK in R 1 have the same domain(s) as the primary key attributes PK in R 2 ; the attributes FK are said to reference or refer to the relation R 2 ; iii)A value of FK in a tuple t 1 of the current state r(R 1 ) either occurs as a value of PK for some tuple t 2 in the current state r(R 2 ) or is null. In the former case, we have t 1 [FK] = t 2 [PK], and we say that the tuple t 1 references or refers to the tuple t 2. Example: Employee(SSN, …, Dno)Dept(Dno, … ) FK

33 Sept. 200533 Integrity Constraints 3. entity integrity no part of a PK can be null 4. referential integrity domain of FK must be same as domain of PK FK must be null or have a value that appears as a PK value 5. semantic integrity other rules that the application domain requires: state constraint: gross salary > net income transition constraint: Widowed can only follow Married; salary of an employee cannot decrease

34 Sept. 200534 More about keys Surrogate key Composite key

35 Sept. 200535 Summarizing different Constraints Rules that restrict the data values that you can enter into a field in a database table Integrity constraints: define primary and foreign keys Value constraints: define specific data values or data ranges that must be inserted into columns and whether values must be unique or not NULL Table constraint: restricts the data value with respect to all other values in the table Field constraint: limits the value that can be placed in a specific field, irrespective of values that exist in other table records

36 Sept. 200536 fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation Pname, pnumber, plocation, dnum Essn, pno, hours Essn, dependentname, sex, bdate, relationship EMPLOYEE DEPARTMENT DEPT _LOCATIONS WORKS_ON PROJECT DEPENDENT reference integrity

37 Sept. 200537 Updating and constraints insert Insert the following tuple into EMPLOYEE: When inserting, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity update Update the SALARY of the EMPLOYEE tuple with ssn = ‘999887777’ to 30000. When updating, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity

38 Sept. 200538 Updating and constraints delete Delete the WORK_ON tuple with Essn = ‘999887777’ and pno = 10. When deleting, the referential constraint will be checked. - The following deletion is not acceptable: Delete the EMPLOYEE tuple with ssn = ‘999887777’ -reject, cascade, modify

39 Sept. 200539 cascade – a strategy to enforce referential integrity  ssn  Employee Essn Pno    delete Works-on delete

40 Sept. 200540 cascade – a strategy to enforce referential integrity Employee delete ssn supervisor    null  Employee delete ssn supervisor    null  delete not reasonable

41 Sept. 200541 Modify – a strategy to enforce referential integrity  ssn  Employee Essn Pno    delete Essn Pno null   This violates the entity constraint. Works-on

42 Sept. 200542 Modify – a strategy to enforce referential integrity  ssn  Employee delete This does not violate the entity constraint. Department  Dno   chairman Department null Dno   chairman

43 Sept. 200543 Schema: a description of a database -- meta data the intension of the database Schema evolution: if the database definition changes, we say it evolves. Database State: the data in the database at a particular point in time the extension of the schema

44 Sept. 200544 Sample database Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database 8803 3 CS C 2606 3 CS Section SId CNo Semester Yr Instructor 32 8803 Spring 2000 Smith 25 8803 Winter 2000 Smith 43 2606 Spring 2000 Jones Grades StNo SId Grade 17 25 A 17 43 B

45 Sept. 200545 University database schema: Course Cnamestring(20) CNo string(20) CrHrsinteger Dept.String(20) grades StNointeger SIdinteger GradeString(20) Student Namestring(20) StNointeger Classinteger MajorString(20) Section SIDinteger CNostring(20) Semesterinteger Yrinteger Instructorstring(20)

46 Sept. 200546 Schema evolution: Course Cnamestring(20) CNostring(20) CrHrsinteger Dept.String(20) Student Namestring(20) StNointeger Classinteger MajorString(20) Prerequisite CNostring(20) Pre-CNostring(20) …... new table added

47 Sept. 200547 Database evolution: Course Cnamestring(20) Cnostring(20) CrHrsinteger Dept.String(20) Student Namestring(20) StNointeger Classinteger MajorString(20) Ageinteger Sexstring(20) …... new attributes added

48 Sept. 200548 Sample database Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database 8803 3 CS C 2606 3 CS Section SId CNo Semester Yr Instructor 32 8803 Spring 2000 Smith 25 8803 Winter 2000 Smith 43 2606 Spring 2000 Jones Grades StNo Sid Grade 17 25 A 17 43 B

49 Sept. 200549 Database state changed: Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database 8803 3 CS C 2606 3 CS Section SId CNo Semester Yr Instructor 32 8803 Spring 2000 Smith 25 8803 Winter 2000 Smith Grades StNo Sid Grade 17 25 A 17 43 B

50 Sept. 200550 Definition Manipulation Construction We define the database database state is the empty state with no data initial state when database is populated (loaded) current state changes with each insert, delete, update hopefully, the database goes from one correct/valid state to another

51 Sept. 200551 Mathematical Foundations Relational Model is based on Strong Mathematical Foundations. The Relational model is based on (1) Set Theory, (2) Mathematical Relations, (3) Modern Algebra, (4) Logic, and (5) Lattice Theory.

52 Sept. 200552 Relational Algebra a set of relations a set of operations set operations relation specific select project join division union intersection difference cartesian product

53 Sept. 200553 Relational algebra select horizontal subset project vertical subset join (equijoin, natural join, inner, outer) combine multiple relations cartesian product union, intersection, difference division

54 Sept. 200554 Relational algebra select horizontal subset project vertical subset join (equijoin, natural join, inner, outer) combine multiple relations cartesian product union, intersection, difference Division (won’t be tested)

55 Sept. 200555 Relational algebra - Select horizontal subset symbol:  boolean condition for row filter e.g. employees earning more than 30,000  salary>30000 (Employee) fname minit … salary... Franklin T … 40000... Jennifer S … 43000... James E … 55000... Every column of Employee appears in the result

56 Sept. 200556 Relational algebra - Project vertical subset symbol:  e.g. names of employees  fname, minit, lname (Employee) fname minit lname John B Sarah Franklin T Wong AliciaJZalaya Jennifer S Wallace RameshKNarayan JoyceAEnglish Ahmad VJabbar James EBorg

57 Sept. 200557 Relational algebra - Join join or combine tuples from two relations into single tuples symbol: boolean condition specifies the join condition e.g. to report on employees and their dependents Employee ssn=essn Dependent fname minit … essn dependent_name … All attributes of both employee and dependent will appear

58 Sept. 200558 Essn dependent_name... 333445555 Alice 333445555 Theodore 333445555 Joy 987654321 Abner 123456789 Michael 123456789 Alice 123456789 Elizabeth Relational algebra - Join Employee ssn=essn Dependent fname minit … ssn Franklin T… 333445555 Jennifer S… 987654321 JohnB… 123456789

59 Sept. 200559 fname minit ssn essn Franklin T 333445555 333445555 Alice Franklin T … 333445555 Theodore Franklin T … 333445555 Joy Jennifer S … 987654321 Abner John B … 123456789 Michael John B … 123456789 Alice John B … 123456789 Elizabeth Employee ssn=essn Dependent... dependent_name

60 Sept. 200560 Relational algebra - Join what is the result of Employee Dependent ? Note there is no join condition List all attributes from two tables fname minit … essn dependent_name... There would be 8 times 7 = 56 rows in the result This is the Cartesian Product

61 Sept. 200561 Sample data Empl- name ssnBrith-dategradesenioritysalary A1211948/2/122780k A1321952/3/242560k B1011972/1/24670k C1061987/3/33780k

62 Sept. 200562 Sample queries Empl- name ssnBrith-dategradesenioritysalary A1211948/2/122780k A1321952/3/242560k B1011972/1/24670k C1061987/3/33780k Find out names of all employees born before 1960/11/1

63 Sept. 200563 More detailed discussion of relational algebra will be presented in a separate lecture!

64 64 Transactions A transaction = sequence of statements that either all succeed, or all fail Transactions have the ACID properties: A = atomicity C = consistency I = isolation D = durability

65 65 Transactions Enroll “Mary Johnson” in “CSCI242”: BEGIN TRANSACTION; INSERT INTO Takes SELECT Students.SSN, Courses.CID FROM Students, Courses WHERE Students.name = ‘Mary Johnson’ and Courses.name = ‘CSCI242’ -- More updates here.... IF everything-went-OK THEN COMMIT; ELSE ROLLBACK BEGIN TRANSACTION; INSERT INTO Takes SELECT Students.SSN, Courses.CID FROM Students, Courses WHERE Students.name = ‘Mary Johnson’ and Courses.name = ‘CSCI242’ -- More updates here.... IF everything-went-OK THEN COMMIT; ELSE ROLLBACK If system crashes, the transaction is still either committed or aborted


Download ppt "An introduction to relational database RDB Sept. 20051."

Similar presentations


Ads by Google