dou4381 2015-08-02 22:58
浏览 52
已采纳

表创建中php代码中的外键约束

I have a table named property structured like this:

$sql = "CREATE TABLE property (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            seller_id INT(6) NOT NULL,
            property_name VARCHAR(30) NOT NULL,
            address VARCHAR(30) NOT NULL,
            reg_date TIMESTAMP
            )";
            mysql_query($sql);

and I have another table named dealtype like this:

$sql = "CREATE TABLE dealtype (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            seller_id INT(6) NOT NULL,
            deal_type VARCHAR(30) NOT NULL,
            FOREIGN KEY (property_id) REFERENCES property (id),
            reg_date TIMESTAMP
            )";
            mysql_query($sql);

Now the property_id in the dealtype table is the foreign key of the id in the property table but its not working in the php code. The property table is created but the dealtype doesn't get created.

Where is my mistake?

  • 写回答

1条回答 默认 最新

  • dongman5539 2015-08-02 23:29
    关注

    You need to declare the variable with its type before specifying that it is a foreign key:

    $sql = "CREATE TABLE dealtype (
                id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                seller_id INT(6) NOT NULL,
                deal_type VARCHAR(30) NOT NULL,
                property_id int(6) unsigned,
                reg_date TIMESTAMP,
                FOREIGN KEY (property_id) REFERENCES property (id)
            )";
    

    Presumably, seller_id has a similar relationship to a sellers table (or something similar).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块