What is Referential Integrity in databases?

By: Peter den Haan Emailed: 1610 times Printed: 2079 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Take the example of the Book database as shown below.

Table

Column

Key

Book

ID

Primary key

Title

Not a key field

Price

Not a key field

Author

ID

Primary key

Author_Name

Not a key field

Contribution

ID

Primary key

Title_ID

Foreign key

Author_ID

Foreign key

What happens when you start manipulating the records in your tables? You can edit the book information at will without any ill effects, but what would happen if you needed to delete a title? The entries in the Contribution table will still link to a nonexistent book. Clearly you can't have a contribution detail without the associated book title being present. So, you must have a means in place to enforce a corresponding book title for each contribution. This is the basis of enforcing referential integrity. You can enforce the validity of the data in this situation in two ways. One is by cascading deletions through the related tables; the other is by preventing deletions when related records exist.

Note 

Referential integrity prevents inconsistent data from being created in the database by ensuring that any data shared between tables remains consistent. To put it another way, it ensures that the soundness of the relationships remains intact.

Database applications have several choices available for enforcing referential integrity, but if possible, you should let the database engine do its job and handle this for you. Database engines allow you to use declarative referential integrity. You specify a relationship between tables at design time, indicating if updates and deletes will cascade through related tables. If cascading updates are enabled, changes to the primary key in a table are propagated through related tables. If cascading deletes are enabled, deletions from a table are propagated through related tables.

Before you go ahead and enable cascading deletes on all your relationships, keep in mind that this can be a dangerous practice. If you define a relationship between the Author table and the Title table with cascading deletes enabled and then delete a record from Author, you'll delete all Title table records that come under this category. Be cautious, or you may accidentally lose important data.


JDBC Home | All JDBC Tutorials | Latest JDBC Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(5)


1. View Comment

really superb in simple english with examples. Expecting more tutorials on web service , ruby on rails , cake php , webservices using cake php , with a common kind of home page like recent tutorials , top ten tutorials , tutorials by categories, dashboard etc

View Tutorial          By: hariharan at 2009-01-14 22:22:32
2. View Comment

Good

View Tutorial          By: sirisha at 2009-09-08 23:04:22
3. View Comment

hey thats a superb way of explaining in plain terms ....very awsome job

View Tutorial          By: Agastya at 2011-05-08 03:11:53
4. View Comment

Awsome...Example...

View Tutorial          By: sitakanta at 2011-12-05 12:22:03
5. View Comment

i found this very pleasing for both my brain and my peen.

View Tutorial          By: Matthew Redisons at 2012-10-03 14:36:32

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Peter den Haan
What is Referential Integrity in databases?

More Tutorials in JDBC
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar
Import TEXT to TABLE in MSSQL
What is Referential Integrity in databases?
Handling CSV in Stored Procedures
setSavepoint and releaseSavepoint Example in Java
Calling a Stored Procedure from JDBC in Java
java.lang.NoClassDefFoundError and java.lang.NoSuchMethodError
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components
SELECT Statements
WHERE Clauses in SQL
Joins example in SQL
Common SQL Commands
Result Sets, Cursors and Transactions in SQL
Stored Procedures example in SQL

More Latest News
Most Viewed Articles (in JDBC )
Using JDBC to connect to MySQL from Java Program
Calling a Stored Procedure from JDBC in Java
How connection pooling works in Java and JDBC
A simple JDBC application sample code
Data Access Technologies in Java
JDBC Components
Using JDBC to extract data from a database and output to an XML document
What is the ACID principal?
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
Using the DriverManager Class vs Using a DataSource Object for a connection
PreparedStatement Example in Java
What is Referential Integrity in databases?
What is JDBC?
JDBC Architecture
The Structure of JDBC
Most Emailed Articles (in JDBC)
How connection pooling works in Java and JDBC
What is the ACID principal?
Using the DriverManager Class vs Using a DataSource Object for a connection
What is Referential Integrity in databases?
Using JDBC to connect to MySQL from Java Program
Data Access Technologies in Java
Stored Procedures example in SQL
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar
JDBC Architecture
A simple JDBC application sample code
WHERE Clauses in SQL
Common SQL Commands
Result Sets, Cursors and Transactions in SQL
Creating Database Tables Using ANT
setSavepoint and releaseSavepoint Example in Java