Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.

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.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
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.
Introduction to Structured Query Language (SQL)
SPRING 2004CENG 3521 The Relational Model Chapter 3.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
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.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
1 Introduction to Web Application Introduction to Data Base.
Introduction to Structured Query Language (SQL)
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Database Systems Lecture 5 Natasha Alechina
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
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. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Database Technical Session By: Prof. Adarsh Patel.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
1 The Relational Model Instructor: Mohamed Eltabakh
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
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,
Relational Database. Database Management System (DBMS)
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
11/07/2003Akbar Mokhtarani (LBNL)1 Normalization of Relational Tables Akbar Mokhtarani LBNL (HENPC group) November 7, 2003.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Programming Logic and Design Fourth Edition, Comprehensive Chapter 16 Using Relational Databases.
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 PartII (Chapter 3)
INTRODUCTION lecture1 1. Data base concept Data is a meaningless static value. What does 3421 means? Information is the data you process in a manner that.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 3: Relational Databases
Lecture 03 Constraints. Example Schema CONSTRAINTS.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
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.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
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.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Fundamentals of DBMS Notes-1.
Constraints and Triggers
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Introduction lecture1.
Instructor: Mohamed Eltabakh
The Relational Model Relational Data Model
Relational Database Design
Instructor: Mohamed Eltabakh
Presentation transcript:

Relational Database Management Systems

A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and deleting data Security Integrity (concurrent access, locking, logging, application defined rules) Back ups and recovery Common DBMS: Oracle, MS SqlServer, Postgres, MySQL Database Management System (DBMS) 2

Atomicity: prevents partial transactions, a series of operations either fully occurs or nothing occurs Updating 10 records in a single transaction Consistency: ensures the database remains in consistent state (all rules are satisfied) before the start of a transaction and after the transaction is over (successful or not) Deleting a value that should not be null is not allowed Isolation: ability of the database to handle concurrent transactions Durability: ensures that once the transaction is successful, the change will persist ACID Properties 3

Data model: a collection of concepts that describes the data Relational model: Data is stored in tables (relations) Each relation consists of rows and columns A schema describes the relations (their columns) and the relationships between them Relational Database: a set of relations Data Model 4

Example 5 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… Students Table Schema for table students: Students (studentId: integer, firstname: string, lastname: string, dateOfBirth: date, gpa: float)

Example 6 Course IDSubjectCodeTitle 123CSE201Introduction to Programming 124CSE203Data Structures 125PHY101Mechanics ……..… Courses Table Schema for table courses: Courses (courseId: integer, subject: string, code: string, title: string)

Design Issues 7 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… Do all values in a column have to be distinct? What about combination of values? Can a value be null? How do we model classes taken by students? Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..…

Design Issues 8 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..… StudentIDFirstNameLastNameDateOfBirthGPASubjCode JackKing3/4/ CSE201… JackKing3/4/ ENG337… MaryAdams6/10/ ENG337… LarryJones1/14/ PHY201… LarryJones1/14/ PSY201… LarryJones1/14/ PSY257… StudentCourses Table What happens when Larry takes one more course? What if there is an error in the date of birth of Jack?

Design Issues 9 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..… StudentIDCourseIDTermGrade FS144.0… FS143.5… FS143.5… SS153.0… SS154.0… SS153.5… StudentCourses Table What if there is no course with ID 175? Normalization: organize data to reduce redundancy 3NF: Third normal form

Integrity Constraint: a condition that must be satisfied by any instance of the database Domain constraints: GPA must be between 0 and 4.0 Avoid data entry errors Specify integrity constraints when the schema is defined May add constraints later only if data satisfies it When the relations are modified, the integrity constraints are checked When data is inserted/edited/deleted Integrity Constraints 10

Primary Key: a set of fields that uniquely identifies a record: no two records can have the same value for it When more than one field/set of fields identify the record, only one can be chosen as a primary key The others are called Candidate Keys Integrity Constraints 11

Primary and Candidate Keys 12 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..… StudentIDCourseIDTermGrade FS144.0… FS143.5… FS143.5… SS153.0… SS154.0… SS153.5… CK PK

Foreign Key: a set of fields in one relation that is used to refer to a record in another relation Must correspond to the primary key of the other relation The others are called Candidate Keys Referential Integrity Integrity Constraints 13

Foreign Keys 14 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..… StudentIDCourseIDTermGrade FS144.0… FS143.5… FS143.5… SS153.0… SS154.0… SS153.5… FK PK

Foreign Keys 15 The following changes violate the referential integrity constraints: 1.Entering a new tuple in StudentCourse, with studentID not in Student 2.Updating studentID of an existing tuple in StudentCourse to a non-existing one in Student 3.Modifying the studentID of a tuple in Student 4.Deleting a tuple in Student referenced by StudentCourse StudentID (PK) Firstname Lastname DateOfBirth GPA Student StudentID (FK) CourseID (FK) Term Grade StudentCourse Reject Reject OR Cascade change: delete tuple in StudentCourse Reject OR Cascade change: update FK value in StudentCourse

Simple and powerful query language Data Definition Language (DDL): Create tables, integrity constraints, triggers Data Manipulation Language (DML): Insert/update/delete records SQL – Structured Query Language 16

Create table 17 CREATE TABLE students ( student_id int NOT NULL, firstname varchar(25) NOT NULL, lastname varchar(25), date_of_birth date); Table name Field name Field type NOT NULL Constraint

Unique Constraint 18 CREATE TABLE students ( student_id int NOT NULL UNIQUE, firstname varchar(25) NOT NULL, lastname varchar(25), date_of_birth date); UNIQUE Constraint to designate candidate keys SQL Server / Oracle / Postgres: UNIQUE Constraint to designate candidate keys CREATE TABLE students ( student_id int NOT NULL, firstname varchar(25) NOT NULL, lastname varchar(25), date_of_birth date, UNIQUE(student_id) );

Primary Key 19 CREATE TABLE students ( student_id int NOT NULL, firstname varchar(25) NOT NULL, lastname varchar(25), date_of_birth date CONSTRAINT students_pkey PRIMARY KEY (student_id)); Primary Key Constraint Name

Foreign Key 20 CREATE TABLE studentCourse ( student_id int NOT NULL, course_id int NOT NULL, term_id int NOT NULL, grade real, CONSTRAINT student_course_pkey PRIMARY KEY (student_id, course_id, term_id), CONSTRAINT student_fkey FOREIGN KEY (student_id) REFERENCES student (student_id), CONSTRAINT course_fkey FOREIGN KEY (course_id) REFERENCES course (course_id), ); Foreign Key Constraints ALTER TABLE studentCourse ADD FOREIGN KEY (term_id) REFERENCES term (term_id) To add a constraint after the table has been created

CHECK Constraint 21 CREATE TABLE studentCourse ( student_id int NOT NULL, course_id int NOT NULL, term_id int NOT NULL, grade real CHECK (grade > 0), enrollment int, CONSTRAINT valid_grad_chk CHECK (enrollment < 3 AND grade <= 4.0) ); Two different ways for check constraint. Use second one if multiple columns involved

Insert a new tuple into the table: insert into student (student_id, firstname, lastname, date_of_birth, gpa) values ( , ‘Jim’, ‘Raymond’, ‘5/5/1985’, 3.12) Delete all tuple satisfying given condition delete from student where firstname = ‘Jack’ delete from student where date_of _birth < ‘1/1/1990’ and gpa = 3.0 delete from student Update the birth date of student update student set date_of_birth = ‘3/4/1993’ where student_id = Adding/Updating/Deleting tuples 22 Student_idFirstNameLastNameDate_Of_BirthGPA JackKing3/4/ …………

SELECT [distinct] attributes FROM tables WHERE condition Select all students: ◦select * from students Select all lastnames and birth dates for students with firstname Jack: ◦select lastname, dateofbirth from student where firstname = ‘Jack’ Querying 23 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ LarryJones1/14/ ………… LastNameDateOfBirth King3/4/1989 Johnson7/11/1994

Where clause: Comparisons:, =, !=, <>, >=, <=, is null, is not null combined with AND, OR select * from course where (subject = ‘CSE’ and code = ‘891’) or (subject = ‘STAT’ and code IS NULL) Wildcard: like and %: select * from student where firstname like ‘J%’ Set operation: IN, EXISTS, NOT IN, NOT EXISTS: select * from student where gpa in (2.0, 3.0, 4.0) Order Clause: Order by lastname, firstname ASC(by default: ascending) Order by birth_date DESC Querying 24

Select the subject name of courses, without repetitions ◦Select distinct subject from courses Querying 25 Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics 126ENG201English I 127ENG202English II 128ENG337Poetry Subject CSE PHY ENG Subject CSE PHY ENG Select the subject name of all courses ◦Select subject from courses

Nested Queries 26 Select the students who took at least one course: select * from student where studentid in (select studentid from studentCourses) select * from student s where EXISTS (select * from studentCourses enr.studentid = s.studentid)

select * from students, courses Cross product: joins every row from students with each row from courses Querying from multiple tables 27 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ Course IDSubjectCodeTitle 123CSE201Intro to … 125PHY101Mechanics 126ENG201English I StudentIDFirstNameLastNameDateOfBirthGPACourse IDSubjectCodeTitle JackKing3/4/ CSE201Intro to … JackKing3/4/ PHY101Mechanics JackKing3/4/ ENG201English I MaryAdams6/10/ CSE201Intro to … MaryAdams6/10/ PHY101Mechanics MaryAdams6/10/ ENG201English I

Retrieve the names of all students with all their grades select s.firstname, s.lastname, enr.grade from students s, studentCourses enr where s.studentID = enr.studentID select s.firstname, s.lastname, enr.grade from students s join studentCourses enr on (s.studentID = enr.studentID) Querying from multiple tables 28 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ BillCarlson6/12/ FirstNameLastNameGrade JackKing201 JackKing101 JackKing201 MaryAdams201 MaryAdams101 MaryAdams201 StudentIDCourseIDTermGrade FS FS FS143.5

Retrieve the names of all students, the subject and code of each course they took and the grade they received: Querying from multiple tables 29 StudentIDFirstNameLastNameDateOfBirthGPA JackKing3/4/ MaryAdams6/10/ BillCarlson6/12/ StudentIDCourseIDTermGrade FS FS FS143.5 CourseIDSubjectCodeTitle 123CSE201Intro to … 125PHY101Mechanics 126ENG201English I select s.firstname, s.lastname, c.subject, c.code, enr.grade from students s, courses c, studentCourses enr where s.studentid = enr.studentid and enr.courseid = c.courseid

max(col), min(col), sum([distinct] col), avg([distinct] col), count([distinct] col), count(*) select col_names, aggregate_function(col) from table where condition group by col_names Select count(*) from students Select avg(grade) from studentcourses Select count(*) from students where birth_date >= ‘1/1/1990’ Select student_id, count(*) from studentCourse group by student_id Select student_id, count(*) from studentCourse where subject = ‘CSE’ group by student_id Aggregation 30