Can anyone please explain to me what is Primary key and what is Foreign Key and the purpose of using this type of keys for databases
Answered By
khalekbd
0 points
N/A
#94669
Primary key and Foreign Key
Â
Primary key: It uniquely identifies every record in the relational table. It can either be a guaranteed normal attribute to be unique (such as SSS or Social Security Number in a table with no greater than one record/person) or it can be created by the DBMS (such as a globally unique identifier, in Microsoft SQL Server, or GUID). Primary keys might contain a single attribute or multiple attributes that are combined.
Â
Examples:
Â
Let’s take for example we have a STUDENTS table that consists a record for every student at a university. It would be a great choice to use the student's unique ID number for a primary key in the STUDENTS table. The reason behind this is that the student's first and last name would not be a good choice, as sometimes two or more students have the same names which is very common.
Â
Foreign Key: It is a field in a relational table that compares the primary key column of another table. In order to do cross-referencing of tables, you can use the foreign key. Establishing relationships between database tables is one of the most important concepts in databases. These provide a mechanism for linking data stored in different tables and retrieving it in an efficient way possible. In order to establish a link between two tables, you must specify a foreign key in one table that references a column from a different table.
Â