What is foreign key and pramary keys

A primary key is a unique identifier for a record in a database table. It is used to uniquely identify each row in the table and should not be duplicated or null.

A foreign key is a field in a database table that is used to establish a link or relationship between two tables. It references the primary key of another table, creating a connection between the two tables. Foreign keys help maintain data integrity and enforce referential integrity, ensuring that the data between related tables remains consistent.

Foreign key and primary key are both concepts in database design.

A primary key is a column or a set of columns in a database table that uniquely identifies each row in that table. It cannot contain NULL values, and each table can have only one primary key. The primary key is used to enforce data uniqueness and integrity within a table.

For example, in a table of employees, the employee ID might be used as the primary key since each employee should have a unique ID.

A foreign key, on the other hand, is a column or a set of columns in one table that refers to the primary key in another table. It establishes a relationship or connection between two tables in a database.

For example, if there is a separate table for departments, the employees table might have a foreign key column that references the department ID in the departments table. This foreign key ensures that you cannot insert a row in the employees table with a department ID that doesn't exist in the departments table.

Foreign keys are used to create relationships between tables and maintain data integrity across the database by enforcing referential integrity.