Plz tell me with example the difference between Primary key,foreign key,unique key
Plz tell me with example the difference between Primary key,foreign key,unique key
Unique Key prohibits multiple rows from having the same value in the same column or combination of columns . But it allows null values.
Primary key is a combination of UNIQUE KEY and NOT NULL. It will not allow duplicate values and null values.
Foreign key ensures conformity between two tables. If a column is defined as foreign key it can contain only values from a parent table's primary key or a unique key.
For examples follow the link
Primary key---------enforces both uniqueness and not null(no duplicate and no null in the column)
foreign key----------maintains referential integrity(child record refers to parent record)
unique key----------maintains uniqueness of data in the column(no duplicate records)
primary key not accept null value & it is related with unique table while foreign
key is related with more than one table with one simillar coloumn.
just to add here unique key can contain null value, while primary key can not contain null value.
primary key is combination of both unique and notnull keys.
unique key is a key which is not accept any duplication.
foriegn key is a key which is act as a primary key to some other table.
Set of fields in database taken collectively to identify any row uniquely is called Candidate key...
So Database has more than 1 Candidate key...
Primary key is the Candidate key choosen by the Database Designer...
Unique Key is the constraint that is used to insert unique value in the field of the database...