用带有外键的sql语句创建mysql表出错

lshoo 2005-12-06 09:42:34
用带外键的sql语句:
---------------------------
create table voterregistration (
ssn integer not null,
firstname varchar(32) not null,
lastname varchar(32) not null,
countynumber integer not null,
primary key(SSN),
foreign key(countynumber) references county);
--------------------------
创建voterregistration表时,用命令行执行时提示:
------------------------
ERROR 1005 (HY000): Can't create table '.\test\voterregstration.frm' (errno: 150);
-----------------
用Mysql-Front3.2执行时提示:
-------------------------
SQL执行错误#1005.从数据库的响应:
Can't create table '.\test\voterregstration.frm'(errno: 150)。
--------------------------
运行环境:windows xp sp2, MySQL 4.1.15.
另外试过多次,凡是用有定义外键的sql语句来创建表都会失败,不知道是原因?
另外:county表已经创建成功,附county的sql语句:
create table county (
countynumber integer not null auto_increment,
countyname varchar(32),
state varchar(32),
primary key(countynumber));
...全文
664 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
SOMAN0324 2006-06-12
  • 打赏
  • 举报
回复
原来INDEX那么伟大。。。哈哈
rardge 2005-12-08
  • 打赏
  • 举报
回复
我说乱了,毕竟不是写书的料啊,呵呵。
还是给你一个手册上的例子,看了就会明白的。

CREATE TABLE parent(id INT NOT NULL,
PRIMARY KEY (id)
) TYPE=INNODB;

CREATE TABLE child(id INT,
parent_id INT,
INDEX par_ind (parent_id), <----对外键字段进行索引
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) TYPE=INNODB;

mysql 论坛我也不知道哪个好点,我一般都是看手册,然后google搜索。
www.chinaunix.com 可以去看看
lshoo 2005-12-08
  • 打赏
  • 举报
回复
foreign key(countynumber) references county <---这个地方,括号里面应该是 index 的名字,而不是字段名!
-----------------
这一句不是很明白,你说的index的名字是指哪个表的索引名字,字段名又是哪个表的字段名?
lshoo 2005-12-08
  • 打赏
  • 举报
回复
谢谢!用修正后的SQL语句执行就没问题,非常感谢!知道哪里有mysql的论坛吗?
rardge 2005-12-08
  • 打赏
  • 举报
回复
哦,我没有用到4.1.15,也没有默认配置为InnoDB,所以有点奇怪。
仔细看了一下,你的SQL语句是错的。
create table voterregistration (
ssn integer not null,
firstname varchar(32) not null,
lastname varchar(32) not null,
countynumber integer not null,
primary key(SSN),
foreign key(countynumber) references county <---这个地方,括号里面应该是 index 的名字,而不是字段名!
)
type=InnoDB;

用这个:
create table voterregistration (
ssn integer not null primary key,
firstname varchar(32) not null,
lastname varchar(32) not null,
countynumber integer not null,
index (countynumber),
foreign key(countynumber) references county(countynumber)
)
type=InnoDB;

我这里试验过通过了。
lshoo 2005-12-08
  • 打赏
  • 举报
回复
MySQL4.1.15安装后,配置文件中默认的数据库类型是InnoDB。如果有外键,就是加了type = InnoDB还是报一样的错,不知道你试过没有?
create table voterregistration (
ssn integer not null,
firstname varchar(32) not null,
lastname varchar(32) not null,
countynumber integer not null,
primary key(SSN),
foreign key(countynumber) references county)
type=InnoDB;
SQL语句应该没错啊!
rardge 2005-12-07
  • 打赏
  • 举报
回复
会自动生成InnoDB类型
--------------------
你在配置文件中设置的么?

create table county (
countynumber integer not null auto_increment,
countyname varchar(32),
state varchar(32),
primary key(countynumber)
)type=innodb;
------------
加上这个

我只有英文手册。
lshoo 2005-12-07
  • 打赏
  • 举报
回复
我创建county表的语句也没有指定类型,但它会自动生成InnoDB类型。
如何要指定InnoDB,如何写?请问你有mysql的中文手册吗?
rardge 2005-12-07
  • 打赏
  • 举报
回复
mysql会自动处理InnoDB类型的,创建county表的语句就是InnoDB类型的。
----------------------------------------------------------------
什么意思啊?
lshoo 2005-12-07
  • 打赏
  • 举报
回复
mysql会自动处理InnoDB类型的,创建county表的语句就是InnoDB类型的。
还请问一下,谁有最新的mysql的中文手册?
rardge 2005-12-06
  • 打赏
  • 举报
回复
你的表类型???
看手册,foreign key 好像只有innodb表类型才支持吧。

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧