Question:
can some one tell me the difference between primary key & foriegn key?
hemy
2007-12-19 04:37:35 UTC
can some one tell me the difference between primary key & foriegn key?
Six answers:
@bhishek Dh@r
2007-12-19 04:46:10 UTC
Primary Key::::::::::::::::::::::::::::::::::::::

In relational database design, a unique key or primary key is a candidate key to uniquely identify each row in a table. A unique key or primary key comprises a single column or set of columns. No two distinct rows in a table can have the same value (or combination of values) in those columns. Depending on its design, a table may have arbitrarily many unique keys but at most one primary key.



A unique key must uniquely identify all possible rows that exist in a table and not only the currently existing rows. Examples of unique keys are Social Security numbers (associated with a specific person) or ISBNs (associated with a specific book). Telephone books and dictionaries cannot use names or words or Dewey Decimal system numbers as candidate keys because they do not uniquely identify telephone numbers or words.



A primary key is a special case of unique keys. The major difference is that for unique keys the implicit NOT NULL constraint is not automatically enforced, while for primary keys it is. Thus, the values in a unique key columns may or may not be NULL. Another difference is that primary keys must be defined using another syntax.



The relational model, as expressed through relational calculus and relational algebra, does not distinguish between primary keys and other kinds of keys. Primary keys were added to the SQL standard mainly as a convenience to the application programmer.



Unique keys as well as primary keys can be referenced by foreign keys.



***********************************************************************************************



Foreign Key::::::::::::::::::::::::::::::::::::::::

In the context of relational databases, a foreign key is a referential constraint between two tables.[1] 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 form a primary key or unique key. 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. 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.



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 SQL:2003 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.



Improper foreign key/primary key relationships or not enforcing those relationships are often the source of many database and data modeling problems.
anonymous
2007-12-19 04:40:56 UTC
In a database table, the primary key is a unique identifier for each element in that specific table. A table may also have a foreign key, which matches to a primary key in another table.
Gaurav Saxena
2007-12-19 04:54:35 UTC
Primary key is an attribute in a table itself by which a row is UNIQUELY identified i.e. it cannot be repeated. For ex- Roll Number in class is a unique identifier of every student Names could be repeated but roll number would be unique. By definition "The attribute or column by which a row is uniquely identified is known as primary key."

Foreign key is used to describe a link between two table. By definition "When a primary key of one table is present in another table then it is known as Foreign key." When we join two different tables then the attribute which we use to join the tables is known as Foreign Key.
Quel
2007-12-19 04:42:05 UTC
a primary key is a unique identifier of an entity. a foreign key is a primary key of another table that is just related to another entity.
holier than thou
2007-12-19 06:20:18 UTC
a table can have only one primary key but one or more foreign keys
?
2016-11-04 04:55:24 UTC
candidate key - is a minimum superkey which may well be used as a regular key. regular key- has unique vallue for each checklist. is a candidate key which you have chosen as superkey. replace keys- ?? dnt be attentive to approximately that desire this might help.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...