+ -
当前位置:首页 → 问答吧 → 无法建立外键,请大哥们指点

无法建立外键,请大哥们指点

时间:2011-12-28

来源:互联网

SQL code

/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2011/12/28 11:43:47                          */
/*==============================================================*/


drop table if exists business;

drop table if exists carquote;

drop table if exists message;

drop table if exists news;

/*==============================================================*/
/* Table: business                                              */
/*==============================================================*/
create table business
(
   bnid                 int not null,
   bnname               varchar(20),
   bntype               int,
   bncaraid             int,
   bncarbid             int,
   bnjt                 varchar(20),
   bnsellphone          varchar(20),
   bncollphone          varchar(20),
   website              varchar(30),
   bnfax                varchar(10),
   bnmap                varchar(20),
   bnabout              text,
   you                  int,
   bnmail               varchar(20),
   bnwebsite            varchar(30),
   bnaddress            varchar(30),
   addressmore          varchar(30),
   topimg               varchar(80),
   seo                  varchar(256),
   primary key (bnid)
);

alter table business comment '商家信息表,存储商家名称和电话等详细信息。';

/*==============================================================*/
/* Table: carquote                                              */
/*==============================================================*/
create table carquote
(
   pdid                 int not null,
   pdbusbnid            int not null,
   bnid                 int,
   pdtitle              varchar(30),
   caraid               int,
   carbid               int,
   carcid               int,
   pdimg                varchar(70),
   pdpricea             float,
   pdpriceb             float,
   pdinfo               varchar(256),
   pdstatus             varchar(2),
   readnum              int,
   primary key (pdid)
);

alter table carquote comment '商家发布的产品信息';

/*==============================================================*/
/* Table: message                                               */
/*==============================================================*/
create table message
(
   msgid                int not null,
   msgbusbnid           int not null,
   bnid                 int,
   msgtime              int,
   msgtext              varchar(256),
   reply                varchar(256),
   rptime               int,
   msgsuatus            varchar(2),
   primary key (msgid)
);

/*==============================================================*/
/* Table: news                                                  */
/*==============================================================*/
create table news
(
   newsid               int not null,
   nbusbnid             int not null,
   bnid                 int,
   newstitle            varchar(20),
   newstag              varchar(20),
   newstype             int,
   ncontent             text,
   newstime             int,
   readnum              int,
   primary key (newsid)
);

alter table carquote add constraint FK_HAS_MANY foreign key (pdbusbnid)
      references business (bnid) on delete restrict on update restrict;

alter table message add constraint FK_HAS_MANY foreign key (msgbusbnid)
      references business (bnid) on delete restrict on update restrict;

alter table news add constraint FK_HAS_MANY foreign key (nbusbnid)
      references business (bnid) on delete restrict on update restrict;




上面三个外键,只能建立第一个,第二个的时候就报错:
[SQL] alter table carquote add constraint FK_HAS_MANY foreign key (pdbusbnid)
  references business (bnid) on delete restrict on update restrict;
受影响的行: 0
时间: 0.110ms

[SQL] 

alter table message add constraint FK_HAS_MANY foreign key (msgbusbnid)
  references business (bnid) on delete restrict on update restrict;
[Err] 1005 - Can't create table 'test.#sql-6d4_17' (errno: 121)

外键名称不一样,但还是无法创建,为何啊,跪求指点,谢谢了。

作者: q309056448   发布时间: 2011-12-28

改个外间名字就可以创建了

mysql> alter table message add constraint FK_HAS_MANY1 foreign key (msgbusbnid) references business (bnid) on delete restrict on update restrict;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0

作者: rucypli   发布时间: 2011-12-28

alter table carquote add constraint FK_HAS_MANY foreign key (pdbusbnid)
  references business (bnid) on delete restrict on update restrict;

alter table message add constraint FK_HAS_MANY foreign key (msgbusbnid)
  references business (bnid) on delete restrict on update restrict;

alter table news add constraint FK_HAS_MANY foreign key (nbusbnid)
  references business (bnid) on delete restrict on update restrict;

名字重复了。

作者: ACMAIN_CHM   发布时间: 2011-12-28