mysql创建主键时错误:1005 - Can't create table 'xx' (errno: 150)

mysql创建主键时错误:1005 - Can’t create table ‘xx’ (errno: 150)

该错误大概有以下原因,需要逐一对照检查排除:

  • 当前表关联键与外键的类型和长度不符,两个键需要同类型、同长度,包括SIGNED和UNSIGNED类型都要一致
  • 其中一个键没有设置为索引或不是主键
  • 外键名称与已存在的键名重复
  • 其中一个或者两个表都是MyISAM类型,请将它们改换为InnoDB类型
  • 两个键的NULL和NOT NULL类型不一致
  • 两张表的字符集不同
  • 外键设置了默认值为0
  • 其中一个键是关联索引的一部分,请将其设置为独立索引
  • Alter语句有语法错误
  • 外键名称超过了64个字符

求助:1005:Can't create table 'vote.voteradd' (errno: 150)外键连接的表创建错误

09-16

其实就是为了图中的两个表,创建外键连接。rnhttp://img165.poco.cn/mypoco/myphoto/20110916/12/5636976620110916122927018.pngrnrn[code=PHP]rn"; rn rn rn rnrn class ctb extends concls2rn rn private $dbname;rn private $tbname;rn private $tbname2;rn public function __construct($dbname1,$tbname1,$tbname21)rn rn $this->dbname = $dbname1;rn $this->tbname = $tbname1;rn $this->tbname2 = $tbname21;rn var_dump($this->dbname);rn var_dump($this->tbname);rn var_dump($this->tbname2);rn rn rn public function crtdb()rn rn $mysql_command1="create database ".$this->dbname;rn $result1=mysql_query($mysql_command1);rn if($result1)rn echo "database created successfully!"." "; elsern echo "database creation failed."." ";rnrn rn public function slctdb()rn rn $dbopen2=mysql_select_db($this->dbname); if($dbopen2)rn echo "certain database selected "." "; rn else rn echo "failed to select certain database "." ";rn rn rn rn public function crttable()rn rn $mysql_command2 ="create table ".$this->tbname."(id int not null auto_increment primary key ,inf0 tinyint(1),inf1 tinyint(1),inf2 tinyint(1), inf3 tinyint(1),inf4 tinyint(1),inf5 tinyint(1),inf6 tinyint(1),name char(15) not null) ENGINE = INNODB"; rn $result2=mysql_query($mysql_command2) or die ("failed to create table:".mysql_errno().":".mysql_error()); rn if($result2)rn echo "table created successfully!"." "; elsern echo "table creation failed."." ";rn rn rn public function crttable1()rn rn $mysql_command2 ="create table ".$this->tbname2." (nameb char(15) NOT NULL,tel int(12) NOT NULL,PRIMARY KEY (nameb,tel),FOREIGN KEY (nameb) REFERENCES ".$this->tbname." (name) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = INNODB"; rn $result2=mysql_query($mysql_command2) or die ("failed to create table:".mysql_errno().":".mysql_error()); rn if($result2)rn echo "table created successfully!"." "; elsern echo "table creation failed."." ";rn rn rn rn rnrn $newtbl= new ctb("vote","voteinfo","voteradd");rn $newtbl->conn1();rn $newtbl->crtdb();rn $newtbl->slctdb();rn $newtbl->crttable();rn $newtbl->crttable1();rnrn?>rn[/code]rnrnrnrndatabase created successfully!rncertain database selected rntable created successfully!rnfailed to create table:1005:Can't create table 'vote.voteradd' (errno: 150)rnrn问题出在创建第二个表的方法,crttable1()里面。这个创建外键连接的第二个表的mysql命令好像总是不对,求高手指正!rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭