0

I have created a table, i have inserted some data as well. I have 1 primary key on the table which is incremented by auto-generated number. I have user_id and semester_course_id columns both are foreign_keys. I want to add composite key on 2 Columns user_id and semester_course_id .

So that 1 Student_id can register to 1 Course only. Semester_Course_id and student_id will repeat in the table, but together they have to appear one time only.

Kindly tell me how to add composite primary key.

enter image description here enter image description here

Thanks

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
user3480644
  • 577
  • 4
  • 8
  • 23

2 Answers2

2

Add unique constraint on combination of both columns.

alter table table_name
  add constraint uk_sid_coursid
      unique key ( studentid, semistercourseid )

For this composite key to work as secondary primary key, define each of this field as not null.

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
0

Try this

ALTER TABLE TABLE_NAME ADD unique index(StudentId,SemisterCourseId);
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122