Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?

MySQLMySQLi Database

Yes, you can use below syntax. Following is the syntax −

PRIMARY KEY(yourColumnName1,yourColumnName2);

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> StudentFirstName varchar(100),
   -> StudentLastName varchar(100),
   -> StudentAge int,
   -> StudentCountryName varchar(100),
   -> PRIMARY KEY(StudentFirstName,StudentLastName)
   -> );
Query OK, 0 rows affected (0.74 sec)

Let us check the description of the table −

mysql> desc DemoTable;

This will produce the following output −

+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| StudentFirstName   | varchar(100) | NO   | PRI | NULL    |       |
| StudentLastName    | varchar(100) | NO   | PRI | NULL    |       |
| StudentAge         | int(11)      | YES  |     | NULL    |       |
| StudentCountryName | varchar(100) | YES  |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 14:19:37
Advertisements