contador Saltar al contenido

Difference between primary and secondary memory

Keys are the attribute or set of attributes that are used to access tuples from a table or are also used to construct a relationship between two tables. In this article, we will discuss the primary and candidate key and the differences between them. Both the primary key and the candidate key uniquely identify a tuple in a relationship or table. But the most important point that differentiates them is that there can be only one primary key in a relationship. However, there may be more than one candidate key in a relationship.

There are some more differences between the primary and candidate key that you will discuss with the help of the comparison table shown below.

Comparative graph

Basis for comparison Primary key Candidate candidate
BasicThere can only be one primary key in any relationship.There may be more than one candidate key in a relationship.
NULLNo attribute of a primary key can contain a NULL value.The attribute of a Candidate key can have a NULL value.
To specify optional specify a primary key for any relationship.There cannot be a relationship without the specified candidate key.
featureThe primary key describes the most important attribute for the relationship.Candidate keys have suitable candidates for the primary key.
The other way aroundA primary key is a candidate key.But it is not mandatory that every candidate key can be a primary key.

Definition of the primary key

The primary key an attribute or set of attributes that will uniquely identify each tuple in a relationship. There can only be one a primary key for each relationship. need to make sure a primary key not contains never a value NULL and that it should have value univocal for each tuple in the report. The values โ€‹โ€‹of the primary key attribute [s] must be static, that is the value of the attribute must never or rarely be modified.

One of Candidate Keys becomes qualified to become a primary key. The rules What a candidate key must qualify to become primary is that the value of the key should never be NULL and must be univocal for all the tuples.

If a relationship contains an attribute that a primary key of another relationship, then that attribute called foreign key .

It is advisable to calculate the primary key of a relationship before introducing other attributes of a relationship since the primary key uniquely identifies each tuple. It is preferable to choose a single attribute or a small number of attributes as a primary key that simplifies relationship management.

Now let's see an example of a primary key.

 Studente {ID, Nome, Cognome, Et, Indirizzo} 

Here we will find the candidate keys first. I found two candidate keys {ID} is {First_name, Last_name} since they will uniquely identify each student in the Student report. Now, here I will choose the ID as my primary key, because sometimes it can happen that two students have the same first and last name, so it will be easy to track down a student with his ID .

Definition of Candidate Key

A candidate key an attribute or set of attributes that uniquely define a tuple in a relationship. There is one more than one candidate key in a report. These Candidate keys are the candidates who can qualify to become a primary key.

Although each candidate key becomes a primary key, only one can be chosen as the primary key. The rules that a candidate key requires to become a primary key are that the value of the key attribute can never be NULL in any domain of the key, it must be univocal is static .

If all candidate keys are suitable for the primary key, a DBA expert must make the decision to understand the primary key. There can never be a keyless candidate relationship.

Let's try to understand the candidate's key with an example. If we add some other attributes to the Student report, I discussed above.

 Studente {ID, First_name, Cognome, Et, Indirizzo, DOB, Department_name} 

Here I can understand two candidate keys that are {ID}, {First_name, Last_name, DOB} . So you can understand that the candidate keys are one that uniquely identifies a tuple in a relationship.

Key differences between primary and candidate key

  1. The basic point that distinguishes the primary key from the candidate key that there can be only one primary for any relationship in a schema. However, there may be multiple candidate keys for a single relationship.
  2. The attribute under primary key can never contain a NULL value since the primary function of the primary key is to uniquely identify a related record. Even a primary key can be used as a foreign key in another relationship, and therefore it must not be NULL so that the reference relationship can find the tuples in a referenced relationship. The candidate key can be NULL unless the attribute constraint is specified null.
  3. optional specify a primary key, but a relationship cannot exist without candidate keys.
  4. The primary key describes the unique and most important attribute of a relationship, while the candidate keys provide the candidates among which one can be selected as the primary key.
  5. Each primary key is a candidate key, but vice versa is not true.

Conclusion:

optional for a relationship specify a primary key. On the other hand, if you are declaring a relationship, the candidate keys must be present in that relationship in order to build a good relationship.