0
create table bankdb.customer(
    customer_name varchar(45) not null,
    social_security int not null,
    customer_street varchar(45),
    customer_city varchar(45),
primary key(`social_security`)
)engine=InnoDB;

create table bankdb.branch(
    branch_name varchar(45) not null,
    branch_city varchar(45),
    assets int,
primary key (`branch_name`)
)engine=InnoDB;

create table bankdb.account(
    branch_name varchar(45),
    account_number varchar(45) not null,
    balance int,
primary key (`account_number`),
constraint fk_acount_branch
    foreign key (`branch_name`)
    references bankdb.branch(`branch_name`)
)engine=InnoDB;  

create table bankdb.depositor(
    customer_name varchar(45) not null,
    account_number varchar(45) not null,
primary key (`customer_name`, `account_number`),
constraint fk_depositor_customer
    foreign key(`customer_name`)
    references bankdb.customer(`customer_name`),
constraint fk_depositor_account
    foreign key(`account_number`)
    references bankdb.account(`account_number`)
)engine=InnoDB;

那是我的 sql 代码...我收到错误无法为表 bankdb.depositor 创建表...我的外键有什么问题吗?有什么线索吗?

4

1 回答 1

2

编辑,根据您的新脚本,以下似乎在 SQL Fiddle 中工作。我不得不添加索引:

create table bankdb.customer(
    customer_name varchar(45) not null,
    social_security int not null,
    customer_street varchar(45),
    customer_city varchar(45),
  primary key(`social_security`),
  INDEX (customer_name)
)engine=InnoDB;

create table bankdb.branch(
    branch_name varchar(45) not null,
    branch_city varchar(45),
    assets int,
  primary key (`branch_name`)
)engine=InnoDB;

create table bankdb.account(
    branch_name varchar(45),
    account_number varchar(45) not null,
    balance int,
  primary key (`account_number`),
  INDEX (account_number),
  constraint fk_acount_branch
      foreign key (`branch_name`)
      references bankdb.branch(`branch_name`)
)engine=InnoDB;  

create table bankdb.depositor(
  customer_name varchar(45) not null,
  account_number varchar(45) not null,
  primary key (`customer_name`, `account_number`),
  INDEX (customer_name),  
  INDEX (account_number),
  constraint fk_depositor_customer
      foreign key(`customer_name`)
      references bankdb.customer(`customer_name`),
  constraint fk_depositor_account
      foreign key(`account_number`)
      references bankdb.account(`account_number`)
)engine=InnoDB;

请参阅带有演示的 SQL Fiddle

如果这是您的完整脚本,那么您的问题在于为account.

您正在尝试在branch似乎不存在的表上创建一个外键:

create table bankdb.account(
    branch_name varchar(45),
    account_number varchar(45) not null,
    balance int,
primary key (`account_number`),
constraint fk_acount_branch
    foreign key (`branch_name`)
    references bankdb.branch(`branch_name`)  -- does this exist
)engine=InnoDB;  

于 2013-02-05T22:20:03.113 回答