ERROR 1075只能有一个自动列,必须将其定义为键。帮助我确定如何保留ReportID的外键

问题描述 投票:0回答:1

为什么这不起作用?我需要第二个auto_increment,因为我正在将它用作另一个表中的主键。我想拥有它,这样犯罪行为就可以直接与它们在

中的报告相关
Create table ReportsFiled (
    ReportID INT(10) NOT NULL AUTO_INCREMENT,
    DateandTime DATETIME NOT NULL,
    VehicleID VARCHAR(10) NOT NULL,
    LicenseNumber VARCHAR(16) NOT NULL,
    OffenceCommitted VARCHAR(255) NOT NULL,
    ReportStatement VARCHAR(255) NOT NULL,
    constraint pkReportsFiled primary key (ReportID, VehicleID),
    constraint fkReportsFiledVehID foreign key (VehicleID) references Vehicles(VehicleID),
    constraint fkReportsFiledLicNum foreign key (LicenseNumber) references People(LicenseNumber)
    ) ENGINE=InnoDB ; 

Insert into ReportsFiled 
(ReportID, DateandTime, VehicleID, LicenseNumber, OffenceCommitted, ReportStatement)
Values 
(NULL, '2019-01-20 10:30:00', 'FD13 5TY', 'WRIGY96543867FD4', 'Moderate Speeding', 'Speeding in a residential area, given points and fine appropriate to the nature of the offence'),
(NULL, '2019-03-12 16:30:00', 'FT17 8HU', 'BARTO87252867BO4', 'Dangerous driving and Moderate Speeding', 'Driving recklessly in a residential area and driving above the speed limit, 3 points and £350'),
(NULL, '2019-05-28 12:23:00', 'SJ12 J8S', 'SMITH54398866QW4', 'Dangerous driving', 'Taking a corner too fast with no indication, 3 points given but no fine issued'),
(NULL, '2019-01-01 11:21:00', 'FT17 8HU', 'BARTO87252867BO4', 'Moderate Speeding', 'Driving at 42mph in a 30mph zone, 3 points and a £200 fine'),
(NULL, '2019-03-30 14:26:00', 'QW15 X4U', 'SPENC32975368DW8', 'Driving dangerously under the influence of alcohol', 'Licence ban and £2,500 fine'),
(NULL, '2019-08-21 22:14:00', 'FT17 8HU', 'BARTO87252867BO4', 'Driving while using a mobile device', 'Driving using a mobile device, 6 points and £500 fine') ;

Create table Offences (
    OffenceID INT(10) NOT NULL AUTO_INCREMENT,
    OffenceStatement VARCHAR(255) NOT NULL, 
    MaximumFine VARCHAR(100),
    ReportID INT(10) NOT NULL AUTO_INCREMENT,
    constraint pkOffences primary key (OffenceID, ReportID),
    constraint fkOffencesReID foreign key (ReportID) references ReportsFiled(ReportID)
    ) ENGINE=InnoDB ;

Insert into Offences
(OffenseID, OffenceStatement, MaximumFine, ReportID)
Values
(NULL, 'Moderate Speeding', '3-6 points and £0-£200 fine', NULL),
(NULL, 'Driving under the influence of alcohol', 'Licence ban and £2,500 fine', NULL),
(NULL, 'Extreme Speeding', 'Licence ban and £2,000 fine', NULL),
(NULL, 'Dangerous driving', '3 points and £0-£500', NULL),
(NULL, 'Driving while using a mobile device', '6 points and £500 fine', NULL) ; 
mysql sql database create-table
1个回答
0
投票

我认为您在这里混合了主键外键的概念。

auto_incremented主键为您提供了每个记录的唯一整数id。

外键定义了一种关系,其中一个表的引用列中的每个值必须出现在另一个表的引用列中。外键列是not自动递增的。引用的列可能是。

为了使您的设计有意义(并被MySQL引擎接受),您需要将每个表的自动递增列设为主键,并定义适当的外键。

考虑:

create table ReportsFiled (
    ReportID int(10) not null primary key auto_increment,       -- this is the primary key
    DateandTime datetime not null,
    VehicleID varchar(10) not null,
    LicenseNumber varchar(16) not null,
    OffenceCommitted varchar(255) not null,
    ReportStatement varchar(255) not null,
    constraint fkReportsFiledVehID                              -- foreign key to the Vehicle table
        foreign key (VehicleID) references Vehicles(VehicleID),  
    constraint fkReportsFiledLicNum                             -- foreign key to People table
        foreign key (LicenseNumber) references People(LicenseNumber)
) engine = InnoDB; 


create table Offences (
    OffenceID int(10) not null auto_increment primary key,
    OffenceStatement varchar(255) not null, 
    MaximumFine varchar(100),
    ReportID int(10) not null auto_increment,
    constraint fkOffencesReID                                   -- foreign key to People table
        foreign key (ReportID) references ReportsFiled(ReportID)
) engine = InnoDB ;

注意:

  1. 我对People(LicenseNumber)的外键表示怀疑:LicenseNumber是表People的主键吗?如果不是,则应意识到必须至少对该列进行索引(无论是单独索引还是在复合索引的第一位置)。或者只是考虑参考People的主键,这似乎是您的意图:在需要时,您始终可以通过连接检索许可证号。

  2. 引用和被引用列必须具有相同的数据类型

© www.soinside.com 2019 - 2024. All rights reserved.