简体   繁体   中英

Derby foreign key

Hello guys and good day. I have Table "T1" that has primary key "ID" and another Table "T2" that has foreign key "T1ID" reference from the primary key"ID", My problem is the foreign key does not show any records that I insert from the primary key "ID", it should shows the data of the records what I insert in the primary key.

在此处输入图像描述

and this is the code that I execute to make foreign key:

Alter table T2 add FOREIGN KEY (T1ID) references T1(ID);

Please help and thank you in advance.

Foreign key"T1ID" supposed to shows the data of the records what I insert in the primary key"ID"

I think you misunderstand what a foreign key is and what it does. A foreign key doesn't "show any records" or "show the data". You use SQL SELECT statements to show records and show data.

A foreign key is an integrity constraint, and it instructs the database engine to enforce certain integrity rules. A foreign key tells the database that it should enforce a relationship between two table. In this case, specifically, you are telling the database to enforce the validity of the values in the t2.t1id column.

So the database will ensure that for each row in t2 , the value in t2.t1id from that row is a value of t1.id for some row in t1 .

The database will then do things like:

  • refuse to let you insert a row in t2 with a value of t2.t1id that doesn't exist in any row in t1
  • and refuse to let you delete a row from t1 if there are any rows in t2 which have values of t1id that match that particular t1.id value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM