why Mysql is giving me error 1280 "Wrong Index" - mysql

Can anyone explain why Mysql is giving me error 1280 ("wrong index for 'fk_chart_aid_aid' ") error whend I try to create the "CHART OF ACCOUNTS" table. I'm completly confused here. How can I fix this so I can create the table? The "ACCOUNT" table already exists in the database and has data in it.
Thanks for the help.
MYSQL Server version: 5.1.54
CHART OF ACCOUNTS:
DROP TABLE IF EXISTS `rst`.`acctg_chart_of_accounts` ;
CREATE TABLE IF NOT EXISTS `rst`.`acctg_chart_of_accounts` (
`acctg_chart_of_accounts_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`account_id` INT UNSIGNED NOT NULL ,
`account_nbr` VARCHAR(45) NULL ,
`description` VARCHAR(45) NULL ,
`account_type` INT UNSIGNED NULL ,
`commissionable` TINYINT UNSIGNED NULL ,
`hidden` TINYINT UNSIGNED NULL ,
`deduct_balance_from_owner_check` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`acctg_chart_of_accounts_id`) ,
CONSTRAINT `fk_chart_aid_aid`
FOREIGN KEY (`account_id` )
REFERENCES `rst`.`account` (`account_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `fk_chart_aid_aid` ON `rst`.`acctg_chart_of_accounts` (`account_id` ASC) ;
ACCOUNTS TABLE THAT IS BEING REFERENCED:
CREATE TABLE IF NOT EXISTS `account` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_status_id` int(10) unsigned NOT NULL,
`company_name` varchar(155) DEFAULT NULL,
`address1` varchar(155) DEFAULT NULL,
`address2` varchar(155) DEFAULT NULL,
`city` varchar(155) DEFAULT NULL,
`state` varchar(155) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`work_phone` varchar(45) DEFAULT NULL,
`mobile_phone` varchar(45) DEFAULT NULL,
`time_zone` varchar(45) DEFAULT NULL,
`subdomain` varchar(155) DEFAULT NULL,
`cname_URL` varchar(255) DEFAULT NULL,
`promotion_code` varchar(45) DEFAULT NULL,
`can_we_contact_you` tinyint(4) DEFAULT NULL COMMENT '0=false, 1=true',
`units_managed_nbr` varchar(10) DEFAULT NULL,
`a_hear_about_us_list_id` tinyint(3) unsigned DEFAULT NULL COMMENT 'populated from dropdown list.',
`receive_special_offers` tinyint(4) DEFAULT NULL,
`receive_announcements` tinyint(4) DEFAULT NULL,
`receive_newsletter` tinyint(4) DEFAULT NULL,
`create_ts` timestamp NULL DEFAULT NULL,
`expires` timestamp NULL DEFAULT NULL,
`storage_capacity` varchar(255) DEFAULT NULL COMMENT '1073741824 = 1GB',
`logo` varchar(455) DEFAULT NULL,
`max_active_connections` int(11) DEFAULT '3',
`_product_id` int(11) DEFAULT NULL,
`report_footer` varchar(455) DEFAULT NULL,
`welcome_dialog` tinyint(4) DEFAULT '1',
`ARB_subscription_id` int(11) DEFAULT NULL,
`trashbin` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`account_id`),
KEY `fk_account_account_status_id` (`account_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;

Are you getting the error after the CREATE TABLE statement, or after the subsequent CREATE INDEX?
Looks like you are attempting to name both a FOREIGN KEY constraint and an INDEX fk_chart_aid_aid. Try choosing a different name for either one of them.
Also, in the accounts table, account_id is INT(10). Try also to change the column definition in acctg_chart_of_accounts to:
`account_id` INT(10) UNSIGNED NOT NULL ,
Though, I think that mysql defaults type INT to INT(10) anyway...

I met the same issue; tried manually rename the index to a different name but don't like the idea of 'manually' and neither I don't really understand why we need to generate the index separately. so I decide to generate it within the create statement by unchecking the option of 'generate separate index statement' in 'forwarding engineer', and this fix the issue.

Related

Transferring table from MySQL 5.7 to 8.0, showing wrong data on a blob column

I am transferring a single table from MySQL 5.7 to MySQL 8.
First, I used mysqldump db_name table_name > table_name.sql in my current server(MYSQL 5.7); taken from here
Then, in my VM with MySQL 8, I used mysql -u username -p db_name < /path/to/table_name.sql.
The columns with the 'blob' data type are not correct, the other columns are okay.
the 'blob' columns went from "International Master (1973); Grandmaster (1975); FIDE Senior Trainer (2004).Alexander Genrikhovich Beliavsky was..." to "0x3C703E496E7465726E6174696F6E616C204D6173746572202831393733293B204772616E646D617374657220...".
Both databases have character-set = latin1 and collation = latin1_swedish_ci.
How can I properly transfer the 'blob' columns from MySQL 5.7 to MySQL 8?
Output when running SHOW CREATE TABLE table_name:
OLD:
CREATE TABLE `Player` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(34) DEFAULT NULL,
`longname` varchar(64) DEFAULT NULL,
`rating` int(10) unsigned NOT NULL DEFAULT '0',
`born` varchar(10) DEFAULT NULL,
`died` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`nationality` varchar(10) DEFAULT NULL,
`gender` enum('M','F') DEFAULT NULL,
`bio` blob,
`fiderating` int(10) unsigned NOT NULL DEFAULT '0',
`rawbio` blob,
`fidenumber` int(10) unsigned DEFAULT NULL,
`fideblitz` int(10) unsigned DEFAULT '0',
`fiderapid` int(10) unsigned DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `name` (`name`),
KEY `rating` (`rating`),
KEY `country` (`country`),
KEY `nationality` (`nationality`),
KEY `gender` (`gender`),
KEY `fidenumber` (`fidenumber`),
FULLTEXT KEY `longname` (`longname`)
) ENGINE=MyISAM AUTO_INCREMENT=168045 DEFAULT CHARSET=latin1
NEW:
| Player | CREATE TABLE `Player` (
`pid` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(34) DEFAULT NULL,
`longname` varchar(64) DEFAULT NULL,
`rating` int unsigned NOT NULL DEFAULT '0',
`born` varchar(10) DEFAULT NULL,
`died` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`nationality` varchar(10) DEFAULT NULL,
`gender` enum('M','F') DEFAULT NULL,
`bio` blob,
`fiderating` int unsigned NOT NULL DEFAULT '0',
`rawbio` blob,
`fidenumber` int unsigned DEFAULT NULL,
`fideblitz` int unsigned DEFAULT '0',
`fiderapid` int unsigned DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `name` (`name`),
KEY `rating` (`rating`),
KEY `country` (`country`),
KEY `nationality` (`nationality`),
KEY `gender` (`gender`),
KEY `fidenumber` (`fidenumber`),
FULLTEXT KEY `longname` (`longname`)
) ENGINE=MyISAM AUTO_INCREMENT=168045 DEFAULT CHARSET=latin1 |

How do you design a database schema for a user profile page

Trying to set up a user profile page for a job site. The database I plan to use is the MySQL database.
Looking into a few database design I came up with this schema.
First, the user management tables
CREATE TABLE `user` (
`user_id` int(11) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`email` varchar(96) NOT NULL,
`mobile_number` varchar(32) NOT NULL,
`password` varchar(40) NOT NULL,
`salt` varchar(9) NOT NULL,
`address_id` int(11) NOT NULL DEFAULT '0',
`ip` varchar(40) NOT NULL,
`status` tinyint(1) NOT NULL,
`approved` tinyint(1) NOT NULL,
`registration_date` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `user_address` (
`user_id` int(11) NOT NULL,
`city` varchar(128) NOT NULL
`work_city` varchar(128) NOT NULL,
`postal_code` varchar(10) NOT NULL,
`country_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `user_description` (
`user_id` int(11) NOT NULL,
`description` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and then the education table and work experience
CREATE TABLE `education_detail` (
`user_id` int(11) NOT NULL,
`certificate_degree_name` varchar(255) DEFAULT NULL,
`major` varchar(255) DEFAULT NULL,
`institute_university_name` varchar(255) DEFAULT NULL,
`start_date` date NOT NULL DEFAULT '0000-00-00',
`completion_date` date NOT NULL DEFAULT '0000-00-00'
)
CREATE TABLE `experience_detail` (
`user_id` int(11) NOT NULL,
`is_current_job` int(2) DEFAULT NULL,
`start_date` date NOT NULL DEFAULT '0000-00-00',
`end_date` date NOT NULL DEFAULT '0000-00-00',
`job_title` varchar(255) DEFAULT NULL,
`company_name` varchar(255) DEFAULT NULL,
`job_location_city` varchar(255) DEFAULT NULL,
`job_location_state` varchar(255) DEFAULT NULL,
`job_location_country` varchar(255) DEFAULT NULL,
`job_description` varchar(255) DEFAULT NULL
)
Note that user_id in table user_address, user_description, education_detail and experience_detail is a foreign key referencing it to the table user.
There are a few more table like skills, certification etc to which I plan on using user_id as a FK.
My question, is this database design good enough? Can you suggest me what should be done more to make the design much better?
Keep in mind not all will have work experience, some may be freshers.
Use InnoDB, not MyISAM. (There are many Q&A explaining 'why'.)
NULL or an empty string is perfectly fine for a missing description. Do you have any further argument for disliking such? (Meanwhile, InnoDB is more efficient at handling optional big strings.)
Every table should have a PRIMARY KEY; you don't seem to have any. The first table probably needs user_id as the PK. Read about AUTO_INCREMENT.
As with description, why is address in a separate table?
May I suggest this for country name/code/id:
country_code CHAR(2) CHARACTER SET ascii
Education is 1:many from users, so user_id cannot be the PK. Ditto for jobs.

Cant create a relation in mysql - Error creating foreign key on id_produk (check data types)

here is my table :
CREATE TABLE `pesanan` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ref_number` varchar(50) NOT NULL DEFAULT '',
`id_produk` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`qty` int(11) NOT NULL,
`total` varchar(50) NOT NULL DEFAULT '',
`tanggal` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
i want to create a relation of id_produk to table produk.id and id_user to user.id, here is the other table :
CREATE TABLE `produk` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_kategori` int(11) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT '',
`slug` varchar(100) NOT NULL DEFAULT '',
`judul` varchar(100) NOT NULL DEFAULT '',
`harga` varchar(10) DEFAULT '',
`target` int(11) DEFAULT NULL,
`desc` text,
`cover` varchar(100) DEFAULT NULL,
`tanggal` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1;
and user table
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`suspended` int(11) DEFAULT NULL,
`level` varchar(11) DEFAULT NULL,
`nama` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`alamat` text,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
when i try to relation it , it give me this error :
Error creating foreign key on id_produk (check data types)
The columns should have the exact same data type in both tables. In your case in the "produk" and "user" tables they are unsigned, which means they go from 0 to 4294967295 but in the "pesanan" table they are signed which means they go from -2147483648 to 2147483647. See more here.
If the server allowed you to create such foreign keys, it would possibly create situations where, for example, a user is added with and ID of 2147483648 that cannot ever be referenced in the "pesanan" table.
You should change the "id_produk" and "id_user" columns in the "pesanan" table to be unsigned.

Long time to exec a simple query in mysql

I am with a performance problem in my MySQL database, and do not know if it's the server configuration issue or disk space, or simply not riding the query correctly.
a simple query that summarizes the total number of rows in a table by a distinct field, it takes more than 3 minutes. The status is "Sending data" for a long time.
the query is as follows:
SELECT COUNT (DISTINCT (ca.nm_slug)) the qtd_total
FROM consulta_atual ca
WHERE ca.cd_categoria_site = 436
below the description of the table (has a 4 million records)
CREATE TABLE IF NOT EXISTS `consulta_atual` (
`cd_categoria` int(11) NOT NULL DEFAULT '0',
`cd_categoria_site` int(11) DEFAULT NULL,
`ds_categoria` varchar(100) NOT NULL,
`ds_subcategoria` varchar(200) NOT NULL,
`cd_produto_price` bigint(11) NOT NULL,
`cd_seq` bigint(20) NOT NULL DEFAULT '0',
`tp_fornecedor` int(11) NOT NULL DEFAULT '0',
`name` varchar(300) NOT NULL,
`nm_slug` varchar(200) DEFAULT NULL,
`fornecedor` int(11) NOT NULL,
`url_img_fornecedor` varchar(200) NOT NULL,
`url_raiz_fornecedor` varchar(200) NOT NULL,
`url_imagem` varchar(500) NOT NULL,
`url_produto` varchar(500) NOT NULL,
`vlr_produto` varchar(49) NOT NULL DEFAULT '',
`pnt_produto` varchar(53) NOT NULL DEFAULT '',
`vlr_produto_original` float(10,2) NOT NULL,
`menor_valor` float DEFAULT NULL,
`maior_valor` float DEFAULT NULL,
`qtd_lojas` int(11) DEFAULT NULL,
PRIMARY KEY (`cd_produto_price`,`fornecedor`),
KEY `nm_slug_2` (`nm_slug`),
KEY `ds_categoria` (`ds_categoria`),
KEY `vlr_produto_original` (`vlr_produto_original`),
KEY `cd_categoria_site` (`cd_categoria_site`),
KEY `fornecedor` (`fornecedor`),
KEY `tp_fornecedor` (`tp_fornecedor`),
FULLTEXT KEY `name` (`name`),
FULLTEXT KEY `ds_categoria_2` (`ds_categoria`),
FULLTEXT KEY `ds_categoria_3` (`ds_categoria`,`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
How can I improve execution performance of this simple query?

mysqldiff tool drops indexes and adds duplicates and causes errors

I'm using the mysqldiff tool (v1.5.3) from MySQL to synchronize the schema between two databases. The databases should have the same schema, but would like to produce a file that only has the ALTER TABLE differences between them so it can (after thorough testing) be run in production.
The problem is that the mysqldiff tool produces some weirdness. For example:
ALTER TABLE `database1`.`ACCOUNT_TRANSACTIONS`
DROP INDEX MATCHING_TRANSACTION_ID,
DROP INDEX PAYEE_ID,
ADD INDEX CHECK_NUMBER (ACCOUNT_ID),
ADD INDEX PAYEE_ID (PAYEE_ID),
ADD INDEX CHECK_NUMBER (PAYEE_ID,CHECK_NUMBER);
It seems to not only drop the indexes, but tries to recreate the new index through two statements that produce an error. The final schema should have CHECK_NUMBER (ACCOUNT_ID, PAYEE_ID, CHECK_NUMBER) as the index.
What might be causing this?
Final version:
CREATE TABLE `ACCOUNT_TRANSACTIONS` (
`TRANSACTION_ID` int(12) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(1) DEFAULT NULL,
`REF_NUMBER` varchar(15) DEFAULT NULL,
`PAYEE_ID` int(6) DEFAULT NULL,
`ACCOUNT_CHART_ID` int(12) DEFAULT NULL,
`TRANSACTION_AMOUNT` decimal(10,2) DEFAULT NULL,
`TRANSACTION_DATE` date DEFAULT NULL,
`MEMO` varchar(100) DEFAULT NULL,
`ACCOUNT_BALANCE` decimal(10,2) DEFAULT NULL,
`DEBIT_OR_CREDIT` char(1) DEFAULT 'D',
`MATCHING_TRANSACTION_ID` int(11) DEFAULT '0',
`RECONCILED_DATE` date DEFAULT NULL,
`RECONCILED_EMPLOYEE_ID` int(12) DEFAULT NULL,
`TIME_UPDATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CHECK_NUMBER` bigint(20) unsigned DEFAULT NULL,
`STATEMENT_ID` int(12) DEFAULT NULL,
PRIMARY KEY (`TRANSACTION_ID`),
KEY `PAYEE_ID` (`PAYEE_ID`),
KEY `CHECK_NUMBER` (`CHECK_NUMBER`,`ACCOUNT_ID`,`PAYEE_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=xxx DEFAULT CHARSET=latin1
Original version (needs to be altered to be final version)
CREATE TABLE `ACCOUNT_TRANSACTIONS` (
`TRANSACTION_ID` int(12) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(1) DEFAULT NULL,
`REF_NUMBER` varchar(15) DEFAULT NULL,
`PAYEE_ID` int(6) DEFAULT NULL,
`ACCOUNT_CHART_ID` int(12) DEFAULT NULL,
`TRANSACTION_AMOUNT` decimal(10,2) DEFAULT NULL,
`TRANSACTION_DATE` date DEFAULT NULL,
`MEMO` varchar(100) DEFAULT NULL,
`ACCOUNT_BALANCE` decimal(10,2) DEFAULT NULL,
`DEBIT_OR_CREDIT` char(1) DEFAULT 'D',
`MATCHING_TRANSACTION_ID` int(11) DEFAULT '0',
`RECONCILED_DATE` date DEFAULT NULL,
`RECONCILED_EMPLOYEE_ID` int(12) DEFAULT NULL,
`TIME_UPDATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CHECK_NUMBER` bigint(20) unsigned DEFAULT NULL,
`STATEMENT_ID` int(12) DEFAULT NULL,
PRIMARY KEY (`TRANSACTION_ID`),
KEY `PAYEE_ID` (`PAYEE_ID`),
KEY `MATCHING_TRANSACTION_ID` (`MATCHING_TRANSACTION_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=xxx DEFAULT CHARSET=latin1