MySQL

How to Create a Primary Key in MySQL

In this tutorial, we are going to see how to create a primary key in MySQL. In MySQL, a primary key is a single field or a combination of fields that uniquely defines a record. None of the fields in the primary key can contain NULL value. In MySQL, a table can have only one primary key.
 

How to Create a Primary Key in MySQL

You can create a primary key in MySQL with the CREATE TABLE statement. The following example creates a table named “Users” and its primary key is the userID column:
 

CREATE TABLE Users (
    userID int AUTO_INCREMENT PRIMARY KEY,
    Name VARCHAR(20) NOT NULL,
    Age int,
    Address VARCHAR(100)
);

The following statement creates “Users” table which has the PRIMARY KEY constraint:

CREATE TABLE Users (
    userID int AUTO_INCREMENT,
    Name VARCHAR(20) NOT NULL,
    Age int,
    Address VARCHAR(100),
    PRIMARY KEY(userID)
);
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *