
- 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
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)
- Related Articles
- Can we remove a primary key from MySQL table?
- What do you mean by PRIMARY KEY and how can we use it in MySQL table?
- How to make MySQL table primary key auto increment?
- CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
- How can we say that in MySQL, AUTO_INCREMENT is taking precedence over PRIMARY KEY?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL
- How can we set PRIMARY KEY on multiple columns of an existing MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How to get the equivalent for SELECT column1, column2 FROM tbl in MongoDB Database?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How to make a primary key start from 1000?
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?

Advertisements