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


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;

Output

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)

Updated on: 30-Jun-2020

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements