简体   繁体   中英

I cant add MySQL foreign key constraint

I have three tables user, department, and department_hod user has a department_id, which is the primary key of department, also departement has hod which is the primary key of user. But i am getting an error when adding the foreign key constraint of username in department_hod table,

Please help

--
-- Create a user table
--

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` char(80) NOT NULL,
  `emp_id` int(11) NOT NULL,
  `designation` varchar(50) NOT NULL,
  `department_id` int(11) NOT NULL,
  `status` varchar(2) NOT NULL DEFAULT 'A',
  `email_id` varchar(50) NOT NULL,
  `account_status` varchar(2) NOT NULL DEFAULT 'U',
  `validity_date` TIMESTAMP,
  `deactivation_date` TIMESTAMP ,
  `deactivated_by` varchar(50),
  `deactivation_remarks` varchar(1000),
  `creation_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updation_date` TIMESTAMP,
  `created_by` varchar(50),
  `updated_by` varchar(50),

  CONSTRAINT `PK_USER_ID` PRIMARY KEY (`id`,`username`),
  
  KEY `FK_DEPARTMENT_IDX_01` (`department_id`),
  
  CONSTRAINT `FK_DEPARTMENT_ID_01` FOREIGN KEY (`department_id`) 
  REFERENCES `department` (`id`) 
  ON DELETE NO ACTION ON UPDATE NO ACTION
  
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;


--
-- Create a department table
--

CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `status` varchar(2) NOT NULL DEFAULT 'A',
  `creation_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updation_date` TIMESTAMP,
  `created_by` varchar(50),
  `updated_by` varchar(50),

  PRIMARY KEY (`id`)
  
 
  
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

--
-- Add department_hod table
--

CREATE TABLE `department_hod` (
  `department_id` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  
  PRIMARY KEY (`username`,`department_id`),
  
  KEY `FK_DEPARTMENT_idx_02` (`department_id`),
  
  CONSTRAINT `FK_DEPARTMENT_id_02` FOREIGN KEY (`department_id`) 
  REFERENCES `department` (`id`) 
  ON DELETE NO ACTION ON UPDATE NO ACTION,
  
  CONSTRAINT `FK_USER_01` FOREIGN KEY (`username`) 
  REFERENCES `user` (`username`) 
  ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

SET FOREIGN_KEY_CHECKS = 1;

error is here

CONSTRAINT `FK_USER_01` FOREIGN KEY (`username`) 
  REFERENCES `user` (`username`) 
  ON DELETE NO ACTION ON UPDATE NO ACTION

Error is

Error Code: 1215. Cannot add foreign key constraint

Always provide complete error message. In a half of choices you may find the solution in it.

For your code the error message is

Failed to add the foreign key constraint. Missing index for constraint 'FK_USER_01' in the referenced table 'user'

So the problem can be fixed by absent index creation:

CREATE INDEX idx_user_username ON user (username);

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1f0fd24a8eb9cb5692b9d35dd1903045


can you please tell me that is it necessary to use index here? as it is only used for speeding up, do i have to use it beacuse of two primary keys in my user table? – shah-123

MySQL 8.0 Reference Manual /... / FOREIGN KEY Constraints / Conditions and Restrictions

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM