An introduction to relational database RDB Sept
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) –…
Sept 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
Sept 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).
Sept 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.
Sept 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.
Sept 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 –…
Sept 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.
Sept 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.
Sept 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.
Sept 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
Sept 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
Sept 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).
Sept 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 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
Sept A Relation is a Table namemanf WinterbrewPete’s Bud LiteAnheuser-Busch Beers Attributes (column headers) Tuples (rows)
Sept 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.
Sept 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.
Sept 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.
Sept 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
Sept Cartesian product Emp(SSN, name, sex) JDJD mfmf JDJD J), (1, D), (2, J), (2, D), (3, J), (3, D)}
Sept 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
Sept 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.
Sept 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).
Sept Relation Schema example EMPLOYEE(Name, SSN, HomePhone, Address, OfficePhone, …) EMPLOYEE Relation example: EMPLOYEENameSSNHomePhoneAddress Benjamin Bayer Bluebonnet Lane... Katherine Ashly Kirby Road... Dick Davidson null 3452 Elgin Road...
Sept 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
Sept employee worksproject jones smith A Z B C D jonesz A C smithz B D
Sept 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 ),...}
Sept 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:
Sept fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation EMPLOYEE DEPARTMENT DEPT _LOCATIONS A database state: JohnBSmith FondrenM FranklinTWong VossM Research Houston Stafford r(EMPLOYEE) r(DEPARTMENT) r(DEPT_LOCATION)
Sept 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)
Sept 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
Sept 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
Sept More about keys Surrogate key Composite key
Sept 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
Sept 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
Sept 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 = ‘ ’ to When updating, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity
Sept Updating and constraints delete Delete the WORK_ON tuple with Essn = ‘ ’ and pno = 10. When deleting, the referential constraint will be checked. - The following deletion is not acceptable: Delete the EMPLOYEE tuple with ssn = ‘ ’ -reject, cascade, modify
Sept cascade – a strategy to enforce referential integrity ssn Employee Essn Pno delete Works-on delete
Sept cascade – a strategy to enforce referential integrity Employee delete ssn supervisor null Employee delete ssn supervisor null delete not reasonable
Sept Modify – a strategy to enforce referential integrity ssn Employee Essn Pno delete Essn Pno null This violates the entity constraint. Works-on
Sept Modify – a strategy to enforce referential integrity ssn Employee delete This does not violate the entity constraint. Department Dno chairman Department null Dno chairman
Sept 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
Sept Sample database Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database CS C CS Section SId CNo Semester Yr Instructor Spring 2000 Smith Winter 2000 Smith Spring 2000 Jones Grades StNo SId Grade A B
Sept 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)
Sept 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
Sept Database evolution: Course Cnamestring(20) Cnostring(20) CrHrsinteger Dept.String(20) Student Namestring(20) StNointeger Classinteger MajorString(20) Ageinteger Sexstring(20) …... new attributes added
Sept Sample database Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database CS C CS Section SId CNo Semester Yr Instructor Spring 2000 Smith Winter 2000 Smith Spring 2000 Jones Grades StNo Sid Grade A B
Sept Database state changed: Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database CS C CS Section SId CNo Semester Yr Instructor Spring 2000 Smith Winter 2000 Smith Grades StNo Sid Grade A B
Sept 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
Sept 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.
Sept Relational Algebra a set of relations a set of operations set operations relation specific select project join division union intersection difference cartesian product
Sept Relational algebra select horizontal subset project vertical subset join (equijoin, natural join, inner, outer) combine multiple relations cartesian product union, intersection, difference division
Sept 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)
Sept 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 … Jennifer S … James E … Every column of Employee appears in the result
Sept 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
Sept 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
Sept Essn dependent_name Alice Theodore Joy Abner Michael Alice Elizabeth Relational algebra - Join Employee ssn=essn Dependent fname minit … ssn Franklin T… Jennifer S… JohnB…
Sept fname minit ssn essn Franklin T Alice Franklin T … Theodore Franklin T … Joy Jennifer S … Abner John B … Michael John B … Alice John B … Elizabeth Employee ssn=essn Dependent... dependent_name
Sept 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
Sept Sample data Empl- name ssnBrith-dategradesenioritysalary A /2/122780k A /3/242560k B /1/24670k C /3/33780k
Sept Sample queries Empl- name ssnBrith-dategradesenioritysalary A /2/122780k A /3/242560k B /1/24670k C /3/33780k Find out names of all employees born before 1960/11/1
Sept More detailed discussion of relational algebra will be presented in a separate lecture!
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 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