Database Construction (and Usage)

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.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Oracle Data Definition Language (DDL)
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
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.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management COP4540, SCS, FIU Database Trigger.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Trigger used in PosgreSQL
Tables and Triggers.
Chapter 6 - Database Implementation and Use
Constraints and Triggers
Active Database Concepts
Instructor: Jason Carter
Foreign Keys Local and Global Constraints Triggers
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Error Handling Summary of the next few pages: Error Handling Cursors.
Introduction to Database Systems, CS420
Database Construction and Usage
CPSC-608 Database Systems
CPSC-310 Database Systems
SQL: Constraints and Triggers
CPSC-310 Database Systems
Lecturer: Mukhtar Mohamed Ali “Hakaale”
2018, Fall Pusan National University Ki-Joune Li
Advanced SQL: Views & Triggers
Unit I-2.
CMSC-461 Database Management Systems
SQL – Constraints & Triggers
CPSC-608 Database Systems
A – Pre Join Indexes.
Lecture 05: SQL Wednesday, October 9, 2002.
-Transactions in SQL -Constraints and Triggers
Assertions and Triggers
Presentation transcript:

Database Construction (and Usage) Lecture 10 Database Construction (and Usage) More on Modifications and Table Creation Assertions Triggers

Summary – Modifications Modifying the contents of a database: Insertions INSERT INTO tablename VALUES tuple Deletions DELETE FROM tablename WHERE test over rows Updates UPDATE tablename SET attribute = value WHERE test over rows 2007-11-14 (GK): Aligned SQL.

Insertions with queries The values to be inserted could be taken from the result of a query: Example: INSERT INTO tablename (query) INSERT INTO GivenCourses (SELECT course, period + 2, teacher, NULL FROM GivenCourses WHERE period <= 2); 2007-11-14 (GK): Aligned SQL. Quiz text expanded. 2007-11-21 (GK): Quiz box removed. All courses that are given in periods one and two are also scheduled to be given two periods later, with the same teacher.

Explicit attribute lists Attribute order could be given explicit when inserting. Example: INSERT INTO GivenCourses(course, period, teacher, nrStudents) (SELECT course, period + 2, teacher, NULL FROM GivenCourses WHERE period <= 2); 2007-11-14 (GK): Aligned SQL. Perhaps the teacher and nrStudents attributes were listed in the other order in the definition of the table? Doesn’t matter anymore since they are explicitly listed.

Quiz What will the following insertion result in? Attribute lists can be partial. Any attributes not mentioned will be given the value a default value, which by default is NULL. course period teacher numStud INSERT INTO GivenCourses(course, period, teacher) VALUES (’TDA357’, 3, ’Mickey’);

Default values Attributes can be given default values. Specified when a table is defined using the DEFAULT keyword. Example: Default default value is NULL. CREATE TABLE GivenCourses ( course CHAR(6), period INT, teacher VARCHAR(50), nrStudents INT DEFAULT 0, … constraints … ); 2007-11-14 (GK): Aligned SQL.

Insertion with default values Leaving out an attribute in an insertion with explicitly named attributes gives that row the default value for that attribute: When no attribute list is given, the same effect can be achieved using the DEFAULT keyword: INSERT INTO GivenCourses(course, period, teacher) VALUES (’TDA357’, 3, ’Mickey’); 2007-11-21 (GK): “gotten” changed to “achieved”. INSERT INTO GivenCourses VALUES (’TDA357’, 3, ’Mickey’, DEFAULT);

Quiz! DELETE FROM Courses WHERE code = ’TDA357’; GivenCourses Courses course per teacher nrSt TDA357 2 Mickey 130 4 Tweety 95 TIN090 1 Pluto 62 code name TDA357 Databases TIN090 Algorithms DELETE FROM Courses WHERE code = ’TDA357’; Error, because of the reference from GivenCourses to Courses. Is this reasonable? 2007-11-14 (GK): Aligned SQL.

Policies for updates and deletions Rejecting a deletion or update in the presence of a reference isn’t always the best option. SQL provides two other methods to resolve the problem: Cascading or Set NULL. Default is RESTRICT: reject the deletion/update.

Cascading Cascading: When the referenced row is deleted/updated, also delete/update any rows that refer to it. Typically used for ”parts of a whole”. Set using ON [DELETE|UPDATE] CASCADE CREATE TABLE GivenCourses ( course CHAR(6), CONSTRAINT CourseExists FOREIGN KEY course REFERENCES Courses(code) ON DELETE CASCADE ON UPDATE CASCADE … more columns and constraints … ); 2007-11-21 (GK): “parent” changed to “referenced”. “child” deleted.

Set NULL Set NULL: When the referenced row is deleted/updated, set the corresponding attribute in any referencing rows to NULL. Typically used when there is a connection, but one that does not affect the actual existence of the referencing row. Set using ON [DELETE|UPDATE] SET NULL CREATE TABLE GivenCourses ( teacher VARCHAR(50), CONSTRAINT TeacherExists FOREIGN KEY teacher REFERENCES Teachers(name) ON DELETE SET NULL ON UPDATE CASCADE … more columns and constraints … ); 2007-11-21 (GK): “existance” changed to “existence”. “parent” changed to “referenced”. “child” changed to “referencing” and “referencing row”.

Quiz! Argue for sensible policies for deletions and updates for the Lectures table. GivenCourses.(course, period): ON DELETE CASCADE ON UPDATE CASCADE or RESTRICT Rooms.name: ON DELETE SET NULL or RESTRICT ON UPDATE CASCADE Lectures(course, period, weekday, hour, room) (course, period) -> GivenCourses.(course, period) room -> Rooms.name What? What? 2007-11-21 (GK): Modified to make clear that it is the tuples in the referenced relations that are modified. 2008-11-20 (GK): “Rooms.room” changed to “Rooms.name”. What? What?

Quiz! Argue for sensible policies for deletions and updates for the Lectures table. GivenCourses.(course, period): ON DELETE CASCADE ON UPDATE CASCADE or RESTRICT Rooms.name: ON DELETE SET NULL or RESTRICT ON UPDATE CASCADE Lectures(course, period, weekday, hour, room) (course, period) -> GivenCourses.(course, period) room -> Rooms.name 2007-11-21 (GK): Modified to make clear that it is the tuples in the referenced relations that are modified. 2008-11-20 (GK): “Rooms.room” changed to “Rooms.name”.

Single-attribute constraints Many constraints affect only the values of a single attribute. SQL allows us to specify such constraints together with the attribute itself, as inline constraints. More than one inline constraint on the same attribute is fine, just put them after one another. Default values should be specified before constraints. CREATE TABLE Courses ( code CHAR(6) CONSTRAINT CourseCode PRIMARY KEY, name VARCHAR(50) ); 2007-11-21 (GK): “so called” changed to “as”.

Special case: NOT NULL Specifying that a value must be non-NULL can be done with a simplified syntax: instead of CREATE TABLE Courses ( code CHAR(6) CONSTRAINT CourseCode PRIMARY KEY, name VARCHAR(50) NOT NULL ); CREATE TABLE Courses ( code CHAR(6) CONSTRAINT CourseCode PRIMARY KEY, name VARCHAR(50) CHECK (name IS NOT NULL) );

Special case: REFERENCES When a foreign key constraint is defined inline, the FOREIGN KEY keywords can be left out. An attribute that references another attribute could be seen as holding copies of that other attribute. Why specify the type again? The type can be left out even if the foreign key constraint is specified separately. CREATE TABLE GivenCourses ( course REFERENCES Courses(code), … more columns and constraints … ); 2007-11-21 (GK): First point rephrased.

Quiz! It might be tempting to write Why will this not work? An inline constraint only constrains the current attribute. What the above tries to achieve is to declare two separate primary keys, which is not allowed in a table. CREATE TABLE GivenCourses ( course REFERENCES Courses(code) PRIMARY KEY, period INT CHECK (period IN (1,2,3,4)) PRIMARY KEY, … more columns and constraints … );

Constraints We have different kinds of constraints: Dependency constraints (X → A) Table structure, PRIMARY KEY, UNIQUE Referential constraints FOREIGN KEY … REFERENCES Value constraints CHECK Miscellaneous constraints (like multiplicity) E.g. no teacher may hold more than 2 courses at the same time. How do we handle these?

Quiz! ”No teacher may hold more than two courses in the same period!” How can we formulate this constraint in SQL? NOT EXISTS ( SELECT teacher, period FROM GivenCourses GROUP BY teacher, period HAVING COUNT(course) > 2 ); 2007-11-14 (GK): Aligned SQL. course period teacher numStud

Assertions Assertions are a way to specify global constraints on a database. Create using CREATE ASSERTION: Example: CREATE ASSERTION name CHECK test PostgreSQL does not support CREATE ASSERTION, So we emulate them using TRIGGER CREATE ASSERTION NotOverworked CHECK (NOT EXISTS (SELECT teacher, period FROM GivenCourses GROUP BY teacher, period HAVING COUNT(course) > 2) ); 2007-11-14 (GK): Aligned SQL.

Triggers When something wants to change the database in some way, trigger another action as well or instead. Example (silly): Whenever a new course is inserted in Courses, schedule that course to be given in period 1, with NULL for the teacher and nrStudents fields. Example: Whenever a lecture is scheduled to take place at 8:00, schedule the lecture to 10:00 instead.

Assertions as triggers ”Instead” could mean to do nothing, i.e. reject the update, which means we can use triggers to simulate assertions. Still costly, but puts the burden on the user to specify when the conditions should be checked (hand optimization). Example: Whenever a teacher is scheduled to hold a course in a period where he or she already holds two courses, reject the insertion.

Basic trigger structure CREATE TRIGGER name [BEFORE|AFTER] [INSERT|DELETE|UPDATE] ON tablename FOR EACH [ROW|STATEMENT] WHEN condition EXECUTE PROCEDURE function() Decide whether to run the trigger or not. What should happen when the trigger is triggered. 2007-11-21 (GK): “rule” added (twice). A trigger is sometimes referred to as an Event-Condition-Action rule (or ECA rule)

We only consider TRIGGER procedures Stored procedures CREATE FUNCTION name() RETURNS TRIGGER AS $$ BEGIN <statements> END $$ LANGUAGE ’plpgsql’; We only consider TRIGGER procedures Where statement is IF (condition) THEN statement ELSE statement END IF; RAISE EXCEPTION ‘message’; sqlstatement;

’NEW’ refers to the newly inserted tuple Example trigger: CREATE FUNCTION addDefaultGivenCourse() RETURNS TRIGGER AS $$ BEGIN INSERT INTO GivenCourses(course, period) VALUES (NEW.code, 1); END $$ LANGUAGE ’plgsql’; CREATE TRIGGER DefaultScheduling AFTER INSERT ON Courses FOR EACH ROW EXECUTE PROCEDURE addDefaultGivenCourse(); ’NEW’ refers to the newly inserted tuple

Out shorthand notation: CREATE FUNCTION addDefaultGivenCourse() RETURNS TRIGGER AS $$ BEGIN INSERT INTO GivenCourses(course, period) VALUES (NEW.code, 1); END $$ LANGUAGE ’plgsql’; addDefaultGivenCourse() 

Trigger events The event clause of a trigger definition defines when to try the trigger: AFTER or BEFORE (for tables) INSERT, DELETE or UPDATE An update could be an UPDATE OF (attributes) to make it consider only certain attributes. ON which table to apply the trigger. Example: CREATE TRIGGER name event clause ”for each” clause condition clause EXECUTE PROCEDURE x() 2007-11-21 (GK): “apply the trigger on” changed to “apply the trigger”. AFTER INSERT ON Courses

FOR EACH ROW A single insert, update or deletion statement could affect more than one row. If FOR EACH ROW is specified, the trigger is run once for each row affected, otherwise once for each statement. Default is FOR EACH STATEMENT, which could also be stated explicitly. CREATE TRIGGER name event clause ”for each” clause condition clause EXECUTE PROCEDURE x() 2011-02-13 (NB): Added work ”affected”.

Trigger Condition The condition specifies whether the action should be run or not. Any boolean-valued expression may be used. Evaluated before or after the event, depending on BEFORE or AFTER. Can refer to the NEW and OLD rows CREATE TRIGGER name event clause ”for each” clause condition clause EXECUTE PROCEDURE x() Example: WHEN (NEW.code LIKE ’TDA%’)

Example revisited f()  INSERT INTO GivenCourses(course, period) VALUES (NEW.code, 1); CREATE TRIGGER DefaultScheduling AFTER INSERT ON Courses FOR EACH ROW EXECUTE PROCEDURE f(); Why must this be run AFTER INSERT? Why not BEFORE? Why? Because there is a foreign key constraint from GivenCourses to Courses, and until we have inserted the row into Courses, there would be nothing for the new row in GivenCourses to refer to. 2007-11-21 (GK): Quiz box pushed to the back so that answer is visible. 2008-02-13 (NB): Undid that edit – I don’t want the answer visible.

Example revisited Why must this be run AFTER INSERT? Why not BEFORE? f()  INSERT INTO GivenCourses(course, period) VALUES (NEW.code, 1); CREATE TRIGGER DefaultScheduling AFTER INSERT ON Courses FOR EACH ROW EXECUTE PROCEDURE f(); Why must this be run AFTER INSERT? Why not BEFORE? Because there is a foreign key constraint from GivenCourses to Courses, and until we have inserted the row into Courses, there would be nothing for the new row in GivenCourses to refer to. 2007-11-21 (GK): Quiz box pushed to the back so that answer is visible. 2008-02-13 (NB): Undid that edit – I don’t want the answer visible.

Recap on views Views are persistent named queries – they can be referred to just as if they were tables, but their data is contained in other (base) tables. Also referred to as virtual tables. CREATE VIEW DBLectures AS SELECT room, hour, weekday FROM Lectures WHERE course = ’TDA357’ AND period = 3; 2007-11-14 (GK): Aligned SQL.

Updating views Views contain no data of their own, and so cannot normally be updated. But views can be queried without containing any data of their own. The trick is to translate the query on the view into what it really means, i.e. the view definition. Why not do the same for modifications?

Triggers on views We can define what modifications on views mean using triggers. Special form of event for views only: INSTEAD OF. f()  INSERT INTO Lectures VALUES (’TDA357’, 2, NEW.weekday, NEW.hour, NEW.room); CREATE TRIGGER DBLectureInsert INSTEAD OF INSERT ON DBLectures FOR EACH ROW EXECUTE PROCEDURE f()

Summary – Triggers Triggers specify extra actions to take on certain events. Event: BEFORE or AFTER a modification Condition: test if we should run the trigger Action: The stuff to be done. SET to change values in the rows being modified. Triggers can be defined on views Event: INSTEAD OF

Next time, Lecture 11