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

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: 2020-06-30T09:33:04+05:30

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements