我有一个Person
具有以下属性的表:
我想实现一个名为的表,该表Car
将保存有关Person
驱动的汽车的信息。我想以一种方式实现它,如果多个用户驾驶“Toyota Corolla”,那么“Toyota Corolla”应该只作为字符串存储一次。另外,我假设不是每个人都只拥有一辆车,这也是公平的。条目将定期记录,但不同的Car
记录可能仅达到 5000 条。到目前为止,我已经完成了以下操作,但是如果多个用户拥有同一辆车,我该如何实现一个让我保存一次的属性。ModelNames
CREATE TABLE `Car` (
`idCar` MEDIUMINT UNSIGNED AUTO_INCREMENT ,
`ModelName` varchar(80),
`idPerson` INT NOT NULL,
PRIMARY KEY (`idCar`) ,
FOREIGN KEY (`idPerson`) REFERENCES `Person` (`idPerson`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE=InnoDB;
UPDATE Car, Person
Set Car.idPerson = Person.idPerson
WHERE Car.idPerson = Person.idPerson;
INSERT INTO Car VALUES (1, 'Toyota Corolla', 1);
INSERT INTO Car VALUES (2, 'Mazda 6', 1);
INSERT INTO Car VALUES (3, 'Toyota Corolla', 2);
INSERT INTO Car VALUES (4, 'Pulsar GTI', 3);
SELECT DISTINCT Car.idCar ,Car.ModelName, Person.FullName, Person.idPerson
FROM Person, Car
WHERE Car.idPerson = Person.idPerson
Order by Car.idCar;