MariaDB Add Auto Increment Column

This MariaDB tutorial will discuss the MariaDB Add Auto Increment Column. We will discuss and draw lessons from several cases to help you comprehend the subject better. The whole list of subjects we’ll discuss is provided below.

  • MariaDB Add Auto Increment Column
  • Add Column in MariaDB With Auto Increment
  • How to add Auto Increment Column in Existing Table in MariaDB
  • MariaDB Add Auto Increment Column As Primary Key

Also, check the latest MariaDB tutorial: MariaDB Alter Table Add Foreign Key

MariaDB Add Auto Increment Column

Here we will learn and understand how to use the MariaDB ALTER TABLE statement to the add auto_increment column on the table by the query, which will be explained with the help of syntax and an example.

In MariaDB, the ALTER TABLE statement is used to add, modify and drop/ delete the column from the table. It is also used to rename the column of the table. Normally, the user uses the AUTO_INCREMENT keywords in the PRIMARY KEY column of the table.

Here is the syntax to the add auto_increment column in the table by the following query:

SYNTAX:

ALTER TABLE TABLE_NAME 
MODIFY COLUMN_NAME DATATYPE_DEFINITION AUTO_INCREMENT;

EXAMPLE:

ALTER TABLE STATES_OF_USA
MODIFY STATE_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT; 

DESC STATES_OF_USA;

As we see in the above query, we have used the ALTER TABLE statement to modify the STATE_ID column and add the AUTO_INCREMENT keyword in the STATES_OF_USA table. To check modification has been done in the STATE_ID column of the STATES_OF_USA table by using the DESC statement.

MariaDB add auto increment column example
Example of MariaDB Add Auto Increment Column

We hope that you have understood the subtopic “MariaDB Add Auto Increment Column” by using the MariaDB ALTER TABLE statement on the table by the query. For a better explanation, we have used an example and explained it in depth.

Read: MariaDB Str_To_Date

Add Column in MariaDB With Auto Increment

We will learn and understand how to use the MariaDB ALTER TABLE statement to add the Auto Increment column in the table by the query, which will be explained with the help of an illustrated example.

EXAMPLE:

ALTER TABLE HARVARD_UNIVERSITY MODIFY COLUMN STUDENT_ID INT AUTO_INCREMENT 
PRIMARY KEY;

DESC HARVARD_UNIVERSITY;

In the aforementioned query, the ALTER TABLE statement is used to add the AUTO_INCREMENT keyword to the STUDENT_ID column in the HARVARD_UNIVERSITY table. If we want to check a new modification done to the STUDENT_ID column in the HARVARD_UNIVERSITY table by using the DESC statement for the description of the table.

Add column in MariaDB with auto increment example
Example of Add Column in MariaDB With Auto Increment

We hope that you have understood the subtopic “Add Column in MariaDB With Auto Increment” by using the MariaDB ALTER TABLE statement on the table by the query. We have used an example and demonstrated it in depth, for a better explanation.

Read: MariaDB Unique Key [Useful Guide]

How to add Auto Increment Column in Existing Table in MariaDB

Here we will learn and understand how to add an auto-increment column in the existing table by the following query:

EXAMPLE:

ALTER TABLE JOHNS_HOPKINS_HOSPITAL MODIFY COLUMN PATIENT_ID INT AUTO_INCREMENT 
PRIMARY KEY;

DESC JOHNS_HOPKINS_HOSPITAL;

As we see in the above query, we have used the ALTER TABLE statement to add the AUTO_INCREMENT keyword and modify the PATIENT_ID column of the JOHNS_HOPKINS_HOSIPTAL table. Then we used the DESC statement for new modifications done in the PATIENT_ID column of the JOHNS_HOPKINS_HOSPITAL table.

Using the MariaDB ALTER TABLE statement on the table returned by the query, we hope you have grasped the subtopic “How to Add Auto Increment Column in Existing Table in MariaDB.” We have used an example and gone into great detail to provide a better explanation.

Read: MariaDB Difference Between Two Dates

MariaDB Add Auto Increment Column As Primary Key

We will learn and comprehend how to utilize the MariaDB ALTER TABLE statement in this subtopic on MariaDB to add the AUTO_INCREMENT keyword to the PRIMARY KEY column of the table by the query and that will be clarified with the aid of an example.

EXAMPLE:

ALTER TABLE MIT_STUDENTS MODIFY COLUMN ID INT AUTO_INCREMENT 
PRIMARY KEY;

DESC MIT_STUDENTS;

In the aforementioned query, we have used the ALTER TABLE statement to modify and add the AUTO_INCREMENT keyword in the ID column which is the PRIMARY KEY column of the MIT_STUDENTS table. If we want to see new modifications done in the MIT_STUDENTS table for the ID column with the AUTO_INCREMENT keyword then we will use the DESC statement.

We hope that you have understood the subtopic “MariaDB Add Auto Increment Column As Primary Key” by using the MariaDB ALTER TABLE statement on the table by the query. For a better explanation, we have used an example and clarified it in depth.

You may also like to read the following MariaDB tutorials.

After reading this lesson, use MariaDB Add Auto Increment Columns. To assist you to understand the concept, we also covered a few examples. Here is a list of every subject we have discussed.

  • MariaDB Add Auto Increment Column
  • Add Column in MariaDB With Auto Increment
  • How to add Auto Increment Column in Existing Table in MariaDB
  • MariaDB Add Auto Increment Column As Primary Key