简体   繁体   中英

MySQL: Foreign key constraint getting ignored in InnoDB

I am creating a table called fac_master which has a foreign key which refers to dept_id of dept_master. The table is getting created but foreign key is not getting enforced on this table. I also have a foreign key in dept_master which works very well but not for this table.

create table Dept_Master
(   dept_id smallint unsigned auto_increment not null comment 'Department/Branch ID',
    dept_name varchar(100) not null comment 'Department Name such as Computer Engineering',
    prog_id tinyint unsigned not null comment 'Program ID under which this department falls',
    PRIMARY KEY(dept_id),
    CONSTRAINT fk_dept FOREIGN KEY(prog_id) REFERENCES Prog_Master(prog_id) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB COLLATE latin1_general_ci;


create table Fac_Master
(   fac_id smallint unsigned auto_increment not null comment 'Faculty ID',
    dept_id smallint unsigned not null comment 'Department Id of the department in which this faculty works',
    fac_name varchar(30) not null comment 'Name of the Faculty',
    fac_father_name varchar(30) comment 'Father\'s name of the faculty',
    fac_surname varchar(30) comment 'Surname of the faculty',
    fac_designation varchar(30) not null comment 'Designation of the faculty',
    fac_mail_id varchar(50) comment 'E-mail id of the faculty',
    fac_mobile bigint(10) unsigned comment 'Mobile number of the faculty',
    fac_address varchar(100) comment 'Permanent Address of the faculty',
    fac_status varchar(1) not null comment 'Status of Faculty: A=Active D=Deactive',
    fac_joining_date date comment 'Joining Date of the Faculty',
    PRIMARY KEY(fac_id),
    CONSTRAINT fk_faculty FOREIGN KEY(dept_id) REFERENCES Dept_Master(dept_id) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB COLLATE latin1_general_ci;

When i try to add a value in "prog_id" of "dept_master" which is not present in the "prog_id" of "prog_master" then it gives fk constraint error which is fine but when i try to add a value in "dept_id" of "fac_master" which is not present in the "dept_id" of "dept_master" then it gets added but it should have given a fk constraint error. I also checked foreign key constraint in information schema and found that foreign key constraint was not there for table fac_master. I am using WAMP Server 2.2 on windows 7 HP 64 bit version.

What is the problem? please help..

Edit:

alter table Fac_Master
add constraint fk_faculty FOREIGN KEY(dept_id) REFERENCES Dept_Master(dept_id) ON DELETE RESTRICT ON UPDATE RESTRICT;

using alter table as shown above works but not when used with create table. What could be the reason for it?

It appears the problem is caused by the way you escape the ' in 'Father\\'s name of the faculty' . When you change it in 'Father''s name of the faculty' , you'll find the foreign key constraints are correctly created.

Both ways of including a single quote are correct according to the manual, so it is a bug. See this MySQL bug ticket .

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