The Relational Data Model

The Relational Data Model

Why Study the Relational Model? The Relational Model Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy systems” in older models Chapter 3 E.G., IBM’s IMS Recent competitor: object-oriented model ObjectStore, Versant, Ontos A synthesis emerging: object-relational model • Informix Universal Server, UniSQL, O2, Oracle, DB2 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Relational Database: Definitions Example Instance of Students Relation Relational database: a set of relations sid name login age gpa Relation: made up of 2 parts: 53666 Jones jones@cs 18 3.4 Instance : a table, with rows and columns. #Rows = cardinality, #fields = degree / arity. 53688 Smith smith@eecs 18 3.2 Schema : specifies name of relation, plus name and 53650 Smith smith@math 19 3.8 type of each column. • E.G. Students(sid: string, name: string, login: string, age: integer, gpa: real). Cardinality = 3, degree = 5, all rows distinct Can think of a relation as a set of rows or Do all columns in a relation instance have to tuples (i.e., all rows are distinct). be distinct? Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4 Relational Query Languages The SQL Query Language A major strength of the relational model: Developed by IBM (system R) in the 1970s supports simple, powerful querying of data. Need for a standard since it is used by many Queries can be written intuitively, and the vendors DBMS is responsible for efficient evaluation. Standards: The key: precise semantics for relational queries. SQL-86 Allows the optimizer to extensively re-order SQL-89 (minor revision) operations, and still ensure that the answer does SQL-92 (major revision) not change. SQL-99 (major extensions, current standard) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 The SQL Query Language Querying Multiple Relations What does the following query compute? To find all 18 year old students, we can write: SELECT S.name, E.cid FROM Students S, Enrolled E SELECT * sid name login age gpa WHERE S.sid=E.sid AND E.grade=“A” FROM Students S 53666 Jones jones@cs 18 3.4 WHERE S.age=18 53688 Smith smith@ee 18 3.2 Given the following instance sid cid grade of Enrolled (is this possible if 53831 Carnatic101 C 53831 Reggae203 B •To find just names and logins, replace the first line: the DBMS ensures referential 53650 Topology112 A integrity?): SELECT S.name, S.login 53666 History105 B we get: S.name E.cid Smith Topology112 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8 Creating Relations in SQL Destroying and Altering Relations Creates the Students CREATE TABLE Students DROP TABLE Students relation. Observe that the (sid: CHAR(20), name: CHAR(20), type (domain) of each field Destroys the relation Students. The schema login: CHAR(10), is specified, and enforced by information and the tuples are deleted. age: INTEGER, the DBMS whenever tuples gpa: REAL) are added or modified. ALTER TABLE Students As another example, the ADD COLUMN firstYear: integer Enrolled table holds CREATE TABLE Enrolled (sid: CHAR(20), The schema of Students is altered by adding a information about courses cid: CHAR(20), new field; every tuple in the current instance that students take. grade: CHAR(2)) is extended with a null value in the new field. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10 Adding and Deleting Tuples Integrity Constraints (ICs) Can insert a single tuple using: IC: condition that must be true for any instance of the database; e.g., domain constraints. INSERT INTO Students (sid, name, login, age, gpa) ICs are specified when schema is defined. VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2) ICs are checked when relations are modified. Can delete all tuples satisfying some A legal instance of a relation is one that satisfies condition (e.g., name = Smith): all specified ICs. DELETE DBMS should not allow illegal instances. FROM Students S If the DBMS checks ICs, stored data is more WHERE S.name = ‘Smith’ faithful to real-world meaning. Avoids data entry errors, too! * Powerful variants of these commands are available; more later! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 Primary Key Constraints Primary and Candidate Keys in SQL Possibly many candidate keys (specified using A set of fields is a key for a relation if : UNIQUE), one of which is chosen as the primary key. 1. No two distinct tuples can have same values in all “For a given student and course, CREATE TABLE Enrolled key fields, and there is a single grade.” vs. (sid CHAR(20) 2. This is not true for any subset of the key. “Students can take only one cid CHAR(20), grade CHAR(2), Part 2 false? A superkey. course, and receive a single grade for that course; further, no two PRIMARY KEY (sid,cid) ) If there’s >1 key for a relation, one of the keys is students in a course receive the CREATE TABLE Enrolled chosen (by DBA) to be the primary key. same grade.” (sid CHAR(20) E.g., sid is a key for Students. (What about Used carelessly, an IC can prevent cid CHAR(20), name?) The set {sid, gpa} is a superkey. the storage of database instances grade CHAR(2), that arise in practice! PRIMARY KEY (sid), UNIQUE (cid, grade) ) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14 Foreign Keys, Referential Integrity Foreign Keys in SQL Only students listed in the Students relation should Foreign key : Set of fields in one relation that is used be allowed to enroll for courses. to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), Like a `logical pointer’. PRIMARY KEY (sid,cid), E.g. sid is a foreign key referring to Students: FOREIGN KEY (sid) REFERENCES Students ) Enrolled(sid: string, cid: string, grade: string) Enrolled sid cid grade Students If all foreign key constraints are enforced, referential 53666 Carnatic101 C sid name login age gpa integrity is achieved, i.e., no dangling references. 53666 Reggae203 B 53666 Jones jones@cs 18 3.4 Can you name a data model w/o referential integrity? 53650 Topology112 A 53688 Smith smith@eecs 18 3.2 • Links in HTML! 53666 History105 B 53650 Smith smith@math 19 3.8 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16 Enforcing Referential Integrity Referential Integrity in SQL Consider Students and Enrolled; sid in Enrolled is a SQL/92 and SQL:1999 foreign key that references Students. CREATE TABLE Enrolled support all 4 options on (sid CHAR(20), What should be done if an Enrolled tuple with a deletes and updates. cid CHAR(20), non-existent student id is inserted? (Reject it!) Default is NO ACTION grade CHAR(2), What should be done if a Students tuple is deleted? (delete/update is rejected) PRIMARY KEY (sid,cid), Also delete all Enrolled tuples that refer to it. CASCADE (also delete FOREIGN KEY (sid) REFERENCES Students Disallow deletion of a Students tuple that is referred to. all tuples that refer to ON DELETE CASCADE Set sid in Enrolled tuples that refer to it to a default sid. deleted tuple) ON UPDATE SET DEFAULT ) (In SQL, also: Set sid in Enrolled tuples that refer to it to a SET NULL / SET DEFAULT special value null, denoting `unknown’ or `inapplicable’.) (sets foreign key value Similar if primary key of Students tuple is updated. of referencing tuple) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18 Where do ICs Come From? Logical DB Design: ER to Relational ICs are based upon the semantics of the real- world enterprise that is being described in the Entity sets to tables: database relations. We can check a database instance to see if an CREATE TABLE Employees IC is violated, but we can NEVER infer that name (ssn CHAR(11), an IC is true by looking at an instance. ssn lot name CHAR(20), lot INTEGER, An IC is a statement about all possible instances! Employees PRIMARY KEY (ssn)) From example, 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 supported too. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20 Relationship Sets to Tables Review: Key Constraints since In translating a relationship name dname CREATE TABLE Works_In( Each dept has at set to a relation, attributes of ssn lot did budget ssn CHAR(1), most one manager, the relation must include: did INTEGER, according to the Keys for each since DATE, key constraint on Employees Manages Departments participating entity set PRIMARY KEY (ssn, did), Manages. (as foreign keys). FOREIGN KEY (ssn) • This set of attributes REFERENCES Employees, forms a superkey for FOREIGN KEY (did) the relation. REFERENCES Departments) Translation to All descriptive attributes. relational model? 1-to-1 1-to Many Many-to-1 Many-to-Many Database Management Systems 3ed, R.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us