SQLTeam.com | Weblogs | Forums

About Foreign Keys



I have a doubt About Foreign Key.
There is a table having some feilds including Primary key and now
there is another two tables that also having primary keys.
Here i want to give a foreign key relationship with the two tables to the one column of the main table.
Main table name is "rocket" columns(id (Primary key),rid (Foreign key),name,amount)
remaining tables names "store" ,'department'
'Store' columns(id (P),name,place)
"Department" columns(id (P),name,area)

Here in "rocket" table rid (F) is the foreign key from "store" &"Department" - id's is it possible to give this type as foreign key.


This is the Question I asked You Is it possible .

create table store (id int identity(1,1) primary key,
name varchar(15),
Area varchar(15))

create table department (Did int identity(1,1) primary key,
Dname varchar(15),
Dfloor varchar(10))

create table rocket(id int identity(1,1) primary key,
idOrDid int foreign key references store(id),
idOrDid int foreign key references department(Did),
rname varchar(10))


Foreign key is a description of how the field will be used. It is not a type. What you are doing is setting the "Primary Key" of one table to a reference to a "Primary Key" of another table, a foreign table.