Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
PL/SQL.
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
1 Constraints, Triggers and Active Databases Chapter 9.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Design -- Basic SQL
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Exploring Microsoft Access 2003 Chapter 4 Proficiency: Relational Databases, External Data, Charts, Pivot, and the Switchboard.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
Database Systems More SQL Database Design -- More SQL1.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Onsdag The concepts in a relation data model SQL DDL DML.
-Software School of Hunan University-
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
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,...)
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
SQL Chapter 8. SQL or SEQUEL - (Structured English Query Language) Based on relational algebra Developed in 1970's released in early 1980's Standardized.
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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 7: SQL, the Structured Query Language Instructor’s name and.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Database Management COP4540, SCS, FIU Database Trigger.
Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Creating Database Triggers
Chapter 4 Basic SQL.
CS580 Advanced Database Topics
SQL Stored Triggers Presented by: Dr. Samir Tartir
Introduction to Database Systems, CS420
CS4222 Principles of Database System
Introduction to Triggers
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Structured Query Language (3)
Database Design: Relational Model
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Triggers.
Prof. Arfaoui. COM390 Chapter 9
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Views Presented by: Dr. Samir Tartir
Presentation transcript:

Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)

Integrity constraints in Oracle Insert, delete or update can violate a referential integrity constraint A NOT NULL constraint –prohibits a database value from being null. A unique constraint – –allows some values to be null. A primary key constraint –combines a NOT NULL constraint and a unique constraint in a single declaration. A foreign key constraint –requires values in one table to match values in another table. A check constraint –requires a value in the database to comply with a specified condition.

Check Constraint CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), State char(2), CONSTRAINT chk_Person CHECK (P_Id>0 AND State = ‘AL’) )

Violation of Integrity Constraints What operations can violate integrity constraints? –Insert –delete –update

Violation of Integrity Constraints If any constraints are violated, the insert, delete, update is rejected Unless...

Qualified options for Violation SQL allows qualified options to be specified for the foreign key - on delete or update (includes insert) –Set null –Cascade –Set default Not all options available in Oracle –In Oracle, Set null and Cascade on delete are available So what is affected by this? –SQLServer also allows on delete and on update

Oracle If do not include ON DELETE clause –then Oracle does not allow you to delete referenced key values in the parent table that have dependent rows in the child table. –This means the delete is rejected If include ON DELETE –Specify SET NULL if you want Oracle to convert dependent foreign key values to NULL. –Specify CASCADE if you want Oracle to remove all tuples with dependent foreign key values.

Example of cascade create table casc (id int unique, age int not null, dno int, constraint fk foreign key (dno) references department (dnumber) on delete cascade); Examples

Relational Views A view - a virtual table that is derived from other tables (base tables or can be another view) A view can be used to simplify frequent queries e.g. a join condition A view does not necessarily exist in physical form There is no limit on querying a view (limits on an update to a view) Views are useful for security and authorization mechanisms

Create View View attribute names are inherited from other tables If aggregate functions are the result of arithmetic operations, they must be renamed Views can be defined using other views

Create view CREATE VIEW view_name [(col1 {, col2})] AS SELECT col1 {, col2} FROM (table1| view1) {, table2 | view2} WHERE search_condition

Multiple table views To create a view from multiple tables Create View Works_on1 As Select fname, lname, pname, hours From Employee, Project, Works_on Where ssn = essn and pno = pnumber;

Create View Create a view to list for each department: dname, number of employees and total salary paid out Create view Name (cols) As Select query

Views Queries on View - same as queries on base tables Retrieve the last and first names of all employees who work on ProjectX

Maintain views 2 strategies to maintain views: –View is stored as a temporary table for future queries called view materialization view is not realized at the time of the view definition but when specify the query –Another strategy is to modify the view query into a query on the underlying base table called query modification DBMS keeps views up-to-date – how?

Views A view is removed by using the DROP VIEW command. Drop View Works_on1; Drop View Dept_info;

Updating views If specify an update to a view, should it update the corresponding table?. Create View Emp As Select fname, lname, ssn, dno From Employee Update Emp Set dno = 1 Where lname = ‘English’

Updating views It may not make sense to update some views – why? Cannot always guarantee that a view can be updated

Views When would it not make sense? General Rule: (true for ORACLE) – View with aggregate functions are not updatable –Views with more than one table using joins is not updatable –View with one defining table is updatable if the view attributes contain a primary key

Stopped here

Views How to represent derived attributes? –Fig

Triggers Suppose –You want to make sure values you insert are correct (e.g. hours > 0) –You want to know when update an employee’s salary if it is > than supervisor’s You can define a trigger to perform an action when a specific event occurs

Triggers You can create triggers to be fired on any of the following: –DML statements (DELETE, INSERT, UPDATE) –DDL statements (CREATE, ALTER, DROP) –Database operations (SERVERERROR, LOGON, LOGOFF, STARTUP, SHUTDOWN)

Example create trigger tr1 after insert on works_on referencing new x for each row when (x.hours < 1) begin raise_application_error(-20003,'invalid hours on insert'); // insert was performed but error printed end;

Create Trigger CREATE TRIGGER trigger_name {BEFORE | AFTER} {INSERT | DELETE | UPDATE [OF colname {, colname...}]} ON tablename [REFERENCING corr_name_def {, corr_name_def...}] [FOR EACH ROW | FOR EACH STATEMENT] [WHEN (search_condition)] {statement-- action (single statement) | BEGIN ATOMIC statement; { statement;...} END} -- action (multiple statement.) DROP TRIGGER trigger_name;

Example CREATE OR REPLACE TRIGGER Log_salary_increase AFTER UPDATE ON Employee FOR EACH ROW WHEN (new.Salary > ) BEGIN INSERT INTO Emp_log (Emp_id, Log_date, New_salary, Action) VALUES (:new.Empno, SYSDATE, :new.salary, 'NEW SAL'); END;

Using BEFORE BEFORE command fires trigger before UPDATE –Can write to :new value but not to :old value If use After, can write to either :old or :new value

When to use triggers? Don’t define triggers if can do the same with integrity constraints Limit triggers to less than 60 lines of code Do not create recursive triggers Triggers execute every time event occurs on which trigger is created 1/adfns_triggers.htm#i http://docs.oracle.com/cd/B19306_01/appdev.102/b1425 1/adfns_triggers.htm#i