Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the advantages of defining a foreign key

What is the advantage of defining a foreign key when working with an MVC framework that handles the relation?

I'm using a relational database with a framework that allows model definitions with relations. Because the foreign keys are defined through the models, it seems like foreign keys are redundant. When it comes to managing the database of an application in development, editing/deleting tables that are using foreign keys is a hassle.

Is there any advantage to using foreign keys that I'm forgoing by dropping the use of them altogether?

like image 462
Daniel Avatar asked Apr 16 '12 23:04

Daniel


People also ask

What is the advantage of primary key and foreign key?

A primary key ensures unique row identification. This results in faster sorting, searching, and querying operations. A foreign key creates a link between two tables. It maintains referential integrity between the referencing column(s) and the referenced column(s).

Why do we define a foreign key?

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.

What are the advantages and disadvantages of primary key and foreign key?

Advantage : Primary key used to uniquely identify a record. Pimary key used for indexing the table for faster data access. Foreign key : Helps establish a 1 :m / M: 1 relationship with other tables but can increase data access overhead.

What is the advantage of using a key in database?

The primary key helps to quickly identify and find unique rows in a particular database table. In safe mode, only specific records can be uniquely identified using the primary key to ensure that updates and deletion only affect specific rows rather than larger volumes of data.


2 Answers

Foreign keys with constraints(in some DB engines) give you data integrity on the low level(level of database). It means you can't physically create a record that doesn't fulfill relation. It's just a way to be more safe.

like image 178
shark555 Avatar answered Oct 05 '22 16:10

shark555


It gives you data integrity that's enforced at the database level. This helps guard against possibly error in application logic that might cause invalid data.

If any data manipulation is ever done directly in SQL that bypasses your application logic, it also guards against bad data that breaks those constraints.

An additional side-benefit is that it allows tools to automatically generating database diagrams with relationships inferred from the schema itself. Now in theory all the diagramming should be done before the database is created, but as the database evolves beyond its initial incarnation these diagrams often aren't kept up to date, and the ability to generate a diagram from an existing database is helpful both for reviewing, as well as for explaining the structure to new developers joining a project.

It might be a helpful to disable FKs while the database structure is still in flux, but they're good safeguard to have when the schema is more stabilized.

like image 37
Davy8 Avatar answered Oct 05 '22 18:10

Davy8