INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam P. Anthony.

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Introduction to Structured Query Language (SQL)
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS CS AT00000.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Oracle Data Definition Language (DDL)
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
The Relational Model These slides are based on the slides of your text book.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
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
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
SQL Server 7.0 Maintaining Referential Integrity.
Programming using C# Joins SQL Injection Stored Procedures
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Recap of SQL Lab no 8 Advance Database Management System.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
©Silberschatz, Korth and Sudarshan4.1Database System Concepts - 6 th Edition Chapter 4: Intermediate SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Databases Introduction - concepts. Concepts of Relational Databases.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module 6: Intermediate.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL – Part 2.
Introduction to Relational Model
Constraints and Triggers
Chapter 3: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 4: Intermediate SQL Joins
Chapter 4: Intermediate SQL
Chapter 4: Intermediate SQL
Unit I-2.
Oracle Data Definition Language (DDL)
Chapter 4: Intermediate SQL
Chapter 4 : Intermediate SQL
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam P. Anthony

Lecture Overview  Joins  Views  Transactions  Data Integrity Constraints

Joins: The Full Story  Join: by itself, nothing more than a Cartesian product:  FROM student join takes == FROM student, takes  Can Add an ON keyword  student join takes ON student.ID = takes.ID  Very similar to natural join  If names equivalent, save time with using:  student join takes USING (ID)  If you will use ALL equivalent column names, natural saves most time:  student natural join takes

Outer Joins  A normal (INNER) join will skip any tuple that doesn’t have a matching pair between tables  List all courses with their pre-requisites:

Executing an Outer Join  course natural left outer join prereq

Right Outer Join  Left/Right just says which table gets to include un- matched tuples:  course natural right outer join prereq

Full Outer Join  course natural full outer join prereq

Join Practice  Reverse-Lookup: List all courses paired with any course for which it is listed as a pre-requisite. Also include courses that are not a pre-requisite for anything.

Views: Dynamic Tables  View: a “table” in a database that is defined by a query  Simplifying large queries  Storing common subqueries  Restricting Access  Very easy to define:  CREATE VIEW AS  At this point it behaves, programmatically, like a regular table!

View Example  Keep people from seeing instructor salary:  CREATE VIEW faculty AS SELECT ID, Name, dept_name FROM instructor  To encapsulate a complex query:  CREATE VIEW enrollments AS SELECT course_id, sec_id, semester, year, count(*) as enrollment FROM takes GROUP BY course_id, sec_id, semester, year  Select max(enrollment) from enrollments

View Concerns  Every time you use a view its query gets re-executed!  Some systems allow for materialized views, which must be updated whenever the underlying data changes  Some database systems allow for updating views  The changes actually affect the underlying tables  Can be more trouble than it is worth  Know that it can be done; read the book if you ever need to do it

Integrity Constraints  Best laid plans…  Humans Make mistakes!  Designers can build in rules that catch mistakes and keep them from becoming permanent  Not Null  Primary Key  Unique  Default value  Check( P ) where P is some predicate  Foreign Key Rules

Check constraint CREATE TABLE tracks( album_ID VARCHAR (25), vol_num NUMERIC (3) CHECK (vol_num > 0), track_num NUMERIC (3), playing_time NUMERIC (5), instrumental CHAR (1) CHECK (instrumental in (‘Y’,’N’)), vocal CHAR (1) CHECK (vocal in (‘Y’,’N’)), rating CHAR (5) DEFAULT ‘*’ CHECK (rating in (‘*’,’**’,’***’,’****’,’*****’)), track_name VARCHAR (80), rotation CHAR (1), PRIMARY KEY (album_ID,vol_num,track_num));

Foreign Keys CREATE TABLE tracks( album_ID VARCHAR (25), vol_num NUMERIC (3), track_num NUMERIC (3), playing_time NUMERIC (5), instrumental CHAR (1), vocal CHAR (1), rating CHAR (5), track_name VARCHAR (80), rotation CHAR (1), PRIMARY KEY (album_ID,vol_num,track_num) FOREIGN KEY (album_ID) REFERENCES ALBUMS ON DELETE CASCADE ON UPDATE CASCADE);  Foreign Key constraints:  System requires that the listed field(s) already exist in the referenced table  On Delete, Update Rules  Cascade  Set Null  Set Default

Foreign Keys in SQLITE  Relatively new:  Referential Integrity off by default:  sqlite> PRAGMA foreign_keys = ON;  Useful Feature: Deferring Constraints  Add ‘DEFERRABLE INITIALLY DEFERRED’ to the foreign key definition

Deferred Foreign Keys and Transactions  All queries are atomic transactions  A sequence of queries can be made to execute in an all-or-nothing manner:  BEGIN TRANSACTION; insert… update… delete… END TRANSACTION;  If a foreign key in SQLITE is marked as deferred, then foreign key violations are allowed as long as they are fixed before you get to END TRANSACTION