Add FOREIGN KEY constraint after creating a table
Creates
FOREIGN KEY
constraint with
student_id
column and references it to
students.id
ALTER TABLE notes 
ADD CONSTRAINT fk_student_note
    FOREIGN KEY (student_id)  
    REFERENCES students(id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICT
We can omit
CONSTRAINT fk_student_note
sub-clause. The difference is that if we omit the sub-clause, MySQL names the constraint automatically
ALTER TABLE notes 
ADD FOREIGN KEY (student_id)  
    REFERENCES students(id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICT
by Valeri Tandilashvili
4 years ago
MySQL
ALTER TABLE
1
Pro tip: use ```triple backticks around text``` to write in code fences