开发者

PHP/MySQL database question?

I have a question I'm trying to have the q_id column take the value of the id column when a tag is submitted I have been trying to do this using PHP but I was thinking should I just add AUTO INCREMENT to the q_id column since but tables are updated when the tag is submited. or is this the wrong way to do it or is there a better way to do this?

开发者_如何学Python

Here is the MySQL tables below.

CREATE TABLE q_tags (
q_id INT UNSIGNED NOT NULL,
users_q_id INT UNSIGNED NOT NULL
);

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);  


AUTO INCREMENT is used mainly to avoid adding the same ID into a table, so that you can keep a loyal index. I'd suggest you add a primary key to the q_tags, and change the current q_id to a foreign key, so that it can become a reference to tags table.

EDIT :

CREATE TABLE q_tags (
q_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
t_id INT UNSIGNED NOT NULL,
users_q_id INT UNSIGNED NOT NULL,
PRIMARY KEY (q_id)
);

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

t_id in the first table is the "foreign key". You can add it as a real mysql foreign key or just keep it this way (if you're planning on be able to "cascade delete" tags and t_tags related, it would matter)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜