0

WHAT IS A PROBLEM HERE ?

CREATE TABLE marks(
id INT PRIMARY KEY AUTO_INCREMENT,
marks INT,
student_id INT
);


CREATE TABLE googlea(
id INT PRIMARY KEY AUTO_INCREMENT ,
student_name VARCHAR(20) ,
student_id INT ,
FOREIGN KEY(student_id) REFERENCES marks(student_id)
);
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Wrong direction. Have a fk from marks referencing googlea. Why two ID columns? – jarlh Sep 20 '18 at 19:40
  • 2
    Usually foreign keys point to the primary key of the table. It's not required, but it's generally more appropriate. – Barmar Sep 20 '18 at 19:41
  • Target column must be the leftmost or only column of an index. See duplicate topic. – Shadow Sep 20 '18 at 22:49

2 Answers2

0

Use below updated queries:

CREATE TABLE marks( 
    id INT PRIMARY KEY AUTO_INCREMENT, 
    marks INT, 
    student_id INT 
);

CREATE TABLE googlea( 
    id INT PRIMARY KEY AUTO_INCREMENT , 
    student_name VARCHAR(20) , 
    student_id INT , 
    FOREIGN KEY(student_id) REFERENCES marks(id) 
);

Hope it works now.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Suresh
  • 489
  • 3
  • 7
  • `FOREIGN KEY(id) REFERENCES marks(id)` Why would the id from googlea point to the id off marks.. Does not make sense in this case.. – Raymond Nijland Sep 20 '18 at 19:42
  • Please improve your code formatting. Write the queries neatly over multiple lines, then use Ctl-k to indent it so it's displayed ina code block. – Barmar Sep 20 '18 at 19:42
0

The error is in FOREIGN KEY(student_id) REFERENCES marks(student_id). try this FOREIGN KEY(student_id) REFERENCES marks(id)