Primary key in MS SQL Server
A table has a particular number of columns and each column has n number of rows. At times, there might be a probability of repeated rows in a column. For example, a column named identification number has repeated rows. To avoid the duplication of rows, the concept of a key has been introduced.
A key is an attribute or a set of attributes that can be uniquely identifiable. There are different keys like Primary key, Foreign key, Super key, and Candidate key. Primary key is used to identify the unique row from a single column or a set of columns.
Basic Syntax –
create table table name( pk_column datatype primary key .... );
create table student rollnumber number Primary key name varchar2(50) course varchar2(30)
The table is created and the values are inserted –
In the above table, Roll Number 116 is repeated twice. It violates the primary key rule. Hence, gives an error.
The primary key plays a pivotal role in database manipulation. It is impossible to imagine a database without a primary key. A database without a primary key is tedious work. Many problems might arise when a primary key is not included in a table.
A table can have only a single primary key.
NOT NULL constraint must be included along with the columns that have a primary key. In the case of MS SQL SERVER, NOT NULL is automatically created. When the primary key is added to a column, a unique clustered index is created automatically by SQL Server.
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.