【问题标题】:MySQL: Can't create table with FK getting (errno 150)MySQL:无法使用 FK 获取创建表(errno 150)
【发布时间】:2015-01-02 11:57:04
【问题描述】:

得到 ERROR 1005 (HY000): 无法创建表 'at.memdetails' (errno: 150)

create table members 
(UserId varchar(5) primary key, 
Name varchar(30) not null, 
UserName varchar(15) not null unique, 
pwd varchar(20) not null);


create table MemDetails (user_id varchar(5) primary key, 
mobileNo bigint not null unique, 
Email varchar(20) not null, 
City varchar(20) not null, 
State varchar(20) not null, 
constraint FKey1 foreign key (user_id) 
references members(user_id) 
on delete cascade);

【问题讨论】:

  • 两个键都是主自动增量键。这是不可能的

标签: mysql foreign-keys


【解决方案1】:

使用下面的代码创建第二张表:

create table MemDetails (user_id varchar (5) primary key)
mobileNo BIGINT NOT NULL UNIQUE, 
Email VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State VARCHAR(20) NOT NULL, 
CONSTRAINT FKey1 FOREIGN KEY (user_id) 
REFERENCES members(UserId) ON DELETE CASCADE);

【讨论】:

    猜你喜欢
    • 2014-04-05
    • 2010-12-17
    • 2016-06-08
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2015-06-20
    相关资源
    最近更新 更多