SQL DDL. CS3 Database Systems Weeks 4-5 SQL DDL Database design. Key Constraints. Inclusion Constraints

Similar documents
DBS Database Systems Designing Relational Databases

Conceptual Design. The Entity-Relationship (ER) Model

High Level Database Models

MIS Database Systems Entity-Relationship Model.

The Entity-Relationship Model. Overview of Database Design

Review The Big Picture

OVERVIEW OF DATABASE DEVELOPMENT

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

High-Level Database Models (ii)

Database Applications (15-415)

CMPT 354 Database Systems I

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

The Relational Model 2. Week 3

The Relational Model (ii)

Database Applications (15-415)

The Relational Model. Chapter 3

The Relational Model

From ER to Relational Model. Book Chapter 3 (part 2 )

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Database Management Systems. Chapter 3 Part 2

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

CSE 530A. ER Model. Washington University Fall 2013

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

The Entity-Relationship Model

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Conceptual Design. The Entity-Relationship (ER) Model

Introduction to Database Design

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Introduction to Database Design

SYLLABUS ADMIN DATABASE SYSTEMS I WEEK 2 THE ENTITY-RELATIONSHIP MODEL. Assignment #2 changed. A2Q1 moved to A3Q1

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

The Relational Model

CIS 330: Applied Database Systems

The Entity-Relationship Model. Overview of Database Design. ER Model Basics. (Ramakrishnan&Gehrke, Chapter 2)

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

The Relational Model

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

Conceptual Design with ER Model

Introduction to Database Design

Database Systems ( 資料庫系統 )

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

Introduction to Database Design. Dr. Kanda Runapongsa Dept of Computer Engineering Khon Kaen University

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Database Design. ER Model. Overview. Introduction to Database Design. UVic C SC 370. Database design can be divided in six major steps:

The Entity-Relationship Model. Steps in Database Design

Entity-Relationship Models: Good Design and Constraints

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

CS 146 Database Systems


CS W Introduction to Databases Spring Computer Science Department Columbia University

Modeling Your Data. Chapter 2. cs542 1

The Relational Model. Week 2

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

Overview. Introduction to Database Design. ER Model. Database Design

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #5: Entity/Relational Models---Part 1

Database Systems. Course Administration

Database Design. Goal: specification of database schema Methodology:

The Entity-Relationship (ER) Model

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

The Entity-Relationship Model

Using High-Level Conceptual Data Models for Database Design A Sample Database Application Entity Types, Entity Sets, Attributes, and Keys

Database Applications (15-415)

CS54100: Database Systems

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Database Applications (15-415)

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Advance Database Management System

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Chapter 2: Entity-Relationship Model

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

The Entity-Relationship Model

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

CSE 344 MAY 14 TH ENTITIES

CSE 344 AUGUST 1 ST ENTITIES

Introduction to Data Management CSE 344

CS/INFO 330 Entity-Relationship Modeling. Announcements. Goals of This Lecture. Mirek Riedewald

Lecture 4. Lecture 4: The E/R Model

Informatics 1: Data & Analysis

Introduction to Databases

COMP Instructor: Dimitris Papadias WWW page:

CIS 330: Web-driven Web Applications. Lecture 2: Introduction to ER Modeling

The Entity-Relationship (ER) Model 2

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Introduction to Data Management CSE 344

2. DatabaseDesign. Master I Software Engineering. Dr. Imed Bouchrika Dept of Mathematics & Computer Science University of Souk-Ahras

System Concepts and Architecture. Rose-Hulman Institute of Technology Curt Clifton

Entity/Relationship Modelling

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Introduction to Data Management. Lecture #3 (Conceptual DB Design) Instructor: Chen Li

The Relational Model an Introduction. DBS Database Systems The Relational Model, the Relational Algebra and SQL. Why is the database like this?

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Design & Deployment

Transcription:

SQL DDL CS3 Database Systems Weeks 4-5 SQL DDL Database design In its simplest use, SQL s Data Definition Language (DDL) provides a name and a type for each column of a table. CREATE TABLE Hikers ( HId INTEGER, HName CHAR(40), Skill CHAR(3), Age INTEGER ) 31 Oct, 2008 In addition to describing the type or a table, the DDL also allows you to impose constraints. We ll deal with two kinds of constraints here: key constraints and inclusion constraints Key Constraints Inclusion Constraints A key is a subset of the attributes that uniquely identifies a tuple, and for which no subset of the key has this property. CREATE TABLE Hikers ( HId INTEGER, HName CHAR(30), Skill CHAR(3), Age INTEGER, PRIMARY KEY (HId) ) CREATE TABLE Climbs ( HId INTEGER, Date DATE, Time INTEGER, PRIMARY KEY (HId, MId) ) Updates that violate key constraints are rejected. Do you think the key in the second example is the right choice? A field in one table may refer to a tuple in another relation by indicating its key. The referenced tuple must exist in the other relation for the database instance to be valid. For example, we expect any MId value in the Climbs table to be included in the MId column of the Munros table. SQL provides a restricted form of inclusion constraint, foreign key constraints. CREATE TABLE Climbs ( HId INTEGER, Date DATE, Time INTEGER, PRIMARY KEY (HId, MId), FOREIGN KEY (HId) REFERENCES Hikers(HId), FOREIGN KEY (MId) REFERENCES Munros(MId) )

Conceptual Modelling and Entity-Relationship Diagrams Conceptual Modelling a Caution [R&G Chapter 2] Obtaining a good database design is one of the most challenging parts of building a database system. The database design specifies what the users will find in the database and how they will be able to use it. For simple databases, the task is usually trivial, but for complex databases required that serve a commercial enterprise or a scientific discipline, the task can daunting. One can find databases with 1000 tables in them! A commonly used tool to design databases is the Entity Relationship (E-R) model. The basic idea is simple: to conceptualize the database by means of a diagram and then to translate that diagram into a formal database specification (e.g. SQL DDL) There are many tools for conceptual modelling some of them (UML, Rational Rose, etc.) are designed for the more general task of software specification. E-R diagrams are a subclass of these, intended specifically for databases. They all have the same flavour. Even within E-R diagrams, no two textbooks will agree on the details. We ll follow R&G, but be warned that other texts will use different conventions (especially in the way many-one and many-many relationships are described.) Unless you have a formal/mathematical grasp of the meaning of a diagram, conceptual modelling is almost guaranteed to end in flawed designs. Conceptual ER diagrams the basics What are the entities and relationships that we want to describe? What information about entities and relationships should we store in the database? What integrity constraints hold? Represent this information pictorially in an E-R diagram, then map this diagram into a relational schema (SQL DDL.) In ER diagrams we break the world down into three kinds of things: Attributes. These are the things that we typically use as column names: Name, Age, Height, Address etc. Attributes are drawn as ovals: Name Entities. These are the real world objects that we want to represent: Students, Courses, Munros, Hikers,.... A database typically contains sets of entitites. Entity sets are drawn as boxes: Courses Relationships. This describes relationships among entitites, e.g. a students enrolls in a course, a hiker climbs a Munro,... Relationships are drawn as diamonds: Enrolls

Drawing Entity Sets Drawing Relatonships The terms entity and entity set are often confused. Remember that boxes describe sets of entities. To draw an entity set we simpy connect it with its attributes Munros We connect relationships to the entities they relate. However relationships can also have attributes. Note that Date and Time apply to Climbs not to Hikers or Munros. We connect relationships to enitity sets and attributes in the same way that we connected entity sets to attributes. Climbs MId MName Lat Long Rating Note that we have indicated the key for this entity set by underlining it. Height Hikers Date Time Munros The whole diagram Obtaining the relational schema from an ER diagram Munros Date Climbs Time Hikers MId MName Lat Long Rating Height HId HName Skill Age Note that lines connect entitites to attributes and relationships to entities and to attributes. They do not connect attributes to attributes, entitites to entities, or relationships to relationships. This is a toy diagram. Real ER diagrams can cover a whole wall or occupy a whole book! We now translate the ER diagram into a relational schema. Roughly speaking (this will not always be the case) we generate a table for each entity and a table each relationship. For each entity we generate a relation with the key that is specified in the ER diagram. For example (SQL DDL) CREATE TABLE Munros ( MName CHAR(30), Lat REAL, Long REAL, Height INTEGER, Rating REAL, PRIMARY KEY (RId) ) CREATE TABLE Hikers ( HId INTEGER, HName CHAR(30), Skill CHAR(3), Age INTEGER, PRIMARY KEY (HId) )

Obtaining the relational schema continued Many-one Relationships For each relationship we generate a relation scheme with attributes The key(s) of each associated entity Additional attribute keys, if they exist The associated attributes. Also, the keys of associated attributes are foreign keys. CREATE TABLE Climbs ( HId INTEGER, Date DATE, Time REAL, PRIMARY KEY (HId,MId), also Date? FOREIGN KEY (HId) REFERENCES Hikers, FOREIGN KEY (MId) REFERENCES Munros ); The relationship Climbs represents among other things a relation (in the mathematical sense) between the sets associated with Munros and Hikers. That is, a subset of the set of Munro/Hiker pairs. This is a many-many relation, but we need to consider others. Many Many Many one One Many One one A Many-one relationship The Associated DDL Consider the relationship between and Departments. An Employee works in at most one department. There is a many-one relationship between and Departments indicated by an arrow emanating from Works in EmpID Name... DName Departments Addr... Note that an employee can exist without being in a department, and a department need not have any employees. CREATE TABLE Departments ( Address CHAR(80), PRIMARY KEY (DeptId) ) CREATE TABLE ( NAME CHAR(10) PRIMARY KEY (EmpId) ) CREATE TABLE WorksIn ( PRIMARY KEY (EmpId), FOREIGN KEY (EmpId) REFERENCES, FOREIGN KEY (DeptID) REFERENCES Departments ) CREATE TABLE ( NAME CHAR(10), and PRIMARY KEY (EmpId), FOREIGN KEY DeptID REFERENCES Departments ) The key for WorksIn has migrated to. or

1 1 Relationships? Participation Constraints These are uncommon and are typically created by database fusion. Book ISBN Same as US Book...... LOC id Why can t one migrate a key in this case? Suppose we also want to assert that every employee must work in some department. This is indicated (R&G convention) by a thick line. Works in EmpID Name... DName CREATE TABLE Departments ( Address CHAR(80), PRIMARY KEY (EmpId) ) and Many-one = partial function many-one + participation= total function Departments Addr... CREATE TABLE ( NAME CHAR(10), DeptID INTEGER NOT NULL, PRIMARY KEY (EMPID), ) FOREIGN KEY DeptID REFERENCES Departments Labelled Edges ISA relationships It can happen that we need two edges connecting an entity set with (the same) relationship. Assemb Made from Part Sub Qty Id Name... When one sees a figure like this there is typically a recursive query associated with it, e.g., List all the parts needed to make a widget. What are the key and foreign keys for Made-from? An isa relationship indicates that one entity is a special kind of another entity. Persons ISA Salary... Persons Id Name... Id Name... Salary... The textbook draws this relationship as shown on the left, but the right-hand representation is also common. This is not the same as o-o inheritance. Whether there is inheritance of methods depends on the representation and the quirks of the DBMS. Also note that, we expect some form of inclusion to hold between the two entity sets.

Relational schemas for ISA Disjointness in ISA relationships CREATE TABLE Persons ( Id INTEGER, Name CHAR(22),... PRIMARY KEY (Id) ) CREATE TABLE ( Id INTEGER, Salary INTEGER,... PRIMARY KEY (Id), FOREIGN KEY (Id) REFERENCES Persons ) A problem with this representation is that we have to do a join whenever we want to do almost any interesting query on. An alternative would be to have all the attributes of Persons in a disjoint table. What is the disadvantage of this representation? Are there other representations? When we have two entities that are both subclasses of some common entity it is always important to know whether they should be allowed to overlap. Persons ISA Students Can a person be both a student and an employee? There are no mechanisms in SQL DDL for requiring the two sets to be exclusive. However it is common to want this constraint and it has to be enforced in the applications that update the database. Weak Entities Weak Entities the DDL An entity that depends on another entity for its existence is called a weak entity. Id Person Owns Portfolio... FId Mgr... Contains PId Purchase Symbol In this example a Purchase cannot exist unless it is in a Portfolio. The key for a Purchase may be a compound FId/PId. Weak entities are indicated in R&G by thick lines round the entity and relationship. Weak entities tend to show up in XML design. The hierarchical structure limits what we can do with data models. Qty Date CREATE TABLE Portfolio ( FId INTEGER, Owner INTEGER, Mgr CHAR(30), PRIMARY KEY (FId), FOREIGN KEY (Owner) REFERENCES Person(Id) ) CREATE TABLE Purchase ( PId INTEGER, FId INTEGER, Symbol CHAR(5), QTY INTEGER, Date DATE PRIMARY KEY (FId, PId), FOREIGN KEY (FId) REFERENCES Portfolio ON DELETE CASCADE ) ON DELETE CASCADE means that if we delete a portfolio, all the dependent Purchase tuples will automatically be deleted.

Other stuff you may find in E-R diagrams E-R Diagrams, Summary Cardinality constraints, e.g., a student can enroll in at most 4 courses. Aggregation the need to entitise a relationship. Ternary or n-ary relationships. No problem here, but our diagrams aren t rich enough properly to extend the notion of many-one relationships. It is very easy to go overboard in adding arbitrary features to E-R diagrams. Translating them into types/constraints is another matter. E-R diagrams and related techniques are the most useful tools we have for database design. The tools tend to get over-complicated, and the complexities don t match the types/constraint systems we have in DBMSs There is no agreement on notation and little agreement on what basic E-R diagrams should contain. The semantics of E-R diagrams is seldom properly formalized. This can lead to a lot of confusion. Review Basics, many-one, many-many, etc. Mapping to DDL Entitising relationships. Participation, ISA, weak entities.