Module 5: Implementing Data Integrity by Using Constraints

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 6 Implementing Table Structures in SQL Server ®2008 R2.
Introduction to Structured Query Language (SQL)
Module 5: Implementing Data Integrity. Overview Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints.
Introduction to Structured Query Language (SQL)
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
SQL Server 7.0 Maintaining Referential Integrity.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
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.
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.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
13 Copyright © Oracle Corporation, All rights reserved. Maintaining Data Integrity.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
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.
Session 1 Module 1: Introduction to Data Integrity
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
1 Agenda TMA02 M876 Block 4. 2 Model of database development data requirements conceptual data model logical schema schema and database establishing requirements.
In this session, you will learn to: Manage databases Manage tables Objectives.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
Managing Tables, Data Integrity, Constraints by Adrienne Watt
The Basics of Data Manipulation
Implementing Triggers
Overview Implementing Triggers Implementing XML Schemas.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
The Basics of Data Manipulation
Constraints.
Microsoft SQL Server 2014 for Oracle DBAs Module 7
SQL DATA CONSTRAINTS.
CS122 Using Relational Databases and SQL
C1. SQL BAsic.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Relational Database Design
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
IST 318 Database Administration
CS122 Using Relational Databases and SQL
Including Constraints
Presentation transcript:

Module 5: Implementing Data Integrity by Using Constraints

Overview Data Integrity Overview Implementing Constraints

Lesson 1: Data Integrity Overview Types of Data Integrity Options for Enforcing Data Integrity

Types of Data Integrity Domain Integrity (Columns) Entity Integrity (Rows) Referential Integrity (Between Tables or Columns of the Same Table)

Options for Enforcing Data Integrity Mechanism Description Data types Define the type of data that can be stored in a column Rules Define the acceptable values that can be inserted into a column Defaults Define the value of a column if a value is not specified Constraints Define how the Database Engine enforces data integrity Triggers Define code that is executed automatically when table is modified XML schemas Define the acceptable content of XML documents and fragments placed in an xml data column

Lesson 2: Implementing Constraints What Are Constraints? PRIMARY KEY Constraints DEFAULT Constraints CHECK Constraints UNIQUE Constraints FOREIGN KEY Constraints Cascading Referential Integrity Considerations for Constraint Checking Practice: Creating Constraints

What Are Constraints? Integrity type Constraint type Description Domain DEFAULT Specifies default value for column CHECK Specifies allowed value for column FOREIGN KEY Specifies column in which values must exist NULL Specifies whether NULL is permitted Entity PRIMARY KEY Identifies each row uniquely UNIQUE Prevents duplication of nonprimary keys Referential Defines columns whose value must match the primary key of this table Specifies the allowed value for a column based on the contents of another column

PRIMARY KEY Constraints PRIMARY KEY constraints identify one or more columns in a table that constitute a primary key One PRIMARY KEY constraint is allowed per table Value must be unique across constituent columns Null values are not allowed in constituent columns CREATE TABLE [HumanResources].[Department]( [DepartmentID] [smallint] IDENTITY(1,1) NOT NULL, [Name] [dbo].[Name], … CONSTRAINT [PK_Department_DepartmentID] PRIMARY KEY CLUSTERED ([DepartmentID] ASC) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] )

DEFAULT Constraints DEFAULT constraints define a default column value when no value is provided Only one DEFAULT constraint per column Only applicable to INSERT statements Some system supplied functions allowed CREATE TABLE [Production].[Location]( ... [Availability] [decimal](8, 2) NOT NULL CONSTRAINT [DF_Location_Availability] DEFAULT ((0.00)), [ModifiedDate] [datetime] NOT NULL CONSTRAINT [DF_Location_ModifiedDate] DEFAULT (getdate()) )

CHECK Constraints CHECK constraints restrict the values that can be entered into a column on INSERT or UPDATE Can define multiple CHECK constraints per column Can reference columns in the same table Cannot contain subqueries ALTER TABLE [HumanResources].[EmployeeDepartmentHistory] WITH CHECK ADD CONSTRAINT [CK_EmployeeDepartmentHistory_EndDate] CHECK (([EndDate]>=[StartDate] OR [EndDate] IS NULL))

UNIQUE Constraints UNIQUE constraints ensure that every value in a column is unique Only one null value allowed in a unique column Can include one or more columns CREATE TABLE [HumanResources].[Employee]( [EmployeeID] [int] IDENTITY(1,1) NOT NULL, [NationalIDNumber] [nvarchar](15) NOT NULL UNIQUE NONCLUSTERED, … )

FOREIGN KEY Constraints FOREIGN KEY constraints ensure referential integrity between columns in same or different tables Must reference a PRIMARY KEY or UNIQUE constraint User must have REFERENCES permission on referenced table ALTER TABLE [Sales].[SalesOrderHeader] WITH CHECK ADD CONSTRAINT [FK_SalesOrderHeader_Customer_CustomerID] FOREIGN KEY([CustomerID]) REFERENCES [Sales].[Customer] ([CustomerID])

Cascading Referential Integrity Controlled by CASCADE clause of the FOREIGN KEY constraint Cascade option UPDATE behavior DELETE behavior NO ACTION (Default) Raise error; roll back operation CASCADE Update foreign keys in referencing tables Delete rows in referencing tables SET NULL Set foreign keys in referencing tables to NULL SET DEFAULT Set foreign keys in referencing tables to DEFAULT values

Considerations for Constraint Checking Assign meaningful names to constraints Create, change, and drop constraints without having to drop and re-create the table Perform error checking in your applications and transactions Disable CHECK and FOREIGN KEY constraints: To improve performance when you run large batch jobs To avoid checking existing data when you add new constraints to a table

Practice: Creating Constraints In this practice, you will: Drop existing constraints Create a CHECK constraint by using Transact-SQL Create a DEFAULT constraint by using Transact-SQL

Lab: Implementing Data Integrity by Using Constraints Exercise 1: Creating Constraints Exercise 2: Disabling Constraints