[SQL] Referential Integrity Details

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Database Sql
Click For Summary
SUMMARY

This discussion clarifies the process of declaring foreign keys in SQL to enforce referential integrity. Specifically, it explains that a foreign key is declared in the dependent table, such as USER_LOGIN, which references the primary key USER_ID in the USERS table. The example provided demonstrates the SQL syntax for creating a USER_LOGIN table with a foreign key constraint. The user expresses confusion about the declaration process, which is addressed by confirming that it occurs during the table creation phase.

PREREQUISITES
  • Understanding of SQL syntax and table creation
  • Familiarity with primary and foreign key concepts
  • Knowledge of referential integrity principles
  • Experience with SQL Server or similar relational database management systems
NEXT STEPS
  • Learn how to enforce referential integrity in SQL Server
  • Explore SQL Server Data Types and their usage in table definitions
  • Study the implications of cascading updates and deletes on foreign keys
  • Investigate best practices for database normalization and key relationships
USEFUL FOR

Database developers, SQL practitioners, and anyone involved in designing relational databases who seeks to understand the implementation of referential integrity through foreign keys.

WWGD
Science Advisor
Homework Helper
Messages
7,804
Reaction score
13,107
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
Yes, the foreign key is declared on the dependent 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   Reactions: jim mcnamara
Thanks, Borq
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 50 ·
2
Replies
50
Views
9K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K