A beginners\' question about foreign key in MySQL.
In w3school it says,
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.<
A Foreign Key is used to maintain referential integrity, while a WHERE clause is used to join tables together in a SQL operation such as a select. The where clause can operate across multiple tables, but it is purely there as a filter.
Strictly speaking, you can get away without referential integrity, but it's not a good idea. Without referential integrity, you end up relying on your client application not incorrectly deleting or updating something at one end of a relationship chain that would have a knock on effect on the data, e.g. changing a key value to point to one that's not there.
Referential integrity is a great way of ensuring that related data is held in a consistent way.
the RESTRICT operator (WHERE) has nothing to do with referential constraints!
quote from C. J. Date's Relational Database Dictionary
foreign key Let R1 and R2 be relvars, not necessarily distinct, and let K be a key for R1. Let FK be a subset of the heading of R2 such that there exists a possibly empty sequence of attribute renamings that maps K into K' (say), where K' and FK contain exactly the same attributes. Then FK is a foreign key
referential integrity Loosely, the rule that no referencing tuple is allowed to exist if the corresponding referenced tuple doesn't exist. More precisely, let FK be some foreign key in some referencing relvar R2; let K be the corresponding key in the corresponding referenced relvar R1, and let K' be derived from K as described under foreign key. Then the referential integrity rule requires there never to be a time at which there exists an FK value in R2 that isn't the K' value for some (necessarily unique) tuple in R1 at the time in question. R1 and R2 here are the referenced relvar and the referencing relvar, respectively, and the constraint between them is a referential constraint.
Examples: In relvar SP, {S#}
and {P#}
are foreign keys corresponding to the keys {S#}
and {P#}
in relvars S and P, respectively. Note that the key in the referenced relvar that corresponds to a given foreign key is not required to be a primary key specifically.
First of all. Good Question !!
MySql is an RDBMS - Relational DBMS, so all the entities (tables) are related by an column.
EMPLOYEE - EMPID EMPNAME DEPTID
DEPARTMENT - DEPTID DEPTNAME
DEPTID is foriegn key in the EMPLOYEE table and primary key in the DEPARTMENT table.
This relation is imaginary relation of objects just an consideration or kind of designing for structuring data in a easy way to retrieve in future. NOT A PHYSICAL RELATION (because its a programming language)
In order to retrive that data, we need few syntax and described by the Creator of SQL.
SELECT * from EMPLOYEE
SELECT * FROM DEPARTMENT
SELECT * FROM EMPLOYEE WHERE DEPTID = 5
Here we have realted the two tables imaginary for our convinent, but for the required result we used this syntax WHERE DEPTID = 5.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.
I have another good reason to add the key relationships to your database. There are various code generators that use this information to generate an object model from your database. One notable pattern in common use is the ActiveRecord pattern. Without key relationships, the ActiveRecord pattern would not know how your database entities are related so it would generate a much less useful object model.
Code generation is not appropriate for every software project. But, it is helpful on a large number of projects. If you aren't using code generation you owe it to yourself to at least look into it.
Maintaining referential integrity and indexing.