An introduction to relational database RDB Sept. 20051.

Slides:



Advertisements
Similar presentations
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Advertisements

IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Defined by Edgar Codd in 1970 Defined by Edgar Codd in 1970 Considered ingenious but impractical Considered ingenious but impractical Conceptually simple.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Exploring Microsoft Access 2003 Chapter 4 Proficiency: Relational Databases, External Data, Charts, Pivot, and the Switchboard.
Database system concepts and architecture Sept. 2012Yangjun Chen ACS Outline: Concepts and Architecture (Chapter 2 – 3 rd, 4 th, 5 th, and 6 th ed.)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
- relation schema, relations - database schema, database state
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Database system concepts and architecture Winter 2007Ron McFadyen ACS Database schema Database system architecture Data independence concept Database.
Database: Review Sept. 2009Yangjun Chen ACS Database Introduction system architecture, Basic concepts, ER-model, Data modeling, B+-tree Hashing Relational.
Relational Data Model Sept. 2014Yangjun Chen ACS Outline: Relational Data Model Relational Data Model -relation schema, relations -database schema,
Database Architecture The Relational Database Model.
Chapter 5 Relational Model Concepts Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
1 DATABASE By Mr. Abdalla A. Shaame.  What is a database?  The main characters of a database system  The basic database design method  The entity-relationship.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Information storage: Introduction of database 10/7/2004 Xiangming Mu.
The Relational Model These slides are based on the slides of your text book.
1 Lecture 04 The relational data Model, Relational Constraints 1.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Review: Application of Database Systems
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
Relational Algebra - Chapter (7th ed )
SQL Structured Query Language Programming Course.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
Lecture2: Database Environment Prepared by L. Nouf Almujally & Aisha AlArfaj 1 Ref. Chapter2 College of Computer and Information Sciences - Information.
Database system concepts and architecture Jan. 2008Yangjun Chen ACS Outline: Concepts and Architecture Database schema Working process with a database.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Relational Model E.F. Codd at IBM 1970 Chapter 3 (ed. 7 – Chap. 5)
Review Database Application Development Access Database Development Theory Practice.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
Database Design The Relational Model Text Ch5
376a. Database Design Dept. of Computer Science Vassar College
Translation of ER-diagram into Relational Schema
Outline: Relational Data Model
Company Requirements.
1. Explain the following concepts: (a) superkey (b) key
1.(5) Describe the working process with a database system.
1. Explain the following concepts of the ER data model:
Presentation transcript:

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