MySQL foreign key creation with alter table command - mysql

I created some tables using MySQL Workbench, and then did forward ‘forward engineer’ to create scripts to create these tables. BUT, the scripts lead me to a number of problems. One of which involves the foreign keys. So I tried creating separate foreign key additions using alter table and I am still getting problems. The code is below (the set statements, drop/create statements I left in … though I don’t think they should matter for this):
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';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`UserName` VARCHAR(35) NOT NULL ,
`Num_Accts` INT NOT NULL ,
`Password` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`User_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`User_Space`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User_Space` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User_Space` (
`User_UserName` VARCHAR(35) NOT NULL ,
`User_Space_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_Space_ID`),
FOREIGN KEY (`User_UserName`)
REFERENCES `mydb`.`User` (`UserName`)
ON UPDATE CASCADE ON DELETE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
The error this produces is:
Error Code: 1005 Can't create table 'mydb.user_space' (errno: 150)
Anybody know what the heck I’m doing wrong?? And anybody else have problems with the script generation done by mysql workbench? It’s a nice tool, but annoying that it pumps out scripts that don’t work for me.
[As an fyi here’s the script it auto-generates:
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';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`UserName` VARCHAR(35) NOT NULL ,
`Num_Accts` INT NOT NULL ,
`Password` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`User_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`User_Space`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User_Space` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User_Space` (
`User_Space_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_Space_ID`) ,
INDEX `User_ID` () ,
CONSTRAINT `User_ID`
FOREIGN KEY ()
REFERENCES `mydb`.`User` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
**
Thanks!]

I haven't used MySQL Workbench to generate a lot of schemas, but I found the problem in the script. The foreign key definition in the User_Space table is attempting to create a foreign key on an unindexed column in the User table. If you alter the User definition to have an index on UserName, like this:
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`UserName` VARCHAR(35) NOT NULL ,
`Num_Accts` INT NOT NULL ,
`Password` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`User_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_ID`),
INDEX(`UserName`)
)
ENGINE = InnoDB;
... the script will succeed. It sounds like MySQL Workbench probably isn't taking indexes into account when it generates the foreign key definitions. I'm not sure if you can fix this in your schema diagrams or if it's a bigger bug in the program, but I'd see if you could add index definitions in the right places and determine if that fixes the script generation.

Related

Why am I not able to inspect my database's tables?

I'll start by saying that my experience in mySQL is limited. I've only been using workbench for a few months.
Me and a couple of buddies are working on a database for a pet shop scenario, and I've been tasked with writing some queries to test it out. At first my pal's script for the database wasn't running, because I was getting errors with the "VISIBLE" keyword. Since indexes are visible by default I chucked them all out. Then I got an error that some of his prim keys were set to null. I assumed that was by accident and set them all to "NOT NULL".
I proceeded then to run the script, and got no errors, so I assumed everything was fine.
He had told me he recently populated the tables, but it doesn't look like he did. I mean, I'm not certain that he did but he seems to have the columns set up nicely at the very least.
When I ran the code, I inspected the tables but there were no tables listed.
I am not sure where to go from here. I'll attach the code in question. If anyone can point me in the right direction, I'd be grateful.
-- Tue Nov 24 20:45:34 2020
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`species`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`species` (
`species_id` INT NOT NULL,
`species_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`species_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`animals`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`animals` (
`animal_id` INT NOT NULL,
`species_id` INT NOT NULL,
`animal_name` VARCHAR(45) NOT NULL,
`price` DECIMAL(8,2) NOT NULL,
`date_arived` DATETIME not NULL,
`quantity` INT NOT NULL,
PRIMARY KEY (`animal_id`),
INDEX `species_id_fk_idx` (`species_id` ASC),
CONSTRAINT `species_id_fk`
FOREIGN KEY (`species_id`)
REFERENCES `mydb`.`species` (`species_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`distributors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`distributors` (
`distributor_id` INT NOT NULL,
`distributor_email` VARCHAR(255) NOT NULL,
`distributor_name` VARCHAR(45) NOT NULL,
`distributor_address` VARCHAR(45) NOT NULL,
`distributor_phone` VARCHAR(15) NOT NULL,
PRIMARY KEY (`distributor_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`orders`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`orders` (
`order_number` INT not NULL,
`distributor_id` INT NOT NULL,
`order_date` DATETIME not NULL,
`arival_date` DATETIME not NULL,
PRIMARY KEY (`order_number`),
INDEX `distributor_id_fk_idx` (`distributor_id` ASC),
CONSTRAINT `distributor_id_fk`
FOREIGN KEY (`distributor_id`)
REFERENCES `mydb`.`distributors` (`distributor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`invoice`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`invoice` (
`invoice_id` INT NOT NULL,
`order_number` INT NOT NULL,
`animal_id` INT NOT NULL,
`animal_price` DECIMAL(8,2) NOT NULL,
`quantity` INT NOT NULL,
PRIMARY KEY (`invoice_id`),
INDEX `order_number_fk_idx` (`order_number` ASC),
INDEX `animal_id_fk_idx` (`animal_id` ASC),
CONSTRAINT `order_number_fk`
FOREIGN KEY (`order_number`)
REFERENCES `mydb`.`orders` (`order_number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `animal_id_fk`
FOREIGN KEY (`animal_id`)
REFERENCES `mydb`.`animals` (`animal_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;

ERROR: Error 1215: Cannot add foreign key constraint even when both fields are of the same type

When trying to build a mysql database consisting of two tables, some reason I am getting Error 1215 and I'm unable to find the cause. Both fields are of the same type. I am trying to do this with MySQL workbench. Anyone knows the cause of this error?
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Classified_tweets`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Classified_tweets` (
`QueryID` INT NULL,
`TweetID` BIGINT NOT NULL,
`TweetKeyword` VARCHAR(45) NULL,
`TweetUsername` VARCHAR(45) NULL,
`TweetDate` VARCHAR(45) NULL,
`TweetLocation` VARCHAR(45) NULL,
`TweetContent` VARCHAR(150) NULL,
`TweetLabel` TINYINT(1) NULL,
PRIMARY KEY (`TweetID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Words_frequency`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Words_frequency` (
`QueryID` INT NOT NULL,
`Word` VARCHAR(45) NOT NULL,
`Label` INT NULL,
`Frequency` VARCHAR(45) NULL,
PRIMARY KEY (`QueryID`, `Word`),
INDEX `fk_Words_frequency_Classified_tweets_idx` (`QueryID` ASC),
CONSTRAINT `fk_Words_frequency_Classified_tweets`
FOREIGN KEY (`QueryID`)
REFERENCES `mydb`.`Classified_tweets` (`QueryID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
You have defined the primary key on Classified_tweets as TweetID.
You should be using this column for the foreign key relationship. Perhaps:
CREATE TABLE IF NOT EXISTS `mydb`.`Words_frequency` (
`TweetID` BIGINT NOT NULL,
`Word` VARCHAR(45) NOT NULL,
`Label` INT NULL,
`Frequency` VARCHAR(45) NULL,
PRIMARY KEY (`QueryID`, `Word`),
INDEX `fk_Words_frequency_Classified_tweets_idx` (`QueryID` ASC),
CONSTRAINT `fk_Words_frequency_Classified_tweets`
FOREIGN KEY (`TweetID`)
REFERENCES `mydb`.`Classified_tweets` (`TweetID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Note: I changed the first column from QueryId to TweetId. This should fix the problem, although your intention might be something else.
You can only refer to a column which is a primary key or a unique attribute. If you want to refer to query ID itself, then make it as the primary key or a unique attribute. or change the Words_frequency table to refer to tweetID.

Mysql Workbench - Error processing forward engineering

I want to raise a small problem MySQL Workbench, but first of all I want you to know that this question previously made, but without much explanation and a lot of code that leaves many questions, and as I have not been to find the problem, the question will easy and explanatory possible.
1) I made a simple model in MySQL Workbench, a relationship between two tables.
If I make the execution "Forward engineering" of this model is created without problem.
But the problem it is following...
The code that generates me MySQL Workbench:
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `example` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `example` ;
-- -----------------------------------------------------
-- Table `example`.`status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`status` (
`id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `example`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
`status_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_users_status_idx` (`status_id` ASC),
CONSTRAINT `fk_users_status`
FOREIGN KEY (`status_id`)
REFERENCES `example`.`status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
2) I need to make an optimization to the table status, and I will make a simple change because the status.id only have a maximum of 10 records, and the data type INT id field think it's too big for this then what I would do is change id INT to id TINYINT
I made this simple change and stuck:
The code that generates me MySQL Workbench:
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `example` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `example` ;
-- -----------------------------------------------------
-- Table `example`.`status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`status` (
`id` TINYINT UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `example`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
`status_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_users_status_idx` (`status_id` ASC),
CONSTRAINT `fk_users_status`
FOREIGN KEY (`status_id`)
REFERENCES `example`.`status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Error: If I make the execution "Forward engineering" of this model it shows me the following error:
I would greatly appreciate your help, thank you very much.

Can't create table (ERROR 1215) even with SET FOREIGN_KEY_CHECKS=0;

What I am trying to do I've done countless times but now fails having upgraded from MySql 5.6.26 to 5.6.27 (also fails in 5.7.x). I have a database with a bunch of tables with foreign key constraints. I have exported (using phpMyAdmin) replacements tables specifying the following export options:
Disable foreign key checks
Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT / TRIGGER statement
Add CREATE TABLE statement
The resulting export file contains in part:
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `application`;
CREATE TABLE `application` (
`PK_Application` int(11) NOT NULL,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
. . .
. . .
DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
. . .
. . .
ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE;
When this file is input to either mysql.exe or the phpMyAdmin import function when tables application and product_x_application already exist, the DROP statement for table application succeeds but the subsequent CREATE TABLE statement fails with:
ERROR 1215 (HY000): Cannot add foreign key constraint
If, however, I first drop the product_x_application table, which has a foreign key constraint product_x_application_ibfk_2 that references table application, then the CREATE TABLE statement succeeds.
I have since learned more info. I went up to my production machine, which is running Linux with a MySql 5.5 server and phpMyAdmin 4.4.23 phpMyAdmin, and did an export of just the two tables in question and was able to import the resulting file into my MySql 5.6.27 server. Here are the two complete export files (structure only). First from the problematic 5.6.27 server:
-- phpMyAdmin SQL Dump
-- version 4.4.15
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 10, 2016 at 07:01 AM
-- Server version: 5.6.26-log
-- PHP Version: 5.6.12
SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `spmorell_sami`
--
-- --------------------------------------------------------
--
-- Table structure for table `application`
--
DROP TABLE IF EXISTS `application`;
CREATE TABLE `application` (
`PK_Application` int(11) NOT NULL,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `product_x_application`
--
DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `application`
--
ALTER TABLE `application`
ADD PRIMARY KEY (`PK_Application`);
--
-- Indexes for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD PRIMARY KEY (`PK_Product_X_Application`),
ADD KEY `Product_PKID` (`Product_PKID`),
ADD KEY `FK_Application` (`FK_Application`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `application`
--
ALTER TABLE `application`
MODIFY `PK_Application` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `product_x_application`
--
ALTER TABLE `product_x_application`
MODIFY `PK_Product_X_Application` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_1` FOREIGN KEY (`Product_PKID`) REFERENCES `product` (`Product_PKID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE;
SET FOREIGN_KEY_CHECKS=1;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Notice that the primary key for the application table is defined in an ALTER TABLE statement. Here is the exported file form the 5.5 server:
-- phpMyAdmin SQL Dump
-- version 3.5.8.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 10, 2016 at 05:09 AM
-- Server version: 5.5.42-37.1-log
-- PHP Version: 5.4.23
SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `spmorell_sami`
--
-- --------------------------------------------------------
--
-- Table structure for table `application`
--
DROP TABLE IF EXISTS `application`;
CREATE TABLE `application` (
`PK_Application` int(11) NOT NULL AUTO_INCREMENT,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL,
PRIMARY KEY (`PK_Application`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
-- --------------------------------------------------------
--
-- Table structure for table `product_x_application`
--
DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL AUTO_INCREMENT,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL,
PRIMARY KEY (`PK_Product_X_Application`),
KEY `Product_PKID` (`Product_PKID`),
KEY `FK_Application` (`FK_Application`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1633 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `product_x_application_ibfk_1` FOREIGN KEY (`Product_PKID`) REFERENCES `product` (`Product_PKID`) ON DELETE CASCADE ON UPDATE CASCADE;
SET FOREIGN_KEY_CHECKS=1;
Here the primary key for the application table is defined immediately when the table is defined. I believe the problem is that the product_x_application table, which already exists when the system is attempting to re-create the application table, has a foreign key constraint to a table that when re-created does not have the required index on the foreign key because the primary key has not yet been defined.
A foreign key should reference a unique column (e.g., a primary key). PK_Application, currently, is not such a column. From its name, though, it seems as though you meant for it to be a primary key:
ALTER TABLE `application`
ADD CONSTRAINT `application_pk` PRIMARY KEY (`PK_Application`);
I have even more info. I had installed a later version of phpMyAdmin (4.5.0.2) but forgot all about it. For giggles I decided to do the export with that. As before, I specified as options ADD DROP TABLE (ADD CREATE TABLE is automatically checked and cannot be unchecked). But now, next to the IF NOT EXISTS sub-option for ADD CREATE TABLE it says in parentheses, "(less efficient as indexes will be generated during table creation)", which of course is precisely what I need (normally I would not think of checking this option because I know the table can't possibly exist at this point). And the output is:
-- phpMyAdmin SQL Dump
-- version 4.5.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 10, 2016 at 07:54 AM
-- Server version: 5.6.26-log
-- PHP Version: 5.6.12
SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `spmorell_sami`
--
-- --------------------------------------------------------
--
-- Table structure for table `application`
--
DROP TABLE IF EXISTS `application`;
CREATE TABLE IF NOT EXISTS `application` (
`PK_Application` int(11) NOT NULL AUTO_INCREMENT,
`FK_Parent` int(11) DEFAULT NULL,
`ApplicationLevel` tinyint(4) NOT NULL,
`Description` varchar(35) NOT NULL,
`Sequence` tinyint(4) NOT NULL,
PRIMARY KEY (`PK_Application`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `product_x_application`
--
DROP TABLE IF EXISTS `product_x_application`;
CREATE TABLE IF NOT EXISTS `product_x_application` (
`PK_Product_X_Application` int(11) NOT NULL AUTO_INCREMENT,
`Product_PKID` varchar(36) NOT NULL,
`FK_Application` int(11) NOT NULL,
PRIMARY KEY (`PK_Product_X_Application`),
KEY `Product_PKID` (`Product_PKID`),
KEY `FK_Application` (`FK_Application`)
) ENGINE=InnoDB AUTO_INCREMENT=1633 DEFAULT CHARSET=latin1;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `product_x_application`
--
ALTER TABLE `product_x_application`
ADD CONSTRAINT `product_x_application_ibfk_1` FOREIGN KEY (`Product_PKID`) REFERENCES `product` (`Product_PKID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `product_x_application_ibfk_2` FOREIGN KEY (`FK_Application`) REFERENCES `application` (`PK_Application`) ON DELETE CASCADE ON UPDATE CASCADE;
SET FOREIGN_KEY_CHECKS=1;
So the problem all along was the phpMyadmin 4.4.15 release and not the MySql release. Clearly I had been periodically upgrading phpMyAdmin releases and I can infer that I never had the occasion to run the import with the problematic (at least for me) phpMyAdmin release 4.4.15 until now.

DB Schema For Chats?

I need to store chat conversations in a database schema. The way I would use this database is I would post chats on a website. Each chat would not be more than about 20 responses. Can someone please suggest a schema for this?
Here's a start using MySQL Workbench
and the create script
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';
CREATE SCHEMA IF NOT EXISTS `chats` DEFAULT CHARACTER SET utf8 COLLATE default collation ;
-- -----------------------------------------------------
-- Table `chats`.`chat`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `chats`.`chat` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `chats`.`chat_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `chats`.`chat_user` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`handle` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `chats`.`chat_line`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `chats`.`chat_line` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`chat_id` INT UNSIGNED NOT NULL ,
`user_id` INT UNSIGNED NOT NULL ,
`line_text` TEXT NOT NULL ,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
INDEX `fk_chat_line_chat` (`chat_id` ASC) ,
INDEX `fk_chat_line_chat_user1` (`user_id` ASC) ,
CONSTRAINT `fk_chat_line_chat`
FOREIGN KEY (`chat_id` )
REFERENCES `chats`.`chat` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_chat_line_chat_user1`
FOREIGN KEY (`user_id` )
REFERENCES `chats`.`chat_user` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
And you are welcome to download the MWB file from my dropbox.
Conversation has_may Lines
Line belongs_to User, has content & time