
- 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 can we set PRIMARY KEY on multiple columns of an existing MySQL table?
We can set PRIMARY KEY constraint on multiple columns of an existing table by using ADD keyword along with ALTER TABLE statement.
Example
Suppose we have a table ‘Room_allotment’ as follows −
mysql> Create table Room_allotment(Id Int, Name Varchar(20), RoomNo Int); Query OK, 0 rows affected (0.20 sec) mysql> Describe Room_allotment; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | varchar(20) | YES | | NULL | | | RoomNo | int(11) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.11 sec)
Now we can add composite PRIMARY KEY on multiple columns, ‘id’ and ‘Name’, with the following query
mysql> Alter Table Room_allotment ADD PRIMARY KEY(Id, Name); Query OK, 0 rows affected (0.29 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> Describe Room_allotment; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | Id | int(11) | NO | PRI | 0 | | | Name | varchar(20) | NO | PRI | | | | RoomNo | int(11) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.12 sec)
It can be observed from the above result set that PRIMARY KEY has been added on multiple columns.
- Related Articles
- 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?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How can we add multiple columns, with single command, to an existing MySQL table?
- Create Primary Key on an existing table in PostgreSQL?
- How can we get a list of columns in an existing MySQL table?
- Set existing column as Primary Key in MySQL?
- Can we remove a primary key from MySQL table?
- How can we add columns with default values to an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we assign FOREIGN KEY constraint on multiple columns?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can we get more details about columns of an existing table than return by MySQL SHOW COLUMNS statement?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?

Advertisements