如何制作外键

时间:2016-02-09 15:29:02

标签: php mysql prestashop

我试图创建一个sql create table但是我没能创建一个外键。

我的代码:

Db::getInstance()->execute('

      CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'sondage`(
      `id` int(6) NOT NULL AUTO_INCREMENT,
      `question` varchar(60) NOT NULL,
      PRIMARY KEY(`id`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8')

|| !Db::getInstance()->execute('

      CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'sondage_reponse`(
      `id` int(6) NOT NULL AUTO_INCREMENT,
      `champ1` int(4) NOT NULL,
      `champ2` int(4) NOT NULL,
      `champ3` int(4) NOT NULL,
      PRIMARY KEY(`id`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8')


|| !Db::getInstance()->execute('

 INSERT INTO '._DB_PREFIX_.'sondage_reponse (champ1,champ2,champ3)
 VALUES (0,0,0)')

我想从" sondage" " sondage_reponse"中的外键。我试过:

 ALTER TABLE ps_sondage_reponse
            ADD FOREIGN KEY fk_id_sdg(id)
            REFERENCES sondage(id)')

但是Prestashop无法添加外键。

THX

1 个答案:

答案 0 :(得分:0)

我想说试试这个:

ALTER TABLE ps_sondage_reponse
ADD CONSTRAINT FK_id_sdg
FOREIGN KEY (id) REFERENCES sondage(id)