简体   繁体   中英

Is it really needed to define a column as foreign key in all tables where it is used

I have a table which has as primary key and it is used in other 40 to 50 tables(approx.). 表,该表具有作为主键,并且在其他40到50个表(约)中使用。 Is it necessary to define column as foreign key in each and every table where is being used? 表中将列定义为外键?

It depends. RI/FKs were introduced in Oracle in v7, so databases most definitely existed without them, and some still do. But it is common to have these defined and enabled as you basically get this checking 'for free'.

There may be some specialist cases where you might prefer not to use them. I worked on one low latency system where speed was everything, and the order in which parent/child data was posted to the database from the application management layer could not be guaranteed. In this case we found it faster to build the integrity rules in the apps management layer. As I said, a specialist case, seldom seen elsewhere, will make the purists spit blood but it worked very well and has been a successful system for over 10 years.

However, I do stress again, that FK/RI are good overall, and I would reverse you question to say "When should I consider not having FK?".

In my experience, you'll most commonly find these missing out of sheer laziness on the part of developers.

It is not required, but you want to maintain data integrity, it is necessary. Many databases, use it to find data faster. It is a good practice.

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