Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

Similar documents
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Sql Server Syllabus. Overview

SQL Server. Lecture3 Cascading referential integrity constraint

UNIT-IV (Relational Database Language, PL/SQL)

SQL Interview Questions

Lecture 08. Spring 2018 Borough of Manhattan Community College

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

Database Management System 9

5. Single-row function

Index. Accent Sensitive (AS), 20 Aggregate functions, 286 Atomicity consistency isolation durability (ACID), 265

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Chapter 1 SQL and Data

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Chapter 8: Working With Databases & Tables

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

6232B: Implementing a Microsoft SQL Server 2008 R2 Database

Module 9: Managing Schema Objects

Oracle Syllabus Course code-r10605 SQL

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

8) A top-to-bottom relationship among the items in a database is established by a

Developing SQL Databases (762)

Querying Microsoft SQL Server (461)

SQL DATA DEFINITION LANGUAGE

Introduction to Information Systems

COSC344 Database Theory and Applications. Lecture 11 Triggers

SQL DATA DEFINITION LANGUAGE

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

Database Technology. Topic 6: Triggers and Stored Procedures

Transaction Management Chapter 11. Class 9: Transaction Management 1

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

Data Manipulation (DML) and Data Definition (DDL)

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

ORACLE DATABASE 12C INTRODUCTION

Managing Data. Copyright 2004, Oracle. All rights reserved.

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Physical Design of Relational Databases

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Intermediate SQL ( )

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment

Oracle PL SQL Training & Certification

SQL DATA DEFINITION LANGUAGE

Working with DB2 Data Using SQL and XQuery Answers

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL


Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

VU Mobile Powered by S NO Group All Rights Reserved S NO Group 2013

Referential Integrity and Other Table Constraints Ray Lockwood

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Full file at

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

M.C.A. (CBCS) Sem.-III Examination November-2013 CCA-3004 : Database Concepts and Tools. Faculty Code: 003 Subject Code:

Oracle Database 11g: SQL and PL/SQL Fundamentals

Microsoft Developing SQL Databases

Chapter 2. DB2 concepts

MCSA SQL Server 2012/2014. A Success Guide to Prepare- Querying Microsoft SQL Server 2012/2014. edusum.com

Fundamentals, Design, and Implementation, 9/e Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M.

How Oracle Does It. No Read Locks

Microsoft. [MS20762]: Developing SQL Databases

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

Assignment Session : July-March

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Concepts of Database Management Seventh Edition. Chapter 4 The Relational Model 3: Advanced Topics

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

DATABASES SQL INFOTEK SOLUTIONS TEAM

Developing SQL Databases

IBM EXAM QUESTIONS & ANSWERS

PL/SQL Block structure

Microsoft Querying Data with Transact-SQL - Performance Course

Now, we can refer to a sequence without having to use any SELECT command as follows:

Oracle Class VII More on Exception Sequence Triggers RowID & Rownum Views

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

SQL functions fit into two broad categories: Data definition language Data manipulation language

EDUVITZ TECHNOLOGIES

CSC 261/461 Database Systems Lecture 6. Fall 2017

Querying Data with Transact-SQL

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

20464 Developing Microsoft SQL Server Databases

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

SQL: Data Definition Language

[MS20464]: Developing Microsoft SQL Server 2014 Databases

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

20761B: QUERYING DATA WITH TRANSACT-SQL

Data Definition Guide InterBase 2009

20761C: Querying Data with Transact-SQL

Transcription:

Data Integrity IT 4153 Advanced Database J.G. Zheng Spring 2012 Overview Three basic types of data integrity Integrity implementation and enforcement Database constraints Transaction Trigger 2 1

Data Integrity Data integrity: Completeness, accuracy/correctness, consistency, validity, accessibility, currency Relational databases ensure data integrity in 3 aspects: Entity Integrity: no empty or duplicate rows in a table. Use primary key constraints Referential integrity: rows cannot be deleted or modified, if they are referenced by other records in other tables Use foreign key constraints Domain Integrity: valid entries for a given column is restricted by the type, the format, or the range of possible values Use Check rules on columns 3 Entity Integrity Entity Integrity No empty or duplicate rows in a table. Implementation Primary key Unique key 4 2

Domain Integrity Domain Integrity Valid entries for a given column is restricted by the type, the format, or the range of possible values Implementation Use Check constraints on columns Range of values Finite number of choices Patterns, regex XML 5 Referential Integrity Every value of a foreign key must match a value of the referenced primary key or be NULL Implementation Foreign key constraints 6 3

Enforcing Referential Integrity in Databases In Microsoft Access In SQL Server 7 Referential Actions Actions when a dependent row (primary key) is updated or deleted CASCADE SET NULL SET DEFAULT NO ACTION RESTRICT 8 4

Transaction A serials of database actions (operations) defined as a single logical unit (atomic) They should either all succeed, or all fail, in case of system errors General process Begin Transaction Execute a set of data manipulations and/or queries; (Previous changes are uncommitted data ) If no error, commit changes; Otherwise, rollback changes; (End Transaction) 9 Transaction Example Update Nancy's hire date to 1992/6/1, and Jack's hire date 1 day after Nancy's hire date. BEGIN TRANSACTION DECLARE @newdate date='8/1/1992'; update Employees set HireDate=@newDate where EmployeeID=1; update Employees set HireDate=dateadd(DAY, 1, @newdate) where EmployeeID=10; COMMIT; 10 5

Error Handling Use @@ERROR and @@ROWCOUNT to validate the operation of a DML statement @@ERROR is checked for any indication of an error Returns 0 if no errors. Because @@ERROR is cleared and reset on each statement executed, check it immediately following the statement being verified, or save it to a local variable that can be checked later. @@ROWCOUNT is the number of rows that were affected in the last modification 11 Transaction with Error Handling BEGIN TRANSACTION DECLARE @newdate date='10/1/1992'; update Employees set HireDate=@newDate where EmployeeID=1; if @@ERROR <> 0 or @@ROWCOUNT<>1 begin end rollback tran; return; update Employees set HireDate=dateadd(DAY, 1, @newdate) where EmployeeID=11; if @@ERROR = 0 and @@ROWCOUNT=1 commit; else rollback; Check for errors after each statement. RETURN is immediate and complete and can be used at any point to exit from a procedure, batch, or statement block. Statements that follow RETURN are not executed. 12 6

Trigger A trigger is a special kind of stored procedure that automatically executes when an event occurs in the DBMS. DML triggers execute when a user tries to modify data through a data manipulation language (DML) event (INSERT, UPDATE, or DELETE statements) These triggers fire when any valid event is fired, regardless of whether or not any table rows are affected. DDL triggers execute in response to a variety of data definition language (DDL) events (primarily correspond to T-SQL CREATE, ALTER, and DROP statements, and certain system stored procedures that perform DDL-like operations) Logon triggers fire in response to the LOGON event that is raised when a user sessions is being established. 13 DML Triggers DML triggers are frequently used for enforcing business rules and data integrity. The trigger and the statement that fires it are treated as a single transaction, which can be rolled back from within the trigger. Types AFTER Triggers: executed after the action of the INSERT, UPDATE, or DELETE statement is performed. AFTER triggers can be specified only on tables. INSTEAD OF Triggers: executed in place of the usual triggering action. INSTEAD OF triggers can also be defined on views with one or more base tables. http://msdn.microsoft.com/en-us/library/ms190267.aspx 14 7

Create an AFTER Trigger Special tables used in DML triggers CREATE TRIGGER TestTrigger on Products AFTER UPDATE as select * from deleted; select * from inserted; rollback transaction; go UPDATE Products set UnitPrice=20 WHERE ProductName='Chai' This is an AFTER UPDATE trigger These two temporary tables are used to hold uncommitted data. Ignore all modifications if conditions are not met. 15 AFTER Trigger Practical Example CREATE TRIGGER PriceUpdateLog ON Products AFTER UPDATE AS Select SYSTEM_user as Who, deleted.unitprice as OldValue, inserted.unitprice as NewValue, getdate() as ChangeDateTime from deleted inner join inserted on deleted.productid = inserted.productid go Log all price changes for the products table. These can be inserted into a log table if we created one. 16 8

DML Triggers and Constraints Constraints and DML triggers each have benefits that make them useful in special situations. The primary benefit of DML triggers is that they can contain complex processing logic that uses Transact-SQL code. If constraints exist on the trigger table, they are checked after the INSTEAD OF trigger execution and before the AFTER trigger execution. If the constraints are violated, the INSTEAD OF trigger actions are rolled back and the AFTER trigger is not fired. 17 Summary Key concepts Data integrity Domain integrity, entity integrity, referential integrity Transaction, commit, rollback Trigger, after trigger, instead of trigger Key skills Declare explicit transactions Create database DML triggers 18 9