INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Database objects User schema DCL Oracle dictionary.
Data Integrity Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
Database Architecture The Relational Database Model.
Chapter 5 Relational Model Concepts Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 1 Week4 Relational Model and Relational Database Constraints Relational Model Concepts.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
11/07/2003Akbar Mokhtarani (LBNL)1 Normalization of Relational Tables Akbar Mokhtarani LBNL (HENPC group) November 7, 2003.
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.
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.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
1 ER Modeling BUAD/American University Mapping ER modeling to Relationships.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Chapter 3 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
Relational Model Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 3.
Fundamental of Database Systems
SQL: Schema Definition and Constraints Chapter 6 week 6
RELATION.
Constraints and Triggers
Chapter (9) ER and EER-to-Relational Mapping, and other Relational Languages Objectives How a relational database schema can be created from a conceptual.
Lecture # 13 (After 1st Exam)
Constraints AND Examples
Chapter (9) ER and EER-to-Relational Mapping, and other Relational Languages Objectives How a relational database schema can be created from a conceptual.
SQL: Constraints and Triggers
Relational Integrity Constraints
SQL data definition using Oracle
Database Design: Relational Model
Relational Data Model - 2
Constraints AND Examples
Chapter (7) ER-to-Relational Mapping, and other Relational Languages
Presentation transcript:

INTEGRITY

Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the schema. Integrity type for relational model Entity integrity Referential integrity Domain integrity

Domain Constraint Specify that within each tuple, the value of each attribute A must be an atomic value from DOM(A) Data Type FORMATE RANGE NULL or NOT NULL UNIQUE OR NOT UNIQUE

Entity integrity, Referential Integrity and foreign key Entity integrity constraint states that no primary key value can be null. Why? Because the primary key is used to identified individual tuples in a relation If having null values implies that we can not identify some tuples. Ex. 2 or more tuples have null values, we might not able to distinguish them if we tried to reference them from other relations.

Referential integrity constraint It is specified between 2 relations It is used to maintain the consistency among tuples in the two relations Informally, the referential integrity constraint states that a tuple in one relation that refer to another relation must refer to existing tuple in that relation.

Key Property The key satisfied 2 conditions Two distinct tuples in any state of the relation cannot have identical values for (all) the attributes in the key (Uniqueness) It is minimum set – that is can not remove any attributes and still have the uniqueness constraint in condition 1 hold (Minimality)

Candidate and Primary key In a relation may have more than one key. Each of Key is called a Candidate Key Example Relation Student (ID,FNAME,LNAME, TCODE, FACTCODE, DEPTCODE) Has 2 candidate keys: ID and FNAME+LNAME One candidate key is selected to be Primary Key of the relation

Foreign key A set of attributes FK in relation R1 is a foreign key of R1 that references relation R2 if it satisfied the following 2 rules The attributes in FK have the same domain(s) as the primary key attributes PK of R2; the attributes FK are said to reference or refer to the relation R2 A value of FK in tuple t1 of the current state r1(R) either occurs as a value of PK for some tuple t2 in the current state r2(R) or is null. In the former case, we have t1[FK] = t2[PK], and we say that the tuple t1 references or refers to the tuple t2.

Specified constraints Must clear meaning and role of that each set of attributes plays in the various relation schemas of the database. Referential integrity constraint the from relationship among entities.

Specifying Basic Constraints in SQL Create table Table_name (Col1data_type constraint, col2,…, ColnData_type constraint, Constraint Cont_name Primary Key (key atts)

Example GStudent (ID,NAME) SQL> create table GStudent (id varchar2(10), name varchar2(30) not null, constraint gStuPk primary key (id));

Oracle example SQL> connect Connected. SQL> desc dba_constraints Name Null? Type OWNER NOT NULL VARCHAR2(30) CONSTRAINT_NAME NOT NULL VARCHAR2(30) CONSTRAINT_TYPE VARCHAR2(1) TABLE_NAME NOT NULL VARCHAR2(30) SEARCH_CONDITION LONG R_OWNER VARCHAR2(30) R_CONSTRAINT_NAME VARCHAR2(30) DELETE_RULE VARCHAR2(9) STATUS VARCHAR2(8) DEFERRABLE VARCHAR2(14) DEFERRED VARCHAR2(9) VALIDATED VARCHAR2(13) GENERATED VARCHAR2(14) BAD VARCHAR2(3) RELY VARCHAR2(4) LAST_CHANGE DATE SQL> select constraint_name, constraint_type from dba_constraints where owner = 'A' CONSTRAINT_NAME C GSTUPK P SYS_C C

Specifying key and referential integrity constraint Primary CONSTRINT Constraint_Name Primary Key (Key Attribute) Referential Integrity CONSTRAINT Constraint_Name Foreign Key (FK attri) references Table_name (Key_attri) On BusinessRule

Example create table Teacher (tcode varchar2(2), tname varchar2(30) not null, constraint TeacherPK Primary key (tcode)) alter table gstudent add (tcode varchar2(2)); alter table gStudent add (constraint GSTUFK foreign key (tcode) references teacher(tcode))

WHY INTEGRITY is Importance?

IN DATABSE Design should consider Foreign key can be NULL? Answer depend on the Business Rule

Insert DEPENDENT AUTOMATIC NULLIFY DEFAULT

Delete (Refer to others) Restrict Cascade Nullified Default

UPDATE OPERATIONS and DEALING with Constraint Violation INSERT OPEARTION Provide of a list of a new Tuple t that insert in Relation R Domain Constraint can be violated if an attribute value is given that does not appear in the corresponding domain. Key constraint can be violated if a key value in the new tuple t already exist in another tuple in the relation r(R) Entity integrity can be violated if the primary key of the new tuple t is null. Referential Integrity can be violated if the value of any foreign key in t refer to a tuple that does not exist in the reference relation.