MySQL InnoDB unlock a row - mysql

I get a lock timeout when updating a certain row in the database. Other rows update okay.
#1205 - Lock wait timeout exceeded; try restarting transaction
How can I unlock this particular row?
Here are the two related tables. I'm trying to update the email on user. I don't think tenant should be causing any problems though.
CREATE TABLE IF NOT EXISTS `mydb`.`user` (
`username` VARCHAR(45) NOT NULL ,
`email` VARCHAR(60) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`created` TIMESTAMP NULL DEFAULT NULL ,
`last_login` TIMESTAMP NULL ,
PRIMARY KEY (`username`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`tenant` (
`id` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`address` VARCHAR(90) NULL ,
`company` VARCHAR(45) NULL ,
`phone` VARCHAR(25) NOT NULL ,
`fax` VARCHAR(25) NULL ,
`notes` TEXT NULL ,
`contacts` TEXT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_tenant_user1` (`username` ASC) ,
CONSTRAINT `fk_tenant_user1`
FOREIGN KEY (`username` )
REFERENCES `mydb`.`user` (`username` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;

I ended up just running FLUSH TABLE user and it seems fine now.

Try running the following in an interactive mysql client:
SHOW ENGINE INNODB STATUS\G
And look at the currently open transactions. See if one of them is causing the row to be locked.

In my case MySql fixed itself after 6-7 hours. (When I wake up to solve this problem. It was gone)

Related

Can't delete a row from Mysql Not metadata-lock

I am a new-baby in MySQL, so I follow the instructions but there is something wrong! I can delete a whole table except the first row.
Here is the code about create table that I copy from others
CREATE TABLE `top_100` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ranking` INT(11) NOT NULL,
`title` VARCHAR(255) NOT NULL,
`stars` VARCHAR(255) NOT NULL,
`release_time` VARCHAR(255) NOT NULL,
`score` FLOAT(3,1) NOT NULL,
`img_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `title` (`title`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=337;
Now I drop several rows successfully, but the first-row I try everything, but it doesn't work.
here is the row
id ranking title stars release_time score img_url
1 1 '霸王别姬' 0.0
I search several methods like drop table or DELETE FROM top_100 WHERE id=1
, but this instruction doesn't work. The HeidiSQL is stuck!
Someone says you may fall into the metadata lock but I kill the process id
, it doesn't work.
I am confused, having no idea.

Why MySQL does not accept my foreign keys?

I have 26 tables that are very dependent from each other. I crated each table and then executed in the database through MySQL Workbench. Then I build the model where I linked those tables creating the foreign keys. I exported that back in to metadata and I had now the complete code of the tables with the proper foreign data created by the application, so I made not mistakes.
But when I put the file in my server to run by phpMyAdmin, I get a 150 error that does not specify where it is. I Googled it but each case is different and mine is that the FK are not the auto incremented field, but a string field called UT that I created which has the Time Unit when I created a row.
CREATE TABLE IF NOT EXISTS `eduardo8_plataforma`.`aplicativo` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`ut` VARCHAR(20) NOT NULL ,
`nome` VARCHAR(99) NOT NULL ,
`ver` VARCHAR(20) NULL DEFAULT NULL ,
`descr` VARCHAR(254) NULL DEFAULT NULL ,
`tag` VARCHAR(254) NULL DEFAULT NULL ,
`url` VARCHAR(254) NULL DEFAULT NULL ,
`cad` VARCHAR(20) NULL DEFAULT NULL ,
`obj` TEXT NULL DEFAULT NULL ,
`tab` VARCHAR(254) NULL DEFAULT NULL ,
`dbn` VARCHAR(254) NULL DEFAULT NULL ,
`dbu` VARCHAR(254) NULL DEFAULT NULL ,
`dbs` VARCHAR(254) NULL DEFAULT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `nome` (`nome` ASC) ,
UNIQUE INDEX `ut_UNIQUE` (`ut` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_swedish_ci;
CREATE TABLE IF NOT EXISTS `eduardo8_plataforma`.`modulo` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`ut` VARCHAR(20) NOT NULL ,
`app` VARCHAR(20) NULL DEFAULT NULL ,
`lic` VARCHAR(20) NULL DEFAULT NULL ,
`tipo` VARCHAR(20) NULL DEFAULT NULL ,
`nome` VARCHAR(99) NOT NULL ,
`classe` VARCHAR(99) NOT NULL ,
`obj` TEXT NULL DEFAULT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `ut_UNIQUE` (`ut` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_swedish_ci;
CREATE TABLE IF NOT EXISTS `eduardo8_plataforma`.`modulo_app` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`ut` VARCHAR(20) NOT NULL ,
`app` VARCHAR(20) NOT NULL ,
`modulo` VARCHAR(20) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_modulo_app_modulo1` (`modulo` ASC) ,
INDEX `fk_modulo_app_aplicativo1` (`app` ASC) ,
UNIQUE INDEX `ut_UNIQUE` (`ut` ASC) ,
CONSTRAINT `fk_modulo_app_modulo1`
FOREIGN KEY (`modulo` )
REFERENCES `eduardo8_plataforma`.`modulo` (`ut` )
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_modulo_app_aplicativo1`
FOREIGN KEY (`app` )
REFERENCES `eduardo8_plataforma`.`aplicativo` (`ut` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_swedish_ci;
The table that worked (after answers):
CREATE TABLE IF NOT EXISTS `eduardo8_plataforma`.`modulo_app` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`ut` VARCHAR(20) CHARACTER SET 'utf8' COLLATE 'utf8_swedish_ci' UNIQUE NOT NULL ,
`app` VARCHAR(20) CHARACTER SET 'utf8' COLLATE 'utf8_swedish_ci' NOT NULL ,
`modulo` VARCHAR(20) CHARACTER SET 'utf8' COLLATE 'utf8_swedish_ci' NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_modulo_app_modulo1` (`modulo` ASC) ,
INDEX `fk_modulo_app_aplicativo1` (`app` ASC) ,
UNIQUE INDEX `ut_UNIQUE` (`ut` ASC) ,
CONSTRAINT `fk_modulo_app_modulo1`
FOREIGN KEY (`modulo` )
REFERENCES `eduardo8_plataforma`.`modulo` (`ut` )
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_modulo_app_aplicativo1`
FOREIGN KEY (`app` )
REFERENCES `eduardo8_plataforma`.`aplicativo` (`ut` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_swedish_ci;
You need to move the CREATE INDEX clauses before the foreign keys, as foreign key cannot reference non-indexed field.
The short story is that a foreign key should reference a candidate key column. That is, it should reference a column that is declared either
primary key, or
not null unique.
A standard SQL dbms will raise an error if you reference a column that isn't unique. MySQL should, too, but it won't.
Mysql Docs say
. . . the system does not enforce a requirement that the referenced columns
be UNIQUE or be declared NOT NULL. The handling of foreign key
references to nonunique keys or keys that contain NULL values is not
well defined for operations such as UPDATE or DELETE CASCADE. You are
advised to use foreign keys that reference only UNIQUE (including
PRIMARY) and NOT NULL keys.
If you need non-unique data from another table, join it in a query, or join it in a view.

MySQL - delete rows rejected on INNODB table

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;

Can't create mysql table with foreign key

I am getting errno 150 when I try to create the following two tables.
CREATE TABLE `mydatabase`.`userstatus`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(255) ,
PRIMARY KEY (`id`)
);
CREATE TABLE `mydatabase`.users(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`email` VARCHAR(200) NOT NULL ,
`username` VARCHAR(20) NOT NULL DEFAULT 'Unknown' ,
`userstatusid` INT UNSIGNED NOT NULL DEFAULT 2 ,
`datemodified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`datecreated` DATETIME DEFAULT 0 NOT NULL ,
PRIMARY KEY (`id`),
UNIQUE (username),
UNIQUE (email),
INDEX userstatusid_index (userstatusid),
CONSTRAINT fk_users_userstatus FOREIGN KEY (userstatusid) REFERENCES userstatus(id)
ON DELETE SET NULL
ON UPDATE CASCADE
) ENGINE=INNODB ROW_FORMAT=DEFAULT ;
What's causing the error and how do I fix it?
I put on delete set null but the column has been defined as not null. I was focusing too much attention to the types which usually causes the errno 150.
Maybe you could try set a Unique key on userstatusid.
Sometimes that may help.
You can try SHOW INNODB STATUS which will show the last InnoDB error (e.g. foreign key constraint definition problems).
(Otherwise you have to just guess based on the error number and limited information which the error from the statement provides, which isn't ideal.)

MySQL: Creating table with FK error (errno 150)

I've created a model with MySQL Workbench and am now attempting to install it to a mysql server.
Using File > Export > Forward Engineer SQL CREATE Script... it outputs a nice big file for me, with all the settings I ask for. I switch over to MySQL GUI Tools (the Query Browser specifically) and load up this script (note that I'm going form one official MySQL tool to another). However, when I try to actually execute this file, I get the same error over and over
SQLSTATE[HY000]: General error: 1005
Can't create table
'./srs_dev/location.frm' (errno: 150)
"OK", I say to myself, something is wrong with the location table. So I check out the definition in the output file.
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
-- -----------------------------------------------------
-- Table `state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `state` ;
CREATE TABLE IF NOT EXISTS `state` (
`state_id` INT NOT NULL AUTO_INCREMENT ,
`iso_3166_2_code` VARCHAR(2) NOT NULL ,
`name` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`state_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brand`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `brand` ;
CREATE TABLE IF NOT EXISTS `brand` (
`brand_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`domain` VARCHAR(45) NOT NULL ,
`manager_name` VARCHAR(100) NULL ,
`manager_email` VARCHAR(255) NULL ,
PRIMARY KEY (`brand_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `location`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `location` ;
CREATE TABLE IF NOT EXISTS `location` (
`location_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NOT NULL ,
`address_line_1` VARCHAR(255) NULL ,
`address_line_2` VARCHAR(255) NULL ,
`city` VARCHAR(100) NULL ,
`state_id` TINYINT UNSIGNED NULL DEFAULT NULL ,
`postal_code` VARCHAR(10) NULL ,
`phone_number` VARCHAR(20) NULL ,
`fax_number` VARCHAR(20) NULL ,
`lat` DECIMAL(9,6) NOT NULL ,
`lng` DECIMAL(9,6) NOT NULL ,
`contact_url` VARCHAR(255) NULL ,
`brand_id` TINYINT UNSIGNED NOT NULL ,
`summer_hours` VARCHAR(255) NULL ,
`winter_hours` VARCHAR(255) NULL ,
`after_hours_emergency` VARCHAR(255) NULL ,
`image_file_name` VARCHAR(100) NULL ,
`manager_name` VARCHAR(100) NULL ,
`manager_email` VARCHAR(255) NULL ,
`created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`location_id`) ,
CONSTRAINT `fk_location_state`
FOREIGN KEY (`state_id` )
REFERENCES `state` (`state_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_location_brand`
FOREIGN KEY (`brand_id` )
REFERENCES `brand` (`brand_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_location_state` ON `location` (`state_id` ASC) ;
CREATE INDEX `fk_location_brand` ON `location` (`brand_id` ASC) ;
CREATE INDEX `idx_lat` ON `location` (`lat` ASC) ;
CREATE INDEX `idx_lng` ON `location` (`lng` ASC) ;
Looks ok to me. I surmise that maybe something is wrong with the Query Browser, so I put this file on the server and try to load it this way
] mysql -u admin -p -D dbname < path/to/create_file.sql
And I get the same error. So I start to Google this issue and find all kinds of accounts that talk about an error with InnoDB style tables that fail with foreign keys, and the fix is to add "SET FOREIGN_KEY_CHECKS=0;" to the SQL script. Well, as you can see, that's already part of the file that MySQL Workbench spat out.
So, my question is then, why is this not working when I'm doing what I think I'm supposed to be doing?
Version Info:
MySQL: 5.0.45
GUI Tools: 1.2.17
Workbench: 5.0.30
The type of the field in a foreign key must be the same as the type of the column they're referencing. You have the following (snipping):
CREATE TABLE IF NOT EXISTS `state` (
`state_id` INT NOT NULL AUTO_INCREMENT ,
...
CREATE TABLE IF NOT EXISTS `brand` (
`brand_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
...
CREATE TABLE IF NOT EXISTS `location` (
...
`state_id` TINYINT UNSIGNED NULL DEFAULT NULL ,
...
`brand_id` TINYINT UNSIGNED NOT NULL ,
so you're trying to refer to INT fields (in tables state and brand) with TINYINT fields in table location. I think that's the error it's complaining about. Not sure how it came up in the first place, or why zeroing out FOREIGN_KEY_CHECKS doesn't stop MySQL from diagnosing the error, but what happens if you fix this type mismatch?
For others looking at this thread, there are a LOT of reasons that you can get this error:
See the following link for a complete list for errno 150, errno 121 and other MySQL Foreign Key Errors:
MySQL Foreign Key Errors and Errno 150
just checked #juacala 's answer. But didn't help me. Here's the message I sent #ELIACOM when I found my source of trouble.:
I was reading this great source of solutions, but my issue is not
adressed. I was trying to add an FK in a INNODB table pointing to a
PK in a MyISAM. After setting the MyIsam to INNODB it worked. I
checked your whole list before realizing that. Cheers.
I had same issue. Somehow I didn't notice to set auto_increment value to the table id.
Maybe it helps someone.