简体   繁体   中英

Creating table in MySQL fails with error 2103, error 1146 and error 1051

I have a very strange Mysql behaviour.

I try to create a simple new table :

CREATE TABLE IF NOT EXISTS `voucherCampaignProduct` (
 `id` int(12) NOT NULL AUTO_INCREMENT,
 `voucherCampaignId` int(12) NOT NULL,
 `productId` int(10) NOT NULL,
 `shopId` int(10) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`voucherCampaignId`) REFERENCES voucherCampaign(id),
FOREIGN KEY (`productId`) REFERENCES product(id),
INDEX (`shopId`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

When I run this instead of creating the table I recieve a : Error Code: 2013. Lost connection to MySQL server during query

If I run the same query again I recieve a : Error Code: 1146. Table 'schemaName.voucherCampaignProduct' doesn't exist -> Why do I recieve this error on table create ?!

If I drop the table I recieve the error Error Code: 1051. Unknown table 'voucherCampaignProduct' . But AFTER I run this, instead of the error 1146 I recieve again the 2013 error ...

And now the strangest part : The creation of the table without the productId foreign key works perfectly. Even if I use a normal index it works.

Can anybody tell me why i have this strange behaviour ?

I can't say that your problem is same as I am explaining but as per error codes it can be-

First time you got : Error Code: 2013 : which is related with session time out:

It seems DBMS connection read time out option set very low in your query browser and may be table creation statement also took some time due to some problem, so you got this error you can increase this in your browser.

At this time table was created but due to some reason currupted. Reason may be due to case sensitivity as there is an option in mysql lower_case_table_names if you did not set as 1 in your config file then Product and product will be 2 tables but after setting it 1 both will be same table. So some column most prob. Productid is violating such type any mysql rule.

Second time you got: Error Code: 1146 : table does not exist.

At this time due to table currupt you got this error.

Third time you got : Error Code: 1051: table exist during drop.

It was due to table was there but as it was currupted, in this position you can't create table again with same name and can't drop it.

But now you are able to access table, it seems you have re-started your mysql service or due any other action your currupt table has been repaired.

So I have found the error. I created the table without the product index and then tried to add it.

Then I recieved the error : Error code: 1005. Can't create the table 'schema.name'.#sql-24b8_25 (errorno: 150)

After a bit of googling it appeared that my definitions for the child and parent key were not the same. Well the parentID must have also been UNSIGNED... It worked without problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM