What is the difference between primary
, unique
and foreign key constraints
, and indexes
?
I work on Oracle 10g<
Here are some reference for you:
Primary & foreign key Constraint.
Primary Key: A primary key is a field or combination of fields that uniquely identify a record in a table, so that an individual record can be located without confusion.
Foreign Key: A foreign key (sometimes called a referencing key) is a key used to link two tables together. Typically you take the primary key field from one table and insert it into the other table where it becomes a foreign key (it remains a primary key in the original table).
Index, on the other hand, is an attribute that you can apply on some columns so that the data retrieval done on those columns can be speed up.
Primary Key and Unique Key are Entity integrity constraints
Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist and no null values are entered.
Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values. (In oracle one null is not equal to another null).
see Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?
Key/index : A key is an aspect of a LOGICAL database design, an index is an aspect of a PHYSICAL database design. A key corresponds to an integrity constraint, an index is a technique of physically recording values that can be usefully applied when enforcing those constraints.
Primary/foreign : A "primary" key is a set of attributes whose values must form a combination that is unique in the entire table. There can be more than one such set (> 1 key), and the word "primary" is a remnant from the earlier days when the designer was then forced to choose one of those multiple keys as being "the most important/relevant one". The reason for this was primarily in combination with foreign keys :
Like a "primary" key, a "foreign" key is also a set of attributes. The values of these attributes must form a combination that is an existing primary key value in the referenced table. I don't know exactly how strict this rule still applies in SQL today. The terminology has remained anyway.
Unique : keyword used to indicate that an index cannot accept duplicate entries. Unique indexes are obviously an excellent means to enforce primary keys. To the extent that the word 'unique' is used in contexts of LOGICAL design, it is superfluous, sloppy, unnecessary and confusing. Keys (primary keys, that is) are unique by definition.
1)A primary key
is a set of one or more attributes that uniquely identifies tuple within relation.
2)A foreign key
is a set of attributes from a relation scheme which can be uniquely identify tuples fron another relation scheme.
Primary Key: identify uniquely every row it can not be null. it can not be a duplicate.
Foreign Key: create relationship between two tables. can be null. can be a duplicate