create table customers
(
cid int unsigned not null auto_increment primary key,
name char(50) not null,
city char(100) not null
);create table orders
(
oid int unsigned not null auto_increment primary key,
cid int unsigned not null index idx_cid(cid),
amount float(4,2),
foreign key(cid),
references customers(cid),
on update cascade,
on delete cascade
);

解决方案 »

  1.   

    再发一次,应该是这样create table customers 

    cid int unsigned not null auto_increment primary key, 
    name char(50) not null, 
    city char(100) not null 
    ); create table orders 

    oid int unsigned not null auto_increment primary key, 
    cid int unsigned not null index idx_cid(cid), 
    amount float(4,2), 
    foreign key(cid) references customers(cid), 
    on update cascade, 
    on delete cascade 
    ); 
      

  2.   


    create table customers
    (
    cid int unsigned not null auto_increment primary key,
    `name` char(50) not null,
    city char(100) not null
    ) engine innodb;create table orders
    (
    oid int unsigned not null auto_increment primary key,
    cid int unsigned not null,
    index idx_cid(cid),
    amount float(4,2),
    foreign key(cid) references customers(cid) on update cascade on delete cascade
    ) engine innodb; 
      

  3.   

    太感谢楼上这位大哥了,问题已解决非,原来mysql默认引擎是不支持外键的,要innodb下面修改了一下
    create table customers
    (
    cid int unsigned not null auto_increment primary key,
    `name` char(50) not null,
    city char(100) not null
    )Type= INNODB CHARACTER SET utf8;create table orders
    (
    oid int unsigned not null auto_increment primary key,
    cid int unsigned not null,
    index idx_cid(cid),
    amount float(4,2),
    foreign key(cid) references customers(cid) on update cascade on delete cascade
    )Type= INNODB CHARACTER SET utf8;