How to create a foreign key in phpmyadmin

I want to make a foreign key on my patient’s desk as a doctor.

So, I have created all my tables - the main problem is that when I go to the table view> structure> relation, only the primary key appears, which I can create a foreign key (and it is already the primary key of a specific table that I I want to save, i.e. the patient-patient can be changed, but the doctor’s Id - I also have a doctor’s table - is not activated).

I have another table with two compound keys (medicid and patientid), which allows me to change how

Do I need to point the doctor identifier index in the patient table to something else? both cannot be primary keys, since the patient identifier is the main one for the patient table - the doctor is foreign.

table

I hope someone can help

Yours faithfully

+11
database php mysql phpmyadmin
source share
4 answers

You can do it the old fashioned way ... with an SQL statement that looks something like this:

ALTER TABLE table_name ADD CONSTRAINT fk_foreign_key_name FOREIGN KEY (foreign_key_name) REFERENCES target_table(target_key_name); 

It is assumed that keys already exist in the corresponding table

+17
source share

The key must be indexed to apply a foreign key constraint. To do this, follow the steps.

  • Open the table structure. (Second tab)
  • See the last column action for options for multiple actions. Click Index , this will index the column.
  • Open a relationship view and add a foreign key constraint.

Now you can designate DOCTOR_ID as foreign.

+15
source share

When creating a table that you can give as follows.

 CREATE TABLE categories( cat_id int not null auto_increment primary key, cat_name varchar(255) not null, cat_description text ) ENGINE=InnoDB; CREATE TABLE products( prd_id int not null auto_increment primary key, prd_name varchar(355) not null, prd_price decimal, cat_id int not null, FOREIGN KEY fk_cat(cat_id) REFERENCES categories(cat_id) ON UPDATE CASCADE ON DELETE RESTRICT )ENGINE=InnoDB; 

and when after creating the table create

  ALTER table_name ADD CONSTRAINT constraint_name FOREIGN KEY foreign_key_name(columns) REFERENCES parent_table(columns) ON DELETE action ON UPDATE action; 

Following suit for him.

 CREATE TABLE vendors( vdr_id int not null auto_increment primary key, vdr_name varchar(255) )ENGINE=InnoDB; ALTER TABLE products ADD COLUMN vdr_id int not null AFTER cat_id; 

To add a foreign key to the product table, you use the following statement:

 ALTER TABLE products ADD FOREIGN KEY fk_vendor(vdr_id) REFERENCES vendors(vdr_id) ON DELETE NO ACTION ON UPDATE CASCADE; 

To delete a key

 ALTER TABLE table_name DROP FOREIGN KEY constraint_name; 

We hope for this help to find out what the FOREIGN key works.

+2
source share

To be able to create relationships, the Storage Engine table must be InnoDB. You can edit on the Operations tab. Storage engine configuration

Then you must be sure that the id column in the main table is indexed. It should appear in the "Index" section of the "Structure" tab.

Index list

Finally, you can see the “Relationship View” option on the “Structure” tab. When editing, you can select the parent column in the external table to create the relationship.

enter image description here

See attachments. I hope this can be useful to everyone.

0
source share

All Articles