mysql - 我不断收到错误 : There is already an object named fk_driver_id and i'm not sure how to fix this error. 帮助:(

标签 mysql sql foreign-keys syntax-error

If exists (select * from Information_schema.TABLES where 
TABLE_NAME='fd_driver_vehicles') 
Drop table fd_Driver_vehicles 
GO

If exists (select * from Information_schema.TABLES where 
TABLE_NAME='fd_vehicles') 
Drop table fd_vehicles
GO

If exists (select * from Information_schema.TABLES where 
TABLE_NAME='fd_driver_territories') 
Drop table fd_driver_territories
GO

If exists (select * from Information_schema.TABLES where 
TABLE_NAME='fd_sizes') 
Drop table fd_sizes
GO

If exists (select * from Information_schema.TABLES where 
TABLE_NAME='fd_drivers') 
Drop table fd_drivers
GO

create table fd_drivers
(
    driver_id int not null,
    driver_lastname varchar(50) not null,
    driver_firstname varchar(50) not null,
    driver_charge money not null,
    constraint pk_drivers primary key(driver_id),
    constraint ck_driver_charge check (driver_charge >0)
)

create table fd_sizes
(
    size_id char(1) not null,
    size_charge money not null,
    constraint pk_sizes primary key(size_id),
    constraint ck_size_charge check (size_charge >0)
)

create table fd_driver_territories
(
    driver_id int not null,
    driver_territory varchar(20) not null,
    constraint pk_driver_territories 
        primary key(driver_id, driver_territory),
    constraint fk_driver_id foreign key (driver_id)
        references fd_drivers (driver_id)

)

create table fd_vehicles
(
    vehicle_lp varchar(12) not null,
    vehicle_make varchar(50) not null,
    vehicle_model varchar(50) not null,
    vehicle_size char(1) not null,
    constraint pk_vehicles
        primary key(vehicle_lp),
    constraint fk_vehicle_size foreign key (vehicle_size)
        references fd_sizes (size_id)
)

create table fd_driver_vehicles 
(
    driver_id int not null,
    vehicle_lp varchar(12) not null,
    expiration_date datetime not null,
    constraint pk_driver_vehicles
        primary key (driver_id, vehicle_lp),
    constraint fk_driver_id2 foreign key (driver_id)
        references fd_drivers (driver_id),
    constraint fk_vehicle_lp foreign key (vehicle_lp)
        references fd_vehicles (vehicle_lp)
)

最佳答案

您正在添加已存在的外键fk_driver_id,因此会引发错误,请删除第一个条目,然后添加新条目。

或者将其名称更改为其他名称。

关于mysql - 我不断收到错误 : There is already an object named fk_driver_id and i'm not sure how to fix this error. 帮助:(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018880/

相关文章:

sql - 从一个表中选择列并从另一个表中计数

mysql - Laravel 5 迁移 - 外键约束失败

mysql - 有没有更有效的方法来编写这个 MySQL 查询?

php - 如何使用面向对象代码减少 MySQL 查询的数量

mySQL join 引用父代码

php - 分别从两个单独的 MySQL 表中删除信息?

php - SQL首先获取一会儿选择的id,然后从表中获取其他的?

php - 将 SQL 数据保存到 PHP 文件

MySQL 引擎=InnoDB 错误 1005 errno 150

mysql - 什么时候在 MySQL 中使用外键