使用MyISAM Engine实现此目的。这是两列作为具有自动增量的主键的示例。
创建一个以两列为主键的表-
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;将记录插入表
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('John','Fail');
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result)values('Carol','First');
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Smith','Third');
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Johnson','Second');
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Johnson','Third');
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Carol','Second');
mysql> insert into TwoPrimaryKeyTableDemo(StudentName,Result) values('Carol','Fail');现在,我们可以在带有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)