Finding the nth query in a very long sql file - sql

I am getting this error on import of a ~1g SQL dump:
[ERROR in query 620] Row size too large (> 8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help. In current row format, BLOB prefix of 768 bytes is stored inline.
I would like to look at this query, but there are 4537 of them (at least by count of the semicolons) so how do I find the 620th of them quickly? I opened the sql file in xcode, but I am open to grepping if that is the solution.
Per Steven’s request I am generalizing this more. Say we have an sql file of form:
# Dump of table table1
# ------------------------------------------------------------
DROP TABLE IF EXISTS `table1`;
CREATE TABLE `table1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(255) NOT NULL DEFAULT '',
`c` varchar(255) NOT NULL DEFAULT '',
`d` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`a`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dump of table table2
# ------------------------------------------------------------
DROP TABLE IF EXISTS `table2`;
CREATE TABLE `table2` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(255) NOT NULL DEFAULT '',
`c` varchar(255) NOT NULL DEFAULT '',
`d` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`a`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
...
# Dump of table table310
# ------------------------------------------------------------
DROP TABLE IF EXISTS `table310`;
CREATE TABLE `table310` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(255) NOT NULL DEFAULT '',
`c` varchar(255) NOT NULL DEFAULT '',
`d` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`a`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dump of table table311
# ------------------------------------------------------------
DROP TABLE IF EXISTS `table311`;
CREATE TABLE `table311` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(255) NOT NULL DEFAULT '',
`c` varchar(255) NOT NULL DEFAULT '',
`d` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`a`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So in this example (... represents intervening tables) the query that starts with CREATE TABLE table310 would be the 620th Query if everything followed this pattern. Say the pattern and naming convention was not so regular, how would I be able to go to the 620th query referenced in the Error?

#!/usr/bin/awk -f
BEGIN {
RS = ORS = ";"
}
NR == 6
Result
CREATE TABLE `table310` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(255) NOT NULL DEFAULT '',
`c` varchar(255) NOT NULL DEFAULT '',
`d` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`a`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Related

How to resolve 'Foreign key constraint is incorrectly formed' issue

I am trying to create a database table called wp_tokens with a foreign key relationship to another table called wp_users. However, every time I attempt to run the create table SQL, I get the error "Foreign key constraint is incorrectly formed". I have tried multiple re-edits of the same code, but I just cannot figure out what is going on.
This is the code for wp_users
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
This is the SQL for wp_tokens
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Any help would be greatly appreciated!
The unsigned is important -- the types need to be identical. Try this:
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Here is a rextester.

Error when creating multiple columns in one table with same values

When I try to exeecute the folliwng in SQL, I get error, I am trying to add multiple columns with same value to one table.
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
), `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
), `Brand_Name` varchar(255) NOT NULL
), `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ERROR: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' Vendor_SKU_or_Stock_Number varchar(255) NOT NULL
), Brand_Name varchar(255)' at line 13
Please help
You have too many closing parenthesis, and your table name and field names should not be quoted.
Try this:
CREATE TABLE IF NOT EXISTS vendor (
product_id varchar(255) NOT NULL
, Vendor_SKU_or_Stock_Number varchar(255) NOT NULL
, Brand_Name varchar(255) NOT NULL
, Image_URL5 varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Removed ")"
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
, `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
, `Brand_Name` varchar(255) NOT NULL
, `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Try this
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
, `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
, `Brand_Name` varchar(255) NOT NULL
, `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Try this
CREATE TABLE IF NOT EXISTS vendor (
product_id varchar(255) NOT NULL,
vendor_SKU_or_Stock_Number varchar(255) NOT NULL,
brand_Name varchar(255) NOT NULL,
image_URL5 varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARACTER SET=latin1;

Why is the comma messing up the insert

here is my query
insert into invoices
set Invoice_number = '823N9823',
price = '11,768.00',
code = 'ret_4_business',
created_at = '2010-09-27';
but my price in my db is 11, not 11768
how do i handle money in mysql? the field is a decimal
CREATE TABLE `invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Invoice_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`price` decimal(10,0) DEFAULT NULL,
`code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Any non-number character will terminate the parsing and save only part of the number. Remove the comma characters from the query.

MySQL slow query

SELECT
items.item_id, items.category_id, items.title, items.description, items.quality,
items.type, items.status, items.price, items.posted, items.modified,
zip_code.state_prefix, zip_code.city, books.isbn13, books.isbn10, books.authors,
books.publisher
FROM
(
(
items
LEFT JOIN bookitems ON items.item_id = bookitems.item_id
)
LEFT JOIN books ON books.isbn13 = bookitems.isbn13
)
LEFT JOIN zip_code ON zip_code.zip_code = items.item_zip
WHERE items.rid = $rid`
I am running this query to get the list of a user's items and their location. The zip_code table has over 40k records and this might be the issue. It currently takes up to 15 seconds to return a list of about 20 items! What can I do to make this query more efficient?
UPDATE: The following is the table creation code for the relevant tables. Sorry about the formatting!
CREATE TABLE `bookitems` (
`bookitem_id` int(10) unsigned NOT NULL auto_increment COMMENT 'BookItem ID',
`item_id` int(10) unsigned NOT NULL default '0' COMMENT 'Item ID',
`isbn13` varchar(13) NOT NULL default '' COMMENT 'Book ISBN13',
`modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Date of Last Modification',
PRIMARY KEY (`bookitem_id`),
UNIQUE KEY `item_id` (`item_id`),
KEY `fk_bookitems_isbn13` (`isbn13`),
CONSTRAINT `fk_bookitems_isbn13` FOREIGN KEY (`isbn13`) REFERENCES `books` (`isbn13`),
CONSTRAINT `fk_bookitems_item_id` FOREIGN KEY (`item_id`) REFERENCES `items` (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=latin1;
CREATE TABLE `books` (
`isbn13` varchar(13) NOT NULL default '' COMMENT 'Book ISBN13 (pk)',
`isbn10` varchar(10) NOT NULL default '' COMMENT 'Book ISBN10 (u)',
`title` text NOT NULL COMMENT 'Book Title',
`title_long` text NOT NULL,
`authors` text NOT NULL COMMENT 'Book Authors',
`publisher` text NOT NULL COMMENT 'ISBNdb publisher_text',
PRIMARY KEY (`isbn13`),
UNIQUE KEY `isbn10` (`isbn10`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `items` (
`item_id` int(10) unsigned NOT NULL auto_increment COMMENT 'Item ID',
`rid` int(10) unsigned NOT NULL default '0' COMMENT 'Item Owner User ID',
`category_id` int(10) unsigned NOT NULL default '0' COMMENT 'Item Category ID',
`title` tinytext NOT NULL COMMENT 'Item Title',
`description` text NOT NULL COMMENT 'Item Description',
`quality` enum('0','1','2','3','4','5') NOT NULL default '0' COMMENT 'Item Quality',
`type` enum('forsale','wanted','pending') NOT NULL default 'pending' COMMENT 'Item Status',
`price` int(6) unsigned NOT NULL default '0' COMMENT 'Price',
`posted` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'Date of Listing',
`modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Date of Last Modification',
`status` enum('sold','found','flagged','removed','active','expired') NOT NULL default 'active',
`item_zip` int(5) unsigned zerofill NOT NULL default '00000',
PRIMARY KEY (`item_id`),
KEY `fk_items_rid` (`rid`),
KEY `fk_items_category_id` (`category_id`),
CONSTRAINT `fk_items_category_id` FOREIGN KEY (`category_id`) REFERENCES `categories` (`category_id`),
CONSTRAINT `fk_items_rid` FOREIGN KEY (`rid`) REFERENCES `users` (`rid`)
) ENGINE=InnoDB AUTO_INCREMENT=123 DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`rid` int(10) unsigned NOT NULL auto_increment COMMENT 'User ID',
`fid` int(10) unsigned NOT NULL default '0' COMMENT 'Facebook User ID',
`role_id` int(10) unsigned NOT NULL default '4',
`zip` int(5) unsigned zerofill NOT NULL default '00000' COMMENT 'Zip Code',
`joined` timestamp NOT NULL default CURRENT_TIMESTAMP COMMENT 'INSERT Timestamp',
`email` varchar(255) NOT NULL default '',
`notes` varchar(255) NOT NULL default '',
PRIMARY KEY (`rid`),
UNIQUE KEY `fid` (`fid`),
KEY `fk_users_role` (`role_id`),
CONSTRAINT `fk_users_role` FOREIGN KEY (`role_id`) REFERENCES `roles` (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1013 DEFAULT CHARSET=latin1;
CREATE TABLE `zip_code` (
`id` int(11) unsigned NOT NULL auto_increment,
`zip_code` varchar(5) character set utf8 collate utf8_bin NOT NULL,
`city` varchar(50) character set utf8 collate utf8_bin default NULL,
`county` varchar(50) character set utf8 collate utf8_bin default NULL,
`state_name` varchar(50) character set utf8 collate utf8_bin default NULL,
`state_prefix` varchar(2) character set utf8 collate utf8_bin default NULL,
`area_code` varchar(3) character set utf8 collate utf8_bin default NULL,
`time_zone` varchar(50) character set utf8 collate utf8_bin default NULL,
`lat` float NOT NULL,
`lon` float NOT NULL,
`search_string` varchar(52) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `zip_code` (`zip_code`)
) ENGINE=MyISAM AUTO_INCREMENT=42625 DEFAULT CHARSET=utf8;
SELECT items.item_id,
items.category_id,
items.title,
items.description,
items.quality,
items.TYPE,
items.status,
items.price,
items.posted,
items.modified,
zip_code.state_prefix,
zip_code.city,
books.isbn13,
books.isbn10,
books.authors,
books.publisher
FROM items
LEFT JOIN
bookitems
ON bookitems.item_id = items.item_id
LEFT JOIN
books
ON books.isbn13 = bookitems.isbn13
LEFT JOIN
zip_code
ON zip_code.zip_code = items.item_zip
WHERE items.rid = $rid
Create the following indexes:
items (rid)
bookitems (item_id)
books (isbn13)
zip_code (zip_code)
Your first option should be to optimise your database.
Is all those 40k rows useful data? Or can you move some old data to a table containing archived data? Are you using proper indexing? The list goes on..
The first question is what indexes you have. Do you have an index on zip_code (zip_code) ? How big are your other tables? Indexes that would obviously help are, as I say, zip_code (zip_code), then items (rid), bookitems (item_id), and books (isbn13).
I'd think you would almost certainly want on index on zip_code, that's likely used in almost any query against that table. You likely also want indexes on isbn13 and bookitems (item_id). I don't know what items (rid) is supposed to be. An index on that would help this query, but might or might not be generally useful.
Besides that, there are no obvious flaws in the query.
BTW, the parentheses around "items left join bookitems ..." are superfluous. Tables are joined left to right by default.
I am not sure about this just by looking at the query, but
It seems you are joining your rows with string data types, that is a bit slower to compare than using integers, like IDs, specially if not indexed.

Mysql Syntax Error

Any idea why this is popping up :( ?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `clocks` ( `id` int(11) NOT NULL AUTO_INCREMENT, ' at line 6
** Here is the query **
CREATE TABLE `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientname` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=127 DEFAULT CHARSET=latin1;
CREATE TABLE `clocks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`projid` int(11) DEFAULT NULL,
`staffid` int(11) DEFAULT NULL,
`clientid` int(11) DEFAULT NULL,
`desc` longtext,
`hours` varchar(255) DEFAULT NULL,
`date` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
CREATE TABLE `config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key` varchar(255) DEFAULT NULL,
`value` longtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
CREATE TABLE `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientid` int(11) DEFAULT NULL,
`projectname` varchar(255) DEFAULT NULL,
`projectdesc` longtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=36 DEFAULT CHARSET=latin1;
CREATE TABLE `staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`active` int(11) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
CREATE TABLE `temp_clocks` (
`id` int(11) DEFAULT NULL,
`projid` int(11) DEFAULT NULL,
`staffid` int(11) DEFAULT NULL,
`clientid` int(11) DEFAULT NULL,
`desc` longtext,
`timestamp` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
You didn't separate multiple queries with a semicolon.
Also, mysql_query doesn't take multiple queries. Use the new mysqli extension and mysqli::multi_query for that.
You've specified a size of 11 for an int type - but this only comes in 1,2,,4 and 8 byte flavours and IIRC the size is implicit in the type (tinyint, smallint, mediumint, int and bigint).
C.