I have two DB tabels which form a parent child relationship from Planung to Aufgabe:
Planung:
CREATE TABLE `planung` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`Bezeichnung` varchar(255) DEFAULT NULL,
-- lots of ohter columns
PRIMARY KEY (`id`),
) ENGINE=InnoDB
Aufgabe:
CREATE TABLE `aufgabe` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`planung_id` bigint(20) DEFAULT NULL, -- foreign key to Planung.id
`Nummer` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
-- lots of ohter columns
) ENGINE=InnoDB
I’m looking for a query which gives me all Planung.id and Aufgabe.ID for all duplicate Nummer per Planung. Or in other words: For every Planung Aufgabe.Nummer must be unique, I want to check whehter this is really the case in my DB (I know it isn’t).
This query gives all planung_id which has duplicate Nummer and displays them like:
Which means, for Planung 1, there exists three Aufgabe with Nummer 1, and they are 2,5 and 8.
Edit: Shamelessly stolen from @dgw‘s comment:
Once you have run this query, and corrected all the duplicates. Add a unique index to
aufgabe {planung_id, Nummer}to ensure that the database maintains this constraint: