how can we check whether the primary and foreign key are working in the database in mysql

Recommended Answers

All 2 Replies

I think you need not to check if you have created constraints properly.
But still if you want to check, then to chekc PK try to insert record with existing PK value, and to check FK try to insert record with FK value that does not exist in master table referred.

For primary key, try to insert a new record with the same PK as an existing record. If you can't, then the PK is working.

For a foreign key, try to insert a new record where the FK doesn't equal a record for the referenced field. If you can't insert such a record, the FK is working.

EDIT: D'oh, urtrivedi beat me to it :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.