MyISAM 엔진을 사용하여 이를 달성하십시오. 다음은 자동 증분을 사용하는 기본 키로 두 열의 예입니다.
기본 키로 두 개의 열이 있는 테이블 만들기 -
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) 테이블에 레코드 삽입
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) 이제 order by 절이 있는 select 문을 사용하여 레코드를 확인할 수 있습니다. 쿼리는 다음과 같습니다.
mysql> select *from TwoPrimaryKeyTableDemo order by StudentId,Result;
다음은 출력입니다 -
+--------+-----------+-------------+ | 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)