Database Constraints Ashima Wadhwa.

Slides:



Advertisements
Similar presentations
Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary.
Advertisements

The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Relational Model Stores data as tables –Each column contains values about the same attribute –Each column has a distinct name –Each row contains values.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Database Architecture The Relational Database Model.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Module Title? DBMS E-R Model to Relational Model.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
MIS 385/MBA 664 Systems Implementation with DBMS/ Database Management Dave Salisbury ( )
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Lecture # 14 Chapter # 5 The Relational Data Model and Relational Database Constraints Database Systems.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
Standards and Conventions
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
ER Diagrams and Relational Model CS 174a (Winter 2015)
Entity-Relationship Data Model
COP Introduction to Database Structures
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Order Database – ER Diagram
Relational Model Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 3.
Fundamental of Database Systems
COP Introduction to Database Structures
Normalization Karolina muszyńska
Chapter 2: Relational Model
The Relational Data Model and Relational Database Constraints
RELATION.
Entity-Relationship Model
Tables and Their Characteristics
Entity-Relationship Model
Lecture 2 The Relational Model
Order Database – ER Diagram
CSCI-100 Introduction to Computing
Lecture # 13 (After 1st Exam)
Constraints AND Examples
The Relational Database Model
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Normalization Referential Integrity
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
CS4222 Principles of Database System
Constraints.
CHAPTER 4: LOGICAL DATABASE DESIGN AND THE RELATIONAL MODEL
The Relational Model Transparencies
SQL DATA CONSTRAINTS.
Chapter 4 The Relational Model Pearson Education © 2009.
Database Design: Relational Model
Chapter 4 The Relational Model Pearson Education © 2009.
Mapping an ERD to a Relational Database
INSTRUCTOR: MRS T.G. ZHOU
Entity-Relationship Data Model
Including Constraints
Chapter 3 The Relational Model
RELATIONAL DATA MODEL - 1
Presentation transcript:

Database Constraints Ashima Wadhwa

Keys It is important that any entity in an entity set be uniquely identifiable. Practically, we use the values of certain attributes to uniquely identify an entity. For example, when the clerk at a bank keys in a customer’s SSN, the customer’s full information can be brought up.

Types of Keys Types of Keys: super key candidate key primary key Foreign Key Secondary Key Unique Key

Keys Superkey: is a set of attributes whose value can uniquely identify an entity in the entity set. A superkey contains one or more attributes. Example: The combination of “SSN” and “Name” is a superkey of the following entity set customer.

Notationally, we write, Superkey: (SSN, name) Other superkeys for the entity set customer: Superkey: (SSN) Superkey: (SSN, Street) Superkey: (SSN, City) Superkey: (SSN, Name, Street) Superkey: (SSN, Name, City) Superkey: (SSN, Street, City) Superkey: (SSN, Name, Street, City)

(2) Candidate key: is a set of one or more attributes whose value can uniquely identify an entity in the entity set, and any attribute in the candidate key cannot be omitted without destroying the uniqueness property of the candidate key. (It is minimal superkey). Example: (SSN, Name) is NOT a candidate key, because taking out “name” still leaves “SSN” which can uniquely identify an entity. “SSN” is a candidate key of customer. Candidate key could have more than one attributes.

(3) Primary Key: is the candidate key that is chosen by the database designer as the unique identifier of an entity. The database designer chooses only one candidate key as the primary key in building the system. Example: “SSN” and “License #” are both candidate keys of Driver entity set. The database designer can choose either one as the primary key. In an E-R diagram, we usually underline the primary key attribute(s).

Foreign Keys Foreign key – a (simple or composite) column which refers to the primary key of some table in a database. Foreign and primary keys must be defined on same data type. Bordoloi

Foreign Key Directors Movies primary key field parent table relationship child table Movies foreign key field

secondary keys. An entity may have one or more choices for the primary key. Collectively these are known as candidate keys. One is selected as the primary key. Those not selected are known as secondary keys. For example, an employee has an employee number, a National Insurance (NI) number and an email address. If the employee number is chosen as the primary key then the NI number and email address are secondary keys. 

Unique Key The attribute which uniquely identifies all the attribute of table is called unique key. Unique key allows null ( two nulls have different values for system)

Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way to guarantee that: rows in a table have valid primary or unique key values rows in a dependent table have valid foreign key values that reference rows in a parent table individual column values are valid

Database Constraints Integrity rules: primary key constraint (to enforce existence integrity) unique constraint (to enforce candidate key integrity) foreign key constraint (to enforce foreign key, or referential integrity) check constraint (to restrict a column's values; a partial enforcement of domain integrity) Not Null Constraint ( to restrict a column value to be blank) You specify one or more of these constraints when you use the Create Table statement to create a base table. You can also add or drop constraints with the Alter Table statement.

Primary Key Constraints A primary key serves as the unique identifier for rows in the table. The syntax of the primary key constraint is CREATE TABLE employee  ( id number(5) PRIMARY KEY,  name char(20),  dept char(10),  age number(2),  salary number(10),  location char(10)  ); CREATE TABLE employee ( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY,  name char(20), dept char(10), age number(2), salary number(10), location char(10) );

Unique Constraints This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated. CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) UNIQUE  ); CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT location UNIQUE  );

Foreign Key Constraints This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key. CREATE TABLE product  ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY,  product_name char(20), supplier_name char(20), unit_price number(10) ); CREATE TABLE order_items ( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY, product_id number(5) CONSTRAINT pd_id_fk REFERENCES, product(product_id), product_name char(20), supplier_name char(20), unit_price number(10) );

Check Constraints This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns. CREATE TABLE employee  ( id number(5) PRIMARY KEY,  name char(20),  dept char(10),  age number(2),  gender char(1) CHECK (gender in ('M','F')),  salary number(10),  location char(10)  ); 

Not Null This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. Which means a null value is not allowed. CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10)  );

Questions??