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