如何使用多列创建主键&自动增加ID?

时间:2014-05-01 05:46:11

标签: mysql database mysql-workbench composite-primary-key

这是我的表结构

mysql> DESCRIBE sections;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| sec_id   | int(5)      | NO   | PRI | NULL    | auto_increment |
| sec_name | varchar(45) | YES  |     | NULL    |                |
| sec_type | tinyint(4)  | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

mysql> DESCRIBE subjects;
+-----------------+-------------+------+-----+---------+----------------+
| Field           | Type        | Null | Key | Default | Extra          |
+-----------------+-------------+------+-----+---------+----------------+
| sub_id          | int(5)      | NO   | PRI | NULL    | auto_increment |
| sub_name        | varchar(45) | YES  |     | NULL    |                |
| sections_sec_id | int(5)      | NO   | MUL | NULL    |                |
+-----------------+-------------+------+-----+---------+----------------+

mysql> DESCRIBE chapters;
+-----------------+-------------+------+-----+---------+----------------+
| Field           | Type        | Null | Key | Default | Extra          |
+-----------------+-------------+------+-----+---------+----------------+
| chp_id          | int(5)      | NO   | PRI | NULL    | auto_increment |
| chp_name        | varchar(45) | YES  |     | NULL    |                |
| subjects_sub_id | int(5)      | NO   | MUL | NULL    |                |
| sections_sec_id | int(5)      | NO   | MUL | NULL    |                |
+-----------------+-------------+------+-----+---------+----------------+

mysql> DESCRIBE questions;
+-----------------+-----------+------+-----+---------+----------------+
| Field           | Type      | Null | Key | Default | Extra          |
+-----------------+-----------+------+-----+---------+----------------+
| que_id          | int(11)   | NO   | PRI | NULL    | auto_increment |
| que_text        | text      | YES  |     | NULL    |                |
| que_created     | timestamp | YES  |     | NULL    |                |
| chapters_chp_id | int(5)    | NO   | MUL | NULL    |                |
| subjects_sub_id | int(5)    | NO   | MUL | NULL    |                |
| sections_sec_id | int(5)    | NO   | MUL | NULL    |                |
+-----------------+-----------+------+-----+---------+----------------+

mysql> DESCRIBE answers;
+------------------+-----------+------+-----+---------+----------------+
| Field            | Type      | Null | Key | Default | Extra          |
+------------------+-----------+------+-----+---------+----------------+
| ans_id           | int(11)   | NO   | PRI | NULL    | auto_increment |
| ans_text         | text      | YES  |     | NULL    |                |
| ans_created      | timestamp | YES  |     | NULL    |                |
| questions_que_id | int(11)   | NO   | MUL | NULL    |                |
| chapters_chp_id  | int(5)    | NO   | MUL | NULL    |                |
| subjects_sub_id  | int(5)    | NO   | MUL | NULL    |                |
| sections_sec_id  | int(5)    | NO   | MUL | NULL    |                |
+------------------+-----------+------+-----+---------+----------------+

我们假设'答案'表

我创建了' ans_id '作为'答案'表的主键,使用 <创建了另一个 UNIQUE 键EM>(ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id)

This how i created UNIQUE key using mysql workbench

我想做什么。

使用(ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id)列创建'Answers'表的主键,并在INSERT 之前创建触发器来创建'auto_increment'id。

最后我想完成这个

I want to do like this

1 个答案:

答案 0 :(得分:0)

定义:主键是最小确定表中每一行所需的一组属性

UNIQUE索引应该是{questions_que_id} + {chapters_chp_id} + {subjects_sub_id} + {sections_sec_id}并确保它与这4列合并为一个索引,如果你将它设置为不同的4个索引,那么你可能会实现不希望的结果,正如佐哈已经指出的那样。根据我的经验,使用像这样的复合键可能是不切实际的。对于基本表操作(CRUD),您必须提供许多参数。

也许尝试这种方式:自动增量数字id作为主键,其余重要属性作为UNIQUE索引。然后,您可以计算已插入的答案,以确保将正确的号码分配给ans_id。提示:从应用层实现这一点可能更容易,而不是作为MySQL的触发器或过程。如果您的用户将逐步提供答案,那么只需保存时间戳,您就会知道哪个答案较旧,所以您根本不需要ans_id。