mysql - whats wrong. when i call foreign key -
create table applicant ( applicant_id varchar(10) not null, applicant_name nvarchar (25) not null, father_name nvarchar (25), gender nvarchar (15), applicant_dob datetime, qualifaction nvarchar (45), application_id varchar (15), primary key (applicant_id, applicant_name) ); create table application ( application_id varchar (10), application_name nvarchar(25), applicant_id varchar (10), applicant_name nvarchar (25), primary key (application_id) ); create table cell_number ( cell_no varchar (15) not null , applicant_id varchar (10), applicant_name nvarchar (25), primary key (cell_no) ); alter table cell_number add constraint fk_applicant_id foreign key (applicant_id) references applicant (applicant_id); alter table cell_number add constraint fk_applicant_name foreign key (applicant_name) references applicant (applicant_name);
show error
msg 1776, level 16, state 0, line 34 there no primary or candidate keys in referenced table 'applicant' match referencing column list in foreign key 'fk_applicant_id'. msg 1750, level 16, state 0, line 34 not create constraint. see previous errors.
you can't create foreign key references 1 column of primary key pair.
alter table cell_number add constraint fk_applicant foreign key (applicant_id, applicant_name) references applicant (applicant_id, applicant_name)
this because foreign key needs identify 1 row in applicant table, , since primary keys reference 2 columns, can't use 1 column generate foreign key.
edit: can reference 1 column of composite primary key, corrected justin
Comments
Post a Comment