Module 4: Implementing Data Integrity

Slides:



Advertisements
Similar presentations
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
Advertisements

ERWin Template Overview By: Dave Wentzel. Agenda u Overview of Templates/Macros u Template editor u Available templates u Independent column browser u.
Module 5: Implementing Data Integrity. Overview Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
Data Integrity Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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.
Chapter 14 & 15 Conceptual & Logical Database Design Methodology
Overview What is SQL Server? Creating databases Administration Security Backup.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
Chapter 4 Intermediate SQL: Integrity Constraints.
An Investigation of Oracle and SQL Server with respect to Integrity, and SQL Language standards Presented by: Paul Tarwireyi Supervisor: John Ebden Date:
Database Technical Session By: Prof. Adarsh Patel.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
SQL Server 7.0 Maintaining Referential Integrity.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Module 3: Creating Data Types and Tables. Overview Working with Data Types Working with Tables Generating Column Values Generating Scripts.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
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.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Module 7: Modifying Data. Overview Using Transactions Inserting Data Deleting Data Updating Data Performance Considerations.
13 Copyright © Oracle Corporation, All rights reserved. Maintaining Data Integrity.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
SQL IMPLEMENTATION & ADMINISTRATION Data Integrity, Constraints, Relationships & Joins.
SQL Basics Review Reviewing what we’ve learned so far…….
In this session, you will learn to: Manage databases Manage tables Objectives.
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.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Getting started with Accurately Storing Data
Fundamental of Database Systems
Managing Tables, Data Integrity, Constraints by Adrienne Watt
The Basics of Data Manipulation
Constraints and Triggers
SQL: Constraints and Triggers
Module 5: Implementing Data Integrity by Using Constraints
The Basics of Data Manipulation
Constraints.
Data Integrity TÍNH TOÀN VẸN DỮ LiỆU.
Microsoft SQL Server 2014 for Oracle DBAs Module 7
SQL DATA CONSTRAINTS.
Contents Preface I Introduction Lesson Objectives I-2
SQL – Constraints & Triggers
Presentation transcript:

Module 4: Implementing Data Integrity

Overview Introducing Data Integrity Defining Constraints Understanding Constraint Types Disabling Constraints Using Defaults and Rules Deciding Which Enforcement Method to Use

Lesson: Introducing Data Integrity Types of Data Integrity How Data Integrity Is Enforced

Types of Data Integrity Domain Integrity (columns) Entity Integrity (rows) Referential Integrity (between tables or columns of the same table)

How Data Integrity is Enforced Declarative Data Integrity Criteria defined in object definitions SQL Server enforces automatically Implement by using constraints, defaults, and rules Procedural Data Integrity Criteria defined in scripts Criteria enforced by scripts Implement by using triggers and stored procedures

Lesson: Defining Constraints Guidelines for Determining Which Type of Constraint to Use Constraint Creation Considerations for Using Constraints

Guidelines for Determining Which Type of Constraint to Use Type of integrity Constraint type Domain DEFAULT CHECK FOREIGN KEY Entity PRIMARY KEY UNIQUE Referential

Constraint Creation Use CREATE TABLE or ALTER TABLE Can Add Constraints to a Table with Existing Data Can Place Constraints on Single or Multiple Columns Column-Level Constraint Can only reference a single column Specify as part of the column definition Table-Level Constraint Can reference one or more columns Specify as part of the table definition (after all column definitions)

Considerations for Using Constraints Can Be Changed Without Re-creating a Table Require Error-Checking in Applications and Transactions Verify Existing Data

Lesson: Understanding Constraint Types DEFAULT Constraints CHECK Constraints PRIMARY KEY Constraints UNIQUE Constraints FOREIGN KEY Constraints Cascading Referential Integrity

DEFAULT Constraints Apply Only to INSERT Statements Only One DEFAULT Constraint Per Column Cannot Be Used with IDENTITY Property or rowversion Data Type Allow Some System-Supplied Values USE Northwind ALTER TABLE dbo.Customers ADD CONSTRAINT DF_contactname DEFAULT 'UNKNOWN' FOR ContactName

CHECK Constraints Are Invoked by INSERT and UPDATE Statements Can Reference Other Columns in the Same Table Cannot: Be used with the rowversion data type Contain subqueries USE Northwind ALTER TABLE dbo.Employees ADD CONSTRAINT CK_birthdate CHECK (BirthDate > '01-01-1900' AND BirthDate < getdate())

PRIMARY KEY Constraints Only One PRIMARY KEY Constraint Per Table Values Must Be Unique Null Values Are Not Allowed Creates a Unique Index on Specified Columns USE Northwind ALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY NONCLUSTERED (CustomerID)

UNIQUE Constraints Allow One Null Value Allow Multiple UNIQUE Constraints on a Table Defined with One or More Columns Enforced with a Unique Index USE Northwind ALTER TABLE dbo.Suppliers ADD CONSTRAINT U_CompanyName UNIQUE NONCLUSTERED (CompanyName)

FOREIGN KEY Constraints Must Reference a PRIMARY KEY or UNIQUE Constraint Provide Single or Multicolumn Referential Integrity Do Not Automatically Create Indexes Requires Users to Have REFERENCES Permissions on Referenced Tables Use Only REFERENCES Clause Within Same Table USE Northwind ALTER TABLE dbo.Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers(CustomerID)

Cascading Referential Integrity NO ACTION CASCADE Customers Customers CustomerID (PK) CustomerID (PK) 1 1 INSERT new CustomerID UPDATE CustomerID CASCADE Orders 2 CustomerID (FK) Orders UPDATE old CustomerID to new CustomerID CustomerID (FK) Customers CustomerID (PK) 3 DELETE old CustomerID

Lesson: Disabling Constraints Guidelines for Disabling Constraint Checking on Existing Data Guidelines for Disabling Constraint Checking When Loading New Data

Guidelines for Disabling Constraint Checking on Existing Data Applies to CHECK and FOREIGN KEY Constraints Use WITH NOCHECK Option When Adding a New Constraint Existing Data Will Be Checked Only During Future Updates of the Constraint Column Can Change Existing Data Before Adding Constraints USE Northwind ALTER TABLE dbo.Employees WITH NOCHECK ADD CONSTRAINT FK_Employees_Employees FOREIGN KEY (ReportsTo) REFERENCES dbo.Employees(EmployeeID)

Guidelines for Disabling Constraint Checking When Loading New Data Applies to CHECK and FOREIGN KEY Constraints Use When: Data conforms to constraints You load new data that does not conform to constraints USE Northwind ALTER TABLE dbo.Employees NOCHECK CONSTRAINT FK_Employees_Employees

Lesson: Using Defaults and Rules

Defaults Considerations: Defined once, can be bound to one or more columns or user-defined data types Rules on the column validates the value of a default Check constraints on the column validates the value of a default. CREATE DEFAULT phone_no_default AS '(000)000-0000' GO EXEC sp_bindefault phone_no_default, 'Customers.Phone'

Rules Considerations: Defined once, can be bound to one or more columns or user-defined data types Can contain any expression valid in a WHERE clause A column or user-defined data type can have only one rule that is bound to it. CREATE RULE regioncode_rule AS @regioncode IN ('IA', 'IL', 'KS', 'MO') GO EXEC sp_bindrule regioncode_rule, 'Customers.Region'

Lesson: Deciding Which Enforcement Method to Use Functionality and Performance Considerations Comparison Matrix Best Practices

Functionality and Performance Considerations Declarative Integrity Use for fundamental integrity logic, such as: When enforcing valid values Maintaining the relationships between tables Triggers Or Stored Procedures Use to maintain complex redundant data that is not part of a primary or foreign key relationship

Data integrity components Before or after modification Comparison Matrix Data integrity components Functionality Performance costs Before or after modification Constraints Medium Low Before Defaults and rules Triggers High Medium-High After Data types, Null/Not Null

Best Practices Use Constraints Instead of Triggers Because They Incur Less Overhead ü ü Use Constraints Because They Are ANSI-Compliant Use Cascading Referential Integrity Instead of Triggers ü

Lab A: Implementing Data Integrity Exercise 1: Defining DEFAULT Constraints Exercise 2: Defining CHECK Constraints Exercise 3: Defining PRIMARY KEY Constraints Exercise 4: Defining FOREIGN KEY Constraints Optional Exercise: Creating Defaults and Rules