optimize slow sql query with 23 tables - mysql

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.

Related

Unable to create foreign key constraint MySql

I have 2 tables like these:
'CREATE TABLE `preoperativeassessments` (
`Id` char(36) NOT NULL,
`SurgeonName` varchar(100) NOT NULL,
`SurgeonExperience` int(11) DEFAULT NULL,
`AnesthetistName` varchar(100) DEFAULT NULL,
`DateOfBirthYear` int(11) DEFAULT NULL,
`Gender` int(11) DEFAULT NULL,
`Status` int(11) DEFAULT NULL,
`SurgeryDate` datetime(6) DEFAULT NULL,
`HospitalId` varchar(20) NOT NULL,
`PatientId` varchar(50) NOT NULL,
`SurgeonId` bigint(20) NOT NULL,
`TheaterId` varchar(16) DEFAULT NULL,
`AssessmentDate` datetime(6) DEFAULT NULL,
`BodyStructureId` int(11) NOT NULL,
`MethodId` varchar(100) DEFAULT NULL,
`EthnicityId` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `FK_PreOperativeAssessments_Hospitals_HospitalId_idx` (`HospitalId`),
KEY `FK_PreOperativeAssessments_Patients_PatientId_idx` (`PatientId`),
KEY `FK_PreOperativeAssessments_Users_SurgeonId_idx` (`SurgeonId`),
KEY `FK_PreOperativeAssessments_BodyStructures_BodyStructureId_idx` (`BodyStructureId`),
CONSTRAINT `FK_PreOperativeAssessments_BodyStructures_BodyStructureId` FOREIGN KEY (`BodyStructureId`) REFERENCES `bodystructures` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_PreOperativeAssessments_Hospitals_HospitalId` FOREIGN KEY (`HospitalId`) REFERENCES `hospitals` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_PreOperativeAssessments_Patients_PatientId` FOREIGN KEY (`PatientId`) REFERENCES `patients` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_PreOperativeAssessments_Users_SurgeonId` FOREIGN KEY (`SurgeonId`) REFERENCES `abpusers` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1'
CREATE TABLE `ethnicities` (
`Id` int(11) NOT NULL,
`Description` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
and I try to add foreign key constraint using this:
ALTER TABLE casemix.preoperativeassessments ADD CONSTRAINT FK_PreOperativeAssessments_Ethnicities_EthnicityId FOREIGN KEY (EthnicityId) REFERENCES casemix.ethnicities (Id) ON UPDATE NO ACTION ON DELETE CASCADE
But I keep getting this error: "Error Code: 1215. Cannot add foreign key constraint"
What could be the issue here?
'InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are the first columns in the same order.' - https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html or a primary key

Foreign key constraint fails when I want delete table

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.

MySQL table - performance, count of indexes

I have table with this structure:
CREATE TABLE `supplies` (
`cycle_id` int(11) NOT NULL,
`subject_id` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`market_id` int(11) NOT NULL,
`price` int(11) NOT NULL,
`currency_id` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`offered_order` bigint(20) DEFAULT NULL,
PRIMARY KEY (`subject_id`,`market_id`,`cycle_id`,`price`),
KEY `fk_supplies_subjects` (`subject_id`),
KEY `fk_supplies_markets` (`market_id`),
KEY `fk_supplies_currencies` (`currency_id`),
CONSTRAINT `fk_supplies_currencies` FOREIGN KEY (`currency_id`) REFERENCES `currencies` (`currency_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_supplies_subjects` FOREIGN KEY (`subject_id`) REFERENCES `subjects` (`subject_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_supplies_markets` FOREIGN KEY (`market_id`) REFERENCES `markets` (`market_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Table has about 20,000 rows. I have problems with performance. I am logging SQL queries that are taking long time and from the log I can see:
Slow INSERTs into table.
INSERT INTO supplies VALUES (11, 'userName', 18, 30, 'pound', 20) ;
Slow DELETEs from table
DELETE FROM supplies WHERE cycle_id = 6 AND market_id = 18 AND subject_id = 'userName' ;
What could I improve?
I guess, changing the indexes would help, because the PRIMARY key is formed from 4 columns (and some are even varchars). But how should I change the indexes?
Thanks everyone.
You should create a primary key with only one field. This is a basic performance optimization
CREATE TABLE `supplies` (
`supplies_id` int(11) NOT NULL AUTO_INCREMENT,
`cycle_id` int(11) NOT NULL,
`subject_id` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`market_id` int(11) NOT NULL,
`price` int(11) NOT NULL,
`currency_id` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`offered_order` bigint(20) DEFAULT NULL,
PRIMARY KEY (`supplies_id`),
INDEX `index` (`subject_id` ASC,`market_id` ASC,`cycle_id` ASC,`price` ASC))
KEY `fk_supplies_subjects` (`subject_id`),
KEY `fk_supplies_markets` (`market_id`),
KEY `fk_supplies_currencies` (`currency_id`),
CONSTRAINT `fk_supplies_currencies` FOREIGN KEY (`currency_id`) REFERENCES `currencies` (`currency_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_supplies_subjects` FOREIGN KEY (`subject_id`) REFERENCES `subjects` (`subject_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_supplies_markets` FOREIGN KEY (`market_id`) REFERENCES `markets` (`market_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Another possible improvement can be removing cascade constraints. Check if they are really necessary.

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.