This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I made this using Spring Data. I can't figure whats the problem with this sql statement.This query was auto generated by Spring data.
It says.
#1064 - 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 'database bit not null, designation
varchar(255), equipment varchar(255), hardwar' at line 1
My query:
create table joborder (id bigint not null auto_increment,
barPrinter bit not null,
birPermit bit not null,
cabling bit not null,
cashDrawer bit not null,
cashierPrinter bit not null,
ccccods bit not null,
configuration bit not null,
cpu bit not null,
database bit not null,
designation varchar(255),
equipment varchar(255),
hardwareActionTaken varchar(255),
hardwareOthers bit not null,
hardwareRemarks varchar(255),
invoiceNumber varchar(255),
keyboard bit not null,
kitchenPrinter bit not null,
logNumber varchar(255),
magneticCardReader bit not null,
mallInterface bit not null,
menu bit not null,
monitor bit not null,
mouse bit not null,
operatingSystem bit not null,
othersActionTaken varchar(255),
othersOthers bit not null,
othersRemarks varchar(255),
powerSupply bit not null,
reportProblemTask varchar(255),
scanner bit not null,
softwareActionTaken varchar(255),
softwareOthers bit not null,
softwareRemarks varchar(255),
storeLocation varchar(255),
storeRepresentative varchar(255),
technician varchar(255),
timeIn varchar(255),
timeOut varchar(255),
transightECLM bit not null,
transightHQ bit not null,
transightPOS bit not null,
uat bit not null,
store_id bigint,
primary key (id))
Use this query
CREATE TABLE `joborder` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`barPrinter` BIT(1) NOT NULL,
`birPermit` BIT(1) NOT NULL,
`cabling` BIT(1) NOT NULL,
`cashDrawer` BIT(1) NOT NULL,
`cashierPrinter` BIT(1) NOT NULL,
`ccccods` BIT(1) NOT NULL,
`configuration` BIT(1) NOT NULL,
`cpu` BIT(1) NOT NULL,
`database` BIT NOT NULL,
`designation` VARCHAR(255) DEFAULT NULL,
`equipment` VARCHAR(255) DEFAULT NULL,
`hardwareActionTaken` VARCHAR(255) DEFAULT NULL,
`hardwareOthers` BIT(1) NOT NULL,
`hardwareRemarks` VARCHAR(255) DEFAULT NULL,
`invoiceNumber` VARCHAR(255) DEFAULT NULL,
`keyboard` BIT(1) NOT NULL,
`kitchenPrinter` BIT(1) NOT NULL,
`logNumber` VARCHAR(255) DEFAULT NULL,
`magneticCardReader` BIT(1) NOT NULL,
`mallInterface` BIT(1) NOT NULL,
`menu` BIT(1) NOT NULL,
`monitor` BIT(1) NOT NULL,
`mouse` BIT(1) NOT NULL,
`operatingSystem` BIT(1) NOT NULL,
`othersActionTaken` VARCHAR(255) DEFAULT NULL,
`othersOthers` BIT(1) NOT NULL,
`othersRemarks` VARCHAR(255) DEFAULT NULL,
`powerSupply` BIT(1) NOT NULL,
`reportProblemTask` VARCHAR(255) DEFAULT NULL,
`scanner` BIT(1) NOT NULL,
`softwareActionTaken` VARCHAR(255) DEFAULT NULL,
`softwareOthers` BIT(1) NOT NULL,
`softwareRemarks` VARCHAR(255) DEFAULT NULL,
`storeLocation` VARCHAR(255) DEFAULT NULL,
`storeRepresentative` VARCHAR(255) DEFAULT NULL,
`technician` VARCHAR(255) DEFAULT NULL,
`timeIn` VARCHAR(255) DEFAULT NULL,
`timeOut` VARCHAR(255) DEFAULT NULL,
`transightECLM` BIT(1) NOT NULL,
`transightHQ` BIT(1) NOT NULL,
`transightPOS` BIT(1) NOT NULL,
`uat` BIT(1) NOT NULL,
`store_id` BIGINT(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;
Database is a reserved keyword
you have to use like this Databse
Try this
create table joborder (id bigint not null auto_increment,
barPrinter bit not null,
birPermit bit not null,
cabling bit not null,
cashDrawer bit not null,
cashierPrinter bit not null,
ccccods bit not null,
configuration bit not null,
cpu bit not null,
`database` bit not null,
designation varchar(255),
equipment varchar(255),
hardwareActionTaken varchar(255),
hardwareOthers bit not null,
hardwareRemarks varchar(255),
invoiceNumber varchar(255),
keyboard bit not null,
kitchenPrinter bit not null,
logNumber varchar(255),
magneticCardReader bit not null,
mallInterface bit not null,
menu bit not null,
monitor bit not null,
mouse bit not null,
operatingSystem bit not null,
othersActionTaken varchar(255),
othersOthers bit not null,
othersRemarks varchar(255),
powerSupply bit not null,
reportProblemTask varchar(255),
scanner bit not null,
softwareActionTaken varchar(255),
softwareOthers bit not null,
softwareRemarks varchar(255),
storeLocation varchar(255),
storeRepresentative varchar(255),
technician varchar(255),
timeIn varchar(255),
timeOut varchar(255),
transightECLM bit not null,
transightHQ bit not null,
transightPOS bit not null,
uat bit not null,
store_id bigint,
primary key (id))
you should try this: and check this (http://sqlfiddle.com/#!2/4817b)
CREATE TABLE joborder
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
barPrinter BIT(1) NOT NULL,
birPermit BIT(1) NOT NULL,
cabling BIT(1) NOT NULL,
cashDrawer BIT(1) NOT NULL,
cashierPrinter BIT(1) NOT NULL,
ccccods BIT(1) NOT NULL,
configuration BIT(1) NOT NULL,
cpu BIT(1) NOT NULL,
`database` BIT NOT NULL,
designation VARCHAR(255) DEFAULT NULL,
equipment VARCHAR(255) DEFAULT NULL,
hardwareActionTaken VARCHAR(255) DEFAULT NULL,
hardwareOthers BIT(1) NOT NULL,
hardwareRemarks VARCHAR(255) DEFAULT NULL,
invoiceNumber VARCHAR(255) DEFAULT NULL,
keyboard BIT(1) NOT NULL,
kitchenPrinter BIT(1) NOT NULL,
logNumber VARCHAR(255) DEFAULT NULL,
magneticCardReader BIT(1) NOT NULL,
mallInterface BIT(1) NOT NULL,
menu BIT(1) NOT NULL,
monitor BIT(1) NOT NULL,
mouse BIT(1) NOT NULL,
operatingSystem BIT(1) NOT NULL,
othersActionTaken VARCHAR(255) DEFAULT NULL,
othersOthers BIT(1) NOT NULL,
othersRemarks VARCHAR(255) DEFAULT NULL,
powerSupply BIT(1) NOT NULL,
reportProblemTask VARCHAR(255) DEFAULT NULL,
scanner BIT(1) NOT NULL,
softwareActionTaken VARCHAR(255) DEFAULT NULL,
softwareOthers BIT(1) NOT NULL,
softwareRemarks VARCHAR(255) DEFAULT NULL,
storeLocation VARCHAR(255) DEFAULT NULL,
storeRepresentative VARCHAR(255) DEFAULT NULL,
technician VARCHAR(255) DEFAULT NULL,
timeIn VARCHAR(255) DEFAULT NULL,
timeOut VARCHAR(255) DEFAULT NULL,
transightECLM BIT(1) NOT NULL,
transightHQ BIT(1) NOT NULL,
transightPOS BIT(1) NOT NULL,
uat BIT(1) NOT NULL,
store_id BIGINT(20) DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=INNODB DEFAULT CHARSET=latin1;
Related
I have mariadb database that used to have CHARSET utf8 COLLATE utf8_general_ci config but now CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci. All tables have the same CHARSET and COLLATE as those of the database.
When I run ./manage.py test, stacktrace looks like this:
....
django.db.utils.OperationalError: (1118, 'Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current row format, BLOB prefix of 0 bytes is stored inline.')
I managed to find out what the troubling table is, and the sql query looks like the following. Note that I changed names of table and fields for security:
CREATE TABLE `troubling_table`
(
`id` INTEGER auto_increment NOT NULL PRIMARY KEY,
`no_tax` VARCHAR(20) NOT NULL,
`cd_pc` VARCHAR(7) NOT NULL,
`cd_wdept` VARCHAR(12) NOT NULL,
`id_write` VARCHAR(20) NULL,
`cd_docu` VARCHAR(10) NULL,
`dt_acct` VARCHAR(8) NULL,
`st_docu` VARCHAR(3) NULL,
`tp_drcr` VARCHAR(3) NULL,
`cd_acct` VARCHAR(20) NULL,
`amt` NUMERIC(19, 4) NULL,
`cd_partner` VARCHAR(20) NULL,
`nm_partner` VARCHAR(50) NULL,
`tp_job` VARCHAR(40) NULL,
`cls_job` VARCHAR(40) NULL,
`ads_hd` VARCHAR(400) NULL,
`nm_ceo` VARCHAR(40) NULL,
`dt_start` VARCHAR(8) NULL,
`dt_end` VARCHAR(8) NULL,
`am_taxstd` NUMERIC(19, 4) NULL,
`am_addtax` NUMERIC(19, 4) NULL,
`tp_tax` VARCHAR(10) NULL,
`no_company` VARCHAR(20) NULL,
`dts_insert` VARCHAR(20) NULL,
`id_insert` VARCHAR(20) NULL,
`dts_update` VARCHAR(20) NULL,
`id_update` VARCHAR(20) NULL,
`nm_note` VARCHAR(100) NULL,
`cd_bizarea` VARCHAR(12) NULL,
`cd_dept` VARCHAR(12) NULL,
`cd_cc` VARCHAR(12) NULL,
`cd_pjt` VARCHAR(20) NULL,
`cd_fund` VARCHAR(20) NULL,
`cd_budget` VARCHAR(20) NULL,
`no_cash` VARCHAR(20) NULL,
`st_mutual` VARCHAR(3) NULL,
`cd_card` VARCHAR(20) NULL,
`no_deposit` VARCHAR(20) NULL,
`cd_bank` VARCHAR(20) NULL,
`ucd_mng1` VARCHAR(20) NULL,
`ucd_mng2` VARCHAR(20) NULL,
`ucd_mng3` VARCHAR(20) NULL,
`ucd_mng4` VARCHAR(20) NULL,
`ucd_mng5` VARCHAR(20) NULL,
`cd_employ` VARCHAR(20) NULL,
`cd_mng` VARCHAR(20) NULL,
`no_bdocu` VARCHAR(20) NULL,
`no_bdoline` NUMERIC(4, 0) NULL,
`tp_docu` VARCHAR(3) NULL,
`no_acct` NUMERIC(5, 0) NULL,
`tp_trade` VARCHAR(10) NULL,
`no_check` VARCHAR(20) NULL,
`no_check1` VARCHAR(20) NULL,
`cd_exch` VARCHAR(10) NULL,
`rt_exch` NUMERIC(10, 4) NULL,
`cd_trade` VARCHAR(10) NULL,
`no_check2` VARCHAR(50) NULL,
`no_check3` VARCHAR(50) NULL,
`no_check4` VARCHAR(100) NULL,
`tp_cross` VARCHAR(1) NULL,
`erp_cd` VARCHAR(50) NULL,
`am_ex` NUMERIC(19, 4) NULL,
`tp_export` VARCHAR(1) NULL,
`no_to` VARCHAR(20) NULL,
`dt_shipping` VARCHAR(8) NULL,
`tp_gubun` VARCHAR(3) NULL,
`no_invoice` VARCHAR(20) NULL,
`no_item` VARCHAR(20) NULL,
`md_tax1` VARCHAR(4) NULL,
`nm_item1` VARCHAR(50) NULL,
`nm_size1` VARCHAR(20) NULL,
`qt_tax1` NUMERIC(17, 4) NULL,
`am_prc1` NUMERIC(19, 4) NULL,
`am_supply1` NUMERIC(19, 4) NULL,
`am_tax1` NUMERIC(19, 4) NULL,
`nm_note1` VARCHAR(20) NULL,
`cd_bizplan` VARCHAR(20) NULL,
`cd_bgacct` VARCHAR(10) NULL,
`cd_mngd1` VARCHAR(20) NULL,
`nm_mngd1` VARCHAR(100) NULL,
`cd_mngd2` VARCHAR(20) NULL,
`nm_mngd2` VARCHAR(100) NULL,
`cd_mngd3` VARCHAR(20) NULL,
`nm_mngd3` VARCHAR(100) NULL,
`cd_mngd4` VARCHAR(20) NULL,
`nm_mngd4` VARCHAR(100) NULL,
`cd_mngd5` VARCHAR(20) NULL,
`nm_mngd5` VARCHAR(100) NULL,
`cd_mngd6` VARCHAR(20) NULL,
`nm_mngd6` VARCHAR(100) NULL,
`cd_mngd7` VARCHAR(20) NULL,
`nm_mngd7` VARCHAR(100) NULL,
`cd_mngd8` VARCHAR(20) NULL,
`nm_mngd8` VARCHAR(100) NULL,
`yn_iss` VARCHAR(1) NULL,
`final_status` VARCHAR(2) NULL,
`no_bill` VARCHAR(24) NULL,
`tp_bill` VARCHAR(1) NULL,
`tp_record` VARCHAR(1) NULL,
`tp_etcacct` VARCHAR(1) NULL,
`st_gware` VARCHAR(3) NULL,
`sell_dam_nm` VARCHAR(30) NULL,
`sell_dam_email` VARCHAR(50) NULL,
`sell_dam_mobil` VARCHAR(20) NULL,
`nm_pumm` VARCHAR(100) NULL,
`jeonjasend15_yn` VARCHAR(1) NULL,
`dt_write` VARCHAR(8) NULL,
`st_tax` VARCHAR(1) NULL,
`md_tax2` VARCHAR(4) NULL,
`nm_item2` VARCHAR(50) NULL,
`nm_size2` VARCHAR(20) NULL,
`qt_tax2` NUMERIC(17, 4) NULL,
`am_prc2` NUMERIC(19, 4) NULL,
`am_supply2` NUMERIC(19, 4) NULL,
`am_tax2` NUMERIC(19, 4) NULL,
`nm_note2` VARCHAR(20) NULL,
`md_tax3` VARCHAR(4) NULL,
`nm_item3` VARCHAR(50) NULL,
`nm_size3` VARCHAR(20) NULL,
`qt_tax3` NUMERIC(17, 4) NULL,
`am_prc3` NUMERIC(19, 4) NULL,
`am_supply3` NUMERIC(19, 4) NULL,
`am_tax3` NUMERIC(19, 4) NULL,
`nm_note3` VARCHAR(20) NULL,
`md_tax4` VARCHAR(4) NULL,
`nm_item4` VARCHAR(50) NULL,
`nm_size4` VARCHAR(20) NULL,
`qt_tax4` NUMERIC(17, 4) NULL,
`am_prc4` NUMERIC(19, 4) NULL,
`am_supply4` NUMERIC(19, 4) NULL,
`am_tax4` NUMERIC(19, 4) NULL,
`nm_note4` VARCHAR(20) NULL,
`no_asset` VARCHAR(20) NULL,
`nm_bigo` VARCHAR(100) NULL,
`nm_ptr` VARCHAR(20) NULL,
`ex_hp` VARCHAR(15) NULL,
`ex_emil` VARCHAR(100) NULL,
`no_biztax` VARCHAR(8) NULL,
`yn_import` VARCHAR(1) NULL,
`ref_no_docu` VARCHAR(20) NULL,
`cd_fx` VARCHAR(2) NULL,
`fx_bill` VARCHAR(20) NULL,
`no_iss` VARCHAR(24) NULL,
`file_attach` VARCHAR(100) NULL,
`tp_evidence` VARCHAR(4) NULL,
`st_bizbox` VARCHAR(1) NULL,
`tp_input` VARCHAR(30) NULL,
`sell_dam_tel` VARCHAR(20) NULL,
`no_car` VARCHAR(20) NULL,
`no_carbody` VARCHAR(17) NULL,
`dec_lease` VARCHAR(100) NULL,
`no_tdocu` VARCHAR(20) NULL,
`no_tdoline` NUMERIC(4, 0) NULL,
`cd_bizcar` VARCHAR(20) NULL,
`cd_taxacct` VARCHAR(10) NULL,
`yn_fixasset` VARCHAR(1) NULL
)
So if I run this query in sql editor, the error looks the same as that of django. This error didn't happend when I created database with the now gone pair of characterset and collate. But when I run the test it raises error. Different charset may be one of the reaons.
So I deleted the model and apply migrations, since that model is no longer in use. So, stale.
But even after that model is erased, django test runner still seems to bother to create that stale table.
Does django test runner go through every migraion files from start? Is that why I can't run test against model that used to be created with old charset and collate?
How can I prevent django test runner from creating stale, no longer existed, table because old table conflicts in charset and collate with new table, without changing db charset and collate?
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`type` VARCHAR(255) NOT NULL,
`local_root_directory` VARCHAR(255),
`local_directory` VARCHAR(255) NOT NULL,
`remote_host` VARCHAR(255) NOT NULL,
`remote_port` INTEGER DEFAULT 21 NOT NULL,
`is_ssl` TINYINT DEFAULT 0 NOT NULL,
`is_sftp` TINYINT DEFAULT 0 NOT NULL,
`is_passive` TINYINT DEFAULT 1 NOT NULL,
`timeout` INTEGER DEFAULT 30 NOT NULL,
`username` VARCHAR(255) NOT NULL,
`password` VARCHAR(255),
`private_key` VARCHAR(255),
`remote_directory` VARCHAR(255) NOT NULL,
`is_recursive` TINYINT DEFAULT 0 NOT NULL,
`is_delete_after_success` TINYINT DEFAULT 0 NOT NULL,
`is_overwrite_existing` TINYINT DEFAULT 0 NOT NULL,
`filename_pattern` VARCHAR(255) DEFAULT '.*' NOT NULL,
`is_active` TINYINT DEFAULT 0 NOT NULL,
`weekday` VARCHAR(255) DEFAULT '*' NOT NULL,
`year` VARCHAR(255) DEFAULT '*' NOT NULL,
`month` VARCHAR(255) DEFAULT '*' NOT NULL,
`day` VARCHAR(255) DEFAULT '*' NOT NULL,
`hour` VARCHAR(255) DEFAULT '*' NOT NULL,
`minute` VARCHAR(255) DEFAULT '*' NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `name_unique` (`name`)
) ENGINE=InnoDB". Aborting migration.
Problem
It gives an error after I importet a new table from a database dump. Could it be, that I importet the database twice. And if yes, how can fix this in my local repository? Thank you :-)
Please could anyone help with the following query? (180352 rows)
SELECT COUNT(p.stock_id) AS num_products,
p.master_photo, p.product_photo, p.stock_id, p.master, p.title, p.price, p.stock_level, p.on_order, p.location, p.supplier, p.category, p.sub_category, p.reorder
FROM products AS p
WHERE p.sub_category != 'Subscriptions'
GROUP BY p.master
ORDER BY p.stock_id ASC
LIMIT 0, 20
It's running at 6 seconds.
When I remove the order by it run's at 0.0023 seconds.
And also the same when I remove the group by.
The stock_id (unique) and sub_category are indexed.
I can't think of another way to approach a query like this as it is vital that I group by the master to get the count of product variations and also vital that they can be ordered (not necessarily by stock_id but that's the default).
Thank you
As requested by e4c5 below is the result of the explain with the order by
id: 1
select_type: SIMPLE
table: p
type: range
possible_keys: sub_category
key: sub_category
key_len: 52
ref: NULL
rows: 181691
Extra: Using where; Using temporary; Using filesort
and then without the order by
id: 1
select_type: SIMPLE
table: p
type: index
possible_keys: sub_category
key: master
key_len: 52
ref: NULL
rows: 21
Extra: Using where
and then below is the create table
CREATE TABLE IF NOT EXISTS `products` (
`stock_id` varchar(50) NOT NULL,
`conv_stock_id` varchar(100) NOT NULL,
`conv_quantity` decimal(10,2) NOT NULL,
`master` varchar(50) NOT NULL,
`master_photo` varchar(255) NOT NULL,
`free_guide_photo` varchar(255) NOT NULL,
`product_var_photo` varchar(255) NOT NULL,
`master_title` varchar(255) NOT NULL,
`master_slug` varchar(255) NOT NULL,
`master_page_title` varchar(255) NOT NULL,
`product_photo` varchar(255) NOT NULL,
`original_product_photo` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`orig_title` varchar(255) NOT NULL,
`page_title` varchar(255) NOT NULL,
`description` longtext NOT NULL,
`slug` varchar(255) NOT NULL,
`custom_url` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL,
`supplier` varchar(50) NOT NULL,
`supplier_stock_id` varchar(50) NOT NULL,
`supplier_discount` int(11) NOT NULL,
`category` varchar(50) NOT NULL,
`sub_category` varchar(50) NOT NULL,
`cost_price` decimal(10,2) NOT NULL,
`discount_cost_price` decimal(10,2) NOT NULL,
`price` decimal(10,2) NOT NULL,
`sale_price` decimal(10,2) NOT NULL,
`sale_price_startdate` date NOT NULL,
`sale_price_enddate` date NOT NULL,
`orig_price_trail` int(3) NOT NULL,
`price_trail` varchar(50) NOT NULL,
`price_rule` int(1) NOT NULL,
`pack_size` int(11) NOT NULL,
`parcel_size` int(1) NOT NULL,
`packaging_rule` int(11) NOT NULL,
`cut_tear` int(1) NOT NULL,
`oversized_parcel` int(1) NOT NULL,
`print_label` int(1) NOT NULL,
`stock_level` decimal(10,1) NOT NULL,
`stock_level_group` varchar(50) NOT NULL,
`stock_level_increment` decimal(10,2) NOT NULL,
`stock_check_date` datetime NOT NULL,
`reorder` int(1) NOT NULL,
`reorder_level` decimal(10,1) NOT NULL,
`reorder_quantity` decimal(10,1) NOT NULL,
`reorder_attempts` int(1) NOT NULL,
`unit_size` decimal(10,1) NOT NULL,
`on_order` decimal(10,1) NOT NULL,
`date_ordered` datetime NOT NULL,
`back_order` decimal(10,1) NOT NULL,
`uom` decimal(10,1) NOT NULL,
`uom_value` varchar(100) NOT NULL,
`stock_estimate` int(1) NOT NULL,
`due_date` datetime NOT NULL,
`quantity` varchar(255) NOT NULL,
`colour` varchar(255) NOT NULL,
`colour_family` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
`style` varchar(255) NOT NULL,
`pattern` varchar(255) NOT NULL,
`shape` varchar(255) NOT NULL,
`design` varchar(255) NOT NULL,
`fibre` varchar(255) NOT NULL,
`material` varchar(255) NOT NULL,
`pattern_for` varchar(255) NOT NULL,
`difficulty` varchar(255) NOT NULL,
`fabric_count` varchar(255) NOT NULL,
`yarn_thickness` varchar(255) NOT NULL,
`suggested_needle_size` varchar(255) NOT NULL,
`tension` varchar(255) NOT NULL,
`collections` varchar(255) NOT NULL,
`product_features` varchar(255) NOT NULL,
`size` varchar(255) NOT NULL,
`actual_size` varchar(255) NOT NULL,
`length` varchar(255) NOT NULL,
`width` varchar(255) NOT NULL,
`weight` varchar(255) NOT NULL,
`weight_gsm` varchar(255) NOT NULL,
`brand` varchar(255) NOT NULL,
`designer` varchar(255) NOT NULL,
`composition` varchar(255) NOT NULL,
`washing_instructions` varchar(255) NOT NULL,
`matching_thread` varchar(50) NOT NULL,
`sample` varchar(50) NOT NULL,
`fat_quarter` varchar(50) NOT NULL,
`barcode` varchar(13) NOT NULL,
`list_international` int(1) NOT NULL,
`token` varchar(50) NOT NULL,
`create_sample` int(1) NOT NULL,
`create_fatquarter` int(1) NOT NULL,
`create_listing_type` int(1) NOT NULL,
`create_listing_size` int(11) NOT NULL,
`create_listing_price` decimal(10,2) NOT NULL,
`create_listing_price_rule` int(11) NOT NULL,
`create_listing_sale_price` decimal(10,2) NOT NULL,
`create_listing_parcelsize` int(1) NOT NULL,
`create_listing_barcode` varchar(13) NOT NULL,
`auto_listing` int(1) NOT NULL,
`custom_bridal` int(1) NOT NULL,
`pickwave_assign` int(1) NOT NULL,
`kit_product` int(11) NOT NULL,
`fatquarter_product` int(1) NOT NULL,
`sample_product` int(1) NOT NULL,
`grouped_product` int(1) NOT NULL,
`grouped_product_quantity` decimal(10,1) NOT NULL,
`multiple_product` int(1) NOT NULL,
`freepost_product` int(1) NOT NULL,
`status` int(1) NOT NULL,
`update_stock_level` int(1) NOT NULL,
`force_product_photo` int(1) NOT NULL,
`created_master_photo` int(1) NOT NULL,
`force_master_photo` int(1) NOT NULL,
`created_free_guide_photo` int(1) NOT NULL,
`force_free_guide_photo` int(1) NOT NULL,
`created_product_var_photo` int(1) NOT NULL,
`force_product_var_photo` int(1) NOT NULL,
`force_additional_photo` int(1) NOT NULL,
`created_price_levelling` int(1) NOT NULL,
`created_grouped_product` int(1) NOT NULL,
`updated_stock_level` int(1) NOT NULL,
`create_multiple_listing` int(1) NOT NULL,
`create_freepost_listing` int(1) NOT NULL,
`create_freeguide_info` int(1) NOT NULL,
`created_by` int(11) NOT NULL,
`date_created` datetime NOT NULL,
UNIQUE KEY `stock_id` (`stock_id`),
KEY `token` (`token`),
KEY `title` (`title`),
KEY `stock_level_group` (`stock_level_group`),
KEY `sub_category` (`sub_category`),
KEY `stock_level` (`stock_level`),
KEY `category` (`category`),
KEY `conv_stock_id` (`conv_stock_id`),
KEY `conv_quantity` (`conv_quantity`),
KEY `created_price_levelling` (`created_price_levelling`),
KEY `master` (`master`),
KEY `colour` (`colour`),
KEY `auto_listing` (`auto_listing`),
KEY `multiple_product` (`multiple_product`),
KEY `status` (`status`),
KEY `ebay_master` (`ebay_master`),
KEY `parcel_size` (`parcel_size`),
KEY `grouped_product` (`grouped_product`),
KEY `sample_product` (`sample_product`),
KEY `fatquarter_product` (`fatquarter_product`),
KEY `created_grouped_product` (`created_grouped_product`),
KEY `price` (`price`),
KEY `freepost_product` (`freepost_product`),
KEY `master_title` (`master_title`),
KEY `c_sub_category_master` (`sub_category`,`master`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You haven't provided the output from explain, however based on your query it would seem that ORDER BY forces a full table scan. That would make the query very slow.
When you don't use the ORDER BY, the db reads the results for the first 20 master values (there maybe quite a few of them) and groups them together and returns the result.
When you order by stock_id the whole table needs to be looked at to find which masters are associated with the lowest values stock_ids
It maybe possible to improve performance with a composite index on sub_category,master however a conclusion cannot be made unless you share your SHOW CREATE TABLES, EXPLAIN output.
Update
Based on your CREATE TABLE statements, I see that your database isn't normalized. For example Why do I get the feeling that the following columns should in a table of their own?
supplier varchar(50) NOT NULL,
supplier_stock_id varchar(50) NOT NULL,
supplier_discount int(11) NOT NULL,
You should only have a supplier_stock_id in your products table (foreign key to the suppliers table). There are similar sets of columns which really should be moved out.
When you do so you can create leaner and meaner indexes on this table. But that's not all the table becomes narrower. Which in turn means the worst case scenario of a full table scan actually becomes a lot faster.
I also noticed that the table does not have a primary key. Which is a big no-no. The stock_id if it's numeric should be primary key. If it's not numeric it might stil be the best candidate for primary key but this is something you need to decide.
Try adding an Index on stock_id in the products table... that should help.
I am using MySQL Query Browser.
I have tried this code:
CREATE TABLE `something`.`payment_something` (
`firstName` varchar(15) NOT NULL,
`lastName` varchar(15) NOT NULL,
`inputEmail` varchar(55) NOT NULL,
`genderRadios` varchar(15) NOT NULL,
`monthh` varchar(10) NOT NULL,
`dayy` varchar(10) NOT NULL,
`yearr` varchar(10) NOT NULL,
`postalAddress` varchar(15) NOT NULL,
`phoneNumber` varchar(15) NOT NULL,
`ZipCode` varchar(15) NOT NULL,
`CreditCard` varchar(15) NOT NULL,
`expireMonth` varchar(10) NOT NULL,
`expireYear` varchar(10) NOT NULL,
`Institution` varchar(25) NOT NULL,
`textinput` varchar(15) NOT NULL,
`radios` varchar(10) NOT NULL,
PRIMARY KEY ()
) ENGINE=InnoDB DEFAULT CHARSET=greek;
of course it shows error in the PRIMARY KEY line. Any idea?
EDIT Better solution
CREATE TABLE `something`.`payment_something` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`firstName` varchar(15) NOT NULL,
`lastName` varchar(15) NOT NULL,
`inputEmail` varchar(55) NOT NULL,
`genderRadios` varchar(15) NOT NULL,
`monthh` varchar(10) NOT NULL,
`dayy` varchar(10) NOT NULL,
`yearr` varchar(10) NOT NULL,
`postalAddress` varchar(15) NOT NULL,
`phoneNumber` varchar(15) NOT NULL,
`ZipCode` varchar(15) NOT NULL,
`CreditCard` varchar(15) NOT NULL,
`expireMonth` varchar(10) NOT NULL,
`expireYear` varchar(10) NOT NULL,
`Institution` varchar(25) NOT NULL,
`textinput` varchar(15) NOT NULL,
`radios` varchar(10) NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=greek;
It shows wrong in the PRIMARY KEY line why?
Simply exclude the problematic line:
PRIMARY KEY ()
You don't need to have that line, if you are not actually defining a key. In your example, you will also have to remove the comma directly preceding this line, of course.
UPDATE:
In your updated example, just take the '' off of 'id' when you declare it. Use the backtick (`), instead of the apostrophe:
PRIMARY KEY (`id`)
I am using a MySQL DB to manage employees. I maintain the DB in PHPMyAdmin. I want to add 70 new fields into a table by using SQL. I thought this would work. Can you tell me why it doesn't
CREATE TABLE IF NOT EXISTS `dist` (
`e_employee1` varchar(255) NOT NULL,
`e_name1` varchar(255) NOT NULL,
`e_title1` varchar(255) NOT NULL,
`e_phone1` int(11) NOT NULL,
`e_ext1` int(11) NOT NULL,
`e_phone21` int(11) NOT NULL,
`e_email1` varchar(255) NOT NULL,
`e_employee2` varchar(255) NOT NULL,
`e_name2` varchar(255) NOT NULL,
`e_title2` varchar(255) NOT NULL,
`e_phone2` int(11) NOT NULL,
`e_ext2` int(11) NOT NULL,
`e_phone22` int(11) NOT NULL,
`e_email12` varchar(255) NOT NULL,
`e_employee3` varchar(255) NOT NULL,
`e_name3` varchar(255) NOT NULL,
`e_title3` varchar(255) NOT NULL,
`e_phone3` int(11) NOT NULL,
`e_ext3` int(11) NOT NULL,
`e_phone23` int(11) NOT NULL,
`e_email3` varchar(255) NOT NULL,
`e_employee4` varchar(255) NOT NULL,
`e_name4` varchar(255) NOT NULL,
`e_title4` varchar(255) NOT NULL,
`e_phone4` int(11) NOT NULL,
`e_ext4` int(11) NOT NULL,
`e_phone24` int(11) NOT NULL,
`e_email4` varchar(255) NOT NULL,
`e_employee5` varchar(255) NOT NULL,
`e_name5` varchar(255) NOT NULL,
`e_title5` varchar(255) NOT NULL,
`e_phone5` int(11) NOT NULL,
`e_ext5` int(11) NOT NULL,
`e_phone25` int(11) NOT NULL,
`e_email5` varchar(255) NOT NULL,
`e_employee6` varchar(255) NOT NULL,
`e_name6` varchar(255) NOT NULL,
`e_title6` varchar(255) NOT NULL,
`e_phone6` int(11) NOT NULL,
`e_ext6` int(11) NOT NULL,
`e_phone26` int(11) NOT NULL,
`e_email6` varchar(255) NOT NULL,
`e_employee7` varchar(255) NOT NULL,
`e_name7` varchar(255) NOT NULL,
`e_title7` varchar(255) NOT NULL,
`e_phone7` int(11) NOT NULL,
`e_ext7` int(11) NOT NULL,
`e_phone27` int(11) NOT NULL,
`e_email7` varchar(255) NOT NULL,
`e_employee8` varchar(255) NOT NULL,
`e_name8` varchar(255) NOT NULL,
`e_title8` varchar(255) NOT NULL,
`e_phone8` int(11) NOT NULL,
`e_ext8` int(11) NOT NULL,
`e_phone28` int(11) NOT NULL,
`e_email8` varchar(255) NOT NULL,
`e_employee9` varchar(255) NOT NULL,
`e_name9` varchar(255) NOT NULL,
`e_title9` varchar(255) NOT NULL,
`e_phone9` int(11) NOT NULL,
`e_ext9` int(11) NOT NULL,
`e_phone29` int(11) NOT NULL,
`e_email9` varchar(255) NOT NULL,
`e_employee10` varchar(255) NOT NULL,
`e_name10` varchar(255) NOT NULL,
`e_title10` varchar(255) NOT NULL,
`e_phone10` int(11) NOT NULL,
`e_ext10` int(11) NOT NULL,
`e_phone210` int(11) NOT NULL,
`e_email10` varchar(255) NOT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
You are using CREATE TABLE. You should be using ALTER TABLE if you want to add fields. PHPMyAdmin should be showing you an error indicating that the table already exists.