Top Banner
Relational Database Management Systems
30

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

Jan 16, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.

Relational Database Management Systems

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

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)

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

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

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

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

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

5

Example

StudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.122015000104 Mary Adams 6/10/1985 2.782015000161 Larry Jones 1/14/1990 3.87… … … …

Students Table

Schema for table students:Students (studentId: integer, firstname: string, lastname: string, dateOfBirth: date, gpa: float)

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

6

Example

Course ID Subject Code Title123 CSE 201 Introduction to Programming124 CSE 203 Data Structures125 PHY 101 Mechanics… … .. …

Courses Table

Schema for table courses:Courses (courseId: integer, subject: string, code: string, title: string)

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

7

Design IssuesStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000161 Larry Jones 1/14/1990 3.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 ID Subject Code Title

123 CSE 201 Intro to …

124 CSE 203 Data Stru …

125 PHY 101 Mechanics

… … .. …

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

8

Design IssuesStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000161 Larry Jones 1/14/1990 3.87

… … … …

Course ID Subject Code Title123 CSE 201 Intro to …

124 CSE 203 Data Stru …

125 PHY 101 Mechanics

… … .. …

StudentID FirstName LastName DateOfBirth GPA Subj Code2015000103 Jack King 3/4/1989 3.12 CSE 201 …

2015000103 Jack King 3/4/1989 3.12 ENG 337 …

2015000104 Mary Adams 6/10/1985 2.78 ENG 337 …

2015000161 Larry Jones 1/14/1990 3.87 PHY 201 …

2015000161 Larry Jones 1/14/1990 3.87 PSY 201 …

2015000161 Larry Jones 1/14/1990 3.87 PSY 257 …

StudentCourses Table • What happens when Larry takes one more course?

• What if there is an error in the date of birth of Jack?

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

9

Design IssuesStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000161 Larry Jones 1/14/1990 3.87

… … … …

Course ID Subject Code Title123 CSE 201 Intro to …

124 CSE 203 Data Stru …

125 PHY 101 Mechanics

… … .. …

StudentID CourseID Term Grade2015000103 123 FS14 4.0 …

2015000103 155 FS14 3.5 …

2015000104 155 FS14 3.5 …

2015000161 132 SS15 3.0 …

2015000161 164 SS15 4.0 …

2015000161 175 SS15 3.5 …

StudentCourses Table

• What if there is no course with ID 175?

Normalization: organize data to reduce redundancy3NF: Third normal form

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

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

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

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

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

12

Primary and Candidate KeysStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000161 Larry Jones 1/14/1990 3.87

… … … …

Course ID Subject Code Title123 CSE 201 Intro to …

124 CSE 203 Data Stru …

125 PHY 101 Mechanics

… … .. …

StudentID CourseID Term Grade2015000103 123 FS14 4.0 …

2015000103 155 FS14 3.5 …

2015000104 155 FS14 3.5 …

2015000161 132 SS15 3.0 …

2015000161 164 SS15 4.0 …

2015000161 175 SS15 3.5 …

CKPK

PK

PK

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

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

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

14

Foreign KeysStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000161 Larry Jones 1/14/1990 3.87

… … … …

Course ID Subject Code Title123 CSE 201 Intro to …

124 CSE 203 Data Stru …

125 PHY 101 Mechanics

… … .. …

StudentID CourseID Term Grade2015000103 123 FS14 4.0 …

2015000103 155 FS14 3.5 …

2015000104 155 FS14 3.5 …

2015000161 132 SS15 3.0 …

2015000161 164 SS15 4.0 …

2015000161 175 SS15 3.5 …

FK FK

PK PK

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

15

Foreign KeysThe 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)FirstnameLastnameDateOfBirthGPA

Student

StudentID (FK)CourseID (FK)TermGrade

StudentCourse

Reject

Reject ORCascade change: delete tuple in StudentCourse

Reject ORCascade change: update FK value in StudentCourse

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

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

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

17

Create table

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

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

18

Unique Constraint

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

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

19

Primary KeyCREATE TABLE students (

student_id int NOT NULL,firstname varchar(25) NOT NULL,lastname varchar(25),date_of_birth dateCONSTRAINT students_pkey PRIMARY KEY (student_id));

Primary Key

Constraint Name

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

20

Foreign KeyCREATE 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

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

21

CHECK ConstraintCREATE 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

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

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 conditiondelete from student where firstname = ‘Jack’delete from student where date_of _birth < ‘1/1/1990’ and gpa = 3.0delete from student

Update the birth date of student 2015000103update student set date_of_birth = ‘3/4/1993’ where student_id = 2015000103

Adding/Updating/Deleting tuplesStudent_id FirstName LastName Date_Of_Birth GPA2015000103 Jack King 3/4/1989 3.12… … … …

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

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

StudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000161 Larry Jones 1/14/1990 3.87

… … … …

LastName DateOfBirthKing 3/4/1989

Johnson 7/11/1994

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

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

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

25

Select the subject name of courses, without repetitions ◦ Select distinct subject from courses

QueryingCourse ID Subject Code Title123 CSE 201 Intro to …

124 CSE 203 Data Stru …

125 PHY 101 Mechanics

126 ENG 201 English I

127 ENG 202 English II

128 ENG 337 Poetry

SubjectCSE

CSE

PHY

ENG

ENG

ENG Subject

CSE

PHY

ENG

Select the subject name of all courses◦ Select subject from courses

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

26

Nested Queries Select the students who took at least one course:

select * from student where studentid in (select studentid from studentCourses)

select * from student swhere EXISTS (select * from studentCourses enr.studentid = s.studentid)

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

27

select * from students, courses Cross product: joins every row from students with each row from courses

Querying from multiple tablesStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

Course ID Subject Code Title123 CSE 201 Intro to …

125 PHY 101 Mechanics

126 ENG 201 English I

StudentID FirstName LastName DateOfBirth GPA Course ID Subject Code Title2015000103 Jack King 3/4/1989 3.12 123 CSE 201 Intro to …

2015000103 Jack King 3/4/1989 3.12 125 PHY 101 Mechanics

2015000103 Jack King 3/4/1989 3.12 126 ENG 201 English I

2015000104 Mary Adams 6/10/1985 2.78 123 CSE 201 Intro to …

2015000104 Mary Adams 6/10/1985 2.78 125 PHY 101 Mechanics

2015000104 Mary Adams 6/10/1985 2.78 126 ENG 201 English I

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

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 tablesStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000105 Bill Carlson 6/12/1991 3.45

FirstName LastName GradeJack King 201

Jack King 101

Jack King 201

Mary Adams 201

Mary Adams 101

Mary Adams 201

StudentID CourseID Term Grade2015000103 123 FS14 4.0

2015000103 155 FS14 3.5

2015000104 155 FS14 3.5

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

29

Retrieve the names of all students, the subject and code of each course they took and the grade they received:

Querying from multiple tablesStudentID FirstName LastName DateOfBirth GPA2015000103 Jack King 3/4/1989 3.12

2015000104 Mary Adams 6/10/1985 2.78

2015000105 Bill Carlson 6/12/1991 3.45

StudentID CourseID Term Grade2015000103 123 FS14 4.0

2015000103 155 FS14 3.5

2015000104 155 FS14 3.5

CourseID Subject Code Title123 CSE 201 Intro to …

125 PHY 101 Mechanics

126 ENG 201 English 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

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

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