Home  >  Q&A  >  body text

Set primary key column name when using multiple foreign keys

create table teach_enroll(

instructor_id varchar(64) not null,
class_id varchar(64)not null,
course_id varchar(64)not null,
student_id varchar(64) not null,
foreign key (instructor_id) references instructor(id),
foreign key (class_id) references class(id),
foreign key(course_id)references course_type(id),
foreign key (student_id)references student(id),

CONSTRAINT ENROLL_ID primary key (class_id,course_id,student_id) 

);

select * from teach_enroll;

I tried to name my primary key column ENROLL_ID but when I query select * fromteacher_enroll the constraint keyword does not help, I only get coach_id, class_id, course_id, student_id in separate columns but I can't see Primary key column: )

P粉099145710P粉099145710405 days ago438

reply all(1)I'll reply

  • P粉015402013

    P粉0154020132023-09-12 00:18:45

    Your primary key consists of three (3) columns, not a single column, because that is how you defined it. This means that the combination of these three columns must be unique for every row in the table, although any given column defined as a key may not be unique. You cannot apply a single name to three columns.

    This means your foreign key constraint will name all 3 columns. Assuming the column names in the two tables are the same, then:

    CONSTRAINT FOREIGN KEY (class_id,course_id,student_id)
        REFERENCES teach_enroll(class_id,course_id,student_id)
    

    reply
    0
  • Cancelreply