How to make foreign key one-to-many in mysql?

0

I have a student table and a table courses, my table courses have the course eg administration, how do I relate several student to this table using Foreign key?

    
asked by anonymous 04.11.2017 / 02:12

1 answer

1

First, you create the COURSE table with the CUSTOM_ID (PRIMARY_KEY) column, then you create a STUDENTS table with the CAL_ID (primary_key) column and the COS_ID column. This column ID_CURSO you point to FOREIGN_KEY with reference to the COURSE table, column CUSTOM. Here's an example:

CREATE TABLE 'database'.'cursos' (
  'id_cursos' INT NOT NULL AUTO_INCREMENT,
  'nome_curso' VARCHAR(45) NULL,
  PRIMARY KEY ('id_cursos'));


CREATE TABLE 'database'.'alunos' (
  'id_alunos' INT NOT NULL AUTO_INCREMENT,
  'id_curso' INT NULL,
  'nome_aluno' VARCHAR(45) NULL,
  PRIMARY KEY ('id_alunos'),
  INDEX 'FK_ALUNO_CURSO_idx' ('id_curso' ASC),
  CONSTRAINT 'FK_ALUNO_CURSO' 
    FOREIGN KEY ('id_curso') 
    REFERENCES 'vigilant'.'cursos' ('id_cursos')
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION);

Any questions just talk. Hugs.

    
04.11.2017 / 03:01