SQLError - create foreign key - mysql

An error occurred when trying to add a foreign key
SQLError
Before that, the following actions were performed, indexes were installed
maintable
subordinatetable
Acted according to https://folkprog.net/sozdanie-svyazey-phpmyadmin/.
Error text:
ALTER TABLE monoblocks ADD FOREIGN KEY (image_id) REFERENCES monoblock_images(id) ON DELETE RESTRICT ON UPDATE RESTRICT;
#1452 - Cannot add or update a child row: a foreign key constraint fails (myishop.#sql-2150_93, CONSTRAINT #sql-2150_93_ibfk_21 FOREIGN KEY (image_id) REFERENCES monoblock_images (id))
I have found
CREATE TABLE `monoblocks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`goods_id` int(11) NOT NULL,
`screendiagonal_id` int(11) NOT NULL,
`cpu_id` int(11) NOT NULL,
`ramsize_id` int(11) NOT NULL,
`os_id` int(11) NOT NULL,
`hdd_id` int(11) NOT NULL,
`screenresolution_id` int(11) NOT NULL,
`matrixtype_id` int(11) NOT NULL,
`countcpucores` int(11) NOT NULL,
`videocards_id` int(11) NOT NULL,
`videocardtypes_id` int(11) NOT NULL,
`frequency_id` int(11) NOT NULL,
`ramtype_id` int(11) NOT NULL,
`oddtype_id` int(11) NOT NULL,
`bodycolor_id` int(11) NOT NULL,
`weight` int(11) NOT NULL,
`wifisupport_id` int(11) NOT NULL,
`bluetoothsupport_id` int(11) NOT NULL,
`wifistandard_id` int(11) NOT NULL,
`bluetoothstandard_id` int(11) NOT NULL,
`guarantee_id` int(11) NOT NULL,
`connectorsnumberusb2.0` int(11) NOT NULL,
`connectorsnumberusb3.0` int(11) NOT NULL,
`connectorsnumberhdmi` int(11) NOT NULL,
`microphoneconnecors` int(11) NOT NULL,
`headphoneconnectors` int(11) NOT NULL,
`builtinmicrophone` tinyint(1) NOT NULL,
`builtinspeakers` tinyint(1) NOT NULL,
`touchscreen` tinyint(1) NOT NULL,
`webcam` tinyint(1) NOT NULL,
`keyboard_included` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`mouse_included` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`ssd_volume` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`dimensions` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`image_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `goods_id` (`goods_id`),
KEY `screendiagonal_id` (`screendiagonal_id`),
KEY `cpu_id` (`cpu_id`),
KEY `ramsize_id` (`ramsize_id`),
KEY `os_id` (`os_id`),
KEY `hdd` (`hdd_id`),
KEY `screenresolution_id` (`screenresolution_id`),
KEY `matrixtype_id` (`matrixtype_id`),
KEY `countcpucores` (`countcpucores`),
KEY `videocards_id` (`videocards_id`),
KEY `videocardtypes_id` (`videocardtypes_id`),
KEY `frequency_id` (`frequency_id`),
KEY `ramtype_id` (`ramtype_id`),
KEY `oddtype_id` (`oddtype_id`),
KEY `bodycolor_id` (`bodycolor_id`),
KEY `wifisupport_id` (`wifisupport_id`),
KEY `bluetoothsupport_id` (`bluetoothsupport_id`),
KEY `wifistandard_id` (`wifistandard_id`),
KEY `bluetoothstandard_id` (`bluetoothstandard_id`),
KEY `guarantee_id` (`guarantee_id`),
KEY `image_id` (`image_id`),
CONSTRAINT `monoblocks_ibfk_1` FOREIGN KEY (`goods_id`) REFERENCES `goods` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_10` FOREIGN KEY (`videocards_id`) REFERENCES `videocards` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_11` FOREIGN KEY (`videocardtypes_id`) REFERENCES `videocardtypes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_12` FOREIGN KEY (`frequency_id`) REFERENCES `cpufrequency` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_13` FOREIGN KEY (`ramtype_id`) REFERENCES `rammemory` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_14` FOREIGN KEY (`oddtype_id`) REFERENCES `oddtype` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_15` FOREIGN KEY (`bodycolor_id`) REFERENCES `mainbodycolor` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_16` FOREIGN KEY (`wifisupport_id`) REFERENCES `wifisupport` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_17` FOREIGN KEY (`bluetoothsupport_id`) REFERENCES `bluetoothsupport` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_18` FOREIGN KEY (`wifistandard_id`) REFERENCES `wifistandard` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_19` FOREIGN KEY (`bluetoothstandard_id`) REFERENCES `bluetoothstandard` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_2` FOREIGN KEY (`screendiagonal_id`) REFERENCES `screendiagonal` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_20` FOREIGN KEY (`guarantee_id`) REFERENCES `guarantee` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_3` FOREIGN KEY (`cpu_id`) REFERENCES `cpus` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_4` FOREIGN KEY (`ramsize_id`) REFERENCES `ramsize` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_5` FOREIGN KEY (`os_id`) REFERENCES `os` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_6` FOREIGN KEY (`hdd_id`) REFERENCES `disksandrives` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_7` FOREIGN KEY (`screenresolution_id`) REFERENCES `screenresolution` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_8` FOREIGN KEY (`matrixtype_id`) REFERENCES `matrixtype` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `monoblocks_ibfk_9` FOREIGN KEY (`countcpucores`) REFERENCES `numbercpucores` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `monoblock_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`path_to_image` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
(may be help to answer the question)
In answer on SELECT mb.image_id FROM monoblocks AS mb WHERE NOT EXISTS (SELECT 1 FROM monoblock_images AS i WHERE i.id = mb.image_id) AND mb.image_id IS NOT NULL I gave
monoblockquery

image_id can be NULL in the Table monoblocks but not in the other Table.

Related

MySQL Error Code: 1451 Can't Delete or Update Parent Row

I have a self join in the channels table with the field parent_id. I have inserted a row into the table but unable to delete it. Its giving error as stated in the subject.
CREATE TABLE `channels` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`channel_type_id` int(11) DEFAULT NULL,
`approval_type_id` int(11) DEFAULT NULL,
`content_type_id` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`status` tinyint(1) NOT NULL,
`position` int(11) DEFAULT NULL,
`reference_id` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`mobile` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_F314E2B6C53D045F` (`image`),
KEY `IDX_F314E2B6727ACA70` (`parent_id`),
KEY `IDX_F314E2B6720FB392` (`channel_type_id`),
KEY `IDX_F314E2B6510C33D5` (`approval_type_id`),
KEY `IDX_F314E2B61A445520` (`content_type_id`),
CONSTRAINT `FK_F314E2B6727ACA70` FOREIGN KEY (`parent_id`) REFERENCES `channels` (`id`),
CONSTRAINT `FK_F314E2B61A445520` FOREIGN KEY (`content_type_id`) REFERENCES `content_type` (`id`),
CONSTRAINT `FK_F314E2B6510C33D5` FOREIGN KEY (`approval_type_id`) REFERENCES `approval_types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_F314E2B6720FB392` FOREIGN KEY (`channel_type_id`) REFERENCES `channel_types` (`id`),
CONSTRAINT `FK_F314E2B6C53D045F` FOREIGN KEY (`image`) REFERENCES `media__media` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6438 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The error is as following:
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`wenweipo`.`channels`, CONSTRAINT `FK_F314E2B6727ACA70` FOREIGN KEY (`parent_id`) REFERENCES `channels` (`id`))

Foreign key constraint fails even when data types are the same

I'm building a table with a two part foreign key that references a two part key in another table. It isn't working, and I can't see why because the data types line up.
Can't create table 'tsugi.video_comments' (errno: 150)
Here's the code I'm trying to run:
CREATE TABLE `video_comments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(11) NOT NULL DEFAULT '',
`link_id` int(11) NOT NULL,
`videoTime` int(11) NOT NULL,
`comment` text NOT NULL,
`parent` int(11) unsigned DEFAULT NULL,
`private` tinyint(1) DEFAULT NULL,
`replies` int(11) unsigned DEFAULT '0',
`reports` int(11) DEFAULT '0',
`user_id` int(11) NOT NULL,
`displayname` varchar(2048) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `video_ibfk_1` (`link_id`),
KEY `video_ibfk_2` (`user_id`),
CONSTRAINT `video_comments_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `video_comments` (`id`) ON DELETE CASCADE,
CONSTRAINT `video_ibfk_1` FOREIGN KEY (`link_id`) REFERENCES `lti_link` (`link_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `video_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `lti_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `video_key` FOREIGN KEY (`link_id`, `video_id`) REFERENCES `video_ids` (`link_id`, `video_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=285 DEFAULT CHARSET=utf8;
The command runs successfully if I delete the last constraint, so that's where the problem is, not in the other constraints.
Here's the table it's referencing:
CREATE TABLE `video_ids` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(11) NOT NULL DEFAULT '',
`link_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `video_key` (`video_id`,`id`),
KEY `link_id` (`link_id`),
KEY `id` (`id`,`video_id`),
CONSTRAINT `video_ids_ibfk_1` FOREIGN KEY (`link_id`) REFERENCES `t_lti_link` (`link_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
To make sure that the video_id and link_id fields are exactly the same, I copied them directly from the existing table's code to the code for creating the new table. I expected that to solve it, but it did not. Here's the error log:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
141114 22:04:09 Error in foreign key constraint of table tsugi/video_comments:
FOREIGN KEY (`link_id`, `video_id`) REFERENCES `video_ids` (`link_id`, `video_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=285 DEFAULT CHARSET=utf8:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Foreign key references need to be to unique or primary keys. So, you can fix this just by having a unique declaration in video_ids:
CREATE TABLE `video_ids` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(11) NOT NULL DEFAULT '',
`link_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `video_key` (`video_id`,`id`),
KEY `link_id` (`link_id`),
KEY `id` (`id`,`video_id`),
UNIQUE (link_id, video_id),
CONSTRAINT `video_ids_ibfk_1` FOREIGN KEY (`link_id`) REFERENCES `t_lti_link` (`link_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Here is a SQL Fiddle with an example.

Error when updating with foreign keys with ON UPDATE CASCADE

I have this table:
CREATE TABLE `user` (
`idUser` char(13) NOT NULL,
`contrasena` varchar(50) NOT NULL DEFAULT '',
`fechaInicio` datetime DEFAULT NULL,
`emailRegistrado` varchar(100) DEFAULT NULL,
`tipoUsuario` int(11) NOT NULL,
PRIMARY KEY (`idUser`),
KEY `tipoUser` (`tipoUsuario`) USING BTREE,
CONSTRAINT `tipoUser` FOREIGN KEY (`tipoUsuario`) REFERENCES `tipo_user` (`idTipo`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf16;
And then this table:
CREATE TABLE `alumno` (
`idAlumno` char(10) NOT NULL DEFAULT '',
`Nombre` varchar(60) NOT NULL,
`ApePaterno` varchar(30) NOT NULL,
`ApeMaterno` varchar(30) NOT NULL,
`CURP` varchar(18) DEFAULT NULL,
`Sexo` enum('H','M') NOT NULL,
`FechaNac` date NOT NULL,
`Estado_Nac` int(11) DEFAULT NULL,
`Nacionalidad` int(11) DEFAULT NULL,
PRIMARY KEY (`idAlumno`),
KEY `fk_Alumno_Estados1_idx` (`Estado_Nac`) USING BTREE,
KEY `fk_Alumno_Pais1_idx` (`Nacionalidad`) USING BTREE,
CONSTRAINT `fk_Alumno_Estados1` FOREIGN KEY (`Estado_Nac`) REFERENCES `estadomexico` (`idEstados`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Alumno_Pais1` FOREIGN KEY (`Nacionalidad`) REFERENCES `pais` (`idPais`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Al_User` FOREIGN KEY (`idAlumno`) REFERENCES `user` (`idUser`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf16;
When I try to update a value for a user, MySQL throws the following message:
Cannot delete or update a parent row: a foreign key constraint fails (mydb.empleado, CONSTRAINT fk_Empleado_USer FOREIGN KEY
(idEmpleado) REFERENCES user (idUser) ON DELETE CASCADE ON
UPDATE CASCADE)
Can anybody please help me?

MySQL 5.1 not accepting null foreign key from Access 2007

Backend - MySQL 5.1.52 community
Frontend - MS Access 2007
I've got two tables POLIZAS and TRAMITES (both InnoDB)
I'm using Access as the FrontEnd. I need to insert/update records on Tramites where sometimes Tramites is not related to a Polizas yet.
Tramites has a foreign key to Polizas allowing null. But still I get this error when inserting/updating:
[MySQL][ODBC 5.1 Driver][mysqld-5.1.52-community]Cannot add or update a child row: a foreign key constraint fails ('cartera_bd'.'tramites',CONSTRAINT 'fk_tramites_polizas1' FOREIGN KEY ('Poliza') REFERENCES 'polizas' ('Poliza') ON DELETE CASCADE ON UPDATE CASCADE) (#1452)
Any clues?
This is the POLIZAS table description
CREATE TABLE `polizas` (
`Poliza` varchar(20) NOT NULL,
`IdAgente` mediumint(8) unsigned NOT NULL,
`IdPlan` tinyint(3) unsigned NOT NULL,
`IdCliente` int(10) unsigned NOT NULL,
`RenuevaA` varchar(20) DEFAULT NULL,
`InicioVigencia` date NOT NULL,
`IdTipoDerechos` tinyint(1) unsigned NOT NULL,
`IdConducto` tinyint(1) unsigned NOT NULL,
`IdFormaPago` tinyint(1) unsigned NOT NULL,
`IdMoneda` tinyint(1) unsigned NOT NULL,
`PrimaNeta` decimal(11,2) NOT NULL,
`Recargos` decimal(11,2) DEFAULT NULL,
`Derechos` decimal(11,2) DEFAULT NULL,
`IVA` decimal(11,2) DEFAULT NULL,
`PrimaTotal` decimal(11,2) NOT NULL,
`Status` tinyint(1) unsigned NOT NULL,
`Notas` varchar(150) DEFAULT NULL,
`FechaEmision` date DEFAULT NULL,
PRIMARY KEY (`Poliza`),
KEY `IdAgente` (`IdAgente`),
KEY `IdCliente` (`IdCliente`),
KEY `IdConducto` (`IdConducto`),
KEY `IdFormaPago` (`IdFormaPago`),
KEY `IdMoneda` (`IdMoneda`),
KEY `IdPlan` (`IdPlan`),
KEY `IdTipoDerechos` (`IdTipoDerechos`),
KEY `fk_polizas_status1` (`Status`),
CONSTRAINT `fk_polizas_agentes1` FOREIGN KEY (`IdAgente`) REFERENCES `agentes` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_polizas_clientes1` FOREIGN KEY (`IdCliente`) REFERENCES `clientes` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_polizas_conducto1` FOREIGN KEY (`IdConducto`) REFERENCES `conducto` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_polizas_formapago1` FOREIGN KEY (`IdFormaPago`) REFERENCES `formapago` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_polizas_moneda1` FOREIGN KEY (`IdMoneda`) REFERENCES `moneda` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_polizas_planes1` FOREIGN KEY (`IdPlan`) REFERENCES `planes` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_polizas_status1` FOREIGN KEY (`Status`) REFERENCES `status` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_polizas_tipoderechos1` FOREIGN KEY (`IdTipoDerechos`) REFERENCES `tipoderechos` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This is the TRAMITES table description
CREATE TABLE `tramites` (
`IdTramite` int(11) NOT NULL AUTO_INCREMENT,
`IdTipoTramite` tinyint(1) unsigned NOT NULL,
`Folio` int(11) DEFAULT NULL,
`Poliza` varchar(20) DEFAULT NULL,
`Inicio` date NOT NULL,
`Fin` date DEFAULT NULL,
`Tramite` text,
`Dias` int(11) DEFAULT NULL,
`IdCliente` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`IdTramite`),
KEY `fk_tramites_polizas1` (`Poliza`),
KEY `fk_tramites_tipotramite1` (`IdTipoTramite`),
KEY `fk_tramites_clientes1` (`IdCliente`) USING BTREE,
CONSTRAINT `fk_tramites_polizas1` FOREIGN KEY (`Poliza`) REFERENCES `polizas` (`Poliza`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_tramites_tipotramite1` FOREIGN KEY (`IdTipoTramite`) REFERENCES `tipotramite` (`Id`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_tramite_clientes1` FOREIGN KEY (`IdCliente`) REFERENCES `clientes` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

InnoDB unique keys and indexes

I used the following create statement:
CREATE TABLE `subscr` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`media_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY (`media_id`, `user_id`),
CONSTRAINT FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB
Why does InnoDB only put an index on user_id? I thought it needed indexes on all foreign key references.
CREATE TABLE `subscr` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`media_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `media_id` (`media_id`,`user_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `subscriptions_media_ibfk_1` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE,
CONSTRAINT `subscriptions_media_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB
media_id has an index, too, and it's unique. It's sufficient the way it is for MySQL. There's just a separate index created for user_id, since user_id in the unique index is second, and therefore it's useless for the foreign key.