修改表列外键?

时间:2012-05-31 22:01:51

标签: mysql sql foreign-keys

https://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html#.T8foMxTft0x

为例
CREATE TABLE product (category INT NOT NULL, id INT NOT NULL,
                      price DECIMAL,
                      PRIMARY KEY(category, id)) ENGINE=INNODB;
CREATE TABLE customer (id INT NOT NULL,
                       PRIMARY KEY (id)) ENGINE=INNODB;
CREATE TABLE product_order (no INT NOT NULL AUTO_INCREMENT,
                            product_category INT NOT NULL,
                            product_id INT NOT NULL,
                            customer_id INT NOT NULL,
                            PRIMARY KEY(no),
                            INDEX (product_category, product_id),
                            FOREIGN KEY (product_category, product_id)
                              REFERENCES product(category, id)
                              ON UPDATE CASCADE ON DELETE RESTRICT,
                            INDEX (customer_id),
                            FOREIGN KEY (customer_id)
                              REFERENCES customer(id)) ENGINE=INNODB;

我有类似的问题,但我注意到你无法将外键(本例中的客户ID)更改为无符号,设置默认值或长度等。我无法使用alter table更改任何一个表,显然是因为他们是依赖的,必须是同一类型。还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

您可以删除外键,更改列,然后再次添加外键。