D ATABASE S YSTEMS I T HE R ELATIONAL D ATA M ODEL.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
The Relational Model Ramakrishnan & Gehrke Chapter 3.
The Relational Model CS 186, Fall 2006, Lecture 2 R & G, Chap. 3.
The Relational Model CS 186, Spring 2006, Lecture 2 R & G, Chap. 1 & 3.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
The Relational Model Ramakrishnan & Gehrke, Chap. 3.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
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.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
The Relational Model These slides are based on the slides of your text book.
The Relational Model Chapter 3
Data Models Amandeep Kaur Lecturer GPC Khunimajra Call at
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
1 The Relational Model Instructor: Mohamed Eltabakh
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
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,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Levels of Abstraction in DBMS data Many views, single conceptual (logical) schema and physical schema. – Views describe how users see the data (possibly.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 3 The Relational Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Jianping Fan.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Xin Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Multi-billion dollar industry, $15+ bill in  Vendors:
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
CS 186, Fall 2006, Lecture 2 R & G, Chap. 3
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
The Relational Model Relational Data Model
The Relational Model Chapter 3
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
The Relational Model Chapter 3
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
Presentation transcript:

D ATABASE S YSTEMS I T HE R ELATIONAL D ATA M ODEL

22 W HAT IS A D ATA M ODEL ? A formal notation (language) for describing data. Structure of the data Conceptual model Higher level of abstraction than data structures in programming languages such as lists or arrays. Operations on the data Limited set of high level operations: queries and modifications. Speeds-up database programming. Allows DBS to optimize query execution, e.g. choice of most efficient sorting method. Constraints on the data Capture more of the real world meaning of the data.

33 W HY S TUDY THE R ELATIONAL M ODEL ? Most widely used model. Vendors: Oracle, IBM, Microsoft, Sybase, etc. “Legacy systems” in older models. E.g., IBM’s IMS Not so recent competitor: object-oriented model. ObjectStore, Versant, Ontos A synthesis emerging: object-relational model Informix Universal Server, Oracle, DB2 More recent competitor: semi-structured model. XML

44 R ELATIONAL D ATABASE : D EFINITIONS Relational database: a set of relations. Relation made up of 2 parts: Relation Schema : specifies name of relation, plus name and type of each column. E.g. Students( sid : string, name : string, login : string, age : integer, gpa : real). Relation Instance : a table, with rows and columns #rows = cardinality #columns = degree / arity

55 R ELATIONAL D ATABASE : D EFINITIONS Rows are called tuples (or records ), columns called attributes (or fields ). Attributes are referenced not by column number, but by name. Order of attributes does not matter Attribute types are called domains. Domains consist of atomic values such as integers or strings. No structured values such as lists or sets The order of tuples does not matter, a relation is a set of tuples. The order of tuples resulting from a relational query is undefined.

66 R ELATIONAL D ATABASE : D EFINITIONS To put it more formally: Domain: a set of logically connected values, e.g. string, integer, real. Relation R: k domains D 1,..., D k Cardinality = |R| Degree / Arity = k A set does not contain duplicates!

77 E XAMPLE Relation Students Cardinality = 3, degree = 5, all rows distinct. Do all columns in a relation instance have to be distinct?

88 T HE SQL L ANGUAGE Proposed by IBM (system R) in the 1970s. Later developed into a standard since relational data model used by many vendors. Structured Query Language (SQL): retrieval, insertion, updating, and deletion of data, management and administrative functions. All commercial DBSs support SQL, but with proprietary extensions to the standard language.

99 T HE SQL L ANGUAGE Major versions of the standard: SQL-86 first version SQL-92 major revision SQL-99 triggers, object-oriented features, etc. SQL-2003 XML-related features, window functions, etc. SQL-2006 importing XML data, publishing in XML format, integration of XQuery, etc.

10 T HE SQL L ANGUAGE SQL supports the creation CREATE ( ); modification INSERT INTO ( ) VALUES ( ); and querying of relational databases SELECT FROM WHERE ; Queries will be covered in separate chapter.

11 A DDING AND D ELETING T UPLES Can insert a single tuple using: INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, 18, 3.2)  Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students S WHERE S.name = ‘Smith’ * Powerful variants of these commands are available; more later!

12 C REATING R ELATIONS CREATE TABLE specifies the relation name and its attributes. The domain of each attribute is specified, and enforced by the DBMS whenever tuples are added or modified. Attributes can have zero or one value from their domain. NOT NULL specifies that this attribute must have exactly one value. CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL ); CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR (2) NOT NULL);

13 C REATING R ELATIONS SQL datatypes (domains): Character strings fixed length: CHAR(n) exactly n characters variable length: VARCHAR(n) up to n characters Bit strings fixed length: BIT(n) exactly n bits variable length: BIT VARYING(n) up to n bits BOOLEAN TRUE, FALSE, UNKNOWN Numbers INT / INTEGER FLOAT / REAL Dates and times (special character strings) DATE TIME

14 D ESTROYING AND A LTERING R ELATIONS Destroys the relation Students. The schema information and the tuples (table instance) are deleted. The schema of Students is altered by adding a new attribute; every tuple in the current instance is extended with a null value in the new attribute. DROP TABLE Students; ALTER TABLE Students ADD firstYear INTEGER;

15 A DDING AND D ELETING T UPLES INSERT INTO can insert a single tuple, by providing its attribute values: DELETE deletes all tuples satisfying some condition (e.g., name = Smith): More powerful variants of these commands are available; more later! INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, 18, 3.2); DELETE FROM Students WHERE name = ‘Smith’;

16 I NTEGRITY C ONSTRAINTS (IC S ) IC: condition that must be true for any instance of the database; e.g., domain constraints. ICs are specified when schema is defined. ICs are checked when instance is modified. A legal instance of a relation is one that satisfies all specified ICs. DBMS does not allow illegal instances. If the DBMS checks ICs, stored data is more faithful to real-world meaning. Avoids data entry errors, too!

17 P RIMARY K EYS AND C ANDIDATE K EYS A set of attributes is a key for a relation if: 1. No two distinct tuples can (!) have same values in all key attributes, and 2. Condition 1 is not true for any subset of the key. Condition 2 false? A superkey. E.g., sid is a key for Students. (What about name ?) The set { sid, gpa } is a superkey. For each key attribute values need to be provided, i.e. a key cannot have the special value null.

18 P RIMARY K EYS AND C ANDIDATE K EYS Artificial keys are often introduced, since they are fully under the control of the DBS / the enterprise, e.g., sid. Possibly many candidate keys (specified using UNIQUE), but exactly one primary key (specified using PRIMARY KEY). Primary key can be used to express references between tables and may also be used to optimize data storage. DBMS ensures that no two tuples share the same (primary or candidate) key value(s).

19 P RIMARY AND C ANDIDATE K EYS Used carelessly, an IC can prevent the storage of database instances that arise in practice! CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid) ); “For a given student and course, there is a single grade.” “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.” CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid), UNIQUE (cid, grade) );

20 F OREIGN K EYS, R EFERENTIAL I NTEGRITY Foreign key Set of attributes in one relation that is used to `refer’ to a tuple in another relation. Must correspond to primary key of the referred relation. `logical pointer’ E.g. sid in Enrolled is a foreign key referring to Students Enrolled( sid : string, cid : string, grade : string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.

21 F OREIGN K EYS IN SQL Only students listed in the Students relation are allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students); Enrolled Students

22 E NFORCING R EFERENTIAL I NTEGRITY Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students. What should be done if an Enrolled tuple with a non-existent sid is inserted? ( Reject it! ) What should be done if a Students tuple is deleted? Also delete all Enrolled tuples that refer to it. Disallow deletion of a Students tuple that is referred to. Set sid in Enrolled tuples that refer to it to a default sid. In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’. Similar for updates of primary keys.

23 R EFERENTIAL I NTEGRITY IN SQL SQL supports all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected). CASCADE (also delete all tuples that refer to deleted tuple). SET NULL / SET DEFAULT (sets foreign key value of referencing tuple). CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT );

24 W HERE DO IC S C OME F ROM ? ICs are based upon the semantics of the real-world enterprise that is being described in the database relations. We can check a database instance to see if an IC is violated, but we can never infer that an IC is true by looking at an instance. An IC is a statement about all possible instances! E.g., we know name is not a key, but the assertion that sid is a key is given to us. Key and foreign key ICs are the most common; more general ICs discussed later.

25 R ELATIONAL Q UERY L ANGUAGES A major strength of the relational model: supports simple, powerful querying of data. Queries can be written intuitively, and the DBMS is responsible for efficient evaluation. The key: precise semantics for relational queries. Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change.

26 T HE SQL Q UERY L ANGUAGE To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 To find just names and logins, replace the first line: SELECT S.name, S.login

27 Q UERYING M ULTIPLE R ELATIONS What does the following query compute? SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=“A” Given the following instance of Enrolled (is this possible if the DBMS ensures referential integrity?): we get:

28 V IEWS A view is just a relation, but we store a definition, rather than a set of tuples. CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21 Views can be dropped using the DROP VIEW command. How to handle DROP TABLE if there’s a view on the table? DROP TABLE command has options to let the user specify this.

29 V IEWS AND S ECURITY Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s). Given YoungStudents, but not Students or Enrolled, we can find students s who have are enrolled, but not the cid’s of the courses they are enrolled in.

30 S UMMARY The relational model is a tabular representation of data. A relation is a subset of the cartesian product of some domains. Simple and intuitive, currently the most widely used data model. SQL is the standard language for creating, updating and querying relational databases. Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks for violations. Two most important kinds of ICs: primary and foreign key constraints. In addition, we always have domain constraints.