9951 explained code solutions for 126 technologies


python-mysqlHow can I create a foreign key relationship between two tables in a MySQL database using Python?


Creating a foreign key relationship between two tables in a MySQL database using Python is a relatively straightforward process. The following example code will create a foreign key relationship between two tables, table1 and table2.

# Create foreign key
cursor.execute("ALTER TABLE table1 ADD FOREIGN KEY (column1) REFERENCES table2 (column2);")

This code will create a foreign key between the column1 of table1 and the column2 of table2.

The parts of this code are:

  1. ALTER TABLE - This specifies that the command is to alter an existing table.
  2. table1 - This is the name of the table that will have the foreign key created.
  3. ADD FOREIGN KEY - This specifies that the command is to add a foreign key.
  4. column1 - This is the name of the column in table1 that will be the foreign key.
  5. REFERENCES - This specifies that the foreign key is referencing another table.
  6. table2 - This is the name of the table that will be referenced by the foreign key.
  7. column2 - This is the name of the column in table2 that will be referenced by the foreign key.

For more information on creating foreign keys in MySQL using Python, please see the following links:

Edit this code on GitHub