1.

Explain primary and foreign key with example

Answer» What is the Foreign key?FOREIGN KEY\xa0is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity. It acts as a cross-reference between two tables as it references the primary key of another table.Example:DeptCodeDeptName001Science002English005ComputerTeacher IDFnameLnameB002DavidWarnerB017SaraJosephB009MikeBruntonIn this key in dbms example, we have two table, teach and department in a school. However, there is no way to see which search work in which department.In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship between the two tables.Teacher IDDeptCodeFnameLnameB002002DavidWarnerB017002SaraJosephB009001MikeBruntonThis concept is also known as Referential Integrity.
PRIMARY KEY\xa0is a column or group of columns in a table that uniquely identify every row in that table. The Primary Key can\'t be a duplicate meaning the same value can\'t appear more than once in the table. A table cannot have more than one primary key.Rules for defining Primary key:Two rows can\'t have the same primary key valueIt must for every row to have a primary key value.The primary key field cannot be null.The value in a primary key column can never be modified or updated if any foreign key refers to that primary key.Example:In the following example, StudID is a Primary Key.StudIDRoll NoFirst [email\xa0protected]@[email\xa0protected]


Discussion

No Comment Found