what is the meaning of foreign key and use
what is the meaning of foreign key and use
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
MuthuMari MPosted Apr 6, 2012, 2:55 AM
A foreign key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.
Pls refer this url for examples
http://www.1keydata.com/sql/sql-foreign-key.html
Thanks.
lakshmipathi vemulaPosted Apr 17, 2012, 5:46 AM
You have 2 tables and you want to insert data into a table such that one column in that table should have the values which are there in the other table with same column datatype(Here not same number of values but same values..other values which are not there in another table for which this Foreign key is mapped with Primary key should not be entered).This is like setting range for values to be entered into some column in a table..
For each foreign key defined in one table .there should be primary key set in another table.
To know more about SQL http://www.bestdotnettraining.com/Online/Training/SQL-Server/Constraints/152
SenthilkumarPosted Apr 7, 2012, 2:37 AM
The foreign key is key which has the reference in the parent table.
Why we need to use the foreign key in the table?
It ensures that there is an integrity between the tables. For example you have the table called EmployeeMaster. The employee id is the primary key and it is eligible to became the foreign in the child tables. If any body try to enter the salary for unknown employee then its error.
Employee Table:
EmployeeId Name DOB DOJ
1 S 1/1/1900 5/6/2010
2 K 5/7/1989 4/4/2011
EmployeeID is PRIMARY KEY, it ensures here it will not allow the duplicate entries and creates the clustered index.
Salary Table:
ID EmployeeID Month Year Salary
1 1 5 2012 24500
2 2 5 2012 15000
3 3 5 2012 35000
Here EmployeeID is foreign key, it has the relationship withe Employees table. Here i try to insert the record for Employee ID 3, but it is an not relevant. It throws an error while inserting.
Keyur NarkhiPosted Apr 6, 2012, 3:50 AM
A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables. You can create a foreign key by defining a FOREIGN KEY constraint when you create or modify a table.
Below is the Example for FK.
Table Customer
column name
characteristic
SID
Primary Key
Last_Name
First_Name
Table Orders
column name
characteristic
Order_ID
Primary Key
Order_Date
Customer_SID
Foreign Key
Amount
selvi subramanianPosted Apr 6, 2012, 3:49 AM
Satyapriya NayakPosted Apr 6, 2012, 3:13 AM
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Please refer the below link
http://www.w3schools.com/sql/sql_foreignkey.asp
Thanks