
- 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
Can we remove a primary key from MySQL table?
Yes, we can remove a primary key using the DROP in MySQL. Following is the syntax −
alter table yourTableName drop primary key;
Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL PRIMARY KEY ); Query OK, 0 rows affected (0.58 sec)
Following is the query to check the description of table −
mysql> desc DemoTable;
This will produce the following output displaying the Primary Key −
+--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | UserId | int(11) | NO | PRI | NULL | | +--------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec)
Following is the query to remove primary key from table −
mysql> alter table DemoTable drop primary key; Query OK, 0 rows affected (1.51 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of the table once again −
mysql> desc DemoTable;
This will produce the following output. Now the primary key isn’t visible −
+--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | UserId | int(11) | NO | | NULL | | +--------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec)
- Related Articles
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
- Remove Primary Key in MySQL?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we remove a column from MySQL table?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How can we set PRIMARY KEY on multiple columns of an existing MySQL table?
- Remove unique key from MySQL table?
- How to remove primary key from MongoDB?
- What do you mean by PRIMARY KEY and how can we use it in MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?
- MySQL ALTER column to remove primary key and auto_increment?
- ALTER TABLE to add a composite primary key in MySQL?

Advertisements