
- 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
Two columns as primary key with auto-increment in MySQL?
Achieve this using MyISAM Engine. Here is an example of two columns as primary key with auto-increment.
Creating a table with two columns as primary key −
mysql> create table TwoPrimaryKeyTableDemo -> ( -> Result ENUM('First','Second','Third','Fail') not null, -> StudentId int not null auto_increment, -> StudentName varchar(200) not null, -> Primary key(Result,StudentId) -> ) -> ENGINE=MyISAM; Query OK, 0 rows affected (0.20 sec)
Inserting records into table
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('John','Fail'); Query OK, 1 row affected (0.42 sec) mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result)values('Carol','First'); Query OK, 1 row affected (0.09 sec) mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Smith','Third'); Query OK, 1 row affected (0.05 sec) mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Johnson','Second'); Query OK, 1 row affected (0.03 sec) mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Johnson','Third'); Query OK, 1 row affected (0.06 sec) mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Carol','Second'); Query OK, 1 row affected (0.18 sec) mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Carol','Fail'); Query OK, 1 row affected (0.05 sec)
Now we can check the records with the help of select statement with order by clause. The query is as follows.
mysql> select *from TwoPrimaryKeyTableDemo order by StudentId,Result;
The following is the output −
+--------+-----------+-------------+ | Result | StudentId | StudentName | +--------+-----------+-------------+ | First | 1 | Carol | | Second | 1 | Johnson | | Third | 1 | Smith | | Fail | 1 | John | | Second | 2 | Carol | | Third | 2 | Johnson | | Fail | 2 | Carol | +--------+-----------+-------------+ 7 rows in set (0.00 sec)
- Related Articles
- How to make MySQL table primary key auto increment?
- Set existing column as Primary Key in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- Set the MySQL primary keys auto increment to be unlimited (or incredibly huge)?
- How to insert data to MySQL having auto incremented primary key?
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL
- Change the Auto Increment counter in MySQL?
- How to change auto increment number in MySQL?
- How to refer primary key as Foreign to various table in MySQL?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- Passing NULL to MySQL for auto increment?
- How can we set PRIMARY KEY on multiple columns of an existing MySQL table?
- How to create a table with auto-increment column in MySQL using JDBC?
- Is the primary key automatically indexed in MySQL?

Advertisements