What is Primary Key in SQL ?

DBMSMySQLRDBMSSQL

Primary Key is a constraint in SQL. So, before understanding what exactly is a primary key, let’s understand what exactly is a constraint in SQL. Constraints are the rules enforced on data columns on a table. These are used to limit the type of data that can go into a table. Constraints can either be column level or table level.

Let’s look at the different types of constraints which are present in SQL:

Constraint Description
NOT NULL Ensures that a column cannot have a NULL value.
DEFAULT Provides a default value for a column when none is specified.
UNIQUE Ensures that all the values in a column are different
PRIMARY Uniquely identifies each row/record in a database table
FOREIGN Uniquely identifies a row/record in any another database table
CHECK The CHECK constraint ensures that all values in a column satisfy certain    conditions.
INDEX Used to create and retrieve data from the database very quickly.

You can consider Primary Key constraint to be a combination of UNIQUE and NOT NULL constraint. This means that if a column is set as a primary key, then this particular column cannot have any null values present in it and also all the values present in this column must be unique.

     

Advertisements

ads