สามารถเพิ่ม FOREIGN KEY constraint ให้คอลัมน์ของตาราง MySQL ที่มีอยู่แล้วด้วยคำสั่ง ALTER TABLE
ไวยากรณ์
ALTER TABLE table_name ADD FOREIGN KEY (colum_name) REFERENCES table_having_Primary_Key(column_name);
ตัวอย่าง
เพิ่ม FOREIGN KEY constraint บนตาราง 'Orders1' อ้างอิงตาราง 'Customer' ที่มี 'Cust_Id' เป็น Primary Key:
mysql> ALTER TABLE orders1 ADD FOREIGN KEY(Cust_id) REFERENCES Customer(Cust_id); Query OK, 0 rows affected (0.21 sec) mysql> DESCRIBE ORDERS1; +--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | order_id | int(11) | NO | PRI | NULL | | | Product_name | varchar(25) | YES | | NULL | | | orderdate | date | YES | | NULL | | | Cust_id | int(11) | YES | MUL | NULL | | +--------------+-------------+------+-----+---------+-------+