hello,
What is foreign key in database systems ? and when to use foreign keys ?
Does anybody have an information about the same......please share your ideas
thank you
hello,
What is foreign key in database systems ? and when to use foreign keys ?
Does anybody have an information about the same......please share your ideas
thank you
In relational databases, a foreign key is a referential constraint between two tables.
Function - The foreign key identifies a column or a 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 be the primary key or other candidate key 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 (master table, or referenced table) to many (child table, or referencing table) relationship.
Defining foreign keys -
Likewise, foreign keys can be defined as part of the CREATE TABLE SQL statement.Code:ALTER TABLE <table identifier> ADD [ CONSTRAINT <constraint identifier> ] FOREIGN KEY ( <column expression> {, <column expression>}... ) REFERENCES <table identifier> [ ( <column expression> {, <column expression>}... ) ] [ ON UPDATE <referential action> ] [ ON DELETE <referential action> ]
If the foreign key is a single column only, the column can be marked as such using the following syntax:Code:CREATE TABLE table_name ( id INTEGER PRIMARY KEY, col2 CHARACTER VARYING(20), col3 INTEGER, ... CONSTRAINT col3_fk FOREIGN KEY(col3) REFERENCES other_table(key_col) ON DELETE CASCADE, ... )
Code:CREATE TABLE table_name ( id INTEGER PRIMARY KEY, col2 CHARACTER VARYING(20), col3 INTEGER REFERENCES other_table(column_name), ... )
If you are using MySQL, then you can create foreign keys for Quicker Database Development,
Consider the following example Database
The example database is created as follows:
CREATE DATABASE mydb;
USE mydb;
We now define our two tables. Note that InnoDB is specified as the table type and we will also add an index for the employee’s last name.
CREATE TABLE employee (
id smallint(5) unsigned NOT NULL,
firstname varchar(30),
lastname varchar(30),
birthdate date,
PRIMARY KEY (id),
KEY idx_lastname (lastname)
) ENGINE=InnoDB;
CREATE TABLE borrowed (
ref int(10) unsigned NOT NULL auto_increment,
employeeid smallint(5) unsigned NOT NULL,
book varchar(50),
PRIMARY KEY (ref) ) ENGINE=InnoDB;
We can now specify our foreign key (this could be handled in the CREATE TABLE statement, but it is shown separately here):
ALTER TABLE borrowed
ADD CONSTRAINT FK_borrowed
FOREIGN KEY (employeeid) REFERENCES employee(id)
ON UPDATE CASCADE
ON DELETE CASCADE;
This tells MySQL that we want to alter the borrowed table by adding a constraint called ‘FK_borrowed’. The employeeid column will reference the id column in the employee table - in other words, an employee must exist before they can borrow a book.
- The referencing and referenced table may be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known in as self-referencing or recursive foreign key.
- A table may have multiple foreign keys, and each foreign key can have a different referenced table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.
Bookmarks