Primary key and Foreign Key
i want to know how to use primary key and foreign key
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
How do you mean?
Primary key will uniquely identify any row within your table, and the foreign key is used to relate the child table to the parent.
We have a v simple database containing student course information. In one table we have student data (where the primary key is studentid), and in the second table we have course data (where the courseid is pkey).
The relationship between the 2 tables is that a student undertakes one course and a course can be taken by many students - a one to many rel.
In order to relate the two tables, we put the courseid (foreign key) into the student table.
^^ does that help???...
soz-very tired at the moment to get into any more detail!
Last edited by evian; 08-31-2006 at 12:40 PM.
means we have to write sql query to insert data into student table and one more sql query to insert data into course tabel,for ex
stid stname stcid
1 syewna c1
so we will be inserting c1 as the course id in both the tabels,what is the use then we can have the column 'cname' in student tabel itself no,so we can save having 2 columns of cid in both the tabels
Yeap, u need to insert cid into both tables if cid is your primary key-how else would you know what student was doing what course???
Originally Posted by syedwna
If each course name is unique then you could possibly use that as the primary key of the course table, and then put the course name into the child table (student table in your case) .
You may use course id rather than the course name to avoid problems such as:
if course name is written incorrectly and inserted into both tables - to correct this spelling mistake we have to update both tables. If we used the course id - we would just have to update the sp mistake in the course table.
Hope that helps.
primary key,foreign key,index
can u explain in basic example about these keys
primary key enforce uniqueness for values entered in specified columns that do not allow nulls; in other words it uniquely identifies a record in our table.
example: table employee
Emp_ID Emp_Name Emp_Age Dept_ID
1 Rana 24 1
2 nana 27 1
3 Rana 26 2
as you can see above Emp_ID can never be NULL and can never be repeated in the table, thus each employee has its own ID
therefore the Emp_ID field can be assigned as primary key since it uniquely identifies a record.
while as you can see also the name and the department id could be repeated.
As for foreign key:
A foreign key (FK) is a column used to establish and enforce a link between the data in two tables. A link is created between two tables by adding the column that hold one table's primary key values to the other table. This column becomes a foreign key in the second table.
or in other word its Father and child relation, always the child carries his father name and not the opposite.
example dept table:
in this table dept_id is aprimary key since it uniquely identifies a record.
while if u look to the dept_ID in the employee table u will notice that it can be repeated and it shows each employee to which dept he belongs
concerning the index property, its just like the index of a book that helps in fast searching
i guess i couldnt be as clear as possible, but im somehow in a hurry.i will check later and repost with better examples.
By Brian Pittman in forum Database
Last Post: 04-29-2007, 09:23 AM
By Dan Thibodeaux in forum .NET
Last Post: 10-28-2002, 10:06 AM
By Chris in forum Database
Last Post: 04-24-2002, 05:19 PM
By Sankar in forum Database
Last Post: 09-18-2001, 05:53 PM
By Gonzalo in forum Database
Last Post: 04-19-2001, 05:58 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL