如何在 MySQL table 中有一个自动递增列而不将其设为主键?

How to have an auto increment column in MySQL table without making it a primary key?

这是我的 SQL 创建 2 tables 与各自的主键和外键。问题是我想在我的 child table 中有一个 auto-increment 列,但我想将其他一些列作为主键。我该如何解决这个问题?

CREATE TABLE patient (
_id INT(5) UNSIGNED AUTO_INCREMENT NOT NULL,
pn VARCHAR(11) DEFAULT NULL,
first VARCHAR(15) DEFAULT NULL,
last VARCHAR(25) DEFAULT NULL,
dob DATE DEFAULT NULL,
PRIMARY KEY (_id)
);

CREATE TABLE insurance (
_id INT(5) UNSIGNED AUTO_INCREMENT,
patient_id INT(5) UNSIGNED NOT NULL,
iname VARCHAR(40) DEFAULT NULL,
from_date DATE DEFAULT NULL,
to_date DATE DEFAULT NULL,
PRIMARY KEY (patient_id),
CONSTRAINT fk_insurance FOREIGN KEY (patient_id)
REFERENCES patient(_id)
);

它给了我 ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key 但我不知道如何解决这个问题。我是这方面的初学者,我需要帮助谢谢。

考虑:

CREATE TABLE insurance (
    _id INT(5) UNSIGNED AUTO_INCREMENT,
    patient_id INT(5) UNSIGNED NOT NULL,
    iname VARCHAR(40) DEFAULT NULL,
    from_date DATE DEFAULT NULL,
    to_date DATE DEFAULT NULL,
    PRIMARY KEY (patient_id),
    KEY(_id),
    CONSTRAINT fk_insurance FOREIGN KEY (patient_id)
    REFERENCES patient(_id)
);

即自增列至少要有一个key。如果愿意,您仍然可以使用另一列作为主键。