
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL ALTER column to remove primary key and auto_increment?
You can use ALTER command to remove primary key and auto_increment. The syntax is as follows −
ALTER TABLE yourTableName DROP PRIMARY KEY,change yourColumnName yourColumnName data type;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table removePrimaryKey -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentFirstName varchar(20), -> StudentLastName varchar(20), -> PRIMARY KEY(StudentId) -> ); Query OK, 0 rows affected (0.47 sec)
Check the description of table using DESC command. The syntax is as follows −
desc yourTableName;
Check the description of the table ‘removePrimaryKey’ using DESC. The query is as follows −
mysql> desc removePrimaryKey;
The following is the output −
+------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+-------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentFirstName | varchar(20) | YES | | NULL | | | StudentLastName | varchar(20) | YES | | NULL | | +------------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
Now look at the column Key and Extra above. In the Key column, there is PRI and in the Extra column there is auto_increment. You need to remove both of them, using the ALTER DROP command discussed in the beginning −
The query is as follows −
mysql> alter table removePrimaryKey DROP PRIMARY KEY,change StudentId StudentId int; Query OK, 0 rows affected (1.79 sec) Records − 0 Duplicates − 0 Warnings − 0
We have successfully removed primary key and auto increment above. Now you check the primary key and auto_increment have been removed from the table or not using the following query −
mysql> desc removePrimaryKey;
The following is the output −
+------------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+-------------+------+-----+---------+-------+ | StudentId | int(11) | YES | | NULL | | | StudentFirstName | varchar(20) | YES | | NULL | | | StudentLastName | varchar(20) | YES | | NULL | | +------------------+-------------+------+-----+---------+-------+ 3 rows in set (0.07 sec)
- Related Articles
- How to make MySQL table primary key auto increment?
- Two columns as primary key with auto-increment in MySQL?
- Remove Primary Key in MySQL?
- ALTER TABLE to add a composite primary key in MySQL?
- Set auto increment initial value for MySQL table using ALTER command
- How to insert data to MySQL having auto incremented primary key?
- Set the MySQL primary keys auto increment to be unlimited (or incredibly huge)?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- Set existing column as Primary Key in MySQL?
- How to handle fragmentation of auto increment ID column in MySQL?
- Can we remove a primary key from MySQL table?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- How to remove primary key from MongoDB?
