CSC 453 Database Technologies

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

Chapter 3 : Relational Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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.
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.
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.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
The Relational Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
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 Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
1 The Relational Model Instructor: Mohamed Eltabakh
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
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,
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
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 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.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Standards and Conventions
Fundamentals of DBMS Notes-1.
Logical DB Design: ER to Relational
COP Introduction to Database Structures
COP4710 Database Systems Relational Model.
Constraints and Triggers
Designing Tables for a Database System
Translation of ER-diagram into Relational Schema
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Relational Data Model
CS4222 Principles of Database System
SQL OVERVIEW DEFINING A SCHEMA
SQL data definition using Oracle
Translation of ER-diagram into Relational Schema
CSC 453 Database Systems Lecture
Defining a Database Schema
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
CMPT 354: Database System I
Oracle Data Definition Language (DDL)
CSC 453 Database Technologies
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CSC 453 Database Systems Lecture
Database Design: Relational Model
A Very Brief Introduction to Relational Databases
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Instructor: Mohamed Eltabakh
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CMSC-461 Database Management Systems
The Relational Model Content based on Chapter 3
CS 405G: Introduction to Database Systems
CSC 453 Database Technologies
CSC 453 Database Technologies
CSC 453 Database Systems Lecture
CSC 453 Database Systems Lecture
Presentation transcript:

CSC 453 Database Technologies Tanu Malik DePaul University

Pop Quiz A systematic way of organizing and accessing a collection of data is called a ------

A Data Model A notation for describing data or information. Consists of mostly 3 parts: Structure of the data Data structures and relationships Operations on the data programming operations Constraints on the data Describe limitations on data Persistence of data Data is stored permanently on disk

Data Models Models have evolved: Files Hierarchical Network Relational Key-value Graph Document Array/Matrix Column

DBMS Evolution 1960s: Codasyl (network), IMS (hierarchical) 1970s: Relational Model (System R, Ingres) 1980: SQL as a language for RDBMS 1990: Object-Relational Model (disk-based databases) 2000’s: NoSQL 2010: NewSQL (Main-memory databases)

Relational Model Very popular Provides program-data independence Create a 3 level architecture

Program-Data Independence View-Level What data is exposed to users, what are they allowed to see Logical Level Definition of tables, attributes, constraints, etc Physical Level Data stored in files, location of files, indexed, storage layout

3-level architecture View 1 View 2 Table Storage

Program-Data Independence Logical Data Independence Modify table definitions without changing view used in an application Add/drop/rename attributes from a table Rename a table Physical Data Independence Modify the structure and organizaton of physical storage with changing logical schema Change the type of index Compress a table when it is stored on disk Move a table to another disk/machine

Relational Model-Relation Data are divided into two-dimensional tables called relations, each representing some type of entity recorded in the database Each relation has a name, describing what entity it represents

Example of a Relation Attributes Records Schema: name of relation, and name and type of each column/attribute Lastname SID Firstname City Started 192-83-332 019-28-553 192-83-290 321-12-312 019-28-321 Johnson Smith Chan Marcus Deepa Epel Shaw Gloria Brenningan Patel Palo Alto Chicago Naperville 2010 2011 2013 2015 Records Instance = rows/records with values for each column Schema = Student(Sid, Firstname, Lastname, City, Started) This whole table is an instance of the Student schema.

Properties of a Relation A relation is a set of records/tuples All rows are distinct Order of rows does not matter What about columns (?) Values in a record Each column has a domain from which its values are taken A domain consists of atomic values – no multi-valued or divisible attributes are allowed Missing/unknown values are indicated by NULL

Typical Domains Numerical values Character strings Dates and times integers, fixed-point values, floating-point values Character strings fixed-length or variable-length Dates and times DATE 'YYYY-MM-DD’ TO_DATE('1998-DEC-25 17:30','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')

Relation-Mathematical Equivalence A binary relation on a set A is a collection of ordered pairs of elements of A. It is subset of cartesian product A2 = A x A A database relation is a n-ary relation on a set A with attributes A1, A2, …, An Data in a database relation is a subset of A1 x A2 x … x An

Relational Model-Implementation

SQL- A language for relational DBs Data Definition Language (DDL) Create, modify, delete relations Specify constraints Administer users, security, etc

Creating a Table CREATE TABLE TABLE_NAME ( Attribute1 DOMAIN_1 , Attribute2 DOMAIN_2 , ... AttributeN DOMAIN_N ); You can list as many attributes and domains as you want, separated by commas

SQL Domains Numerical domains: Postgres: Integer Oracle: NUMBER: A general floating-point number (also REAL, FLOAT) NUMBER(*, 0): A general integer (also INTEGER, INT) NUMBER(n): An n-digit integer NUMBER(x, y): A fixed-precision number with x total digits, and y digits to the right of the decimal point (also DECIMAL, NUMERIC)

SQL Domains String domains: Dates: CHAR(n): A fixed-length string of n characters VARCHAR(m) or VARCHAR2(m): A variable-length string of up to m characters Dates: DATE: A date in ‘dd-mon-yy’ format (dd = day, mon = month, yy = year) (There are often variations in the date format in different SQL implementations…)

Student database "LASTNAME" "FIRSTNAME" "SID" "PROGRAM" "CITY" "STARTED" "Gloria" "Chan" "12300" "COMP-SCI" “Naperville” “2010” "Brennigan" "Marcus" "90421" "COMP-GAM" "Evanston" "2010" "Patel" "Deepa" "14662" "COMP-SCI" "Evanston" "2013" How do you store important data digitally?

Example create table student ( LastName varchar(40), FirstName varchar(40), SID number(5), SSN number(9), Career varchar(4), Program varchar(10), City varchar(40), Started number(4) );

Record Course Data "CID" "COURSENAME" "DEPARTMENT" "COURSENR" "1020" "Theory of Computation" "CSC" "489" "1092" "Cryptography" "CSC" "440" "3201" "Data Analysis" "IT" "223" "9219" "Databases Systems" "CSC" "355" "3111" "Theory of Computation" "CSC" "389" "8772" "History of Games" "GAM" "206" "2987" "Topics in Digital Cinema" "DC" "270"

Example create table course ( CID number(4), CourseName varchar(40), Department varchar(4), CourseNr char(3) );

Record Enrollments "STUDENTID" "COURSEID" "QUARTER" "YEAR" "11035" "3201" "Fall" "2015" "11035" "1020" "Fall" "2012" "11035" "1092" "Fall" "2012" "75234" "3201" "Winter" "2012" "8871" "1092" "Fall" "2013" "39077" "1092" "Fall" "2013" "57923" "9219" "Winter" "2013" "19992" "3201" "Winter" "2013"

Example create table enrolled( StudentID number(7), CourseID number(4), Quarter varchar(40), Year varchar(4) );

Populating a Table To insert a record into a table: INSERT INTO TABLE_NAME VALUES ( value1, value2, value3, ... ); Values of attributes must be given in the same order as in the schema

Example insert into student values ( 'Brennigan', 'Marcus', 90421, 987654321, 'UGRD', 'COMP-GAM', 'Evanston', 2010 ); values ( 'Patel', 'Deepa', 14662, null, 'GRD', 'COMP-SCI', 'Evanston', 2013 ); values ( 'Snowdon', 'Jonathan', 08871, 123123123, 'GRD', 'INFO-SYS', 'Springfield', 2009 ); insert into course values ( 1020, 'Theory of Computation', 'CSC', 489); values ( 1092, 'Cryptography', 'CSC', 440); values ( 3201, 'Data Analysis', 'IT', 223);

Deleting Tables Deleting entire table: Deleting specific rows: drop table student; Deleting specific rows: delete from student where name = ‘Smith’;

Constraints

Constraints Constraints are maintained by the DBMS: Domain constraints All values of each attribute must come from the specified domain Custom constraints E.g. account balance must be positive Integrity constraints Uniqueness, association

Defaults and Constraints After attribute and domain, before comma: Add default value for the attribute with DEFAULT value Disallow NULL values with NOT NULL Force values to be unique with UNIQUE Impose other constraints with CHECK (condition) e.g., CHECK (low <= Attribute AND Attribute <= high) Applied whenever a tuple is added/modified

Example create table student ( LastName varchar(40) not null, FirstName varchar(40), SID number(5), SSN number(9) unique, Career varchar(4), Program varchar(10), City varchar(40), Started number(4) );

Primary Keys A set of attributes in the relation may be defined to be the primary key – it must be a minimal set of attributes that uniquely identifies each tuple in the relation NULL values are not allowed in the primary key of a relation (“entity integrity”) Primary Key = = Unique, Not Null The primary key is underlined in the schema

Example create table course ( CID number(4), CourseName varchar(40), Department varchar(4), CourseNr char(3), primary key (CID) );

Example create table student ( LastName varchar(40) not null, FirstName varchar(40), SID number(5), SSN number(9), Career varchar(4), Program varchar(10), City varchar(40), Started number(4), primary key (SID), unique(SSN) );

Exercise create table student ( LastName varchar(40), FirstName varchar(40), SID number(5), SSN number(9), Career varchar(4), Program varchar(10), City varchar(40), Started number(4), primary key (SID), unique(SSN) ); create table student ( LastName varchar(40), FirstName varchar(40), SID number(5), SSN number(9), Career varchar(4), Program varchar(10), City varchar(40), Started number(4), primary key (SID, SSN), );

Exercise create table student ( LastName varchar(40), FirstName varchar(40), SID number(5), SSN number(9) not null, Career varchar(4), Program varchar(10), City varchar(40), Started number(4), primary key (SID), unique(SSN) ); create table student ( LastName varchar(40), FirstName varchar(40), SID number(5), SSN number(9), Career varchar(4), Program varchar(10), City varchar(40), Started number(4), primary key (SID, SSN), );

Foreign Keys Set of fields `refering’ to a tuple in another relation Must correspond to the primary key of other relation Like a `logical pointer’

Foreign Keys SID Lastname Firstname SSN 90421 Brennigan Marcus 987654321 14662 Patel Deepa NULL 08871 Snowdon Jon 123123123 StudentID CourseID Quarter Year 90421 1020 Fall 2016 14662 3201 Spring 08871 2987

Example create table enrolled ( StudentID number(5), CourseID number(4), Quarter varchar(6), Year number(4), primary key (StudentID, CourseID), foreign key (StudentID) references student(SID), foreign key (CourseID) references course(CID) );

Exercise Create table for Studentgroups Create table Members Groupname, President, Founded President of a SG is a student Studentgroups are unique Create table Members Students are members of groups Students join groups in a given year

Exercise CREATE TABLE movie( mid integer, title varchar(50), year integer director name varchar(50) not null, language varchar(30), primary key (mid), foreign key director name references director, foreign key (leading actor) references actor

Enforcing Integrity Constraints

Enforcing Referential Integrity Can only insert or update a tuple if the value of every foreign key in the tuple appears among the values of the primary key that it references SID Lastname Firstname SSN 90421 Brennigan Marcus 987654321 14662 Patel Deepa NULL 08871 Snowdon Jon 123123123 StudentID CourseID Quarter Year 90421 1020 Fall 2016 14662 3201 Spring 08871 2987 Insert (40563, 1020, ‘Fall’, `2016’): Reject such an insertion

Can only delete or update a tuple if the value of its primary key does not appear among the values of any of the foreign keys that reference it SID Lastname Firstname SSN 90421 Brennigan Marcus 987654321 14662 Patel Deepa NULL 08871 Snowdon Jon 123123123 StudentID CourseID Quarter Year 90421 1020 Fall 2016 14662 3201 Spring 08871 2987 Delete from Student where SID = 90421 Reject such a deletion

Handling Violations Operations that violate constraints are by default rejected, but there are other options: Prompt user to change offending value(s) Use default value in place of offending value(s) Use NULL in place of offending value(s) Allow operation, but cascade changes from primary keys to foreign keys as needed

Specified as ON DELETE/UPDATE We can specify actions if referential integrity of a foreign key is violated SET NULL SET DEFAULT CASCADE Specified as ON DELETE/UPDATE SET NULL/CASCADE/DEFAULT

Forcing Deletions CASCADE Constraints Remove referencing tuples before referenced tuples create table enrolled ( StudentID number(5), CourseID number(4), Quarter varchar(6), Year number(4), primary key (StudentID, CourseID), foreign key (StudentID) references student(SID), foreign key (CourseID) references course(CID) on delete cascade );

Referential triggered action Example (CASCADE) CREATE TABLE dependent ( ... FOREIGN KEY (essn) REFERENCES employee(ssn) ON DELETE CASCADE, ...) Example (SET NULL) CREATE TABLE studentgroup ( FOREIGN KEY (PresidentID) REFERENCES student(SID) ON DELETE SET NULL Example (SET DEFAULT) CREATE TABLE employee ( ... dno INT NOT NULL DEFAULT 1, FOREIGN KEY (dno) REFERENCES department(dnumber) ON DELETE SET DEFAULT ...)

Enforcing Integrity Constraints-Summary Integrity constraints are specified when schema is defined They must be true at all times Must be checked when relations are modified A DBMS must be responsible for checking them (as opposed to?) Integrity constraints come from applications; a DB instance is never a good evidence to infer ICs

Relation Schema Modifications

ALTER TABLE ALTER TABLE TABLE_NAME … ADD Attribute DOMAIN; or DROP COLUMN Attribute CASCADE CONSTRAINTS; Modifies an existing table schema

Examples Exercise: Add a (named) constraint that 0 <= age <= 120 ALTER TABLE student ADD age integer; Exercise: Add a (named) constraint that 0 <= age <= 120 ALTER TABLE studentgroup ADD FOREIGN KEY(PresidentID) REFERENCES Student(SID); ADD CONSTRAINT fk_sg FOREIGN KEY(PresidentID) REFERENCES Student(SID); ALTER TABLE studentgroup DROP fk_sg;

Cyclic Dependencies Most systems do not allow references to tables that do not exist yet. Two solutions: if no cyclical dependencies: create tables in right order (Example: university.sql) in case of cyclical dependencies: create tables without f.k. constraints, and use ALTER TABLE to add these later

Attribute-level Check create table enrolled ( StudentID number(5), CourseID number(4), Quarter varchar(6) CHECK(quarter in ('Fall','Winter','Spring')), Year number(4), … create table memberof ( GroupName varchar(40), Joined number(4) CHECK(Joined >= (SELECT Started FROM student WHERE studentID = SID)), ... has to be true (compare WHERE)
 attribute checks get evaluated when an attribute is modified i.e. when row is inserted/updated 
 subqueries not allowed in Oracle checks

Tuple-level CHECK create table course ( CID number(4), CourseName varchar(40), Department varchar(4), CourseNr char(3), primary key (CID), check (department <> 'CSC' OR CourseNR > 100) ); same as attribute level check, just involves any number of attributes and different placement