[SQL] Referential Integrity Details

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Database Sql
AI Thread Summary
Referential integrity is crucial in database design, ensuring that relationships between tables remain consistent. A foreign key in a table (T') is declared during the creation of that table, linking it to the primary key of another table (T). In SQL, this is done using the REFERENCES clause in the CREATE TABLE statement. For example, when creating a USER_LOGIN table that tracks login attempts, the USER_ID column can be defined as a foreign key referencing the USER_ID primary key in the USERS table. This establishes the relationship and enforces referential integrity, ensuring that every USER_ID in USER_LOGIN corresponds to a valid USER_ID in USERS.
WWGD
Science Advisor
Homework Helper
Messages
7,678
Reaction score
12,355
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 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
Thanks, Borq
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top