SQL Primary Key

A question is always there when you face an Interview related to Software Developer, Web developer or any profile related Database and Web or Mobile applications. This Section Help you in answering Database Interview Questions, MySQL Interview Questions and Primary Key Interview Questions.

What is a Primary Key?

So answers is :

A Primary keys provide a unique Identification for every record in a database table.  

A table can have only one primary key, which could be either a single column or made up of multiple columns. 

A SQL Primary key could be a single column or set of few columns to create a unique combination. 

When multiple columns are used as a primary key, it is known as a composite primary key.

A Primary Key column cannot have NULL values.

A Primary Key is always unique.

The SQL PRIMARY KEY is also known as Primary Key Constraint.

What are the main features of SQL Primary Key?

  • SQL Primary key implements the integrity in the database table.
  • SQL Primary key is the lifeline of the Relational Database. 
  • SQL Primary key always has unique data.
  • Maximum length of a SQL Primary Key could be 900 bytes.
  • The SQL Primary key can combined maximum of 16 columns at a time

How to create a Primary Key or How to Define a Primary Key Constraint?

1#Create a Primary Key while Creating a New Table

We can define a Primary Key Constraint while creating a Table. 

The syntax for creating a table with Primary key is as follow:

CREATE TABLE Students (

    ID int NOT NULL,

    FirstName varchar(255) NOT NULL,

    LastName varchar(255),

    EmailID varchar(100),

    PRIMARY KEY (ID)

);

2# Using Alter Table 

If Primary Key is not created at the time of Table creation we define Primary Key Using ALTER Command in SQL.

Syntax to create a Primary Key using Alter Command is :

ALTER TABLE Students ADD PRIMARY KEY (ID);

How to Delete or Drop a Primary Key?

Once a SQL Primary Key is created we can drop it or delete it if required. Here is the SQL command of Dropping a Primary Key:

ALTER TABLE Students DROP PRIMARY KEY ;

 

Spread the love
  • 1
    Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.