Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Introduction to Structured Query Language (SQL)
Relational Model Stores data as tables –Each column contains values about the same attribute –Each column has a distinct name –Each row contains values.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Sections 10 – Constraints
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
Chapter 6 Additional Database Objects
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 4 Constraints Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane,
Database Technical Session By: Prof. Adarsh Patel.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
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.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Access Project 3 Notes. Introduction Maintaining the Database  Modifying the data to keep it up-to-date Restructure the Database  To change the database.
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.
Database Programming Sections 9 – Constraints. Marge Hohly2 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
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.
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.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
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,
Session 1 Module 1: Introduction to Data Integrity
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
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.
Chapter 3 Table Creation and Management Oracle 10g: SQL.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
Including Constraints
Module 5: Implementing Data Integrity by Using Constraints
SQL DATA CONSTRAINTS.
A Guide to SQL, Eighth Edition
Oracle Data Definition Language (DDL)
Including Constraints
Presentation transcript:

Chapter 4 Constraints Oracle 10g: SQL

Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, and NOT NULL constraints and the appropriate use for each constraint Understand how constraints can be created when creating a table or modifying an existing table Distinguish between creating constraints at the column level and table level Use DISABLE and ENABLE commands Use the DROP command

Oracle 10g: SQL 3 Constraints Rules used to enforce business rules, practices, and policies Rules used to ensure accuracy and integrity of data

Oracle 10g: SQL 4 Constraint Types

Oracle 10g: SQL 5 Creating Constraints Use the optional CONSTRAINT keyword during creation to assign a name Let the server name the constraint using the default format SYS_Cn Informative names can assist in debugging

Oracle 10g: SQL 6 Creating Constraints (continued) When: –During table creation –After table creation, by modifying the existing table How: –Column level approach –Table level approach

Oracle 10g: SQL 7 Creating Constraints at the Column Level If a constraint is being created at the column level, the constraint applies to the column specified

Oracle 10g: SQL 8 Creating Constraints at the Table Level Approach can be used to create any constraint type except NOT NULL Required if constraint is based on multiple columns

Oracle 10g: SQL 9 Enforcement of Constraints All constraints are enforced at the table level If a data value violates a constraint, the entire row is rejected

Oracle 10g: SQL 10 Adding Constraints to Existing Tables Constraints are added to an existing table with the ALTER TABLE command Add a NOT NULL constraint using MODIFY clause All other constraints are added using ADD clause

Oracle 10g: SQL 11 Using the PRIMARY KEY Constraint Ensures that columns do not contain duplicate or NULL values Only one per table is allowed

Oracle 10g: SQL 12 PRIMARY KEY Constraint for Composite Key List column names within parentheses separated by commas

Oracle 10g: SQL 13 Using the FOREIGN KEY Constraint Requires a value to exist in the referenced column of another table NULL values are allowed Enforces referential integrity Maps to the PRIMARY KEY in parent table

Oracle 10g: SQL 14 FOREIGN KEY Constraint Example

Oracle 10g: SQL 15 Deletion of Foreign Key Values You cannot delete a value in a parent table referenced by a row in a child table Use ON DELETE CASCADE keywords when creating FOREIGN KEY constraint – it automatically deletes the child records when the parent record is deleted

Oracle 10g: SQL 16 Using the UNIQUE Constraint No duplicates are allowed in the referenced column NULL values are permitted

Oracle 10g: SQL 17 Using the CHECK Constraint Updates and additions must meet specified condition

Oracle 10g: SQL 18 Using the NOT NULL Constraint The NOT NULL constraint is a special CHECK constraint with IS NOT NULL condition Can only be created at column level Included in output of DESCRIBE command Can only be added to an existing table using ALTER TABLE…MODIFY command

Oracle 10g: SQL 19 NOT NULL Constraint Example

Oracle 10g: SQL 20 Including Constraints During Table Creation – Column Level Include in column definition

Oracle 10g: SQL 21 Including Constraints During Table Creation – Table Level Include at end of column list

Oracle 10g: SQL 22 Multiple Constraints on a Single Column A column may be included in multiple constraints The order# column is included in a primary key and a foreign key constraint

Oracle 10g: SQL 23 Viewing Constraints – USER_CONSTRAINTS Can display name, type, and condition of CHECK constraints

Oracle 10g: SQL 24 Using DISABLE/ENABLE Use DISABLE or ENABLE clause of ALTER TABLE command

Oracle 10g: SQL 25 Dropping Constraints Constraints cannot be modified; they must be dropped and recreated Actual syntax depends on type of constraint –PRIMARY KEY - just list type of constraint –UNIQUE - include column name –All others - reference constraint name

Oracle 10g: SQL 26 ALTER TABLE…DROP Syntax

Oracle 10g: SQL 27 Drop Constraint Example

Oracle 10g: SQL 28 Drop Constraint Example - Error

Oracle 10g: SQL 29 Summary A constraint is a rule that is applied to data being added to a table –The constraint represents business rules, policies, and/or procedures –Data violating the constraint is not added to the table A constraint can be included during table creation as part of the CREATE TABLE command or added to an existing table using the ALTER TABLE command

Oracle 10g: SQL 30 Summary (continued) A PRIMARY KEY constraint does not allow duplicate or NULL values in the designated column Only one PRIMARY KEY constraint is allowed in a table A FOREIGN KEY constraint requires that the column entry match a referenced column entry in the referenced table or be NULL A UNIQUE constraint is similar to a PRIMARY KEY constraint except it allows NULL values to be stored in the specified column

Oracle 10g: SQL 31 Summary (continued) A constraint can be disabled or enabled using the ALTER TABLE command and the DISABLE and ENABLE keywords A constraint cannot be modified –To change a constraint, the constraint must first be dropped with the DROP command and then re-created