+ Assignment 5 Q’s CSCI 2141 W2013. + Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference.

Slides:



Advertisements
Similar presentations
POSTGRESQL DUNGEON WITH TABLE INHERITANCE AND CONSTRAINTS Edel Sherratt.
Advertisements

1 Constraints, Triggers and Active Databases Chapter 9.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
1 Constraints Foreign Keys Local and Global Constraints Triggers Source: slides by Jeffrey Ullman.
Fundamentals, Design, and Implementation, 9/e Chapter 5 Database Design.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
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 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6 th Edition SQL Schema Changes and table updates instructor teaches.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
1 IT 244 Database Management System Lecture 11 More SQL Constraints &Triggers, SQL Authorization,Transactions Foreign Keys, Local and Global Constraints,
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Data Driven Designs 99% of enterprise applications operate on database data or at least interface databases. Most common DBMS are Microsoft SQL Server,
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Database Systems Defining Database Schema Views.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Assertions and triggers1. 2 Constraints Attribute-based CHECK constraints create table … ( postcode number(4) check (postcode > 0) ); Checked at update.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
Database Management COP4540, SCS, FIU Database Trigger.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
1 Constraints, Views, Indexes Foreign Keys Constraints Triggers Virtual and Materialized Views Speeding Accesses to Data.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Introduction to Database Systems, CS420 SQL Constraints.
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.
Foreign Keys Local and Global Constraints Triggers
CPSC-608 Database Systems
CPSC-310 Database Systems
Referential Integrity
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
Referential Integrity
CMSC-461 Database Management Systems
CPSC-608 Database Systems
SQL – Constraints & Triggers
CPSC-608 Database Systems
-Transactions in SQL -Constraints and Triggers
Chapter 8 Views and Indexes
Assertions and Triggers
Presentation transcript:

+ Assignment 5 Q’s CSCI 2141 W2013

+ Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference the model in two different tables My solution avoids that by collapsing 2 tables into devices Problem: how to enforce constraint of a null value for screen if not a laptop? Could do it semantically (screen is last attribute, trust those inserting data to not provide data if it is not a laptop) Or need some kind of a constraint

+ Solution from Aaron Hallink: I found out it actually hasn't been implemented in MYsql. constraint I had to create a trigger instead, to set the screen to null when the device is a desktop. DELIMITER $$ CREATE TRIGGER device_screen BEFORE INSERT ON Device FOR EACH ROW BEGIN IF (new.type ='DESKTOP') THEN SET new.screen = NULL; END IF; END$$ DELIMITER ;

+ Tuple based constraints Part 3 asks you to write constraints as tuple based constraints Tuple based constraints are required instead of attribute-based constraints when you need to check more than 1 attribute in a tuple – they must be defined after the attributes are defined CHECK (year > 1909 AND length <480) Both attribute-based and tuple-based can refer to other tables through subqueries So you can do a subquery to see if a studio name is in at least one Movies tuple So that section actually doesn’t necessarily need tuple based constraints, just more complex constraints

+ Attribute-Based Checks Constraints on the value of a particular attribute. Add CHECK( ) to the declaration for the attribute. The condition may use the name of the attribute, but any other relation or attribute name must be in a subquery. 5

+ Example: Attribute-Based Check CREATE TABLE Sells ( canteenCHAR(20), productCHAR(20)CHECK ( product IN (SELECT name FROM products)), priceREAL CHECK ( price <= 5.00 ) ); 6

+ Timing of Checks Attribute-based checks are performed only when a value for that attribute is inserted or updated. Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. Example: CHECK (product IN (SELECT name FROM products)) not checked if a product is deleted from products (unlike foreign-keys). 7

+ Tuple-Based Checks CHECK ( ) may be added as a relation-schema element. The condition may refer to any attribute of the relation. But other attributes or relations require a subquery. Checked on insert or update only. 8

+ Example: Tuple-Based Check Only Joe ’ s canteen can sell product for more than $5: CREATE TABLE Sells ( canteenCHAR(20), productCHAR(20), priceREAL, CHECK (canteen = ‘ Joes canteen ’ OR price <= 5.00) ); 9