Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Model 2015, Fall Pusan National University Ki-Joune Li.

Similar presentations


Presentation on theme: "Relational Model 2015, Fall Pusan National University Ki-Joune Li."— Presentation transcript:

1 Relational Model 2015, Fall Pusan National University Ki-Joune Li

2 Basic Ideas of Relational Model A mathematical notation o rather than diagrammatic notations such as UML diagram Structures o A Database : Set of Tables o A Table: Relation R between A 1, A 2,..., A n  R  A 1 x A 2 x...x A n = { (a 1,a 2,a 3,...,a n )| a i  A i }  A i : attribute (or domain, field)  a i : attribute value  schema: R (A 1, A 2,..., A n )  tuple: (a 1,a 2,a 3,...,a n ) 2

3 Relation as a Table 3 NameManufacturer HiteJinro CassOB Attributes (column headers) Tuples (rows) Beers (Name, Manufacturer) Beers

4 Relational Schema Relation schema = o relation name o attributes, in order (+ types of attributes).  Example: Beers(name, manufacturer) or Beers(name: string, manf: string) o key definition o Any additional constraints Example o Schema: Movie(title, year, length, genre) 4 Movie + title + year + length + genre In UML

5 Why Relational Model Very simple model. Often matches how we think about data. Relational Model and SQL o Most important database language today o Not Procedural but Declarative  Once optimized by DBMS, then Ideal Algebraic Background o Relational Algebra 5

6 Schema Definition in SQL SQL (pronounced as “sequel” or SQL) o A declarative Language for relational databases o DDL (Data Definition Language) – Schema Definition  Table  View - External Layer of DB o DML (Data Manipulation Language) – Querying Schema Definition in SQL o Example CREATE TABLE Movies ( titleCHAR(100), yearINT, lengthINT, genreCHAR(10), studioNameCHAR(20), producerC#INT, PRIMARY KEY(title, year) ); 6

7 Basic Data Types in SQL CHAR(n), or VARCHAR(n) BIT(n), or BIT VARYING(n) BOOLEAN INT, or INTEGER, SHORTINT FLOAT or REAL, DOUBLE PRECISION, DECIMAL(n,d) DATE, TIME 7

8 More about SQL for Relation Schema Modification o DROP TABLE Movies; o ALTER TABLE Movies ADD address CHAR(100); or o ALTER TABLE Movies DROP producerC#; Default Values and Keys CREATE TABLE Movies ( movie#INT PRIMARY KEY, titleCHAR(100), yearINT, lengthINT, genreCHAR(10) DEFAULT‘UNKNOWN’, studioNameCHAR(20), producerC#INT, ); 8

9 Relational Algebra Algebra: Set (Operands) + Operators In Relational Algebra o Operand: Relation (Table) o Operator: Relational Operators  Set Operator: Union, Intersection, Difference  Selection  Projection  Join  Aggregate

10 Relational Operators: Set Operators Union (  ) Intersection (  ) Difference (-) 10

11 Relational Operators : Select Selection (  condition ) o Retrieve records satisfying predicates o Example  Find Student where Student.Score > 3.5   score>3.5 (Student) o Index or Hash Select Predicate

12 Relational Operators : Project Project (  attributes ) o Extract interesting attributes o Example  Find Student.name where score > 3.5   name (  acore>3.5 (Student)) o Full Scan Interesting attributes to get Extract

13 Cartesian Product Cartesian Product (  ) o Two Tables : R 1  R 2 o Produce all cross products Join ( ) r 11 r 12 … r1mr1m R1R1 r 21 r 22 … r2nr2n R2R2  = r 11 … r 21 r 22 … r2nr2n r 12 … r 21 r 22 … r2nr2n r1mr1m r 21 r 22 … r2nr2n … r1mr1m r1mr1m … …

14 Join Join ( ) o Select combined records of Cartesian product with same value of a common attribute (Natural Join) o Example Student (StudentName, AdvisorProfessorID, Department, Score) Professor(ProfessorName, ProfessorID, Department) Student AdivsorProfessorID=ProfessorID Professor =  AdivsorProfessorID=ProfessorID (Student  Professor) o Natural Join vs. Theta Join  Natural Join  Theta Join - as for SELECT, condition can be any Boolean condition.  Historic versions of this operator allowed only A theta B, where theta was =, <, etc.; hence the name “ theta-join. ”

15 Join – Natural Join Example 15 BarInfo = Sells Sells.bar = Bars.name Bars = Sells Bars BarInfo (bar, beer, price, name, addr)  BarInfo(bar, beer, price, addr) Sells (bar, beer, price) Bars (name,addr) barbeerpriceaddr Joe’sBud2.50Maple St. Joe’sMiller2.75Maple St. Sue’sBud2.50River Rd. Sue’sCoors3.00River Rd

16 Relational Algebra o Operand : Table (Relation) o Operator : Relational Operator ( , ,, etc) o Example: SQL and relational algebra  find Student.Name from Student, Professor where Student.Score > 3.5 and Student.AdvisorProfessorID=Professor.ID and Professor.Department=‘CSE’  student.name (  score>3.5 (Student)  Department=‘CSE’ (Professor) ) o Relational Algebra Specifies the sequence of operations  

17 Expression Tree, and execution order 17 Expression Tree  student.name  score>3.5 Student Professor  Department=‘CSE’  student.name  Student.score>3.5 and Professor.Department =‘CSE’ Student Professor X Equivalent 12 3 4 1 2 3 R  S = R – (R – S) R C S =  C (R X S)

18 Constraints on relations Referential constraints o One-way association in UML o Student(name, ID, dept, score), Department(name, college, office) o then  dept (Student)   name (Department) or  dept (Student) -  name (Department) =  o i.e. every value of Student.dept must be found in Department.name o i.e. the reference of Department from Student must exist. 18 StudentDepartment

19 Key Constraint At least one attribute or one combination of attributes to identify each tuple. Example o Student (name, ID, dept, score) If ID is key attribute of Student, then  Stud1.ID=Stud2.ID AND Stud2.dept≠Stud2.dept (Stud1 X Stud2) =  where  Stud1 (Student) and  Stud2 (Student) 19 renaming

20 From UML to Relational Model Class to Relational Schema Association Aggregation/Composition Inheritance Weak Entity Set 20

21 From Class to Relation 21 Employee + name + address + citizen ID + division + salary CREATE TABLE Employee ( nameCHAR(30), addressCHAR(100), citizenIDCHAR(12) PRIMARY KEY, divisionCHAR(20), salaryREAL );

22 From Association to Relation Schema One-way association Why not in Company ? 22 Person Company + name + age + ID: Key + name + address + category PK(name, address) worksFor CREATE TABLE Person ( nameCHAR(30), ageINT, IDCHAR(12) PRIMARY KEY, companyNameCHAR(40), companyAddressCHAR(100) );

23 From Association to Relation Schema Two-way association Why not with Player and Team? 23 Player Team + name + age + position + ID PK(ID) + name + address + city + sponsor PK (name) Contract CREATE TABLE Contract( signYearYear, salaryINT, playerIDCHAR(12), teamNameCHAR(40), Primary Key(playerID, teamName) ); - signYear - salary 20..* 1..*

24 From Aggregation to Relation Schema Either as one-way association or Two-way association 24

25 From inheritance to relational schema 25 StudentEmployee Person + name + age + ID: Key + dept + studentID + score + division + salary + position CREATE TABLE Person ( nameCHAR(30), ageINT, IDCHAR(12) PRIMARY KEY, ); CREATE TABLE Student( name CHAR(30), age INT, ID CHAR(12) PRIMARY KEY, dept CHAR(30), studentID INT Key, score REAL );

26 Handling Weak Entity Set 26 PlayerFootball team 1..11..* - name - back number - position - team name - city - sponsor belong to Weak Entity Set CREATE TABLE Player ( nameCHAR(30), backNumberINT, positionCHAR(3), teamNameCHAR(30), Primary Key(backNumber, teamName) );

27 Handling Weak Entity Set 27 PlayerFootball team 1..11..* - name - back number - position - team name - city - sponsor belong to CREATE TABLE belongTo ( backNumberINT, teamNameCHAR(30), Primary Key(backNumber, teamName) );


Download ppt "Relational Model 2015, Fall Pusan National University Ki-Joune Li."

Similar presentations


Ads by Google