52

I want to make doctorid a foreign key in my patient table.

So I have all of my tables created - the main problem is that when I go to the table > structure > relation view only the primary key comes up that I can create a foreign key (and it is already the primary key of the certain table that I want to keep - i.e Patient table patient is enabled to be changed but the doctor Id -I have a doctor table also- is not enabled).

I have another table with two composite keys (medicineid and patientid) in relation view it enables me to change both

Do I have to chance the index of doctor ID in patient table to something else? both cannot be primary keys as patient ID is the primary for the patient table - doctor is the foreign.

table

I hope anyone can help

Kind regards

laurajs
  • 843
  • 1
  • 7
  • 20
  • To generate a foreign key you need to create first index of that field.After you create an index.Then go to relation view you will see that field also along with primary key . – Web Artisan Jun 03 '16 at 13:26

5 Answers5

70

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

ALTER TABLE table_1_name
    ADD CONSTRAINT fk_foreign_key_name
    FOREIGN KEY (table_1_column_name)
    REFERENCES target_table(target_table_column_name);

For example: If you have books table with column created_by which refers to column id in users table:

ALTER TABLE books
    ADD CONSTRAINT books_FK_1
    FOREIGN KEY (created_by)
    REFERENCES users(id);

This assumes the keys already exist in the relevant table

Jon Story
  • 2,881
  • 2
  • 25
  • 41
  • Hi Jon!! thank you for your answer!! I am new to using php and this is how I done the relationship using SQL when I used visual studio but just wanted to make sure I am doing it right - So If I use the SQL statement this will create the foreign key in the table? will I have to make any changes to the column doctor id in te patient table? – laurajs Jun 03 '16 at 13:31
  • 1
    `ALTER TABLE Patient ADD CONSTRAINT fk_to_Doctorid FOREIGN KEY (Doctorid) REFERENCES Doctor(DoctorId); ` - is this correct? – laurajs Jun 03 '16 at 13:33
  • Yes, SQL statements directly affect the underlying structure: PHPMyAdmin is just a graphical interface that produces the SQL statements for you. You shouldn't have to change anything in the doctor_id table as long as it's already indexed. If it isn't, you'll need to turn it into an index as per @Alok's answer – Jon Story Jun 03 '16 at 13:34
  • so before applying the SQL statement index the columns that are going to be foreign keys ? – laurajs Jun 03 '16 at 13:39
  • Yes, exactly, or you'll get an error when running the SQL statement – Jon Story Jun 03 '16 at 13:41
  • Jon thank you - great answer - thank you for your great guidance and help – laurajs Jun 03 '16 at 13:42
  • this now works john thank you - do I now give the contraint name a unique name for the specific relationship - also what options would you give for ON DELETE and ON UPDATE? – laurajs Jun 03 '16 at 13:47
  • also my table with two IDs from other tables (composite keys) will I have to index them aswell – laurajs Jun 03 '16 at 14:06
  • You don't need an entirely unique name for the relationship, typically you'd use fk_doctor_id, assuming your foreign key is doctor_id. As for on delete and on update... that would depend on your specific setup. I suspect you want to prevent deletion of a doctor if there are any patients assigned, therefore forcing the user to re-assign all patients before removing their dotor, which would avoid "orphaned" patients – Jon Story Jun 03 '16 at 14:32
  • Note that this method creates the corresponding indexes if it doesn't find any... So if you're facing the problem of phpmyadmin asking you to create them before trying to add a relation, this is the way to go.. – LukeSavefrogs Jun 02 '19 at 20:41
38

The key must be indexed to apply foreign key constraint. To do that follow the steps.

  1. Open table structure. (2nd tab)
  2. See the last column action where multiples action options are there. Click on Index, this will make the column indexed.
  3. Open relation view and add foreign key constraint.

You will be able to assign DOCTOR_ID as foreign now.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
  • Thank you Alok! great answer I am new to using php so this is a great help - If I opt to create the foreign key using an sql statement - ` ALTER TABLE Patient ADD CONSTRAINT fk_to_Doctorid FOREIGN KEY (Doctorid) REFERENCES Doctor(DoctorId); ` will I have to index the column before doing this? – laurajs Jun 03 '16 at 13:36
  • No, The column needs to be indexed before adding foreign key constraint. You can add index using MySql statement. Refer this : http://dev.mysql.com/doc/refman/5.7/en/create-index.html – Alok Patel Jun 03 '16 at 13:55
  • I have indexed and ran the statement - this came up after - does it mean it was successful? - MySQL returned an empty result set (i.e. zero rows). (Query took 0.2090 sec) – laurajs Jun 03 '16 at 13:57
  • Yeah! Check in relation view for confirmation! – Alok Patel Jun 04 '16 at 03:58
  • 1
    Great answer @Alok. – mLstudent33 Nov 08 '20 at 18:40
28

To be able to create a relation, the table Storage Engine must be InnoDB. You can edit in Operations tab. Storage Engine Configuration

Then, you need to be sure that the id column in your main table has been indexed. It should appear at Index section in Structure tab.

Index list

Finally, you could see the option Relations View in Structure tab. When edditing, you will be able to select the parent column in foreign table to create the relation.

enter image description here

See attachments. I hope this could be useful for anyone.

Víctor
  • 493
  • 5
  • 7
  • Changing Storage Engine locks the tables for transactions. The lock period depends upon the server. Keep this in mind while changing the Storage Engine. – Rahul Kumar Apr 30 '21 at 16:52
8

Create a categories table:

CREATE TABLE categories(
    cat_id int not null auto_increment primary key,
    cat_name varchar(255) not null,
    cat_description text
) ENGINE=InnoDB;

Create a products table and reference categories table:

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;

Create a vendors table and modify products table:

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 (referencing vendors table) to the products 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;

If you wish to drop that key then:

ALTER TABLE table_name 
DROP FOREIGN KEY constraint_name;
trejder
  • 17,148
  • 27
  • 124
  • 216
Yagnik Detroja
  • 921
  • 1
  • 7
  • 22
2

In phpmyadmin, Go to Structure tab, select Relation view as shown in image below. Here you will find the tabular form to add foreign key constrain name, current table column, foreign key database, table and column

enter image description here

Sarfraz Shaikh
  • 492
  • 1
  • 3
  • 11
  • The Relation view option is only available if the Storage Engine is InnoDB. You can edit this in the Operations tab. – Westy92 Mar 26 '23 at 19:49