Can I download the Phpmyadmin Database? - mysql

I accidentally deleted the phpmyadmin database that came with it. Now I'm getting errors like:
SQL query: DocumentationEdit Edit
SELECT `db_name` , `comment`
FROM `phpmyadmin`.`pma_column_info`
WHERE `column_name` = '(db_comment)'
MySQL said: Documentation
#1146 - Table 'phpmyadmin.pma_column_info' doesn't exist
So can I download the database again somewhere? Thanks!

I downloaded the package from their website and extracted the table setup script, you can run the following queries which will re-create the required tables:
-- --------------------------------------------------------
-- SQL Commands to set up the pmadb as described in Documentation.html.
--
-- This file is meant for use with MySQL 5 and above!
--
-- This script expects the user pma to already be existing. If we would put a
-- line here to create him too many users might just use this script and end
-- up with having the same password for the controluser.
--
-- This user "pma" must be defined in config.inc.php (controluser/controlpass)
--
-- Please don't forget to set up the tablenames in config.inc.php
--
-- --------------------------------------------------------
--
-- Database : `phpmyadmin`
--
CREATE DATABASE IF NOT EXISTS `phpmyadmin`
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE phpmyadmin;
-- --------------------------------------------------------
--
-- Privileges
--
-- (activate this statement if necessary)
-- GRANT SELECT, INSERT, DELETE, UPDATE ON `phpmyadmin`.* TO
-- 'pma'#localhost;
-- --------------------------------------------------------
--
-- Table structure for table `pma_bookmark`
--
CREATE TABLE IF NOT EXISTS `pma_bookmark` (
`id` int(11) NOT NULL auto_increment,
`dbase` varchar(255) NOT NULL default '',
`user` varchar(255) NOT NULL default '',
`label` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL default '',
`query` text NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM COMMENT='Bookmarks'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_column_info`
--
CREATE TABLE IF NOT EXISTS `pma_column_info` (
`id` int(5) unsigned NOT NULL auto_increment,
`db_name` varchar(64) NOT NULL default '',
`table_name` varchar(64) NOT NULL default '',
`column_name` varchar(64) NOT NULL default '',
`comment` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL default '',
`mimetype` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL default '',
`transformation` varchar(255) NOT NULL default '',
`transformation_options` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `db_name` (`db_name`,`table_name`,`column_name`)
)
ENGINE=MyISAM COMMENT='Column information for phpMyAdmin'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_history`
--
CREATE TABLE IF NOT EXISTS `pma_history` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`username` varchar(64) NOT NULL default '',
`db` varchar(64) NOT NULL default '',
`table` varchar(64) NOT NULL default '',
`timevalue` timestamp NOT NULL,
`sqlquery` text NOT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`,`db`,`table`,`timevalue`)
)
ENGINE=MyISAM COMMENT='SQL history for phpMyAdmin'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_pdf_pages`
--
CREATE TABLE IF NOT EXISTS `pma_pdf_pages` (
`db_name` varchar(64) NOT NULL default '',
`page_nr` int(10) unsigned NOT NULL auto_increment,
`page_descr` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL default '',
PRIMARY KEY (`page_nr`),
KEY `db_name` (`db_name`)
)
ENGINE=MyISAM COMMENT='PDF relation pages for phpMyAdmin'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_relation`
--
CREATE TABLE IF NOT EXISTS `pma_relation` (
`master_db` varchar(64) NOT NULL default '',
`master_table` varchar(64) NOT NULL default '',
`master_field` varchar(64) NOT NULL default '',
`foreign_db` varchar(64) NOT NULL default '',
`foreign_table` varchar(64) NOT NULL default '',
`foreign_field` varchar(64) NOT NULL default '',
PRIMARY KEY (`master_db`,`master_table`,`master_field`),
KEY `foreign_field` (`foreign_db`,`foreign_table`)
)
ENGINE=MyISAM COMMENT='Relation table'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_table_coords`
--
CREATE TABLE IF NOT EXISTS `pma_table_coords` (
`db_name` varchar(64) NOT NULL default '',
`table_name` varchar(64) NOT NULL default '',
`pdf_page_number` int(11) NOT NULL default '0',
`x` float unsigned NOT NULL default '0',
`y` float unsigned NOT NULL default '0',
PRIMARY KEY (`db_name`,`table_name`,`pdf_page_number`)
)
ENGINE=MyISAM COMMENT='Table coordinates for phpMyAdmin PDF output'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_table_info`
--
CREATE TABLE IF NOT EXISTS `pma_table_info` (
`db_name` varchar(64) NOT NULL default '',
`table_name` varchar(64) NOT NULL default '',
`display_field` varchar(64) NOT NULL default '',
PRIMARY KEY (`db_name`,`table_name`)
)
ENGINE=MyISAM COMMENT='Table information for phpMyAdmin'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_designer_coords`
--
CREATE TABLE IF NOT EXISTS `pma_designer_coords` (
`db_name` varchar(64) NOT NULL default '',
`table_name` varchar(64) NOT NULL default '',
`x` INT,
`y` INT,
`v` TINYINT,
`h` TINYINT,
PRIMARY KEY (`db_name`,`table_name`)
)
ENGINE=MyISAM COMMENT='Table coordinates for Designer'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
-- --------------------------------------------------------
--
-- Table structure for table `pma_tracking`
--
CREATE TABLE IF NOT EXISTS `pma_tracking` (
`db_name` varchar(64) collate utf8_bin NOT NULL,
`table_name` varchar(64) collate utf8_bin NOT NULL,
`version` int(10) unsigned NOT NULL,
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`schema_snapshot` text collate utf8_bin NOT NULL,
`schema_sql` text collate utf8_bin,
`data_sql` longtext collate utf8_bin,
`tracking` set('UPDATE','REPLACE','INSERT','DELETE','TRUNCATE','CREATE DATABASE','ALTER DATABASE','DROP DATABASE','CREATE TABLE','ALTER TABLE','RENAME TABLE','DROP TABLE','CREATE INDEX','DROP INDEX','CREATE VIEW','ALTER VIEW','DROP VIEW') collate utf8_bin default NULL,
`tracking_active` int(1) unsigned NOT NULL default '1',
PRIMARY KEY (`db_name`,`table_name`,`version`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=COMPACT;
-- --------------------------------------------------------
--
-- Table structure for table `pma_userconfig`
--
CREATE TABLE IF NOT EXISTS `pma_userconfig` (
`username` varchar(64) NOT NULL,
`timevalue` timestamp NOT NULL,
`config_data` text NOT NULL,
PRIMARY KEY (`username`)
)
ENGINE=MyISAM COMMENT='User preferences storage for phpMyAdmin'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

By default, the sql to create the phpmyadmin database is in /PhpMyAdminFolder/examples/create_tables.sql, as stated on the documentation (section "phpMyAdmin configuration storage").
There is no need to reinstall it all just for that.

Related

MySQL - Copy row to another table with foreign key

i want to copy 1 row from this table:
tableold
-----------
oid (primary key, auto_incremment)
name
age
detail
to this table:
tablenew
------------
nid (primary key, auto_incremment)
fid (foreign key to another table)
name
age
detail
With this SQL-Command:
INSERT INTO tablenew (tablenew.name, tablenew.age, tablenew.detail)
SELECT tableold.name, tableold.age, tableold.detail
FROM tableold
WHERE tableold.oid = 123;
But i get the error:
ERROR 1364 (HY000): Field 'fid' doesn't have a default value
Is there any possibility to manually add the foreign key 'fid' to the SQL-Command?
*Edit, i have added the CREATE table statements:
CREATE TABLE `othertable` (
`fid` int NOT NULL AUTO_INCREMENT,
`value1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`value2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`value3` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
`value4` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
PRIMARY KEY (`fid`),
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8;
CREATE TABLE `tablenew` (
`nid` int NOT NULL AUTO_INCREMENT,
`fid` int NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`detail` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`sid`),
KEY `fid_fkey` (`fid`),
CONSTRAINT `fid_fkey` FOREIGN KEY (`fid`) REFERENCES `othertable` (`fid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `tableold` (
`oid` int NOT NULL AUTO_INCREMENT,
`name` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`detail` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=InnoDB AUTO_INCREMENT=157 DEFAULT CHARSET=utf8;
I have solved it:
INSERT INTO tablenew (tablenew.fid, tablenew.name, tablenew.age, tablenew.detail)
SELECT 'value', tableold.name, tableold.age, tableold.detail
FROM tableold
WHERE tableold.oid = value;

MySQL updates column to always NULL

I have two tables which basically one is a master table and the other one is a table for developers. From time to time I would copy review the record from the developer table and move it to the master table:
--SHOW CREATE TABLE DOCUMENT --
CREATE TABLE `document` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(1000) CHARACTER SET utf8 NOT NULL,
`title` varchar(1000) CHARACTER SET utf8 NOT NULL,
`content` longtext CHARACTER SET utf8,
`source_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_crawled` datetime DEFAULT NULL,
`mime_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_posted` datetime DEFAULT NULL,
`tier` int(11) DEFAULT NULL,
`html_content` longtext CHARACTER SET utf8,
`dev` varchar(255) CHARACTER SET utf8 NOT NULL,
`dev_document_id` int(11) NOT NULL,
`promote` int(11) DEFAULT NULL,
`domain` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`,`url`(500),`dev`,`dev_document_id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB AUTO_INCREMENT=628871 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED
--SHOW CREATE TABLE DOCUMENT_DEVELOPER--
CREATE TABLE `document_developer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(1000) CHARACTER SET utf8 NOT NULL,
`title` varchar(1000) CHARACTER SET utf8 DEFAULT NULL,
`content` longtext CHARACTER SET utf8,
`jurisdiction` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`source_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_crawled` datetime DEFAULT NULL,
`mime_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`html_content` longtext CHARACTER SET utf8,
`date_posted` datetime DEFAULT NULL,
`tier` int(11) DEFAULT NULL,
`promote` int(11) NOT NULL,
`modified` tinyint(1) NOT NULL DEFAULT '0',
`date_modified` datetime DEFAULT NULL,
UNIQUE KEY `url_promote_UNIQUE` (`url`,`promote`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=637067 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED
/*!50100 PARTITION BY LIST (promote)
(PARTITION part0 VALUES IN (-5) ENGINE = InnoDB,
PARTITION part1 VALUES IN (-4) ENGINE = InnoDB,
PARTITION part2 VALUES IN (-3) ENGINE = InnoDB,
PARTITION part3 VALUES IN (-2) ENGINE = InnoDB,
PARTITION part4 VALUES IN (-1) ENGINE = InnoDB,
PARTITION part5 VALUES IN (0) ENGINE = InnoDB,
PARTITION part6 VALUES IN (1) ENGINE = InnoDB,
PARTITION part7 VALUES IN (2) ENGINE = InnoDB) */
The column of dev_document_id will be the id of record in document_developer once the copy is done.
However, when i try to run update as below:
UPDATE document as d INNER JOIN document_developer AS dd ON d.dev_document_id = dd.id
SET
d.content=dd.content
WHERE dd.modified='1' AND dd.promote='2';
it will always give me erroneous content value such as NULL NULL NULL ... Any help will be greatly appreciated.
Your query should be
UPDATE document as d INNER JOIN document_developer AS da ON
d.dev_document_id = da.id
SET
d.content=da.content
WHERE da.modified='1' AND da.promote='2';

XAMPP's PHPMyAdmin Ignores AUTO_INCREMENT Attribute When Exporting the Table

The problem that I am going to tell is for all the tables in the DB, When I dump my DB it ignores AUTO_INCREMENT attribute. My actual table is as the following:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL AUTO_INCREMENT,
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
But if I dump the table using PHPMyAdmin it does not add auto_increment which I mentioned before.
The output of the exported .sql file's content is here:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL, -- AUTO_INCREMENT is missing
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
I especially checked that if auto_increment is disabled in CREATE TABLE options but no, it is not.
This was fixed in phpMyAdmin version 4.5.0.1 (Sep 2015):
https://github.com/phpmyadmin/phpmyadmin/issues/11492
I supposed it ignores auto increment but I explore file and at the bottom of sql I found that it altered to auto increment somewhere else

Error 1005: Can't create table can't find where the issue is

I have created a EER model using MySQL Workbench (latest release). Apparently all is fine but when I try to do a FORWARD ENGINEER I get the common Error 1005: Can't create table. As suggested in this post I check every step on the first answer but without success, can any give some extra eyes to my query and try to find where my error is? I can't see it.
This is the error generated by the tool:
Executing SQL script in server
ERROR: Error 1005: Can't create table
'portal_de_logistica.order_has_comment' (errno: 150)
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`order_has_comment` (
`user_id` INT(11) NOT NULL,
`no_order` VARCHAR(15) NOT NULL,
`comment` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`, `no_order`),
CONSTRAINT `fk_fos_user_rnd`
FOREIGN KEY (`user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `fk_tb_order_rnd`
FOREIGN KEY (`no_order`)
REFERENCES `portal_de_logistica`.`tb_order` (`no_order`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
And this is the complete SQL sentences:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `portal_de_logistica` ;
CREATE SCHEMA IF NOT EXISTS `portal_de_logistica` DEFAULT CHARACTER SET latin1 ;
USE `portal_de_logistica` ;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`company`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`company` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`company` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL,
`register_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`country` CHAR(3) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`contact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`contact` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`contact` (
`id` INT NOT NULL AUTO_INCREMENT,
`company_id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(90) NOT NULL,
`c_position` VARCHAR(150) NULL,
`email` VARCHAR(150) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_contact_company1`
FOREIGN KEY (`company_id`)
REFERENCES `portal_de_logistica`.`company` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
CREATE INDEX `fk_contact_company1_idx` ON `portal_de_logistica`.`contact` (`company_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`tb_order`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`tb_order` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`tb_order` (
`no_order` VARCHAR(15) NOT NULL,
`company_id` INT NOT NULL,
`business_case` VARCHAR(15) NULL,
`charge_status` ENUM('0','1','2','3','4','5','6','7','8') NOT NULL COMMENT '0: Ninguno\n1: Proceso de Fabricacion\n2: Pickup en destino\n3: A la espera de recojo por cliente\n4: Carga en transito\n5: Carga arribada\n6: En proceso de aduana\n7: Entregado a cliente\n8: En bodega',
`eta` DATE NULL,
`etd` DATE NULL,
`transport_media` ENUM('0','1','2') NOT NULL COMMENT '0: EXW\n1: Maritimo' /* comment truncated */ /*2: Aereo*/,
`incoterm` ENUM('0','1','2','3','4') NULL COMMENT '0: Ninguno\n1: EWX\n2: FOB\n3: CIF' /* comment truncated */ /*4: DDP*/,
PRIMARY KEY (`no_order`),
CONSTRAINT `fk_order_company1`
FOREIGN KEY (`company_id`)
REFERENCES `portal_de_logistica`.`company` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
CREATE UNIQUE INDEX `no_order_UNIQUE` ON `portal_de_logistica`.`tb_order` (`no_order` ASC);
CREATE INDEX `fk_order_company1_idx` ON `portal_de_logistica`.`tb_order` (`company_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_group`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_group` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_group` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`roles` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL COMMENT '(DC2Type:array)',
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE UNIQUE INDEX `UNIQ_583D1F3E5E237E06` ON `portal_de_logistica`.`fos_user_group` (`name` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_user` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`username_canonical` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`email` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`email_canonical` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`enabled` TINYINT(1) NOT NULL,
`salt` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`password` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`last_login` DATETIME NULL DEFAULT NULL,
`locked` TINYINT(1) NOT NULL,
`expired` TINYINT(1) NOT NULL,
`expires_at` DATETIME NULL DEFAULT NULL,
`confirmation_token` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`password_requested_at` DATETIME NULL DEFAULT NULL,
`roles` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL COMMENT '(DC2Type:array)',
`credentials_expired` TINYINT(1) NOT NULL,
`credentials_expire_at` DATETIME NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
`date_of_birth` DATETIME NULL DEFAULT NULL,
`firstname` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`lastname` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`website` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`biography` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`gender` VARCHAR(1) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`locale` VARCHAR(8) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`timezone` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`phone` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`facebook_uid` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`facebook_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`facebook_data` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`twitter_uid` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`twitter_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`twitter_data` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`gplus_uid` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`gplus_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`gplus_data` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`token` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`two_step_code` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE UNIQUE INDEX `UNIQ_C560D76192FC23A8` ON `portal_de_logistica`.`fos_user_user` (`username_canonical` ASC);
CREATE UNIQUE INDEX `UNIQ_C560D761A0D96FBF` ON `portal_de_logistica`.`fos_user_user` (`email_canonical` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_user_group`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_user_group` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_user_group` (
`user_id` INT(11) NOT NULL,
`group_id` INT(11) NOT NULL,
PRIMARY KEY (`user_id`, `group_id`),
CONSTRAINT `FK_B3C77447FE54D947`
FOREIGN KEY (`group_id`)
REFERENCES `portal_de_logistica`.`fos_user_group` (`id`)
ON DELETE CASCADE,
CONSTRAINT `FK_B3C77447A76ED395`
FOREIGN KEY (`user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `IDX_B3C77447A76ED395` ON `portal_de_logistica`.`fos_user_user_group` (`user_id` ASC);
CREATE INDEX `IDX_B3C77447FE54D947` ON `portal_de_logistica`.`fos_user_user_group` (`group_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`media__gallery`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`media__gallery` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`media__gallery` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`context` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`default_format` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`enabled` TINYINT(1) NOT NULL,
`updated_at` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`media__media`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`media__media` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`media__media` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`description` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`enabled` TINYINT(1) NOT NULL,
`provider_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`provider_status` INT(11) NOT NULL,
`provider_reference` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`provider_metadata` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`width` INT(11) NULL DEFAULT NULL,
`height` INT(11) NULL DEFAULT NULL,
`length` DECIMAL(10,0) NULL DEFAULT NULL,
`content_type` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`content_size` INT(11) NULL DEFAULT NULL,
`copyright` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`author_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`context` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`cdn_is_flushable` TINYINT(1) NULL DEFAULT NULL,
`cdn_flush_at` DATETIME NULL DEFAULT NULL,
`cdn_status` INT(11) NULL DEFAULT NULL,
`updated_at` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`media__gallery_media`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`media__gallery_media` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`media__gallery_media` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`gallery_id` INT(11) NULL DEFAULT NULL,
`media_id` INT(11) NULL DEFAULT NULL,
`position` INT(11) NOT NULL,
`enabled` TINYINT(1) NOT NULL,
`updated_at` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_80D4C541EA9FDD75`
FOREIGN KEY (`media_id`)
REFERENCES `portal_de_logistica`.`media__media` (`id`),
CONSTRAINT `FK_80D4C5414E7AF8F`
FOREIGN KEY (`gallery_id`)
REFERENCES `portal_de_logistica`.`media__gallery` (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `IDX_80D4C5414E7AF8F` ON `portal_de_logistica`.`media__gallery_media` (`gallery_id` ASC);
CREATE INDEX `IDX_80D4C541EA9FDD75` ON `portal_de_logistica`.`media__gallery_media` (`media_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_user_has_company`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_user_has_company` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_user_has_company` (
`fos_user_user_id` INT(11) NOT NULL,
`company_id` INT NOT NULL,
PRIMARY KEY (`fos_user_user_id`, `company_id`),
CONSTRAINT `fk_fos_user_user_has_company_fos_user_user1`
FOREIGN KEY (`fos_user_user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `fk_fos_user_user_has_company_company1`
FOREIGN KEY (`company_id`)
REFERENCES `portal_de_logistica`.`company` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `fk_fos_user_user_has_company_company1_idx` ON `portal_de_logistica`.`fos_user_user_has_company` (`company_id` ASC);
CREATE INDEX `fk_fos_user_user_has_company_fos_user_user1_idx` ON `portal_de_logistica`.`fos_user_user_has_company` (`fos_user_user_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`order_has_media__media`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`order_has_media__media` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`order_has_media__media` (
`order_no_order` VARCHAR(15) NOT NULL,
`media__media_id` INT(11) NOT NULL,
PRIMARY KEY (`order_no_order`, `media__media_id`),
CONSTRAINT `fk_order_has_media__media_order1`
FOREIGN KEY (`order_no_order`)
REFERENCES `portal_de_logistica`.`tb_order` (`no_order`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_order_has_media__media_media__media1`
FOREIGN KEY (`media__media_id`)
REFERENCES `portal_de_logistica`.`media__media` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_order_has_media__media_media__media1_idx` ON `portal_de_logistica`.`order_has_media__media` (`media__media_id` ASC);
CREATE INDEX `fk_order_has_media__media_order1_idx` ON `portal_de_logistica`.`order_has_media__media` (`order_no_order` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`order_has_comment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`order_has_comment` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`order_has_comment` (
`user_id` INT(11) NOT NULL,
`no_order` VARCHAR(15) NOT NULL,
`comment` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`, `no_order`),
CONSTRAINT `fk_fos_user_rnd`
FOREIGN KEY (`user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `fk_tb_order_rnd`
FOREIGN KEY (`no_order`)
REFERENCES `portal_de_logistica`.`tb_order` (`no_order`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `fk_fos_user_user_has_tb_order_tb_order1_idx` ON `portal_de_logistica`.`order_has_comment` (`no_order` ASC);
CREATE INDEX `fk_fos_user_user_has_tb_order_fos_user_user1_idx` ON `portal_de_logistica`.`order_has_comment` (`user_id` ASC);
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
The issue is that tb_order doesn't specify a charset or collation like the other tables so will use the database default.
If the default is different from utf8 and utf8_unicode_ci then the foreign key on a text field wont work.

Help with MySQL Query with many Joins

Setup: Contact database using 4 tables
Contacts
Cities
States
Zips
Structure:
CREATE TABLE `contacts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`first` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`prefix` varchar(50) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`suffix` varchar(50) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`address` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`address_1` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`city_id` int(100) DEFAULT NULL,
`state_id` int(20) DEFAULT NULL,
`alt_address_1` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`alt_address_2` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`alt_city` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`alt_state` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`alt_zip` varchar(15) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`publish_name` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`salutation` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`mail_label` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`solicitor` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`is_volunteer` tinyint(1) DEFAULT NULL,
`is_sponsor` tinyint(1) DEFAULT '0',
`is_company` tinyint(1) DEFAULT '0',
`is_foundation` tinyint(1) DEFAULT '0',
`status` varchar(15) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`created_on` datetime NOT NULL,
`created_by` varchar(30) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modified_by` varchar(100) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`agency_id` int(25) DEFAULT NULL,
`primary_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `primary_id` (`primary_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3008 DEFAULT CHARSET=utf8
CREATE TABLE `cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(50) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`stateid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `city` (`city`)
) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8
CREATE TABLE `states` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`abbreviation` varchar(2) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`state` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `state` (`state`),
UNIQUE KEY `abbreviation` (`abbreviation`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8
CREATE TABLE `zips` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`zip` varchar(10) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`cityid` int(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `zip` (`zip`)
) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8
I have filled the contacts with 111 contacts, the states are simply all of the states, and cities have corresponding id keys that relate to the state id, zip codes have a key to match to a city.
The query is to generate a list of people to match up to the proper fields. Here is the query.
SELECT concat(contacts.last,' ', contacts.first) as name
, cities.city
, zips.zip
FROM contacts
JOIN cities
ON cities.id = contacts.city_id
JOIN states ON states.id = contacts.state_id
JOIN zips ON zips.cityid = cities.id
This query, return 338 rows, of a possible 11 contacts. There obvious duplicates. This happens when I join zip codes, which because they belong to more than 1 city, it gets matched for each city (I think thats what is happening). Anyone have an answer on how to properly join these tables?
Thank you.
Rich
I believe that you should re-think the surrogate key usage on a lot of these tables and use natural keys where possible. Taking the state table for an example, in most cases it will be acceptable to simply use the state short (ie. TX vs. Texas) for data and display purposes. This means that if you removed the incrementing ID on the states table and used a natural key for each state, you would reduce the necessecity of a join in 90% of cases.
Then use state.abbriviation as the FK in tables that need to store state values. Extending this to zipcodes and cities, you can FK the state abbr to the city table, and make a compound FK form the cities table to the contacts table giving you a key for both the city and the state at the same time.
Example schema (excluded zipcodes table and shortened contacts table):
CREATE TABLE IF NOT EXISTS `states` (
`state_id` CHAR(2) NOT NULL ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`state_id`) ,
UNIQUE INDEX `state_name` (`name` ASC)
)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `cities` (
`state_id` CHAR(2) NOT NULL ,
`city_name` VARCHAR(255) NOT NULL ,
PRIMARY KEY (`state_id`, `city_name`) ,
INDEX `fk_city_state_id` (`state_id` ASC) ,
CONSTRAINT `fk_city_state_id`
FOREIGN KEY (`state_id` )
REFERENCES `states` (`state_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `contacts` (
`contacts_id` INT NOT NULL AUTO_INCREMENT ,
`state` CHAR(2) NULL ,
`city` VARCHAR(255) NULL ,
PRIMARY KEY (`contacts_id`) ,
INDEX `fk_contact_city` (`state` ASC, `city` ASC) ,
INDEX `fk_contact_state` (`state` ASC) ,
CONSTRAINT `fk_contact_city`
FOREIGN KEY (`state` , `city` )
REFERENCES `cities` (`state_id` , `city_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_contact_state`
FOREIGN KEY (`state` )
REFERENCES `states` (`state_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Data for table `states`
-- -----------------------------------------------------
SET AUTOCOMMIT=0;
INSERT INTO `states` (`state_id`, `name`) VALUES ('TX', 'Texas');
INSERT INTO `states` (`state_id`, `name`) VALUES ('CA', 'California');
INSERT INTO `states` (`state_id`, `name`) VALUES ('OR', 'Oregon');
COMMIT;
-- -----------------------------------------------------
-- Data for table `cities`
-- -----------------------------------------------------
SET AUTOCOMMIT=0;
INSERT INTO `cities` (`state_id`, `city_name`) VALUES ('CA', 'modesto');
INSERT INTO `cities` (`state_id`, `city_name`) VALUES ('OR', 'protland');
INSERT INTO `cities` (`state_id`, `city_name`) VALUES ('TX', 'Dallas');
COMMIT;
Now your query is simplified except in the extreme case where you'd need a full state nomenclature:
SELECT
concat(contacts.last,' ', contacts.first) as name,
city,
state,
zip
FROM contacts
WHERE {INSERTWHERE}
your tables a properly joined. i think the problem you have here is that you have normalized your data improperly and gone too far. just store the address as provided. don't try and split it in to table with numeric ID's.
for example, what possible benefit do you get by storing the state as a number in the contact table instead of just the state code? and the same question applies to city.