Adding multiple foreign key constraints - mysql

I am having trouble adding multiple foreign key constraints to this table. I get an error saying
Foreign key constraint is incorrectly formed
Both id's referenced are primary keys in their respective tables. What am I doing wrong?
CREATE TABLE `works_on` (
`eid` int(11) DEFAULT NULL,
`pid` int(11) DEFAULT NULL,
`start_date` date NOT NULL,
PRIMARY KEY (`eid`, `pid`),
CONSTRAINT `works_on_ibfk_1` FOREIGN KEY (`eid`)
REFERENCES `employee` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `works_on_ibfk_2` FOREIGN KEY (`pid`)
REFERENCES `project` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB

You have a paradox here - you cannot have a primary key where any of the elements has a null values AND therefore you cannot on delete SET NULL..
You should also be seeing an error message telling you that the primary key is invalid..

Related

importing database using xamp error for table that have multi foreign key

Error in foreign key constraint of table
blood_bank.blood_contact:
Create table blood_bank.blood_contact with foreign key constraint
failed. Referenced table blood_bank.contact not found in the data
dictionary near ' FOREIGN KEY (contact_fk) REFERENCES contact
(contact_id) ON UPDATE CASCADE, FOREIGN KEY (blood_fk)
REFERENCES blood_group (blood_id) ON UPDATE CASCADE )
Here's the table structure:
CREATE TABLE `blood_contact` (
`blood_contact_id` int(100) NOT NULL AUTO_INCREMENT,
`contact_fk` int(100) DEFAULT NULL,
`blood_fk` int(100) DEFAULT NULL,
PRIMARY KEY (`blood_contact_id`),
KEY `contact_fk` (`contact_fk`),
KEY `blood_fk` (`blood_fk`),
CONSTRAINT `blood_contact_ibfk_1` FOREIGN KEY (`contact_fk`) REFERENCES `contact` (`contact_id`) ON UPDATE CASCADE,
FOREIGN KEY (`blood_fk`) REFERENCES `blood_group` (`blood_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

composite primary key as foreign key deleting one set delets all

I have two tables one of them has composite primary key and the other has foreign keys to it. They both are set to cascade on delete. The problem is when I delete lets say a composite key set "name: John date: 02.02.2018" from the main table all the John rows are deleted from the table with the foreign keys, but there can be a set "name: John date: 04.04.2018" and also all rows where date is 02.02.2018 are also deleted how can I make it delete rows where only the set of foreign key is matched?
Update:
CREATE TABLE messages (
session_date date NOT NULL,
chat int(11) NOT NULL,
message longtext NOT NULL,
date time NOT NULL,
receiver int(11) NOT NULL,
PRIMARY KEY (date,receiver),
KEY FK_messages_sessions (session_date,chat),
KEY FK_messages_sessions_2 (chat,session_date),
CONSTRAINT FK_messages_sessions
FOREIGN KEY (session_date)
REFERENCES sessions (session_date)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FK_messages_sessions_2
FOREIGN KEY (chat)
REFERENCES sessions (chat)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
You have defined two foreign keys instead of a composite foreign key.
Try:
CREATE TABLE `messages` (
`session_date` date NOT NULL,
`chat` int(11) NOT NULL,
`message` longtext NOT NULL,
`date` time NOT NULL,
`receiver` int(11) NOT NULL,
PRIMARY KEY (`date`,`receiver`),
KEY `FK_messages_sessions` (`session_date`,`chat`),
CONSTRAINT `FK_messages_sessions`
FOREIGN KEY (`session_date`, `chat`)
REFERENCES `sessions` (`session_date`, `chat`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
(session_date,chat) could also be (chat,session_date) depending on the order of these columns in the primary key defined in the referenced table.

Foreign Key Constraints Errors

I keep getting a ERROR1215 when I create my table. I have narrowed the problem down to two attributes "score" and "criteriaNum". I cannot figure out why its causing problems though.
CREATE TABLE IF NOT EXISTS `sys`.`rubric` (
`programName` VARCHAR(100) NOT NULL,
`criteriaNum` INT UNSIGNED NOT NULL,
`score` INT UNSIGNED NOT NULL,
`criteria` TINYTEXT NOT NULL,
`description` TINYTEXT NOT NULL,
PRIMARY KEY (`programName`, `criteriaNum`,`score`),
CONSTRAINT `rubric_programName`
FOREIGN KEY (`programName`)
REFERENCES `sys`.`degree_program` (`programName`)
ON DELETE CASCADE
ON UPDATE CASCADE)
COMMENT 'Each entry in the rubric table corresponds to a criteria,score pair.';
CREATE TABLE IF NOT EXISTS`sys`.`evaluator_scores` (
`evaluatorName` VARCHAR(100) NOT NULL,
`aid` INT NOT NULL,
`programName` VARCHAR(100) NOT NULL,
`criteriaNum` INT UNSIGNED NOT NULL,
`score` INT UNSIGNED NOT NULL,
PRIMARY KEY (`aid`, `evaluatorName`, `programName`, `criteriaNum`, `score`),
CONSTRAINT `score_aid`
FOREIGN KEY (`aid`)
REFERENCES `sys`.`application` (`aid`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `score_evalName`
FOREIGN KEY (`evaluatorName`)
REFERENCES `sys`.`evaluations` (`evaluatorName`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `score_programName`
FOREIGN KEY (`programName`)
REFERENCES `sys`.`rubric` (`programName`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `score_criteriaNum`
FOREIGN KEY (`criteriaNum`)
REFERENCES `sys`.`rubric` (`criteriaNum`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `score_score`
FOREIGN KEY (`score`)
REFERENCES `sys`.`rubric` (`score`)
ON DELETE CASCADE
ON UPDATE CASCADE);
If I remove the score and criteriaNum constraints, it builds evaluator_scores. Otherwise though I get errors. Can anyone find the problem?
The concept of foreign keys is that the parent table must have a corresponding record in order to include that key in the child. In your parent table, the primary key is (programName, criteriaNum, score). Yet in your child table, you are trying to create three foreign key constraints to the rubric table, one for each individual column.
If you combine those three constraints into one and match the primary key columns, it ought to work.

Foreign key Integrity constraint violation: 1452

I've recently started trying to use foreign keys to make database management easier on myself. I'm having a terrible time trying to figure out how they actually work, and most of the time I can get it working between tables without issue. But I'm currently having an issue with 2 of my tables, and I can't figure it out.
I'm getting an error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(REDACTED.rc_logs, CONSTRAINT rc_logs_ibfk_1 FOREIGN
KEY (user_id) REFERENCES rc_teammates (uid) ON DELETE CASCADE ON
UPDATE CASCADE)
[/home5/redacted/public_html/redacted/rc/public/assets/php/connection.php:25]
but my tables seem to be set up properly, and I'm really confused about why it's not working. Here is my table structures:
rc_teammates
CREATE TABLE `rc_teammates` (
`uid` int(11) NOT NULL,
`name` text NOT NULL,
`primary_line` int(11) NOT NULL,
`hireStatus` text NOT NULL,
`created_on` date NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `rc_teammates`
ADD PRIMARY KEY (`uid`), ADD UNIQUE KEY `uid` (`uid`), ADD KEY `primary_line` (`primary_line`), ADD KEY `primary_line_2` (`primary_line`);
ALTER TABLE `rc_teammates`
MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `rc_teammates`
ADD CONSTRAINT `rc_teammates_ibfk_1` FOREIGN KEY (`primary_line`) REFERENCES `rc_lines` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE;
rc_logs
CREATE TABLE IF NOT EXISTS `rc_logs` (
`uid` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`line` int(11) NOT NULL,
`date` date NOT NULL,
`type` varchar(15) NOT NULL,
`timein` time NOT NULL,
`timeout` time NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=latin1;
ALTER TABLE `rc_logs`
ADD PRIMARY KEY (`uid`), ADD KEY `user_id` (`user_id`), ADD KEY `line` (`line`), ADD KEY `user_id_2` (`user_id`);
ALTER TABLE `rc_logs`
MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=53;
ALTER TABLE `rc_logs`
ADD CONSTRAINT `rc_logs_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `rc_teammates` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `rc_logs_ibfk_2` FOREIGN KEY (`line`) REFERENCES `rc_lines` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE;
I've tried to look up the error, and I've had this issue before but I do not remember how I solved it. What's worse is, this was working earlier, until I emptied the rc_teammates table to start fresh.
I really cannot figure this out, and would love any pointers. Thanks!
As you said you have "emptied" (TRUNCATE?) the table rc_teammates.
And you try to insert a record in rc_logs, and this record has a user_id that doesn't exists in rc_teammates, thus violation of the following constraint:
ADD CONSTRAINT `rc_logs_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `rc_teammates` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
Just add a record in rc_teammates, having a uid equal to the user_id of the record you are trying to insert in rc_logs, and retry.
Also, about this :
ALTER TABLE `rc_teammates`
ADD PRIMARY KEY (`uid`), ADD UNIQUE KEY `uid` (`uid`),
ALTER TABLE `rc_teammates`
MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT;
When you set a column as PRIMARY KEY, it is de facto : UNIQUE, NOT NULL and INDEXED. You don't need to specify all this, PRIMARY KEY is enough. This is valid for your other table as well.

mysql - not able to add foreign key constraint after changing column name

I was changing column name of a column having foreign key constraint, i have done this by
ALTER TABLE `city_details` DROP FOREIGN KEY `tb_city_details_ibfk_1`;
ALTER TABLE `city_details` CHANGE `city_state_id` `city_district_id` int(8) NOT NULL AFTER `city_name`;
now while adding constraint back i'm getting error
1452 - Cannot add or update a child row: a foreign key constraint fails (testdb.#sql-39a8_23, CONSTRAINT city_details_ibfk_1 FOREIGN KEY (city_district_id) REFERENCES district_details (district_id) ON DELETE CASCADE ON UPDATE CASCADE)
i have used this query to add constraint
ALTER TABLE `city_details` ADD CONSTRAINT `city_details_ibfk_1` FOREIGN KEY (`city_district_id`) REFERENCES `district_details`(`district_id`) ON UPDATE CASCADE ON DELETE CASCADE;
SHOW CREATE TABLE city_details
CREATE TABLE `city_details` (
`city_id` int(8) NOT NULL AUTO_INCREMENT,
`city_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`city_district_id` int(8) NOT NULL,
PRIMARY KEY (`city_id`),
KEY `city_state_id` (`city_district_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Got the issue district_details table was empty but city_details table had few values, after adding values to district_details it is working fine