Foreign key constraint fails when I want delete table - mysql

When I am trying
drop tables users;
It shows error: Cannot delete or update a parent row: a foreign key constraint fails
Here are my tables:
CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL,
`users_id` int(11) NOT NULL,
`capacity` int(64) NOT NULL,
`event_date` datetime NOT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_czech_ci,
`last_edit` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`),
CONSTRAINT `events_ibfk_5` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`role` enum('admin','member','guest','registered') NOT NULL DEFAULT 'registered',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users_data` (
`id` int(120) NOT NULL AUTO_INCREMENT,
`users_id` int(11) DEFAULT NULL,
`name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`phone_number` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`),
CONSTRAINT `users_data_ibfk_5` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_matches` (
`user_id` int(120) NOT NULL,
`match_id` int(11) NOT NULL,
KEY `user_id` (`user_id`),
KEY `match_id` (`match_id`),
CONSTRAINT `user_matches_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `users_data` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `user_matches_ibfk_5` FOREIGN KEY (`match_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think I did wrong foreign key but I am not sure how to change it correctly. Thank you for your any advice.

When you submit drop table users MySQL checks for referential integrity constraints violation when the deletion is about to be committed. This check fails because you have defined the following constraint of events:
CONSTRAINTevents_ibfk_5FOREIGN KEY (users_id) REFERENCESusers(id) ON DELETE CASCADE ON UPDATE CASCADE
and at the same time on the events table the users_id field cannot be NULL. Therefore, upon dropping the table, the users_id is set to NULL which fails during commit. One possible way to work around this problem is to remove the NOT NULL constraint on the users_id field on the events table.

Related

(errno: 150) Error Driving Me Nuts

I might be missing something here, I have given up after 2 weeks of trying, Can anyone maybe see something blindingly obvious why this will not import please ?
I have tried to check everything that I can think of but when I try to insert it I just get the 150 error message.
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP TABLE IF EXISTS `classifieds_announcements`;
CREATE TABLE `classifieds_announcements` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`category` int(11) NOT NULL,
`title` tinytext NOT NULL,
`text` text NOT NULL,
`date` int(11) NOT NULL COMMENT 'timestamp',
`expire` int(11) NOT NULL COMMENT 'timestamp',
`photo` varchar(255) NOT NULL,
`approved` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `category` (`category`),
CONSTRAINT `classifieds_announcements_ibfk_1` FOREIGN KEY (`author`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_announcements_ibfk_2` FOREIGN KEY (`category`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_categories`;
CREATE TABLE `classifieds_categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL,
`parent` int(11) DEFAULT NULL,
`allow_posting` int(11) DEFAULT '1' COMMENT 'may be 0 or 1 only if parent=0, else will be 1',
`order` int(11) DEFAULT '50',
PRIMARY KEY (`id`),
KEY `parent` (`parent`),
CONSTRAINT `classifieds_categories_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_config`;
CREATE TABLE `classifieds_config` (
`key` varchar(255) NOT NULL,
`value` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `classifieds_config` (`key`, `value`) VALUES
('allow_emoticons', '1'),
('post_on_user_wall', '1'),
('approve_new_posts', '1'),
('index_columns', '4'),
('time_between_announces', '300'),
('time_between_reviews', '90');
DROP TABLE IF EXISTS `classifieds_inputs`;
CREATE TABLE `classifieds_inputs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` int(11) NOT NULL,
`name` text NOT NULL,
`type` varchar(255) NOT NULL,
`options` text,
`required` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `category` (`category`),
CONSTRAINT `classifieds_inputs_ibfk_1` FOREIGN KEY (`category`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_inputs_answers`;
CREATE TABLE `classifieds_inputs_answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`input` int(11) NOT NULL,
`announce` int(11) NOT NULL,
`answer` text NOT NULL,
PRIMARY KEY (`id`),
KEY `input` (`input`),
KEY `announce` (`announce`),
CONSTRAINT `classifieds_inputs_answers_ibfk_1` FOREIGN KEY (`input`) REFERENCES `classifieds_inputs` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_inputs_answers_ibfk_2` FOREIGN KEY (`input`) REFERENCES `classifieds_inputs` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_inputs_answers_ibfk_3` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_reports`;
CREATE TABLE `classifieds_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reported_by` int(11) NOT NULL,
`reported_announce` int(11) NOT NULL,
`read` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `reported_by` (`reported_by`),
KEY `reported_announce` (`reported_announce`),
CONSTRAINT `classifieds_reports_ibfk_1` FOREIGN KEY (`reported_by`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reports_ibfk_2` FOREIGN KEY (`reported_announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_reviews`;
CREATE TABLE `classifieds_reviews` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`announce` int(11) NOT NULL,
`score` tinyint(4) NOT NULL,
`user` int(11) NOT NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`),
KEY `announce` (`announce`),
KEY `user` (`user`),
CONSTRAINT `classifieds_reviews_ibfk_1` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_2` FOREIGN KEY (`user`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_3` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_4` FOREIGN KEY (`user`) REFERENCES `users` (`idu`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

optimize slow sql query with 23 tables

I triedy to optimize my SQL Query and my SQL-Server (mariaDB 10.1.10 from xampp 5.6.19) for three days but I have to wait for the server response for more than a minute. Sometimes I'll get no answer!
There are two queries, I've a problem with. Today I rewrite the first query from SQL-87-Syntax to SQL-92-Syntax, but there was no speedup. I've read that before.
I read also, that indexes could speedup my query dramatically but all my joins are on keys. Not sure if I could optimize anything at my current database although?
Here is my first query:
SELECT rv.id AS rahmenID, vls.id AS vlsID, vls.leistungsempfanger AS leistungsempfanger, vls.objektbezeichnung AS objektbezeichnung,
str.bezeichnung AS strName, adr.hausnummer AS hausnummer, plz.bezeichnung AS plz, ort.bezeichnung AS ort, land.bezeichnung AS land,
produkt.bezeichnung AS produkt, vls.vorjahresverbrauch AS vorjahresverbrauch, vls.lieferbeginn AS lieferbeginn, vls.lieferende AS lieferende,
vls.kundennummer_evu AS vertragskonto, anetz.bezeichnung AS ausspeisenetz, mgebiet.bezeichnung AS marktgebiet, qualitat.bezeichnung AS qualitat,
lastprofil.bezeichnung AS lastprofil, unVor.name AS vorlieferant, vls.vorlieferant_kundennummer AS vorlieferantKdNr, vls.zahlernummer AS zahlernummer,
ls.zahlpunkt AS zahlpunkt, vls.jahreshochstleistung AS jahreshochstleistung, vls.stichtag_abrechnung AS abrechnungsdatum,
kennzeichen.bezeichnung AS vertragskennzeichen, kalkArt.bezeichnung AS kalkulationsart, maKalk.vorname AS kalkVorname, maKalk.nachname AS kalkNachname,
vls.kalkulationsdatum AS kalkDatum, pb.jahr AS pbJahr, pb.kalenderwoche AS pbKW, maDist.vorname AS distVorname, maDist.nachname AS distNachname,
pbt.nne_mess_ka AS nneMessKa, pbt.regelenergie AS regelenergie, pbt.energiesteuer AS energiesteuer, pbt.erdgaspreis AS erdgaspreis,
pbt.rohmarge AS rohmarge, pbt.marge_pulsar AS marge_pulsar, pbt.arbeitspreis AS arbeitspreis, pbt.nv_arbeitspreis AS nv_arbeitspreis,
pbt.nv_grundpreis AS nv_grundpreis, vStatus.bezeichnung AS vertragsstatus
FROM vertrag__rahmen rv
INNER JOIN vertrag__lieferstelle__gas vls ON rv.id = vls.rahmenID
INNER JOIN lieferstelle__gas ls ON vls.lieferstelle_gasID = ls.id
INNER JOIN address adr ON ls.addressID = adr.id
INNER JOIN address__strasse str ON adr.strasse = str.id
INNER JOIN address__plz plz ON adr.postleitzahl = plz.id
INNER JOIN address__ort ort ON adr.ort = ort.id
INNER JOIN address__land land ON adr.land = land.id
INNER JOIN attribut__produkt produkt ON rv.produktID = produkt.id
INNER JOIN attribut__gas__ausspeisenetz anetz ON ls.ausspeisenetzID = anetz.id
INNER JOIN attribut__gas__marktgebiet mgebiet ON ls.marktgebietID = mgebiet.id
INNER JOIN attribut__gas__qualitat qualitat ON ls.qualitatID = qualitat.id
INNER JOIN attribut__gas__lastprofil lastprofil ON ls.lastprofilID = lastprofil.id
INNER JOIN unternehmen__evu evuVor ON vls.vorlieferant = evuVor.id
INNER JOIN unternehmen unVor ON evuVor.unternehmenID = unVor.id
INNER JOIN attribut__vertrag__kennzeichen kennzeichen ON vls.vertrag_kennzeichenID = kennzeichen.id
INNER JOIN attribut__vertrag__kalkulationsart kalkArt ON vls.vertrag_kalkulationsartID = kalkArt.id
INNER JOIN mitarbeiter maKalk ON vls.kalkulator = maKalk.id
INNER JOIN preisblatt__gas pb ON vls.preisblattID = pb.id
INNER JOIN mitarbeiter maDist ON vls.distributor = maDist.id
INNER JOIN vertrag__lieferstelle__gas__preisbestandteile pbt ON vls.id = pbt.vlsGasID
INNER JOIN vertrag__lieferstelle__gas__status vlsStatus ON vls.id = vlsStatus.vlsGasID
INNER JOIN attribut__vertrag__status vStatus ON vlsStatus.statusID = vStatus.id
WHERE rv.id=11925 AND vlsStatus.id = (SELECT max(tmp.id) FROM vertrag__lieferstelle__gas__status tmp WHERE tmp.vlsGasID=vls.id)
ORDER BY vls.lieferstelle_gasID, vls.lieferbeginn
If it would be possible, I would give you the EXPLAIN of this query but I'll get no answer from my database! :(
My database:
-- phpMyAdmin SQL Dump
-- version 4.6.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Erstellungszeit: 02. Aug 2016 um 18:38
-- Server-Version: 10.1.10-MariaDB-log
-- PHP-Version: 5.6.19
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
CREATE TABLE `address` (
`id` int(11) NOT NULL,
`strasse` int(11) NOT NULL,
`hausnummer` varchar(10) NOT NULL,
`postleitzahl` int(11) NOT NULL,
`ort` int(11) NOT NULL,
`land` int(11) NOT NULL,
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL,
`aktualisiert` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`aktualisiert_durch` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `address__ort` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `address__plz` (
`id` int(11) NOT NULL,
`bezeichnung` char(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
CREATE TABLE `address__strasse` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__gas__ausspeisenetz` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(250) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__gas__lastprofil` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(6) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__gas__marktgebiet` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(20) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__gas__qualitat` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(50) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__produkt` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(50) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__vertrag__kalkulationsart` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(250) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__vertrag__kennzeichen` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(30) NOT NULL,
`beschreibung` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attribut__vertrag__status` (
`id` int(11) NOT NULL,
`bezeichnung` varchar(250) NOT NULL,
`beschreibung` varchar(500) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `lieferstelle__gas` (
`id` int(11) NOT NULL,
`addressID` int(11) NOT NULL,
`ausspeisenetzID` int(11) NOT NULL,
`marktgebietID` int(11) NOT NULL,
`lastprofilID` int(11) NOT NULL,
`qualitatID` int(11) NOT NULL,
`zahlpunkt` varchar(33) DEFAULT NULL,
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL,
`aktualisiert` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`aktualisiert_durch` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mitarbeiter` (
`id` int(11) NOT NULL,
`geschlecht` varchar(1) DEFAULT NULL,
`titel` int(11) DEFAULT NULL,
`vorname` varchar(50) DEFAULT NULL,
`nachname` varchar(50) DEFAULT NULL,
`geburtsname` varchar(50) DEFAULT NULL,
`anmerkung` varchar(500) DEFAULT NULL,
`geburtsdatum` date DEFAULT NULL,
`mitarbeiterNr` varchar(20) DEFAULT NULL,
`foto_dateityp` varchar(150) DEFAULT NULL,
`foto_size` int(11) DEFAULT NULL,
`foto_width` int(6) DEFAULT NULL,
`foto_height` int(6) DEFAULT NULL,
`anzeigen` tinyint(1) NOT NULL DEFAULT '1',
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL,
`aktualisiert` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`aktualisiert_durch` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `preisblatt__gas` (
`id` int(11) NOT NULL,
`evuID` int(11) NOT NULL COMMENT 'Lieferant',
`marktgebietID` int(11) NOT NULL COMMENT 'Marktgebiet',
`erdgasqualitatID` int(11) NOT NULL COMMENT 'Erdgasqualität',
`kalenderwoche` int(2) NOT NULL,
`jahr` int(4) NOT NULL,
`gultig_von` date NOT NULL,
`gultig_bis` date NOT NULL,
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `unternehmen` (
`id` int(11) NOT NULL,
`unternehmenNr` varchar(10) DEFAULT NULL,
`name` varchar(250) NOT NULL,
`kurzel` varchar(5) DEFAULT NULL,
`strasse` int(11) NOT NULL,
`hausnummer` varchar(5) NOT NULL,
`postleitzahl` int(11) NOT NULL,
`ort` int(11) NOT NULL,
`bundesland` int(11) DEFAULT NULL,
`land` int(11) NOT NULL,
`postfach` varchar(10) DEFAULT NULL,
`postfach_postleitzahl` int(11) DEFAULT NULL,
`postfach_ort` int(11) DEFAULT NULL,
`postfach_land` int(11) DEFAULT NULL,
`url` varchar(150) DEFAULT NULL,
`bemerkung` varchar(500) DEFAULT NULL,
`foto_dateityp` varchar(150) DEFAULT NULL,
`foto_size` int(11) DEFAULT NULL,
`foto_width` int(6) DEFAULT NULL,
`foto_height` int(6) DEFAULT NULL,
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL,
`aktualisiert` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`aktualisiert_durch` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `unternehmen__evu` (
`id` int(11) NOT NULL,
`unternehmenID` int(11) NOT NULL,
`jahresumsatz` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `vertrag__lieferstelle__gas` (
`id` int(11) NOT NULL,
`rahmenID` int(11) NOT NULL,
`maklerID` int(11) NOT NULL,
`lieferstelle_gasID` int(11) NOT NULL,
`leistungsempfanger` varchar(250) DEFAULT NULL,
`objektbezeichnung` varchar(10) DEFAULT NULL,
`lieferbeginn` date NOT NULL,
`lieferende` date NOT NULL,
`kundennummer_evu` varchar(20) DEFAULT NULL,
`vorlieferant` int(11) NOT NULL,
`vorlieferant_kundennummer` varchar(30) DEFAULT NULL,
`zahlernummer` varchar(20) NOT NULL COMMENT 'zum Vertragsabschluss',
`vorjahresverbrauch` double NOT NULL COMMENT 'in kWh',
`jahreshochstleistung` double DEFAULT NULL,
`preisblattID` int(11) NOT NULL,
`stichtag_abrechnung` date NOT NULL,
`vertrag_kennzeichenID` int(11) NOT NULL,
`vertrag_kalkulationsartID` int(11) NOT NULL,
`kalkulationsdatum` date NOT NULL,
`kalkulator` int(11) NOT NULL COMMENT 'Excel-Berechner (mitarbeiterID)',
`distributor` int(11) NOT NULL COMMENT 'Verkäufer Außendienst (mitarbeiterID)',
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL,
`aktualisiert` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`aktualisiert_durch` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `vertrag__lieferstelle__gas__preisbestandteile` (
`vlsGasID` int(11) NOT NULL,
`nne_mess_ka` double DEFAULT NULL,
`regelenergie` double DEFAULT NULL,
`energiesteuer` double DEFAULT NULL,
`erdgaspreis` double DEFAULT NULL,
`rohmarge` double DEFAULT NULL,
`marge_vp` double DEFAULT NULL,
`arbeitspreis` double DEFAULT NULL,
`nv_arbeitspreis` double DEFAULT NULL COMMENT 'Summe aus allen',
`nv_grundpreis` double DEFAULT NULL,
`temp` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `vertrag__lieferstelle__gas__status` (
`id` int(11) NOT NULL,
`vlsGasID` int(11) NOT NULL,
`statusID` int(11) NOT NULL,
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `vertrag__rahmen` (
`id` int(11) NOT NULL,
`vertragsnummer` varchar(30) NOT NULL,
`evuID` int(11) NOT NULL,
`verwalterID` int(11) NOT NULL,
`maklerID` int(11) NOT NULL,
`produktID` int(11) NOT NULL,
`abschlussdatum` date NOT NULL,
`bemerkung` varchar(500) NOT NULL,
`angelegt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`angelegt_durch` int(11) NOT NULL,
`aktualisiert` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`aktualisiert_durch` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `address`
ADD PRIMARY KEY (`id`),
ADD KEY `strasse` (`strasse`),
ADD KEY `postleitzahl` (`postleitzahl`),
ADD KEY `ort` (`ort`),
ADD KEY `land` (`land`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `aktualisiert_durch` (`aktualisiert_durch`);
ALTER TABLE `address__ort`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `bezeichnung` (`bezeichnung`);
ALTER TABLE `address__plz`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `bezeichnung` (`bezeichnung`);
ALTER TABLE `address__strasse`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `bezeichnung` (`bezeichnung`);
ALTER TABLE `attribut__gas__ausspeisenetz`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__gas__lastprofil`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__gas__marktgebiet`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__gas__qualitat`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__produkt`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__vertrag__kalkulationsart`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__vertrag__kennzeichen`
ADD PRIMARY KEY (`id`);
ALTER TABLE `attribut__vertrag__status`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `bezeichnung` (`bezeichnung`);
ALTER TABLE `lieferstelle__gas`
ADD PRIMARY KEY (`id`),
ADD KEY `lieferstelleID` (`addressID`),
ADD KEY `ausspeisenetzID` (`ausspeisenetzID`),
ADD KEY `marktgebietID` (`marktgebietID`),
ADD KEY `zahlertypID` (`lastprofilID`),
ADD KEY `qualitatID` (`qualitatID`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `aktualisiert_durch` (`aktualisiert_durch`);
ALTER TABLE `mitarbeiter`
ADD PRIMARY KEY (`id`),
ADD KEY `mitarbeiterID` (`id`),
ADD KEY `titel` (`titel`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `aktualisiert_durch` (`aktualisiert_durch`);
ALTER TABLE `mitarbeiter` ADD FULLTEXT KEY `vorname` (`vorname`);
ALTER TABLE `mitarbeiter` ADD FULLTEXT KEY `nachname` (`nachname`);
ALTER TABLE `mitarbeiter` ADD FULLTEXT KEY `vorname_2` (`vorname`,`nachname`);
ALTER TABLE `preisblatt__gas`
ADD PRIMARY KEY (`id`),
ADD KEY `preisblattID` (`id`),
ADD KEY `unternehmen_evuID` (`evuID`),
ADD KEY `lieferstelle_marktgebietID` (`marktgebietID`),
ADD KEY `lieferstelle_erdgasqualitatID` (`erdgasqualitatID`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `datum` (`jahr`,`kalenderwoche`) USING BTREE;
ALTER TABLE `unternehmen`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `name` (`name`),
ADD UNIQUE KEY `kurzel` (`kurzel`),
ADD UNIQUE KEY `unternehmenNr` (`unternehmenNr`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `aktualisiert_durch` (`aktualisiert_durch`),
ADD KEY `strasse` (`strasse`),
ADD KEY `postleitzahl` (`postleitzahl`),
ADD KEY `ort` (`ort`),
ADD KEY `bundesland` (`bundesland`),
ADD KEY `land` (`land`),
ADD KEY `postfach_postleitzahl` (`postfach_postleitzahl`),
ADD KEY `postfach_ort` (`postfach_ort`),
ADD KEY `postfach_land` (`postfach_land`);
ALTER TABLE `unternehmen__evu`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `unternehmenID_2` (`unternehmenID`),
ADD KEY `unternehmenID` (`unternehmenID`);
ALTER TABLE `vertrag__lieferstelle__gas`
ADD PRIMARY KEY (`id`),
ADD KEY `vertrag_kennzeichenID` (`vertrag_kennzeichenID`),
ADD KEY `lieferstelleID` (`lieferstelle_gasID`),
ADD KEY `preisblattID` (`preisblattID`),
ADD KEY `distributor` (`distributor`),
ADD KEY `maklerID` (`maklerID`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `aktualisiert_durch` (`aktualisiert_durch`),
ADD KEY `kalkulator` (`kalkulator`),
ADD KEY `rahmenID` (`rahmenID`),
ADD KEY `vorlieferant` (`vorlieferant`),
ADD KEY `vertrag_berechnungsartID` (`vertrag_kalkulationsartID`),
ADD KEY `lieferbeginn` (`lieferbeginn`),
ADD KEY `lieferende` (`lieferende`),
ADD KEY `anlage2index` (`id`,`rahmenID`,`lieferstelle_gasID`,`vorlieferant`,`preisblattID`,`vertrag_kennzeichenID`,`vertrag_kalkulationsartID`,`kalkulator`,`distributor`) USING BTREE;
ALTER TABLE `vertrag__lieferstelle__gas__preisbestandteile`
ADD PRIMARY KEY (`vlsGasID`);
ALTER TABLE `vertrag__lieferstelle__gas__status`
ADD PRIMARY KEY (`id`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `vlsGasID` (`vlsGasID`),
ADD KEY `statusID` (`statusID`);
ALTER TABLE `vertrag__rahmen`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `unternehmen_evuID_2` (`evuID`,`produktID`,`verwalterID`) USING BTREE,
ADD KEY `unternehmen_evuID` (`evuID`),
ADD KEY `unternehmen_verwalterID` (`verwalterID`),
ADD KEY `unternehmen_maklerID` (`maklerID`),
ADD KEY `produktID` (`produktID`),
ADD KEY `angelegt_durch` (`angelegt_durch`),
ADD KEY `aktualisiert_durch` (`aktualisiert_durch`);
ALTER TABLE `address`
ADD CONSTRAINT `address_ibfk_05` FOREIGN KEY (`strasse`) REFERENCES `address__strasse` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `address_ibfk_06` FOREIGN KEY (`postleitzahl`) REFERENCES `address__plz` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `address_ibfk_07` FOREIGN KEY (`ort`) REFERENCES `address__ort` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `address_ibfk_08` FOREIGN KEY (`land`) REFERENCES `address__land` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `address_ibfk_09` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `address_ibfk_10` FOREIGN KEY (`aktualisiert_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE;
ALTER TABLE `lieferstelle__gas`
ADD CONSTRAINT `lieferstelle_gas_ibfk_01` FOREIGN KEY (`addressID`) REFERENCES `address` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `lieferstelle_gas_ibfk_02` FOREIGN KEY (`ausspeisenetzID`) REFERENCES `attribut__gas__ausspeisenetz` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `lieferstelle_gas_ibfk_03` FOREIGN KEY (`marktgebietID`) REFERENCES `attribut__gas__marktgebiet` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `lieferstelle_gas_ibfk_04` FOREIGN KEY (`lastprofilID`) REFERENCES `attribut__gas__lastprofil` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `lieferstelle_gas_ibfk_05` FOREIGN KEY (`qualitatID`) REFERENCES `attribut__gas__qualitat` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `lieferstelle_gas_ibfk_06` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `lieferstelle_gas_ibfk_07` FOREIGN KEY (`aktualisiert_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE;
ALTER TABLE `mitarbeiter`
ADD CONSTRAINT `mitarbeiter_ibfk_1` FOREIGN KEY (`titel`) REFERENCES `attribut__mitarbeiter__titel` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `mitarbeiter_ibfk_2` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `mitarbeiter_ibfk_3` FOREIGN KEY (`aktualisiert_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE;
ALTER TABLE `preisblatt__gas`
ADD CONSTRAINT `preisblatt__gas_ibfk_1` FOREIGN KEY (`evuID`) REFERENCES `unternehmen__evu` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `preisblatt__gas_ibfk_2` FOREIGN KEY (`marktgebietID`) REFERENCES `attribut__gas__marktgebiet` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `preisblatt__gas_ibfk_3` FOREIGN KEY (`erdgasqualitatID`) REFERENCES `attribut__gas__qualitat` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `preisblatt__gas_ibfk_4` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE;
ALTER TABLE `unternehmen`
ADD CONSTRAINT `unternehmen_ibfk_1` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_10` FOREIGN KEY (`postfach_land`) REFERENCES `address__land` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_2` FOREIGN KEY (`aktualisiert_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_3` FOREIGN KEY (`strasse`) REFERENCES `address__strasse` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_4` FOREIGN KEY (`postleitzahl`) REFERENCES `address__plz` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_5` FOREIGN KEY (`ort`) REFERENCES `address__ort` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_6` FOREIGN KEY (`bundesland`) REFERENCES `address__bundesland` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_7` FOREIGN KEY (`land`) REFERENCES `address__land` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_8` FOREIGN KEY (`postfach_postleitzahl`) REFERENCES `address__plz` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `unternehmen_ibfk_9` FOREIGN KEY (`postfach_ort`) REFERENCES `address__ort` (`id`) ON UPDATE CASCADE;
ALTER TABLE `unternehmen__evu`
ADD CONSTRAINT `unternehmen__evu_ibfk_1` FOREIGN KEY (`unternehmenID`) REFERENCES `unternehmen` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `vertrag__lieferstelle__gas`
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_01` FOREIGN KEY (`rahmenID`) REFERENCES `vertrag__rahmen` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_02` FOREIGN KEY (`maklerID`) REFERENCES `unternehmen__makler` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_03` FOREIGN KEY (`lieferstelle_gasID`) REFERENCES `lieferstelle__gas` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_04` FOREIGN KEY (`preisblattID`) REFERENCES `preisblatt__gas` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_06` FOREIGN KEY (`vertrag_kennzeichenID`) REFERENCES `attribut__vertrag__kennzeichen` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_07` FOREIGN KEY (`vertrag_kalkulationsartID`) REFERENCES `attribut__vertrag__kalkulationsart` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_08` FOREIGN KEY (`kalkulator`) REFERENCES `mitarbeiter` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_09` FOREIGN KEY (`distributor`) REFERENCES `mitarbeiter` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_10` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_11` FOREIGN KEY (`aktualisiert_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__lieferstelle__gas_ibfk_12` FOREIGN KEY (`vorlieferant`) REFERENCES `unternehmen__evu` (`id`) ON UPDATE CASCADE;
ALTER TABLE `vertrag__lieferstelle__gas__preisbestandteile`
ADD CONSTRAINT `vlsGas_pb2_ibfk_01` FOREIGN KEY (`vlsGasID`) REFERENCES `vertrag__lieferstelle__gas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `vertrag__lieferstelle__gas__status`
ADD CONSTRAINT `vlsGasStatus_ibfk_01` FOREIGN KEY (`vlsGasID`) REFERENCES `vertrag__lieferstelle__gas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `vlsGasStatus_ibfk_02` FOREIGN KEY (`statusID`) REFERENCES `attribut__vertrag__status` (`id`) ON UPDATE CASCADE;
ALTER TABLE `vertrag__rahmen`
ADD CONSTRAINT `vertrag__rahmen_ibfk_01` FOREIGN KEY (`evuID`) REFERENCES `unternehmen__evu` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__rahmen_ibfk_02` FOREIGN KEY (`verwalterID`) REFERENCES `unternehmen__verwalter` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__rahmen_ibfk_03` FOREIGN KEY (`maklerID`) REFERENCES `unternehmen__makler` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__rahmen_ibfk_04` FOREIGN KEY (`produktID`) REFERENCES `attribut__produkt` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__rahmen_ibfk_05` FOREIGN KEY (`angelegt_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `vertrag__rahmen_ibfk_06` FOREIGN KEY (`aktualisiert_durch`) REFERENCES `db__user` (`id`) ON UPDATE CASCADE;
my my.ini:
[mysqld]
port= 3306
socket = "C:/xampp/mysql/mysql.sock"
basedir = "C:/xampp/mysql"
tmpdir = "C:/xampp/tmp"
datadir = "C:/xampp/mysql/data"
pid_file = "mysql.pid"
# enable-named-pipe
key_buffer = 16M
max_allowed_packet = 1M
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log_error = "mysql_error.log"
slow_query_log=ON
long_query_time=1
table_open_cache=10000
thread_cache_size=25
query_cache_type=ON
query_cache_size=128MiB
query_cache_limit=100MiB
tmp_table_size=64MiB
Thank you for your help!
edit: I recognized, that the query starts to be slow from " INNER JOIN vertrag__lieferstelle__gas__preisbestandteile pbt ..." and the other joins after it.
How much RAM do you have? Since I don't see innodb_buffer_pool_size in your my.ini, I suspect you are using some low default. Set it to about 70% of available RAM. This should help this query and others.
table_open_cache=10000 -- lower to 3000
query_cache_size=128MiB -- lower to 50M
tmp_table_size=64MiB -- not more than 1% of RAM
Other notes:
jahr int(4) NOT NULL, -- Why not use the YEAR datatype?
INT(2) -- the (2) says nothing; perhaps you meant TINYINT?
char(5) -- you probably want CHAR(5) CHARACTER SET ascii, otherwise, you are allocating 15 bytes because of utf8.
Because of the large number of normalization tables, this may help:
SET ##optimizer_search_depth=1
You can probably test its utility by trying the EXPLAIN SELECT ... with it 1 versus its default of 62.
Not sure how much of a difference it will make with a 23 table query, but I generally try to avoid correlated subqueries like the one used here:
vlsStatus.id = (SELECT max(tmp.id) FROM vertrag__lieferstelle__gas__status tmp WHERE tmp.vlsGasID=vls.id)
Instead try this:
(vls.id, vlsStatus.id) IN (SELECT vlsGasID, max(id) FROM vertrag__lieferstelle__gas__status GROUP BY vlsGasID)
The subquery will only run once (compared to once for each result in the outer query with the correlated version)
Edit: Also, it may not affect performance, but I generally try to put tables referenced in the WHERE earlier in the JOIN sequence when possible.
Edit2:
Another possibility is to create a temporary table with the subquery, index the temp table appropriately (both fields), and include it in the JOIN portion of the main query.

MySQL: Cannot add or update a child row: a foreign key constraint fails

I have a workflow in my application where an admin for an organization can add a user to their organization. Once a form submission, I first insert a new record into my user table with the users email and the organization id associated with the admin. After that user is created, a member record which connects the user to the organization is created with the users email and the users organization id and user_id. The issue I am running into is with creating a record on the member table with the recently created users user_id. While I have no issue storing the organization_id present, for some reason passing the user_id throws an error related to the foreign key:
Cannot add or update a child row: a foreign key constraint fails (`work`.`member`, CONSTRAINT `member_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `organization` (`organization_id`) ON DELETE CASCADE ON UPDATE CASCADE)]
Why can't I pass the user_id on record creation. Is it because the column is associated to a foreign key?
Here are the inserts:
User insert (No error):
INSERT INTO `user` (`user_id`,`email`,`organization_id`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'test+email#gmail.com','1','2016-06-07 11:51:54','2016-06-07 11:51:54');
Member insert (Error with user_id):
INSERT INTO `member` (`member_id`,`member_email`,`organization_id`,`user_id`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'test+email#gmail.com','1',8,'2016-06-07 11:51:54','2016-06-07 11:51:54');
User Table:
`user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`organization_id` int(11) DEFAULT NULL,
`authentication_token` varchar(255) DEFAULT NULL,
`reset_password_token` varchar(255) DEFAULT NULL,
`reset_password_expires` datetime DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
Organization table:
`organization` (
`organization_id` int(11) NOT NULL AUTO_INCREMENT,
`organization_name` varchar(255) DEFAULT NULL,
`admin` varchar(255) DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`organization_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Member table:
`member` (
`member_id` int(11) NOT NULL AUTO_INCREMENT,
`member_email` varchar(255) DEFAULT NULL,
`organization_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`member_id`),
UNIQUE KEY `member_email` (`member_email`),
UNIQUE KEY `member_user_id_organizationId_unique` (`organization_id`,`user_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `member_ibfk_1` FOREIGN KEY (`organization_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `member_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `organization` (`organization_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
Seem to me that you got your constraints mixed up.
I created the tables using your sql and reverse engineered with MySQLWorkbench to get an EER containing your tables.
On first sight you can see that organization_id in table organization is linked with user_id in members table.
If you change your constraints to the following, it will work:
CONSTRAINT `member_ibfk_1` FOREIGN KEY (`organization_id`) REFERENCES `organization` (`organization_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `member_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
Try to change the CONSTRAINT member_ibfk_1 to this:
CONSTRAINT `member_ibfk_1` FOREIGN KEY (`organization_id`) REFERENCES user(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,

MySQL error when trying to truncate table

I'm having problems to truncate a table on the MySQL Server 5.5.
The table I'm trying to truncate has a column that serves as a foreign key in another table.
The CREATE TABLE of both tables involved is as it follows:
CREATE TABLE `tbluser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`creationDate` datetime NOT NULL,
`creationUserId` int(11) NOT NULL,
`updateDate` datetime NOT NULL,
`updateUserId` int(11) NOT NULL,
`lastAccess` datetime NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `email_UNIQUE` (`email`),
KEY `FK_tbluser_creationUserId` (`creationUserId`),
KEY `FK_tbluser_updateUserId` (`updateUserId`),
CONSTRAINT `FK_tbluser_updateUserId` FOREIGN KEY (`updateUserId`) REFERENCES `tbluser` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_tbluser_creationUserId` FOREIGN KEY (`creationUserId`) REFERENCES `tbluser` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
CREATE TABLE `tblpost` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` mediumtext NOT NULL,
`creationDate` datetime NOT NULL DEFAULT '1901-01-01 00:00:00',
`creationUserId` int(11) NOT NULL,
`updateDate` datetime NOT NULL DEFAULT '1901-01-01 00:00:00',
`updateUserId` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_tblpost_creationUserId` (`creationUserId`),
KEY `FK_tblpost_updateUserId` (`updateUserId`),
CONSTRAINT `FK_tblpost_updateUserId` FOREIGN KEY (`updateUserId`) REFERENCES `tbluser` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_tblpost_creationUserId` FOREIGN KEY (`creationUserId`) REFERENCES `tbluser` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Please note that all the constraints are both set to DELETE and UPDATE ON CASCADE.
When I try to TRUNCATE the table:
TRUNCATE TABLE `<databasename>`.`tbluser`;
I receive the following error message:
Cannot truncate a table referenced in a foreign key constraint
(`<databasename>`.`tblpost`,
CONSTRAINT `FK_tblpost_updateUserId`
FOREIGN KEY (`updateUserId`)
REFERENCES `<databasename>`.`tbluser` (`id`))
In addition to this information, there is the fact that when the action above is attempted on a MySQL Server 5.1, it works!
Does anyone have an idea of why this is happening?
Check here . That makes sense that TRUNCATE TABLE raises an error in such cases; the bad thing that it's not documented.

Foreign Key Constraint Fails - Probably misunderstanding something about my relationships

I'm having a little trouble with some MySQL relationships. I think I'm missing something obvious in my structure. Here's my SQL:
DROP TABLE IF EXISTS `parentlist_comments`;
CREATE TABLE `parentlist_comments` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`comment` char(50) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_parentlist_comments_parentlist` (`parentlist_id`),
KEY `fk_parentlist_comment_user` (`user_id`),
CONSTRAINT `fk_parentlist_comments_parentlist` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_comment_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlist_submissions`;
CREATE TABLE `parentlist_submissions` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(25) NOT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_parentlist_submissions_user` (`user_id`),
KEY `fk_parentlist_submissions_list` (`parentlist_id`),
KEY `fk_parentlist_submissions_type` (`type_id`),
CONSTRAINT `fk_parentlist_submissions_list` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlists`;
CREATE TABLE `parentlists` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(50) NOT NULL,
`user_id` char(36) NOT NULL,
`max_comments` int(3) NOT NULL DEFAULT '0',
`max_submissions` int(3) NOT NULL DEFAULT '10',
`max_votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_list_user` (`user_id`),
CONSTRAINT `fk_list_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;
DROP TABLE IF EXISTS `submissions`;
CREATE TABLE `submissions` (
`id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(30) NOT NULL,
`description` char(50) NOT NULL,
`embed` char(200) DEFAULT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_submission_user` (`user_id`),
KEY `fk_submission_type` (`type_id`),
CONSTRAINT `fk_submission_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_submission_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `types`;
CREATE TABLE `types` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`password` char(20) NOT NULL,
`email` char(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I created an column called submission_id in parentlist_submissions. I'm trying to create a foreign key relationship between parentlist_submissions.submission_id and submissions.id, when I attempt to do this I get the error: Foriegn key constraint fails. For whatever reason my query browser won't let me copy this.
Any help here is greatly appreciated!
That error is usually caused by the tables already being populated with data that violate the constraint. (Note that nulls may be a problem if you've just added the column.)
I'm guessing, because I don't see that you've posted the statement where you create the submission_index column or where you create the foreign key constraint.
You seem to be missing the "parentlist_submissions.submission_id" column.