mysql - 添加外键但出现约束违反错误

标签 mysql foreign-keys constraints

我有四个表:

1)Country:Two columns
  a) country_id
  b) country_name

2)State: Three columns
  a) state_id
  b) state_name
  c) country_id(foreign key)

 3)City: Three Columns
   a) city_id
   b) city_name
   c) state_id(foreign key)

 4) Doctor: 20 Columns
   a) doc_id (primary key)
   b) doc_name
   c) email,gender age and so on 

我已经用 dara 填充了这些表。

我在医生表中添加了三个新列:country_id、state_id 和 city_id 作为外键。

当我在该表中插入 country_id、state_id 和 city_id 作为外键时出现此错误

1452 - Cannot add or update a child row: a foreign key constraint fails (`medical_network`.`#sql-4174_45`, CONSTRAINT `#sql-4174_45_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON DELETE CASCADE ON UPDATE CASCADE)

我知道这是一个基本错误,但找不到错误。

医生表:

CREATE TABLE `doctor` (
 `doc_id` int(9) NOT NULL AUTO_INCREMENT,
 `doc_fname` varchar(15) NOT NULL,
 `doc_lname` varchar(15) NOT NULL,
 `doc_province` varchar(20) NOT NULL,
 `doc_city` varchar(25) NOT NULL,
 `doc_gender` varchar(10) NOT NULL,
 `doc_fathername` varchar(32) NOT NULL,
 `doc_age` varchar(20) NOT NULL,
 `doc_cnic` varchar(16) NOT NULL,
 `doc_medclgname` varchar(100) NOT NULL,
 `doc_specilization` varchar(100) NOT NULL,
 `doc_phoneno` varchar(20) NOT NULL,
 `doc_officeno` varchar(20) NOT NULL,
 `doc_email` varchar(25) NOT NULL,
 `doc_gradyear` int(10) NOT NULL,
 `doc_licenseno` varchar(30) NOT NULL,
 `doc_hosp` varchar(200) NOT NULL,
 `doc_pass` varchar(35) NOT NULL,
 `doc_address` varchar(100) NOT NULL,
 `doc_country` varchar(100) NOT NULL,
 `doc_img` varchar(200) NOT NULL,
 `totime` time NOT NULL,
 `fromtime` time NOT NULL,
 `todate` date NOT NULL,
 `fromdate` date NOT NULL,
 `country_id` int(20) NOT NULL,
 PRIMARY KEY (`doc_id`),
 KEY `country_id` (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin

国家表:

CREATE TABLE `country` (
 `country_id` int(20) NOT NULL AUTO_INCREMENT,
 `country_name` varchar(200) NOT NULL,
 PRIMARY KEY (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=252 DEFAULT CHARSET=latin1

状态表查询:

CREATE TABLE `state` (
 `state_id` int(20) NOT NULL AUTO_INCREMENT,
 `state_name` varchar(200) NOT NULL,
 `country_id` int(20) NOT NULL,
 PRIMARY KEY (`state_id`),
 KEY `country_id` (`country_id`),
 CONSTRAINT `state_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

城市表:

CREATE TABLE `city` (
 `city_id` int(20) NOT NULL AUTO_INCREMENT,
 `city_name` varchar(200) NOT NULL,
 `state_id` int(20) NOT NULL,
 PRIMARY KEY (`city_id`),
 KEY `state_id` (`state_id`),
 CONSTRAINT `city_ibfk_1` FOREIGN KEY (`state_id`) REFERENCES `state` (`state_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1

最佳答案

  1. 首先在表中添加所有你想做外键的列。

  2. 向其中添加一些相关数据。

  3. 现在修改表并使这些新添加的列成为外键。

    即 改变表表名 广告外键 (P_Id) 引用资料 ParentTable(P_Id)

关于mysql - 添加外键但出现约束违反错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28105290/

相关文章:

python - Django-ManyToMany表提取了错误的信息

sql - 重命名 MySQL 中的外键列

android - 我如何处理SQLiteConstraintException错误代码19?

grails - 有关在Grails中实现域类关系和约束的问题

c# - "Forcing"符合通用约束

mysql - 将时间戳从 mysql 格式更改为 unix - MYSQL

Mysql查询无法解决

javascript - PHP mysqli执行不返回值

Mysql:添加外键不会在 MyISAM 表上给出警告/错误

mysql - 也请求具有相同单元格的行数