Joe_B
Joe_B

Reputation: 219

How can I get two auto-increments in one database table?

I want the post_delete column to generate unique id's so that when I want to delete a comment for e.g. I can refer to the post_delete's value.

Here is my users TABLE

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
pic_location  VARCHAR(255) NOT NULL,
post_delete  INT(8) NOT NULL AUTO_INCREMENT, 
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);

and here is my posts TABLE

CREATE TABLE posts (  
post_id         INT(8) NOT NULL AUTO_INCREMENT,  
post_content        TEXT NOT NULL,  
post_date       DATETIME NOT NULL,  
post_topic      INT(8) NOT NULL,
post_by     INT(8) NOT NULL,  
PRIMARY KEY (post_id)  
);

and here is my delete query in php

$sql = "DELETE FROM posts WHERE post_by =" . $_SESSION['post_delete'];

It says when I include the table that there can only be one auto-increment. How can I change this so that the post_delete can generate unique id's?

Thanks!

Upvotes: 0

Views: 142

Answers (1)

pete the pagan-gerbil
pete the pagan-gerbil

Reputation: 3166

Could you use triggers to automatically fill your second auto-incrementing column? I'm not good with triggers, but it sounds like something they could do.

Upvotes: 1

Related Questions