Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > Integrity constraints and referential integrity
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Integrity constraints and referential integrity

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 01 August 2007

What is the difference between integrity constraints and referential integrity?


>

Good question. Referential integrity, also known as relational integrity, means that if a table contains a foreign key column, then every value in that column (except NULL, if it is allowed) will be found in the primary key of the table that it is related to, or references (the syntax for declaring the foreign key uses the keyword REFERENCES).

For example, let's say you have a table of users and a table of images, where each image belongs to a user:

users
id  name
23  Tom
25  Dick
31  Harry

images
id   user   path
187   23    /images/emo.jpg
188   31    /images/kitten.jpg
189   24    /images/manamana.jpg
190   25    /images/phlogiston.jpg

Can you spot the referential integrity problem?

Sometimes it's difficult to convince experienced programmers that they should declare their tables with foreign keys. "Why couldn't we," they ask, "just do a lookup on the user to ensure it exists, and then just insert the image?" The answer to that question, of course, is that (a) not all updates to the database are going to happen through the application (updates can also occur via bulk loads, SQL windows, command lines, etc.), and (b) why do programmers feel they need to reinvent the wheel? The database will look it up for you if the foreign key has been declared.

Referential integrity is only one type of database integrity, although arguably it might be the most important. Integrity constraints are any constraints that ensure integrity in databases. The various types of integrity constraints are:

NOT NULL
ensures that a column cannot have a NULL value.
UNIQUE KEY
ensures that each value in the key occurs at most once (note that unique keys can consist of more than one column).
PRIMARY KEY
same as unique key, except a primary key cannot be NULL. Primary key integrity is also called Entity Integrity.
FOREIGN KEY
values (except NULL, if allowed) must be found in referenced primary or unique key.
CHECK
provides a condition which is enforced on every row of the table. For example, CHECK (age >= 18) ensures that you cannot insert or update a row with an age less than 18. Check constraints are often used for Domain Integrity, to ensure that a column will have only certain specified values.
ASSERTION
a condition enforced across tables (and therefore declared separately from CREATE TABLE statements). Few DBMS systems support assertions.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle and SQL
Can I specify Oracle column order in my database table?
Review: Oracle's 11g R2 database has some good and bad
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
PL/SQL functions and triggers in Oracle
Stored procedures in PL/SQL
Do I need a license for SQL Developer Data Modeler in Oracle?
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
Oracle and SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts