Recognizing foreign key in MySQL script - mysql

I am analyzing a SQL script to find all the primary and foreign keys. I don't know whether the last line of the script "KEY ID_STAFF (id_staff)" is a primary key or a foreign key. The full script is
CREATE TABLE IF NOT EXISTS `conge` (
`id_conge` int(10) NOT NULL AUTO_INCREMENT,
`id_staff` int(10) NOT NULL DEFAULT '0',
`annee` varchar(4) DEFAULT NULL,
`total_conge` double DEFAULT NULL,
`report_conge` double DEFAULT NULL,
`conge_pris` double DEFAULT NULL,
`debut` date DEFAULT NULL,
`fin` date DEFAULT NULL,
`conge_reste` double DEFAULT NULL,
`codeterrain` varchar(14) NOT NULL DEFAULT '',
`typeconge` varchar(6) DEFAULT NULL,
`commentaire` text,
`id_contrat` int(10) DEFAULT NULL,
`typeautre` varchar(6) DEFAULT NULL,
`lienbulletin` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`codeterrain`,`id_conge`),
KEY `ID_STAFF` (`id_staff`)
)

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 |

#1215 - Cannot add foreign key constraint

I am trying to migrate my osclass installation to another server. I have copied all files and created a new database. When trying to import my database from backup, I get "#1215 - Cannot add foreign key constraint".
It shows that this bit is a problem:
--
-- Table structure for table `oc_t_user`
--
CREATE TABLE IF NOT EXISTS `oc_t_user` (
`pk_i_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`dt_reg_date` datetime NOT NULL,
`dt_mod_date` datetime DEFAULT NULL,
`s_name` varchar(100) NOT NULL,
`s_username` varchar(100) NOT NULL,
`s_password` char(60) NOT NULL,
`s_secret` varchar(40) DEFAULT NULL,
`s_email` varchar(100) NOT NULL,
`s_website` varchar(100) DEFAULT NULL,
`s_phone_land` varchar(45) DEFAULT NULL,
`s_phone_mobile` varchar(45) DEFAULT NULL,
`b_enabled` tinyint(1) NOT NULL DEFAULT '1',
`b_active` tinyint(1) NOT NULL DEFAULT '0',
`s_pass_code` varchar(100) DEFAULT NULL,
`s_pass_date` datetime DEFAULT NULL,
`s_pass_ip` varchar(15) DEFAULT NULL,
`fk_c_country_code` char(2) DEFAULT NULL,
`s_country` varchar(40) DEFAULT NULL,
`s_address` varchar(100) DEFAULT NULL,
`s_zip` varchar(15) DEFAULT NULL,
`fk_i_region_id` int(10) unsigned DEFAULT NULL,
`s_region` varchar(100) DEFAULT NULL,
`fk_i_city_id` int(10) unsigned DEFAULT NULL,
`s_city` varchar(100) DEFAULT NULL,
`fk_i_city_area_id` int(10) unsigned DEFAULT NULL,
`s_city_area` varchar(200) DEFAULT NULL,
`d_coord_lat` decimal(10,6) DEFAULT NULL,
`d_coord_long` decimal(10,6) DEFAULT NULL,
`b_company` tinyint(1) NOT NULL DEFAULT '0',
`i_items` int(10) unsigned DEFAULT '0',
`i_comments` int(10) unsigned DEFAULT '0',
`dt_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`s_access_ip` varchar(15) NOT NULL DEFAULT '',
PRIMARY KEY (`pk_i_id`),
UNIQUE KEY `s_email` (`s_email`),
KEY `idx_s_name` (`s_name`(6)),
KEY `idx_s_username` (`s_username`),
KEY `fk_c_country_code` (`fk_c_country_code`),
KEY `fk_i_region_id` (`fk_i_region_id`),
KEY `fk_i_city_id` (`fk_i_city_id`),
KEY `fk_i_city_area_id` (`fk_i_city_area_id`),
CONSTRAINT `oc_t_user_ibfk_1` FOREIGN KEY (`fk_c_country_code`) REFERENCES `oc_t_country` (`pk_c_code`),
CONSTRAINT `oc_t_user_ibfk_2` FOREIGN KEY (`fk_i_region_id`) REFERENCES `oc_t_region` (`pk_i_id`),
CONSTRAINT `oc_t_user_ibfk_3` FOREIGN KEY (`fk_i_city_id`) REFERENCES `oc_t_city` (`pk_i_id`),
CONSTRAINT `oc_t_user_ibfk_4` FOREIGN KEY (`fk_i_city_area_id`) REFERENCES `oc_t_city_area` (`pk_i_id`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
Please help.
You must create first the tables you want to reference, for example, here is one foreing key that reference one table, also the type of the key must match, follow this example an add the rest of your tables, I will show a minimum example that works in the creation of the tables, you must add the correct data of course:
CREATE TABLE IF NOT EXISTS `oc_t_country` (
`pk_c_code` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_c_code`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_region` (
`pk_i_id_region` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_i_id_region`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_city` (
`pk_i_id_city` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_i_id_city`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_city_area` (
`pk_i_id_area` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk_i_id_area`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `oc_t_user` (
`pk_i_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`dt_reg_date` datetime NOT NULL,
`dt_mod_date` datetime DEFAULT NULL,
`s_name` varchar(100) NOT NULL,
`s_username` varchar(100) NOT NULL,
`s_password` char(60) NOT NULL,
`s_secret` varchar(40) DEFAULT NULL,
`s_email` varchar(100) NOT NULL,
`s_website` varchar(100) DEFAULT NULL,
`s_phone_land` varchar(45) DEFAULT NULL,
`s_phone_mobile` varchar(45) DEFAULT NULL,
`b_enabled` tinyint(1) NOT NULL DEFAULT '1',
`b_active` tinyint(1) NOT NULL DEFAULT '0',
`s_pass_code` varchar(100) DEFAULT NULL,
`s_pass_date` datetime DEFAULT NULL,
`s_pass_ip` varchar(15) DEFAULT NULL,
`fk_c_country_code` int(10) unsigned DEFAULT NULL,
`s_country` varchar(40) DEFAULT NULL,
`s_address` varchar(100) DEFAULT NULL,
`s_zip` varchar(15) DEFAULT NULL,
`fk_i_region_id` int(10) unsigned DEFAULT NULL,
`s_region` varchar(100) DEFAULT NULL,
`fk_i_city_id` int(10) unsigned DEFAULT NULL,
`s_city` varchar(100) DEFAULT NULL,
`fk_i_city_area_id` int(10) unsigned DEFAULT NULL,
`s_city_area` varchar(200) DEFAULT NULL,
`d_coord_lat` decimal(10,6) DEFAULT NULL,
`d_coord_long` decimal(10,6) DEFAULT NULL,
`b_company` tinyint(1) NOT NULL DEFAULT '0',
`i_items` int(10) unsigned DEFAULT '0',
`i_comments` int(10) unsigned DEFAULT '0',
`dt_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`s_access_ip` varchar(15) NOT NULL DEFAULT '',
PRIMARY KEY (`pk_i_id`),
UNIQUE KEY `s_email` (`s_email`),
KEY `idx_s_name` (`s_name`(6)),
KEY `idx_s_username` (`s_username`),
KEY `fk_c_country_code` (`fk_c_country_code`),
KEY `fk_i_region_id` (`fk_i_region_id`),
KEY `fk_i_city_id` (`fk_i_city_id`),
KEY `fk_i_city_area_id` (`fk_i_city_area_id`),
CONSTRAINT `oc_t_user_ibfk_1` FOREIGN KEY (`fk_c_country_code`) REFERENCES `oc_t_country` (`pk_c_code`),
CONSTRAINT `oc_t_user_ibfk_2` FOREIGN KEY (`fk_i_region_id`) REFERENCES `oc_t_region` (`pk_i_id_region`),
CONSTRAINT `oc_t_user_ibfk_3` FOREIGN KEY (`fk_i_city_id`) REFERENCES `oc_t_city` (`pk_i_id_city`),
CONSTRAINT `oc_t_user_ibfk_4` FOREIGN KEY (`fk_i_city_area_id`) REFERENCES `oc_t_city_area` (`pk_i_id_area`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8;

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

Auto increment in php myadmin is giving weird numbers

I have a database for contacts and Phpmyadmin is giving me weird ids. Sometimes Ids start from 67000 sth and sometimes from 7. In my operations-->auto_increment is set to 676503. I try to change it and press go. It does not change.It keeps on giving me the same number. Also, when I click to show the data, when Ids and contacts are displayed, a column named rows is displayed. The column has only the value 1 for all contacts, as if all contacts are in row 1.
How can I fix this?
Edit:
contacts
CREATE TABLE `contacts` (
`id` int(5) NOT NULL auto_increment,
`date_entered` datetime default NULL,
`date_modified` datetime default NULL,
`modified_user_id` char(36) default NULL,
`created_by` char(36) default NULL,
`description` text,
`deleted` tinyint(1) default '0',
`assigned_user_id` int(36) NOT NULL,
`salutation` varchar(255) default NULL,
`first_name` varchar(100) default NULL,
`last_name` varchar(100) default NULL,
`title` varchar(100) default NULL,
`department` varchar(255) default NULL,
`do_not_call` tinyint(1) default '0',
`phone_home` varchar(100) default NULL,
`phone_mobile` varchar(100) default NULL,
`phone_work` varchar(100) default NULL,
`phone_other` varchar(100) default NULL,
`phone_fax` varchar(100) default NULL,
`primary_address_street` varchar(150) default NULL,
`primary_address_city` varchar(100) default NULL,
`primary_address_state` varchar(100) default NULL,
`primary_address_postalcode` varchar(20) default NULL,
`primary_address_country` varchar(255) default NULL,
`alt_address_street` varchar(150) default NULL,
`alt_address_city` varchar(100) default NULL,
`alt_address_state` varchar(100) default NULL,
`alt_address_postalcode` varchar(20) default NULL,
`alt_address_country` varchar(255) default NULL,
`assistant` varchar(75) default NULL,
`assistant_phone` varchar(100) default NULL,
`lead_source` varchar(255) default NULL,
`reports_to_id` char(36) default NULL,
`birthdate` date default NULL,
`campaign_id` char(36) default NULL,
`age` datetime default NULL,
`days_old` int(6) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `id_2` (`id`),
UNIQUE KEY `id_3` (`id`),
UNIQUE KEY `id_4` (`id`),
UNIQUE KEY `id_5` (`id`),
UNIQUE KEY `id_6` (`id`),
UNIQUE KEY `id_7` (`id`),
KEY `idx_cont_last_first` (`last_name`,`first_name`,`deleted`),
KEY `idx_contacts_del_last` (`deleted`,`last_name`),
KEY `idx_cont_del_reports` (`deleted`,`reports_to_id`,`last_name`),
KEY `idx_reports_to_id` (`reports_to_id`),
KEY `idx_del_id_user` (`deleted`,`id`,`assigned_user_id`),
KEY `idx_cont_assigned` (`assigned_user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=676504 DEFAULT CHARSET=utf8

why Mysql is giving me error 1280 "Wrong Index"

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.