简体   繁体   中英

primary, candidate and superkey

I have the following table and I wanted to know how to know to get the primary key, superkey and candidate key.

I know that candidate key is the minimum of superkey.

Would this be correct primary key PlaceId CountyId , candidate key PlaceId, CountyId, Date and superkey being PlaceId, CountyId, Date, Attendees ?

CREATE TABLE IF NOT EXISTS `visits` (
  `PlaceId` varchar(45) DEFAULT NULL,
  `CountyId` varchar(45) DEFAULT NULL,
  `Date` date NOT NULL,
  `Attendees` varchar(45) DEFAULT NULL,

INSERT INTO `visits` (`PlaceId`, `CountyId`, `Date`, `Attendees`) VALUES
('Bangor', 'Gwynedd', '2012-05-03', '34'),
('Bangor', 'Gwynedd', '2012-05-04', '24'),
('Rhyl', 'Denbighshire', '2012-05-06', '14');

Based on the sample data you provided, and on my understanding of what place , county , date , and attendee mean, the only candidate key is {PlaceId, CountyId, Date}.

Since there's only one candidate key, that candidate key must also be the primary key.

The set of all attributes is a trivial superkey. So one superkey is {PlaceId, CountyId, Date, Attendees}. The other is {PlaceId, CountyId, Date}.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM