What Are Primary Key And How It Is Different From Other?

In this tutorial, we will learn What are Primary Key and how it is different from other keys where the primary key uniquely identifies each row in the table as it stores only a unique value for each record.

Primary Key

Primary Key

As we know to identify someone around tons of things we need a unique identity of that so that we can find it directly and there will not be any duplicates of it.

Moreover, it can not be without any information which means it can not be null as the primary key is used for identifying the record independently so that when we compare two tables in the relational database we can have the key so that we can refer to the table data easily.

We need to declare the column as primary at the time of creation y using the keyword Primary key although we can also declare any column as primary at any point in time there can be only one column that could be the primary key and it can not store NULL value to identify uniquely.

This key is used when we establish the relation between two or more tables where to match with the data of the same person in both the table the value of the primary key would be the same.

To declare any column as primary follow the code given below.

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(52) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    hire_date DATE NOT NULL,
    salary DECIMAL(10,2) NOT NULL
);

Here we can see I have declared id as the primary key and it is also declared as an auto- increment which means it will be automatically taken as soon as we enter a new record in the table.

 

To learn more about a primary key visit:  Primary Key

To learn more about MySQL Visit MySQL Problems And Tutorials and to learn more about Java and other programming languages visitProgramming Tutorials

Leave a Comment

%d bloggers like this: