
- 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
How do I drop a primary key in MySQL?
To drop a primary key, use ALTER at first to alter the table. With that, use DROP to drop the key as in the below
Syntax
alter table yourTableName drop primary key;
Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL, -> StudentName varchar(20), -> StudentAge int, -> primary key(StudentId) -> ); Query OK, 0 rows affected (0.48 sec)
Here is the query to check the description of table −
mysql> desc DemoTable;
This will produce the following output−
+-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | StudentId | int(11) | NO | PRI | NULL | | | StudentName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
Following is the query to drop a primary key in MySQL −
mysql> alter table DemoTable drop primary key; Query OK, 0 rows affected (1.70 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the table description once again −
mysql> desc DemoTable;
This will produce the following output −
+-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | StudentId | int(11) | NO | | NULL | | | StudentName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How do you get whether a column is a primary key in MySQL?
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL
- How to get primary key of a table in MySQL?
- What happens if I will try to drop PRIMARY KEY constraint from the AUTO_INCREMENT column?
- How to reset the primary key of a table in mysql?
- How to change a primary key in MySQL to auto_increment?\n
- What do you mean by PRIMARY KEY and how can we use it in MySQL table?
- How to make MySQL table primary key auto increment?
- Set existing column as Primary Key in MySQL?
- Is the primary key automatically indexed in MySQL?
- ALTER TABLE to add a composite primary key in MySQL?
- How to identify composite primary key in any MySQL database table?
- Can we remove a primary key from MySQL table?

Advertisements