原来表的FK约束是这样的
foreign key (topic_id) references content(tontent_id)
现在我想加个 on delete cascade  约束,怎么加呢,  alter modify 好像不行。我删除FK 也不行啊, 我用 alter table tablename drop foreign key topic_id

解决方案 »

  1.   

    alter table tablename drop CONSTRAINT  topic_id
      

  2.   

    删除约束名
    alter table tt drop CONSTRAINT  topic_id
      

  3.   

    我没有显示的设置外键约束名默认的应该是这个吧CONSTRAINT `review_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `content` (`content_id`)然后我用alter table tablename drop constraint review_ibfk_1是这样用么
      

  4.   

    我的数据库是 mysql 5.6的
      

  5.   

    show create table table\G Or, query the information_schema.table_constraints得到约束名ALTER TABLE table DROP FOREIGN KEY constraint
      

  6.   

    参考:
    ALTER TABLE tbl1 ADD CONSTRAINT fk_name FOREIGN KEY index (col1) REFERENCES tbl2(col2) referential_actions;
    ALTER TABLE tbl DROP FOREIGN KEY fk_name;
      

  7.   

    CREATE TABLE `review` (
       `review_id` int(11) NOT NULL AUTO_INCREMENT,
       `reviw_user` varchar(20) NOT NULL,
       `topic_id` int(11) NOT NULL,
       `reviw_date` datetime NOT NULL,
       `review_content` text NOT NULL,
       PRIMARY KEY (`review_id`),
       KEY `topic_id` (`topic_id`),
       CONSTRAINT `review_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `content` (`content_id`)
     ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
    这是我的建表语句, 就想删除 那个外键约束
      

  8.   

    主要是为了加个 on delete cascade
      

  9.   

    mysql> alter table `review` drop FOREIGN KEY  `review_ibfk_1`;
    Query OK, 0 rows affected (0.14 sec)
    Records: 0  Duplicates: 0  Warnings: 0mysql> alter table `review` add CONSTRAINT `review_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `content` (`content_id`)  on delete cascade ;
    Query OK, 0 rows affected (0.16 sec)
    Records: 0  Duplicates: 0  Warnings: 0