[SQL] Referential Integrity Details

In summary, referential integrity is used to ensure that data in one table is linked to data in another table through primary and foreign keys. The foreign key is declared on the dependent table, usually when designing or creating it. In SQL, this can be done using the REFERENCES keyword.
  • #1
WWGD
Science Advisor
Gold Member
7,003
10,423
Hi a probably dumb question on Referential Integrity:

I think I understand the basic idea/motivation for it, but one thing I am confused about is this: just when/how
do we declare a column in a given table T' to be a foreign key related to the primary key for table T? Is it when we design/create table T' ? I know how, in ACCESS BQE to declare a field to be a primary key, but I have not seen how to make/declare a field in another table to be a foreign key associated to the primary key (though, strangely, once these two keys have been declared, I do know how to enforce referential integrity). And how is this done in SQL?
Thanks in Advance.
 
Technology news on Phys.org
  • #2
Yes, the foreign key is declared on the dependant table. For example, if you have a USERS table with a primary key of USER_ID and you wanted to create a USER_LOGIN table that tracked login attempts by user, you might create a table like this:
SQL:
CREATE TABLE USER_LOGIN (
    USER_LOGIN_ID NUMERIC(14,0) IDENTITY NOT NULL PRIMARY KEY,
    USER_ID NUMERIC(14) NOT NULL REFERENCES USERS(USER_ID),
    SUCCESS TINYINT NOT NULL,
    DATETIME_ATTEMPTED VARCHAR(14) NOT NULL,
    ATTEMPTED_BY_USER_IP VARCHAR(25) NULL
)
 
Last edited by a moderator:
  • Like
Likes jim mcnamara
  • #3
Thanks, Borq
 

What is referential integrity in SQL?

Referential integrity in SQL is the concept of maintaining consistency and accuracy of data relationships in a relational database. It ensures that any data entered into a foreign key column must exist in the referenced primary key column of another table.

Why is referential integrity important in SQL?

Referential integrity is important in SQL because it helps maintain data integrity and accuracy in a database. It prevents orphan records, data inconsistencies, and data corruption, ensuring that data relationships are properly maintained and data is reliable.

How is referential integrity enforced in SQL?

Referential integrity can be enforced in SQL by using foreign key constraints. This means that when a new record is inserted into a table, the value of the foreign key column must match an existing value in the referenced primary key column of another table, or the insert will fail.

What happens when referential integrity is violated in SQL?

If referential integrity is violated in SQL, it means that data relationships are not properly maintained. This can lead to data inconsistencies, orphan records, and data corruption. In such cases, the database may return an error or the operation may fail.

How can referential integrity be checked in SQL?

Referential integrity can be checked in SQL by using the CHECK constraint. This allows for the creation of rules that must be met before data can be inserted into a table. These rules can include checking for referential integrity before allowing data to be inserted, ensuring data accuracy and consistency.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
7
Views
399
  • Programming and Computer Science
2
Replies
50
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
641
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top