Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Relational Database Management Systems

2 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

3 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

4 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

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

6 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)

7 Design Issues 7 StudentIDFirstNameLastNameDateOfBirthGPA 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 2015000161LarryJones1/14/19903.87 ………… 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 ……..…

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

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

10 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

11 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

12 Primary and Candidate Keys 12 StudentIDFirstNameLastNameDateOfBirthGPA 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 2015000161LarryJones1/14/19903.87 ………… Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..… StudentIDCourseIDTermGrade 2015000103123FS144.0… 2015000103155FS143.5… 2015000104155FS143.5… 2015000161132SS153.0… 2015000161164SS154.0… 2015000161175SS153.5… CK PK

13 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

14 Foreign Keys 14 StudentIDFirstNameLastNameDateOfBirthGPA 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 2015000161LarryJones1/14/19903.87 ………… Course IDSubjectCodeTitle 123CSE201Intro to … 124CSE203Data Stru … 125PHY101Mechanics ……..… StudentIDCourseIDTermGrade 2015000103123FS144.0… 2015000103155FS143.5… 2015000104155FS143.5… 2015000161132SS153.0… 2015000161164SS154.0… 2015000161175SS153.5… FK PK

15 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

16 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

17 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

18 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) );

19 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

20 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

21 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

22 Insert a new tuple into the table: insert into student (student_id, firstname, lastname, date_of_birth, gpa) values (201200344, ‘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 2015000103 update student set date_of_birth = ‘3/4/1993’ where student_id = 2015000103 Adding/Updating/Deleting tuples 22 Student_idFirstNameLastNameDate_Of_BirthGPA 2015000103JackKing3/4/19893.12 …………

23 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 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 2015000161LarryJones1/14/19903.87 ………… LastNameDateOfBirth King3/4/1989 Johnson7/11/1994

24 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

25 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

26 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)

27 select * from students, courses Cross product: joins every row from students with each row from courses Querying from multiple tables 27 StudentIDFirstNameLastNameDateOfBirthGPA 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 Course IDSubjectCodeTitle 123CSE201Intro to … 125PHY101Mechanics 126ENG201English I StudentIDFirstNameLastNameDateOfBirthGPACourse IDSubjectCodeTitle 2015000103JackKing3/4/19893.12123CSE201Intro to … 2015000103JackKing3/4/19893.12125PHY101Mechanics 2015000103JackKing3/4/19893.12126ENG201English I 2015000104MaryAdams6/10/19852.78123CSE201Intro to … 2015000104MaryAdams6/10/19852.78125PHY101Mechanics 2015000104MaryAdams6/10/19852.78126ENG201English I

28 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 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 2015000105BillCarlson6/12/19913.45 FirstNameLastNameGrade JackKing201 JackKing101 JackKing201 MaryAdams201 MaryAdams101 MaryAdams201 StudentIDCourseIDTermGrade 2015000103123FS144.0 2015000103155FS143.5 2015000104155FS143.5

29 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 2015000103JackKing3/4/19893.12 2015000104MaryAdams6/10/19852.78 2015000105BillCarlson6/12/19913.45 StudentIDCourseIDTermGrade 2015000103123FS144.0 2015000103155FS143.5 2015000104155FS143.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

30 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


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

Similar presentations


Ads by Google