2014-04-11 52 views
0
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) 
) 

回答

0

你加入外鍵fk_driver_id已經存在,因此拋出錯誤,刪除第一項,然後添加新的。

或改變其名稱以一些其他的名字。

+0

你能詳細說明一下嗎? – user3358632

+0

你在表或數據庫中已經有一個外鍵了,所以它說它已經存在 –

相關問題