MySQL - delete rows rejected on INNODB table - mysql

I have two tables - a user table and a userlog table.
CREATE TABLE `client_user` (
`id_client_user` int(11) NOT NULL auto_increment,
`Nom` varchar(45) NOT NULL,
`Prenom` varchar(45) NOT NULL,
`email` varchar(255) NOT NULL,
`userid` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`active` tinyint(1) NOT NULL default '0',
`lastaccess` timestamp NULL default NULL,
`user_must_change_pwd` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id_client_user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_log` (
`id_user_log` int(11) NOT NULL auto_increment,
`access` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`zone_updated` varchar(255) NOT NULL,
`id_client_user` int(11) NOT NULL,
PRIMARY KEY (`id_user_log`),
KEY `fk_user_log_client_user1` (`id_client_user`),
CONSTRAINT `fk_user_log_client_user1`
FOREIGN KEY (`id_client_user`)
REFERENCES `client_user` (`id_client_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I create a user in the client_user table and then his activity is logged within the user_log table.
I now need to delete rows in the user_log table.
This is rejected because of the foreign key constraint - that much I have understood.
After having looked at the documentation, I have not seen how I can change the foreign key to allow me to delete the user_log records.
What I need is a foreign key (1:n), client_user (1) to user_log (n), where user_log records can be deleted without impacting the associated client_user record.
I am sure that this is possible with innodb, but I cannot see how.
Help ?

From the specification
InnoDB supports the use of ALTER TABLE to drop foreign keys:
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;

Related

Error creating foreign key on userid, followerid (check data types)

Hey I'm having issues setting foreign keys pointing from my followings table:
CREATE TABLE `followings` (
`id` int(26) NOT NULL,
`userid` varchar(256) NOT NULL,
`followerid` varchar(256) NOT NULL,
`time` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
to my users table:
CREATE TABLE `users` (
`id` int(26) NOT NULL,
`userid` varchar(256) NOT NULL,
`handle` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`password` varchar(256) NOT NULL,
`phone` varchar(256) NOT NULL,
`dateofbirth` varchar(99) NOT NULL,
`phoneverified` varchar(25) DEFAULT NULL,
`emailverified` varchar(25) DEFAULT NULL,
`authentic` tinyint(1) NOT NULL DEFAULT 0,
`datecreated` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `userid` (`userid`),
ADD UNIQUE KEY `handle` (`handle`);
I have checked the collation etc of the fields are the same yet i am still getting an error on phpmyadmin
Error creating foreign key on userid, followerid (check data types)
Any possible reasons for such error
This is the sql generated by phpmyadmin before the error
ALTER TABLE `followings` ADD FOREIGN KEY (`userid`, `followerid`) REFERENCES `users`(`userid`, `userid`) ON DELETE RESTRICT ON UPDATE RESTRICT;
You shouldn't be making a multi-key foreign key. You need separate foreign keys for userid and followerid, since they refer to different rows in the users table.
ALTER TABLE followings ADD FOREIGN KEY (userid) REFERENCES users (userid);
ALTER TABLE followings ADD FOREIGN KEY (followerid) REFERENCES users (userid);
Also, it's usually preferable to have the foreign key reference the primary key.

Enhancing table MYSQL Performance

I have a table with the following scheme :
CREATE TABLE `type_interests` (
`id` int(11) NOT NULL,
`interest_id` int(11) NOT NULL,
`type_id` int(11) NOT NULL,
`type` varchar(64) NOT NULL,
`status` varchar(64) NOT NULL,
`created_date` datetime NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`notes` varchar(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `type_interests`
ADD PRIMARY KEY (`id`),
ADD KEY `interest_id` (`interest_id`,`type_id`),
ADD KEY `interest_id_2` (`interest_id`);
ALTER TABLE `type_interests`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31415;
ALTER TABLE `type_interests`
ADD CONSTRAINT `type_interests_ibfk_1` FOREIGN KEY (`interest_id`) REFERENCES `interests` (`id`);
And i have only about 30,000 records in the database but all queries which use this table is consuming alot of time, is it a design problem? note that type_id is linked to more than one table.
You should index your table properly, more information here: http://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html
It depends on your server aswell, you can "tune" your MySQL with this script: https://www.howtoforge.com/tuning-mysql-performance-with-mysqltuner

"Foreign key contraint" error message although there are no foreign keys

I am trying to change the engine for my table "adverts". It does not let me and I get the message that "a foreign key contraint fails". But I removed all foreign keys and SHOW CREATE TABLE gives me this:
CREATE TABLE `adverts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_users` int(11) NOT NULL,
`id_categories_adverts` int(11) NOT NULL,
`type` int(11) NOT NULL,
`status` int(11) NOT NULL,
`duration` int(11) NOT NULL,
`headline` varchar(200) NOT NULL,
`description` text NOT NULL,
`show_contact` int(11) NOT NULL,
`stamp_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`stamp_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8
Why can't I change the engine of my table? I am using
ALTER TABLE adverts ENGINE=MyISAM
The problem is that there is at least another table with foreign key to your adverts table. You need to tackle with those foreign key constraints, possibly, by temporarily/permanently removing them.

Change master table PK and update related table FK (changing PK from Autoincrement to UUID on Mysql)

I have two related tables: Groups and Clients. Clients belongs to Groups so I have a foreign key "group_id" that references the group a client belongs to.
I'm changing the Group id from an autoincrement to a UUID. So what I need is to generate a UUID for each Group and update the Clients table at once to reflect the changes and keep the records related.
Is there a way to do this with multiple-table update on MySQL?
Adding tables definitions for clarification.
CREATE TABLE `groups` (
`id` char(36) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `clients` (
`id` char(36) NOT NULL,
`name` varchar(255) NOT NULL,
`group_id` char(36) DEFAULT NULL,
`active` tinyint(1) DEFAULT '1'
PRIMARY KEY (`id`),
KEY `fkgp` (`group_id`),
CONSTRAINT `fkgp` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`)
ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$

on duplicate key update fails with error "Cannot add or update a child row"

I have a table with a compound primary key "name" and "id". The fields are actually "name","id","phone","amount","units","alias". I have the query
insert into MyTable (name,id,phone,amount) select "henry" as name, id,phone,amount from anotherTable
on duplicate key update phone=values(phone),amount=values(amount).
MySQL spits the following error:
Cannot add or update a child row: a foreign key constraint fails.
BTW, "id" is a foreign key.
Any help?
as requested below, the schema for other table is
CREATE TABLE `otherTable` (
`otherId` int(11) NOT NULL AUTO_INCREMENT,
`DOBId` int(11) NOT NULL,
`bankAccount` int(11) DEFAULT NULL,
`partialAmount` int(11) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`notes` varchar(299) DEFAULT NULL,
`latitude` decimal(8,5) DEFAULT NULL,
`longitude` decimal(8,5) DEFAULT NULL,
PRIMARY KEY (`otherId `),
KEY `DOBId ` (`DOBId `),
KEY `bankAccount ` (`bankAccount `),
KEY `id ` (`id `)
) ENGINE=InnoDB AUTO_INCREMENT=3305 DEFAULT CHARSET=utf8;
for myTable
CREATE TABLE `myTable` (
`name` int(11) NOT NULL,
`id` int(11) NOT NULL,
`appleNumber` int(11) DEFAULT NULL,
`amount` int(11) DEFAULT NULL,
`windowsNumber` int(11) DEFAULT NULL,
`phone` int(11) DEFAULT NULL,
`pens` int(11) DEFAULT NULL,
`pencils` int(11) DEFAULT NULL,
PRIMARY KEY (`name`,`id`),
KEY `id` (`id`),
CONSTRAINT `myTable_ibfk_1` FOREIGN KEY (`id`) REFERENCES `yet_another` (`id`)
The problem appears to be that the FK constraint you have on myTable is referencing the ids of yet_another, so when you are inserting ids from anotherTable you are breaking this FK constraint. Chances are there are ids in anotherTable that do not exist in yet_another table.
Understand this is a shot in the dark, based on the abstracted schema you posted. If you want a more solid answer, I'd have to see the actual schema.
The on duplicate key applies to the primary key. I take it you're using innodb. This is failing on a foreign key constraint. Which values of you table MyTable are foreign keys? Please post the create statement for the table that shows all keys and constraints for more detailed help.
Just a guess, but for grins I'm betting it's a column that's not in the insert that is a foreign key not allowing a null value.