I think I may have wrongly created a constraint. I have three tables: Activity, Authentication, Login. I wanted Authentication to be the "primary" table, where I would insert data to create a user, and his details. It would have a one-one relation (id
in Login to id
in Authentication) with the newly created table, Authentication which stores session ids. The third table would have a one-many relation with multiple rows for AuthenticationID
which corresponds to id
of Login.
This is what I've created:
| Login | CREATE TABLE `Login` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`TimeLoggedIn` text NOT NULL,
`sessionid` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `id_2` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 |
| Authentication | CREATE TABLE `Authentication` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`userid` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
`role` varchar(20) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`AuthenticationID` int(6) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `Authentication_ibfk_1` FOREIGN KEY (`id`) REFERENCES `Login` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1 |
| Activity | CREATE TABLE `Activity` (
`num` int(11) NOT NULL AUTO_INCREMENT,
`AuthenticationID` int(6) unsigned NOT NULL,
`TorrentMag` mediumtext NOT NULL,
PRIMARY KEY (`num`),
KEY `FK_myKey2` (`AuthenticationID`),
CONSTRAINT `FK_myKey` FOREIGN KEY (`AuthenticationID`) REFERENCES `Authentication` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_myKey2` FOREIGN KEY (`AuthenticationID`) REFERENCES `Authentication` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=latin1 |
Unfortunately, when I tried to insert a new row into Authentication (which used to work till I created the constraint),
INSERT INTO Authentication (userid, password, role, email) VALUES ("user", "SeG^SU;B2_&Uhw", "user", "someone@mydomain.com");
it gave the error:
Cannot add or update a child row: a foreign key constraint fails (`episodescopy`.`Authentication`, CONSTRAINT `Authentication_ibfk_1` FOREIGN KEY (`id`) REFERENCES `Login` (`id`))
So I've inadvertently created an inverse relation of what I needed? Also I seem to have created a duplicate constraint on table Activity? How can I fix this?