I'm new to MySQL, and I'm really confused about the different terms that I've encountered. I tried googling the answer but the results are really confusing and when I try and understand it just seems like they are the same thing.
What exactly are the differences among key, superkey, minimal superkey, candidate key and primary key?
Superkey - An attribute or set of attributes that uniquely defines a tuple within a relation. However, a superkey may contain additional attributes that are not necessary for unique identification.
Candidate key - A superkey such that no proper subset is a superkey within the relation. So, basically has two properties: Each candidate key uniquely identifies tuple in the relation ; & no proper subset of the composite key has the uniqueness property.
Composite key - When a candidate key consists of more than one attribute.
Primary key - The candidate key chosen to identify tuples uniquely within the relation.
Alternate key - Candidate key that is not a primary key.
Foreign key - An attribute or set of attributes within a relation that matches the candidate key of some relation.
SUPER KEY:
Attribute or set of attributes used to uniquely identify tuples in the database.
CANDIDATE KEY:
PRIMARY KEY:
one of the candidate key which is used to identify records in DB uniquely
not null
Primary key is a subset of super key. Which is uniquely define and other field are depend on it. In a table their can be just one primary key and rest sub set are candidate key or alternate keys.
Super Key : Super key is a set of one or more attributes whose values identify tuple in the relation uniquely.
Candidate Key : Candidate key can be defined as a minimal subset of super key. In some cases , candidate key can not alone since there is alone one attribute is the minimal subset. Example,
Employee(id, ssn, name, addrress)
Here Candidate key is (id, ssn) because we can easily identify the tuple using either id or ssn . Althrough, minimal subset of super key is either id or ssn. but both of them can be considered as candidate key.
Primary Key : Primary key is a one of the candidate key.
I have always found it difficult to remember all the keys; so I keep the below notes handy, hope they help someone! Let me know if it can be improved.
Key: An attribute or combination of attributes that uniquely identify an entity/record in a relational table.
PK: A single key that is unique and not-null. It is one of the candidate keys.
Foreign Key: FK is a key in one table (child) that uniquely identifies a row of another table (parent). A FK is not-unique in the child table. It is a candidate key in the parent table. Referential integrity is maintained as the value in FK is present as a value in PK in parent table else it is NULL.
Unique Key: A unique key that may or may not be NULL
Natural key: PK in OLTP. It may be a PK in OLAP.
Surrogate Key: It is the Surrogate PK in OLAP acting as the substitute of the PK in OLTP. Artificial key generated internally in OLAP.
Composite Key: PK made up of multiple attributes
SuperKey: A key that can be uniquely used to identify a database record, that may contain extra attributes that are not necessary to uniquely identify records.
Candidate Key: A candidate key can be uniquely used to identify a database record without any extraneous data. They are Not Null and unique. It is a minimal super-key.
Alternate Key: A candidate key that is not the primary key is called an alternate key.
Candidate Key/s with Extraneous data: Consider that can be used to identify a record in the Employee table but candidate key alone is sufficient for this task. So becomes the extraneous data.
Below I have tried to use set theory to simplify the representation of the membership of the keys w.r.t. each other.
I have summarized it below:
Notes: an-overview-of-the-database-keys-primary-key-composite-key-surrogate-key-et-al