1

您好,当新用户在我的 php 站点中注册时,我正在尝试创建一些表,并且我正在尝试执行事务中的最后一个查询,它给了我以下错误:#1005 - Can't create table 'user_39.records' (errno: 150) 有人可以指出我做错了什么? 我对外键不太熟悉这是我第一次使用它们。谢谢你。

数据库表:

private function createDriversTable($database) {
    $query = "CREATE TABLE IF NOT EXISTS $database.drivers (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `surname` varchar(255) NOT NULL,
      `car_type` varchar(255) NOT NULL,
      `circulation_num` varchar(255) NOT NULL,
      `special_card_num` varchar(255) NOT NULL,
      `special_card_num_exp` date NOT NULL,
      `drivers_license_num` varchar(255) NOT NULL,
      `drivers_license_exp` date NOT NULL,
      `id_num` varchar(255) NOT NULL,
      `vat` varchar(255) NOT NULL,
      `address` varchar(255) NOT NULL,
      `mobile` bigint(20) NOT NULL,
      `radiotaxi_code` bigint(20) NOT NULL,
      `languages` varchar(255) NOT NULL,
      `email` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Driver records' AUTO_INCREMENT=1";

    return $query;
}

private function createClientsTable($database) {
    $query = "CREATE TABLE IF NOT EXISTS $database.clients (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `address` varchar(255) NOT NULL,
      `region` varchar(255) NOT NULL,
      `vat` varchar(255) NOT NULL COMMENT 'AFM',
      `tax_office` varchar(255) NOT NULL COMMENT 'DOY',
      `phone` bigint(20) NOT NULL,
      `mobile` bigint(20) NOT NULL,
      `email` varchar(255) NOT NULL,
      `notes` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Client records' AUTO_INCREMENT=1"; 

    return $query;
}

private function createCarsTable($database) {
    $query = "CREATE TABLE IF NOT EXISTS $database.cars (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `oil_change` date NOT NULL,
      `oil_filter` date NOT NULL,
      `petrol_filter` date NOT NULL,
      `air_filter` date NOT NULL,
      `cabin_filter` date NOT NULL,
      `carbon_filter` date NOT NULL,
      `front_breaks` date NOT NULL,
      `rear_breaks` date NOT NULL,
      `front_disc_breaks` date NOT NULL,
      `rear_disc_breaks` date NOT NULL,
      `break_fluids` date NOT NULL,
      `gear_oil` date NOT NULL,
      `gear_controller` date NOT NULL,
      `gear_filter` date NOT NULL,
      `tires` date NOT NULL,
      `kteo` date NOT NULL,
      `freon` date NOT NULL,
      `freon_filter` date NOT NULL,
      `steering_fluids` date NOT NULL,
      `axle_oil` date NOT NULL,
      `notes` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Car records' AUTO_INCREMENT=1";

    return $query;
}

private function createRecordsTable($database) {
    $query = "CREATE TABLE IF NOT EXISTS $database.records (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `date` date NOT NULL,
      `time` time NOT NULL,
      `driver_id` bigint(20) NOT NULL,
      `client_id` bigint(20) NOT NULL,
      `car_id` bigint(20) NOT NULL,
      `room_num` int(11) NOT NULL,
      `departure` text NOT NULL,
      `course` text NOT NULL,
      `destination` text NOT NULL,
      `arrives_from` text NOT NULL,
      `arrival_info` text NOT NULL,
      `route_type` bigint(20) NOT NULL,
      `payment_method` int(11) NOT NULL,
      `total_cost` float NOT NULL,
      `expenses` float NOT NULL,
      `profit` float NOT NULL,
      `notes` text NOT NULL,
      PRIMARY KEY (`id`),
      FOREIGN KEY (`driver_id`) REFERENCES $database.drivers(`id`),
      FOREIGN KEY (`client_id`) REFERENCES $database.clients(`id`),
      FOREIGN KEY (`car_id`) REFERENCES $database.cars(`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

    return $query;      
}
4

3 回答 3

1

您在结果表中缺少 driver_id 和 client_id 的无符号类型

于 2013-10-17T10:03:02.210 回答
0

首先,您的表格未标准化。尝试快速搜索“数据库规范化”:它将极大地帮助您。

此外,就目前而言,我们无法回答您的问题,因为我们缺少一些关键信息:相关的表结构。

现在,看看以下几点:

  1. user_39.drivers( id) 是 BIGINT 类型吗?
  2. user_39.clients( id) 是 BIGINT 类型吗?
  3. user_39.cars( id) 是 BIGINT 类型吗?

我猜其中一个不是 BIGINT,并且数据类型差异阻止您创建外键

于 2013-10-17T09:44:29.223 回答
0

为了成为另一个表中的FOREIGN KEY,您必须在另一个表上创建一个索引。并且为了能够可靠地引用特定行,因此它应该是唯一索引。否则,如果您有重复的值,引用表将无法识别它引用的行。即使 InnoDB 允许您在非唯一索引上创建关系,它也可能不是您正在寻找的行为

于 2013-10-17T09:42:17.577 回答