Questions tagged [foreign-keys]

Foreign keys are a data integrity feature of relational (and SQL) databases. A foreign key is a set of attributes in a relational table that is subject to a referential integrity constraint. The referential integrity constraint mandates that the values of the foreign key attributes in one table, A, must match with corresponding values in some specified table B (which may sometimes be the same table as A).

The foreign key identifies a column or set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. The columns in the referencing table must reference the columns of the primary key or other superkey in the referenced table. The values in one row of the referencing columns must occur in a single row in the referenced table.

Thus, a row in the referencing table cannot contain values that don't exist in the referenced table (except potentially NULL). This way references can be made to link information together and it is an essential part of database normalization.

Multiple rows in the referencing table may refer to the same row in the referenced table. Most of the time, it reflects the one (parent table or referenced table) to many (child table, or referencing table) relationship.

Composite foreign key consists of two or more columns. All the columns that form a single foreign key reference one table.

In some cases foreign key constraint can reference the same table. For example for employee table, having employee_number, employee_name, and manager_employee_number. Every manager is also an employee, so it can be built a foreign key constraints between manager_employee_number to the employee_number

Reference

11290 questions
920
votes
17 answers

How can foreign key constraints be temporarily disabled using T-SQL?

Are disabling and enabling foreign key constraints supported in SQL Server? Or is my only option to drop and then re-create the constraints?
Ray
  • 187,153
  • 97
  • 222
  • 204
833
votes
15 answers

How to truncate a foreign key constrained table?

Why doesn't a TRUNCATE on mygroup work? Even though I have ON DELETE CASCADE SET I get: ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (mytest.instance, CONSTRAINT instance_ibfk_1 FOREIGN KEY (GroupID) REFERENCES…
user391986
  • 29,536
  • 39
  • 126
  • 205
757
votes
16 answers

How do I see all foreign keys to a table or column?

In MySQL, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for MySQL.
Christian Oudard
  • 48,140
  • 25
  • 66
  • 69
656
votes
6 answers

What is related_name used for?

What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name='maps'? class Map(db.Model): members = models.ManyToManyField(User,…
zjm1126
  • 63,397
  • 81
  • 173
  • 221
578
votes
30 answers

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

Using MSSQL2005, can I truncate a table with a foreign key constraint if I first truncate the child table (the table with the primary key of the FK relationship)? I know that I can either Use a DELETE without a where clause and then RESEED the…
ctrlShiftBryan
  • 27,092
  • 26
  • 73
  • 78
561
votes
27 answers

MySQL DROP all tables, ignoring foreign keys

Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there?
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
555
votes
12 answers

OneToOneField() vs ForeignKey() in Django

What's the difference between Django OneToOneField and ForeignKey?
redice
  • 8,437
  • 9
  • 32
  • 41
515
votes
8 answers

When to use "ON UPDATE CASCADE"

I use ON DELETE CASCADE regularly but I never use ON UPDATE CASCADE as I am not so sure in what situation it will be useful. For the sake of discussion let see some code. CREATE TABLE parent ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY…
NawaMan
  • 25,129
  • 10
  • 51
  • 77
466
votes
9 answers

Postgres and Indexes on Foreign Keys and Primary Keys

Does Postgres automatically put indexes on Foreign Keys and Primary Keys? How can I tell? Is there a command that will return all indexes on a table?
mainstringargs
  • 13,563
  • 35
  • 109
  • 174
465
votes
11 answers

How to select rows with no matching entry in another table?

I'm doing some maintenance work on a database application and I've discovered that, joy of joys, even though values from one table are being used in the style of foreign keys, there's no foreign key constraints on the tables. I'm trying to add FK…
Frosty840
  • 7,965
  • 12
  • 50
  • 86
452
votes
11 answers

Can a foreign key be NULL and/or duplicate?

Please clarify two things for me: Can a Foreign key be NULL? Can a Foreign key be duplicate? As fair as I know, NULL shouldn't be used in foreign keys, but in some application of mine I'm able to input NULL in both Oracle and SQL Server, and I…
Amit
  • 21,570
  • 27
  • 74
  • 94
422
votes
4 answers

How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?

Can anyone explain how to implement one-to-one, one-to-many and many-to-many relationships while designing tables with some examples?
arsenal
  • 23,366
  • 85
  • 225
  • 331
420
votes
16 answers

Add Foreign Key to existing table

I want to add a Foreign Key to a table called "katalog". ALTER TABLE katalog ADD CONSTRAINT `fk_katalog_sprache` FOREIGN KEY (`Sprache`) REFERENCES `Sprache` (`ID`) ON DELETE SET NULL ON UPDATE SET NULL; When I try to do this, I get this error…
frgtv10
  • 5,300
  • 4
  • 30
  • 46
410
votes
19 answers

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

I've been wrestling with this for a while and can't quite figure out what's happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides have a Stage. I'm using EF Codefirst migrations and the migrations are failing…
SB2055
  • 12,272
  • 32
  • 97
  • 202
393
votes
30 answers

MySQL Error 1215: Cannot add foreign key constraint

I am trying to forward engineer my new schema onto my database server, but I can't figure out why I am getting this error. I've tried to search for the answer here, but everything I've found has said to either set the database engine to InnoDB or to…
Robert B
  • 3,933
  • 2
  • 13
  • 6
1
2 3
99 100