sql查询错误外键约束给出错误

时间:2018-10-10 20:20:28

标签: mysql sql mariadb

这是studentfeedback数据库的SQL

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `studentfeedback`
--

-- --------------------------------------------------------

--
-- Table structure for table `feedback`
--

CREATE TABLE IF NOT EXISTS `feedback` (
  `Fed_No` int(3) NOT NULL,
  `Roll_No` int(3) NOT NULL,
  `Sub_Name` varchar(100) NOT NULL,
  `Feedback` varchar(500) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `feedback`
--

INSERT INTO `feedback` (`Fed_No`, `Roll_No`, `Sub_Name`, `Feedback`) VALUES
(1, 1, 'DBMS', 'Subject is Good'),
(2, 1, 'English', 'Subject is Good'),
(3, 2, 'DBMS', 'Subject is Good'),
(4, 2, 'OS', 'Subject is Good'),
(5, 1, 'Algorithms', 'Hate, bruv.'),
(6, 1, 'TOC', 'Great Professor');

-- --------------------------------------------------------

--
-- Table structure for table `student`
--

CREATE TABLE IF NOT EXISTS `student` (
  `Roll_No` int(3) NOT NULL,
  `Name` varchar(100) NOT NULL,
  `Pass` varchar(100) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `student`
--

INSERT INTO `student` (`Roll_No`, `Name`, `Pass`) VALUES
(1, 'agnish', ''),
(2, 'ABC', '');

-- --------------------------------------------------------

--
-- Table structure for table `subject`
--

CREATE TABLE IF NOT EXISTS `subject` (
  `Sub_No` int(3) NOT NULL,
  `Sub_Name` varchar(100) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `subject`
--

INSERT INTO `subject` (`Sub_No`, `Sub_Name`) VALUES
(13, 'Linux'),
(7, 'DBMS'),
(5, 'English'),
(9, 'Political Science'),
(4, 'Hindi'),
(12, 'Networks'),
(11, 'Advanced DBMS'),
(6, 'Maths'),
(10, 'Electrical Engineering'),
(14, 'Algorithms'),
(8, 'Calculus');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `feedback`
--
ALTER TABLE `feedback`
  ADD PRIMARY KEY (`Fed_No`),
  ADD KEY `Roll_No` (`Roll_No`),
  ADD KEY `Sub_Name` (`Sub_Name`);

--
-- Indexes for table `student`
--
ALTER TABLE `student`
  ADD PRIMARY KEY (`Roll_No`);

--
-- Indexes for table `subject`
--
ALTER TABLE `subject`
  ADD PRIMARY KEY (`Sub_No`),
  ADD UNIQUE KEY `Sub_Name` (`Sub_Name`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `feedback`
--
ALTER TABLE `feedback`
  MODIFY `Fed_No` int(3) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
--
-- AUTO_INCREMENT for table `student`
--
ALTER TABLE `student`
  MODIFY `Roll_No` int(3) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT for table `subject`
--
ALTER TABLE `subject`
  MODIFY `Sub_No` int(3) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=15;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `feedback`
--
ALTER TABLE `feedback`
  ADD CONSTRAINT `feedback_ibfk_1` FOREIGN KEY (`Roll_No`) REFERENCES `student` (`Roll_No`),
  ADD CONSTRAINT `feedback_ibfk_2` FOREIGN KEY (`Sub_Name`) REFERENCES `subject` (`Sub_Name`);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

我创建了一个studentfeedback数据库 并执行以上代码, 我在以下几行中出错

SQL query:


--
-- Constraints for dumped tables
--

--
-- Constraints for table `feedback`
--
ALTER TABLE `feedback`
  ADD CONSTRAINT `feedback_ibfk_1` FOREIGN KEY (`Roll_No`) REFERENCES `student` (`Roll_No`),
  ADD CONSTRAINT `feedback_ibfk_2` FOREIGN KEY (`Sub_Name`) REFERENCES `subject` (`Sub_Name`)
MySQL said: Documentation

#1452 - Cannot add or update a child row: a foreign key constraint fails (`studentfeedback`.`#sql-460_70`, CONSTRAINT `feedback_ibfk_2` FOREIGN KEY (`Sub_Name`) REFERENCES `subject` (`Sub_Name`))

我无法理解此错误,为什么会这样,上述MySQL语句的实现有什么问题,请让我知道此错误的原因或原因,但我无法理解应该进行哪些更改。

2 个答案:

答案 0 :(得分:1)

您不能添加外键feedback_ibfk_2,因为表feedback包含表OS中实际上不存在的两个主题TOCsubjects

修复数据,即可通过在subjects表中创建这两行来创建外部数据。

答案 1 :(得分:1)

这意味着您的反馈表中的数据在主题表中不存在。检查现有数据。