Apr 02

A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

Let’s illustrate the foreign key with an example. Look at the following two tables:

The “Persons” table:

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

The “Orders” table:

O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 2
4 24562 1

Note that the “P_Id” column in the “Orders” table points to the “P_Id” column in the “Persons” table. The “P_Id” column in the “Persons” table is the PRIMARY KEY in the “Persons” table. The “P_Id” column in the “Orders” table is a FOREIGN KEY in the “Orders” table. The FOREIGN KEY constraint is used to prevent actions that would destroy link between tables. The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key column, because it has to be one of the values contained in the table it points to.

SQL FOREIGN KEY Constraint on CREATE TABLE

The following SQL creates a FOREIGN KEY on the “P_Id” column when the “Orders” table is created:

MySQL:

CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )

SQL Server / Oracle / MS Access:

CREATE TABLE Orders ( O_Id int NOT NULL PRIMARY KEY, OrderNo int NOT NULL, P_Id int FOREIGN KEY REFERENCES Persons(P_Id) )

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )

SQL FOREIGN KEY Constraint on ALTER TABLE

To create a FOREIGN KEY constraint on the “P_Id” column when the “Orders” table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders ADD CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)

To DROP a FOREIGN KEY Constraint

To drop a FOREIGN KEY constraint, use the following SQL:

MySQL:

ALTER TABLE Orders DROP FOREIGN KEY fk_PerOrders

SQL Server / Oracle / MS Access:

ALTER TABLE Orders DROP CONSTRAINT fk_PerOrders

Incoming search terms for the article:

sql foreign key constraint, access 2010 sql foreign key, foreign key sql access 2010, sql access 2010 fk, foreign keys access 2010 sql, what is this CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES, sql add foreign key constraint, CONSTRAINT fk_PerOrders, ms sql foreign key multi column, sql foreign key constraint multiple columns, HOW TO USE FOREIGN KEY IN COBOL, ms access alter table add fk not null, ms access create sql table with 2 foreign keys, ms access foreign key constraint, ms access foreign key references, ms access sql foreign key constraint, ms sql add foreign key constraint, how add constraint in Access 2010, MS SQL constraint, how to access foreign key sql with constraint, ms access 2010 - foreign key reference, how to add foreign key in sql server 2010, how to use foreign key in cobol sql, how to use foreign keys in cobol, how to create table foreign key sql server, how use fk in sql 2010, how to create multiple foreign keys in sql table, how to create a table in access 2010 sql with primary key and foreign key together, how to add foreign key sql in access 2010, microsoft access alter table foreign key multiple, microsoft access 2010 setting foreign key sql, ms sql foreign key on multiple columns, multiple foreign key references in ms access 2010, sql foreign key constraint mutliple columns order, sql foreign key constraint with multipe columns, sql server 2010 create table foreign key, sql server 2010 create table foreign key constraint, sql server foreign key constraint multiple columns, sql server foreign key constraint on multi column primary key, sql to create primary and foreign keys in access 2010, sql why use foreign key constraint, what is a foreign key in access 2010 oracle, sql foreign key, sql for access 2010, msaccess sql alter tabel foreignkey references, orders and persons tables in which sql?, multiple foreign keys constraints in one table on ms access, multiple foreign keys sql server, mysql one to one sql foreign key create table, oracle create table with foreign key constraint on multiple columns, oracle sql constraint foreign key multiple columns, p_id int foreign key references persons(p_id), sql 2010 create foreign key, sql access 2010 create table foreign key, what is an invalid foreign key constraint in MSS, hora en sql, access 2010 ALTER TABLE INTEGER PRIMARY KEY, access sql foreign key reference multiple, add a constraint foreign value access 2010, add constraint fk_perorders, add constraint Ms sql, add foreign key sql, add foreign keys sql oracle not mandatory, allow naming of a foreign key constraint, ALTER key in sql in cobol, alter table add multiple foreign key in oracle sql, alter table and apply foreign key constraint in sql server, access sql foreign key constraint, Access SQL constraint foreign key multiple, access FOREIGN KEY, access 2010 foreign key constraint, access 2010 foreign key constraint sql, access 2010 primary key sql alter, access 2010 sql alter table, access 2010 sql create table fields references, access 2010 sql foreign and primary keys, access 2010 sql server foreign key, access 2010 sql syntax foreign key, access 2010 sql type constraint foreign key, access alter table FOREIGN KEY sql, alter table definition foreign key reference sql server, alter table delete foreing key sql2010, alter table orders drop foreign key p_id, foreign key constraint on multiple columns in sql server, foreign key in cobol, foreign key ms sql, foreign key multiple columns mssql, foreign key on multiple columns in ms sql, foreign key one to one ms access sql, FOREIGN KEY SQL ACCESS, foreign key sql in access 2010, foreign key sql server, foreign key cobol, FOREIGN KEY constraint ms sql server multiple column, foreign key constraint, fk_perorders sql, CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id), CONSTRAINT fk_PerOrders sql, constraint foreign key access, constraint foreign key microsoft access

Leave a Reply

Features Stats Integration Plugin developed by YD