Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 Constraints, Triggers and Active Databases Chapter 9.
Referential Integrity & Foreign Keys Objectives of the Lecture : To consider Referential Integrity & Foreign Keys; To consider Referential Integrity Constraints.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
SQL Constraints and Triggers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
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.
Introduction to Structured Query Language (SQL)
Database Systems More SQL Database Design -- More SQL1.
Triggers.
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
An Investigation of Oracle and SQL Server with respect to Integrity, and SQL Language standards Presented by: Paul Tarwireyi Supervisor: John Ebden Date:
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
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.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
INTRODUCTION lecture1 1. Data base concept Data is a meaningless static value. What does 3421 means? Information is the data you process in a manner that.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
Session 1 Module 1: Introduction to Data Integrity
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
COP-5725 Practice Exercises
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Triggers Chapter 5.
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)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SQL Triggers, Functions & Stored Procedures Programming Operations.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Fundamentals of DBMS Notes-1.
Chapter 6 - Database Implementation and Use
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Database Construction (and Usage)
Unit I-2.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
SQL – Constraints & Triggers
Triggers 7/11/2019 See scm-intranet.
Presentation transcript:

Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for maintaining a DB. To consider non-scalar attribute values.

The Need to Sort Relations l A relation is a set. l The elements of a set have no order.  The tuples of a relation have no order. PROBLEM !! Sometimes we need sorted data. Example : a telephone directory is just a set of entries, but it would be useles if not sorted into alphabetic order. PROBLEM !! Sometimes we need the same data sorted into different orders. Example : at different times, we require employee details sorted by name, by company ID, by salary, and by department.

Solution Sort a relation when it is retrieved. This also leaves the DBMS free to physically store the relation’s data in any desirable way, and most efficiently serve a variety of different uses and users. Note : l Typically retrieved relations are not whole DB relations but the result of a query expression.  the data may well need to be physically sorted anyway in order to return tuples in the desired order. l Once a sorted copy of the relation is outside the DB, we can treat it how we like.

Sorting l Sort relation on the values of 1 or more attribute(s). l Attribute(s) sorted on must have an orderable type for sorting to be possible. Example : numbers and text can be sorted, photos cannot. l Sorting can be ascending or descending. l If several attributes are used to sort a relation : l sort tuples on the 1 st attribute’s values, l sort tuples with the same 1 st attribute value on the 2 nd attribute’s values, l sort tuples with the same 1 st and 2 nd attribute values on the 3 rd attribute’s values, l etc. l Also expressed as “sort on 3 rd attribute within 2 nd attribute within 1 st attribute”. l Thus a major-to-minor attribute order must be specified.

Sorting in SQL Add an ORDER BY phrase to the end of a SELECT statement. The ORDER BY phrase must always be the last phrase in the SELECT statement. Examples : l SELECT * FROMEMP ORDER BY EName ; Orders the rows in alphabetical order of employees’ names. l SELECT * FROM EMP ORDER BY Sal DESC, EName ASC ; Orders rows into ascending/alphabetical order of employees’ names within decreasing salaries. ASC is the default if no order is given..

Triggers Definition : Piece of code consisting of procedural & declarative statements that is activated by the DBMS if a specific operation is executed on the DB under specific conditions. In SQL DBMSs, triggers are stored in the ‘SQL catalog’. l Their purpose is to provide a facility that will carry out an appropriate action if an error or some specified event occurs. l The motivation and concepts are similar to the handling of interrupts in Operating Systems and exceptions in programming languages. l A trigger has a name & consists of 3 parts :  an event,  a condition (optional),  an action.

Trigger Actions Processing Another process runs to handle the “Special Event”. Normal processing resumes. “Special Event” detected. Processing halts. SQL : Trigger SQL : Triggered Procedure

Using Triggers l Handling errors. l Managing changes to the DB for example : l Cascaded Deletes l Cascaded Updates l Setting NULLs l Setting default values l Prohibiting Changes l Summation Updates to ensure that changes can be made without violating any rules execute useful supplementary activities

SQL Example CREATE TRIGGER CAR_OWNER_DELETE BEFORE DELETE ON EMP FOR EACH ROW BEGIN UPDATE CAR SET OWNER = NULL WHERE OWNER = :OLD.EMP_NO; END; Name of Trigger Event No condition in this example Action refers to old version of triggering table EMP for each row in the deletion

SQL Syntax - Main Points CREATE TRIGGER Trigger_Name ON Table_Name FOR EACH WHEN Condition BEGIN Trigger-Action END; BEFORE AFTER INSTEAD OF INSERT DELETE UPDATE OF colname(s) ROW STATEMENT

Example : Summation Updates (1) INSERT INTO TRANS VALUES ('004', 'A002', 30); Record of transactions Summary of transactions

Example : Summation Updates (2) Record of transactions Summary of transactions Inserted row Updated value

SQL Trigger for Summation Update CREATE TRIGGER ADD_TO_DAILY_TOTAL AFTER INSERT ON TRANS FOR EACH ROW BEGIN UPDATE DAILY_TRANS SET TOTAL = TOTAL + :NEW.AMOUNT; END; refers to new version of triggering table TRANS

Handling Referential Integrity (1) CAR EMP Supposing employee ‘E8’ is deleted from EMP.  car ‘JON 1’ breaks referential integrity. Solutions : 1 Delete car. 2. Employee deletion disallowed. 3. Set car owner to default value. 4. Set car owner to NULL. RegNoTypeOwner NA03 RTYCorsa 1.3E3 DH56 LGRPrimera GLiE5 JON 1Jaguar XKE8 NK07 YXKVolvo S80E6

Handling Referential Integrity (2) l Could use triggers to carry out desired compensatory action. l SQL allows phrases to be added to the Referential Integrity constraint to carry out the chosen solution if the constraint is broken. Example : Create Table CAR ( RegNoChar(9) Primary Key, TypeVarchar2(24), OwnerChar(2) References EMP( EmpNo ) ) ; On Delete CASCADE On Delete RESTRICT On Delete SET DEFAULT On Delete SET NULL Add one of these.

Non-Scalar Attribute Values Attribute values are no longer constrained to be scalar values of traditional type. Attributes can also have the following kinds of type : l ‘Enhanced’ basic types - e.g. multimedia, XML. l Object classes. Typically an object is defined to comprise several values of traditional type, i.e. numbers, text, date. SQL row objects may be considered a special case of this. l Collections. Currently Standard SQL4 (of 2003) only permits arrays and bags. All values in an array/bag must have the same type. l Nested relations. An attribute value consists of a relation. In principle there are 2 possibilities : the contents of the nested relation are / are not visible in the outer/holding relation.

Conceptual Example Values of type ‘Picture’ Values each of which is an array of values of type ‘Text’

SQL Examples CREATE TYPE Money AS DECIMAL(7,2) ; CREATE TYPE Address AS ( HouseNoINTEGER, StreetVARCHAR2(30), TownVARCHAR2(30), PostcodeCHAR(8) ) ; CREATE TABLE Employee ( Emp_IDCHAR(4), HomeAddress, SalaryMoney ); Create scalar type Create row type Use new types to create a table.