php - How do i eliminate redundancy -


how avoid redundant insertion of data in mysql table eg in id 003

student id              subject 003                     maths 003                     maths 004                     english 003                     maths 004                     science  

the database can validate data you, using unique index or constraint (which functionally equivalent):

create unique index idx_t_student_subject on t(student, subject); 

you can define constraint when create table:

create table t (     . . .     constraint unq_t_student_subject unique (student, subject)     . . . ); 

when attempt insert duplicate row, you'll error.

note: primary key have same effect, defining each column not null.


Comments