Jump to content

[SOLVED] sync values of two mysql fields


evanct

Recommended Posts

i want to set it so this particular field in a table automatically takes the value of an auto_increment field. i can't set more than one auto_increment field in a table, and I tried linking the two fields via foreign key but then I get this error: "Cannot add or update a child row: a foreign key constraint fails"

 

how can I do this?

Link to comment
https://forums.phpfreaks.com/topic/170217-solved-sync-values-of-two-mysql-fields/
Share on other sites

I would post in the mySQL board... except that it moves at the speed of molasses on a november day

 

CREATE TABLE `posts` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`post_text` text NOT NULL,
`votes` int(11) DEFAULT NULL,
`parent` int(11) DEFAULT '0',
`children` int(11) DEFAULT '0',
`auth_ip` varchar(40) NOT NULL,
`auth_name` varchar(40) DEFAULT 'Anonymous',
`status` varchar(20) DEFAULT NULL,
`thread_parent` int(11) DEFAULT NULL,
PRIMARY KEY (`post_id`),
KEY `thread_parent` (`thread_parent`),
CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`thread_parent`) REFERENCES `posts` (`post_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

 

If it matters, there is a field in another table that's also linked to post_id.

foreign key is used to reference columns in other tables, not in the same.

do you want that 'thread_parent' be auto_increment too?

if yes, then you don't need this column, because it would have the same value as the 'post_id' column.

 

explain what do you want.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.