Module 5: Implementing Data Integrity. Overview Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
1 Constraints, Triggers and Active Databases Chapter 9.
ERWin Template Overview By: Dave Wentzel. Agenda u Overview of Templates/Macros u Template editor u Available templates u Independent column browser u.
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.
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.
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.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
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.
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 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.
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.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
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.
Module 4: Implementing Data Integrity
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.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
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,
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
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)
Chapter 3: Relational Databases
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.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
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.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
SQL – Constraints & Triggers
CS122 Using Relational Databases and SQL
Presentation transcript:

Module 5: Implementing Data Integrity

Overview Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints Using Defaults and Rules Deciding Which Enforcement Method to Use

Types of Data Integrity Domain Integrity ( columns ) Entity Integrity (rows) Referential Integrity (between tables)

Enforcing Data Integrity 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 script Script enforces Implement by using triggers and stored procedures

 Defining Constraints Determining Which Type of Constraint to Use Creating Constraints Considerations for Using Constraints

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

Creating Constraints Use CREATE TABLE or ALTER TABLE Can Add Constraints to a Table with Existing Data Can Place Constraints on Single or Multiple Columns Single column, called column-level constraint Multiple columns, called table-level constraint

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

 Types of Constraints 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 Used with 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 > ' ' 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) 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) 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 Users Must Have SELECT or 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) USE Northwind ALTER TABLE dbo.Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers(CustomerID)

Cascading Referential Integrity CASCADENO ACTIONCustomersCustomers INSERT new CustomerID CustomerID (PK) 1 OrdersOrders CustomerID (FK) UPDATE old CustomerID to new CustomerID 2 CustomersCustomers CustomerID (PK) UPDATE CustomerID OrdersOrders CustomerID (FK) 1 CASCADE CustomersCustomers DELETE old CustomerID CustomerID (PK) 3

 Disabling Constraints Disabling Constraint Checking on Existing Data Disabling Constraint Checking When Loading New Data

Disabling Constraint Checking on Existing Data Applies to CHECK and FOREIGN KEY Constraints Use WITH NOCHECK Option When Adding a New Constraint Use if Existing Data Will Not Change 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) USE Northwind ALTER TABLE dbo.Employees WITH NOCHECK ADD CONSTRAINT FK_Employees_Employees FOREIGN KEY (ReportsTo) REFERENCES dbo.Employees(EmployeeID)

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 USE Northwind ALTER TABLE dbo.Employees NOCHECK CONSTRAINT FK_Employees_Employees

Using Defaults and Rules As Independent Objects They: Are defined once Can be bound to one or more columns or user-defined data types CREATE DEFAULT phone_no_default AS '(000) ' GO EXEC sp_bindefault phone_no_default, 'Customers.Phone' CREATE RULE regioncode_rule IN ('IA', 'IL', 'KS', 'MO') GO EXEC sp_bindrule regioncode_rule, 'Customers.Region'

Deciding Which Enforcement Method to Use Data integrity components Constraints Defaults and rules Triggers FunctionalityFunctionality Medium Low High Performance costs Low Medium-High Before or after modification Before After Data types, Null/Not Null Low Before

Recommended Practices Use Cascading Referential Integrity Instead of Triggers Use Constraints Because They Are ANSI-compliant

Lab A: Implementing Data Integrity

Review Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints Using Defaults and Rules Deciding Which Enforcement Method to Use