
- 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 a MySQL table?
Actually, MySQL allows us to set PRIMARY KEY on multiple columns. The advantage of doing this is that we can work on multiple columns as a single entity.
Example
We have created the table allotment by defining composite PRIMARY KEY on multiple columns as follows −
mysql> Create table allotment( RollNo Int, Name Varchar(20), RoomNo Int, PRIMARY KEY(RollNo, RoomNo)); Query OK, 0 rows affected (0.23 sec) mysql> Describe allotment; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | RollNo | int(11) | NO | PRI | 0 | | | Name | varchar(20) | YES | | NULL | | | RoomNo | int(11) | NO | PRI | 0 | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.10 sec)
Now, when we will try to insert the duplicate composite values in both the columns then MySQL throws an error as follows −
mysql> Insert Into allotment values(1, 'Aarav', 201),(2,'Harshit',201),(1,'Rahul ',201); ERROR 1062 (23000): Duplicate entry '1-201' for key 'PRIMARY'
- Related Articles
- How can we set PRIMARY KEY on multiple columns of an existing MySQL table?
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
- Can we remove a primary key from MySQL table?
- How can we assign FOREIGN KEY constraint on multiple columns?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How to get primary key of a table in MySQL?
- What do you mean by PRIMARY KEY and how can we use it in MySQL table?
- How can we have multiple virtuals GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- How to reset the primary key of a table in mysql?
- How can we list all the columns of a MySQL view as we can list the columns of a MySQL table?
- How can we add multiple columns, with single command, to an existing MySQL table?
- How to make MySQL table primary key auto increment?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?

Advertisements