Hi,im trying to insert into table student_acedamic_history but I get this error.Heres my code.
CREATE TABLE Student_Acedamic_History(

    S_id INT,


Degree_Title VARCHAR(30),
Institue VARCHAR(30),
Year_Of_Comp INT,
Total_Marks INT,
Obtained_Marks INT,
PercentAge FLOAT,
CONSTRAINT fk_S_id_students FOREIGN KEY student_acedamic_history(S_id)REFERENCES students (S_id)ON UPDATE CASCADE ON DELETE CASCADE

);
And the data:
INSERT INTO student_acedamic_history VALUES
(1,'FSC','GBHS',2013,1100,900,81.81),(2,'FA','PAF',2012,1100,850,77.28),(3,'ICS','APS',2011,1100,800,73),(4,'I_COM','GC',2010,1100,750,68.18),(5,'MATRIC','FC',2009,1050,800,76.2),
(6,'FSC','GBHS',2013,1100,900,81.81),(7,'FA','PAF',2012,1100,850,77.28),(8,'ICS','APS',2011,1100,800,73),(9,'I_COM','GC',2010,1100,750,68.18),(10,'MATRIC','FC',2009,1050,800,76.2),
(11,'FSC','GBHS',2013,1100,900,81.81),(12,'FA','PAF',2012,1100,850,77.28),(13,'ICS','APS',2011,1100,800,73),(14,'I_COM','GC',2010,1100,750,68.18),(15,'MATRIC','FC',2009,1050,800,76.2),
(16,'FSC','GBHS',2013,1100,900,81.81),(17,'FA','PAF',2012,1100,850,77.28),(18,'ICS','APS',2011,1100,800,73),(19,'I_COM','GC',2010,1100,750,68.18),(20,'MATRIC','FC',2009,1050,800,76.2),
(21,'FSC','GBHS',2013,1100,900,81.81),(22,'FA','PAF',2012,1100,850,77.28),(23,'ICS','APS',2011,1100,800,73),(24,'I_COM','GC',2010,1100,750,68.18),(25,'MATRIC','FC',2009,1050,800,76.2),
(26,'FSC','GBHS',2013,1100,900,81.81),(27,'FA','PAF',2012,1100,850,77.28),(28,'ICS','APS',2011,1100,800,73),(29,'I_COM','GC',2010,1100,750,68.18),(30,'MATRIC','FC',2009,1050,800,76.2),
(31,'FSC','GBHS',2013,1100,900,81.81),(32,'FA','PAF',2012,1100,850,77.28),(33,'ICS','APS',2011,1100,800,73),(34,'I_COM','GC',2010,1100,750,68.18),(35,'MATRIC','FC',2009,1050,800,76.2),
(36,'FSC','GBHS',2013,1100,900,81.81),(37,'FA','PAF',2012,1100,850,77.28),(38,'ICS','APS',2011,1100,800,73),(39,'I_COM','GC',2010,1100,750,68.18),(40,'MATRIC','FC',2009,1050,800,76.2),
(41,'FSC','GBHS',2013,1100,900,81.81),(42,'FA','PAF',2012,1100,850,77.28),(43,'ICS','APS',2011,1100,800,73),(44,'I_COM','GC',2010,1100,750,68.18),(45,'MATRIC','FC',2009,1050,800,76.2),
(46,'FSC','GBHS',2013,1100,900,81.81),(47,'FA','PAF',2012,1100,850,77.28),(48,'ICS','APS',2011,1100,800,73),(49,'I_COM','GC',2010,1100,750,68.18),(50,'MATRIC','FC',2009,1050,800,76.2);

Dani AI

Generated

is pointing to the most likely cause: MySQL is refusing the insert because a foreign-key requirement is not being satisfied. Beyond “parent id missing” there are a few common, easy-to-overlook problems to check and simple fixes.

First, run these checks to see what the constraint and data actually look like:

SHOW CREATE TABLE students;
SHOW CREATE TABLE Student_Acedamic_History;

-- find child S_id values that have no matching parent
SELECT c.S_id
FROM Student_Acedamic_History c
LEFT JOIN students p ON c.S_id = p.S_id
WHERE p.S_id IS NULL;

If that query returns rows, those are the exact S_id values causing the 1452 error. Other frequent causes: the referenced column in students is not indexed/PK, the integer types or signed/unsigned attributes differ between parent and child, the two tables use different storage engines (InnoDB required for FKs), or table-name case differences on a case-sensitive filesystem.

Correct FK syntax and a more flexible schema are helpful. Use a dedicated PK on the history table (so a student can have many history rows) and declare the FK like this:

CONSTRAINT fk_S_id_students FOREIGN KEY (S_id)
  REFERENCES students(S_id)
  ON UPDATE CASCADE ON DELETE CASCADE

For bulk imports only: temporarily disabling FK checks can let you load data in the right order, but do so with care:

SET FOREIGN_KEY_CHECKS=0;
-- load or adjust data
SET FOREIGN_KEY_CHECKS=1;

Also inspect SHOW ENGINE INNODB STATUS\G if MySQL names the failing constraint; it gives the exact constraint and table causing the problem. Fix the parent data or the schema mismatch (types, PK/index, engine, naming) and the INSERT will succeed.

Foreign key constraints prevent INSERT and UPDATE commands if the corrosponding key is found in the referenced table.
That means the s_id value you are trying to insert into student_academic_history must already exist in students.
This is half the reason you put in a foreign key constraint anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.