CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files`
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB;
got error:
SQL query:
CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files`
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB
MySQL atsakymas: Dokumentacija
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCA' at line 3
EXPORTED TABLE FROM WHICH I WANT TO GET MANUFACTURER_ID:
-- phpMyAdmin SQL Dump
-- version 4.9.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: 2019 m. Grd 16 d. 11:37
-- Server version: 10.2.27-MariaDB
-- PHP Version: 7.2.23
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
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: `u914452720_yzawa`
--
-- --------------------------------------------------------
--
-- Sukurta duomenų struktūra lentelei `aucs_manufacturer`
--
CREATE TABLE `aucs_manufacturer` (
`manufacturer_id` int(11) NOT NULL,
`name` varchar(64) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`instruction` varchar(255) DEFAULT NULL,
`catalog` varchar(255) DEFAULT NULL,
`sketch` varchar(255) DEFAULT NULL,
`sort_order` int(3) NOT NULL,
`id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `aucs_manufacturer`
--
ALTER TABLE `aucs_manufacturer`
ADD PRIMARY KEY (`manufacturer_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `aucs_manufacturer`
--
ALTER TABLE `aucs_manufacturer`
MODIFY `manufacturer_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31;
COMMIT;
/*!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 */;
Probably the single quotes might not be necessary and both the referenced table and the new table that is being created have to be on the same Engine (MyISAM in this case).
CREATE TABLE `aucs_manufacturer_files`
( `id` INT PRIMARY KEY AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY (manufacturer_id) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=MyISAM DEFAULT CHARSET=utf8;
Without knowing a lot about the other table that you are trying to reference, it's hard for us to guess certain information. I put together this SQLFiddle to demonstrate a working solution.
create table am (
id int unique not null auto_increment
)
ENGINE=INNODB;
CREATE TABLE amf
( `id` INT unique NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY (manufacturer_id) REFERENCES am(id) ON DELETE CASCADE)
ENGINE=INNODB;
insert into am(id) values (null),(null),(null),(null),(null),(null),(null);
insert into amf(id, manufacturer_id) VALUES (null, 1),(null, 2),(null, 3),(null, 2),(null, 1),(null, 4),(null, 6);
delete from am where id = 6;
If you run the SELECT * from amf you will see that there are no values with the manufacturer_id as 6, meaning that the cascade worked correctly.
try this, hope this would work:
CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files`
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT,
FOREIGN KEY (manufacturer_id) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB
After you fixed the single quotes that should have been backticks or removed altogether, you have:
CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files` (
`id` INT NOT NULL AUTO_INCREMENT ,
`manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE
) ENGINE=INNODB;
And you are getting this error:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
This has nothing to do with the foreign key; it results from specifying auto increment for a column that is not a key. Presumably you meant to say:
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
(If you end up with yet another error after fixing this, please make sure to edit your question with the new create statement, error message, and output from show create table aucs_manufacturer.)
Related
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.
I am having a very strange problem. I have developed a database and it works perfectly on my local server however then I tried to put it online on my server I keep getting MySQL error:
#1100 - Table 'USER' was not locked with LOCK TABLES
(This is then I try to import my .sql file via phpMyAdmin)
Once again this same script gives no errors on my local server (WAMP server)
I have no Idea why it does not work then I try to put it online. Also it creates the tables and structure the problem is only with the insert statements.
SQL script:
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`a3823833_MiniPos` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `a3823833_MiniPos`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` int(4) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Table structure for table `profile` */
DROP TABLE IF EXISTS `profile`;
CREATE TABLE `profile` (
`userid` int(4) NOT NULL,
`lastName` varchar(50),
`firstName` varchar(50),
`dob` varchar(10),
PRIMARY KEY (`userid`),
CONSTRAINT `profile_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Table structure for table 'topic'*/
DROP TABLE IF EXISTS `topic`;
CREATE TABLE `topic` (
`topicid` int(4) NOT NULL auto_increment,
`userid` int(4) NOT NULL,
`text` varchar(2000),
`time` datetime,
PRIMARY KEY (`topicid`),
CONSTRAINT `userid_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/* Table structure for table 'post'*/
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
`postid` int(4) NOT NULL auto_increment,
`topicid` int(4),
`userid` int(4) NOT NULL,
`text` varchar(2000),
`time` datetime,
PRIMARY KEY (`postid`),
CONSTRAINT `post_userid_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`),
CONSTRAINT `post_topicid_fk` FOREIGN KEY (`topicid`) REFERENCES `topic` (`topicid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `user` WRITE;
INSERT INTO USER VALUES (1,'Martynas','MartynPass','Martyn#email.com'),(2,'Syed','SyedPass','Syed#email.com'),(3,'Stephanie','StephPass','Steph#email.com');
UNLOCK TABLES;
LOCK TABLES `topic` WRITE;
INSERT INTO topic VALUES (1, 1,'Topic Number 1','2015-06-11 20:15:00'),(2, 2,'Topic Number 2','2015-06-10 19:15:00'),(3, 3,'Topic Number 3','2015-06-09 18:15:00');
UNLOCK TABLES;
LOCK TABLES `post` WRITE;
INSERT INTO post VALUES (1, 1, 1, 'Very Interesting Topic Number 1','2015-06-11 20:15:00'),(2, 1, 2, 'Agree!','2015-06-11 21:15:00');
UNLOCK TABLES;
I'm having an error restoring tables which have Foreign Keys after lowercasing the contents of a mysql dump file.
My goal is to lowercase all structure in a mysql database.
I used vim to lowercase all contents of a mysqldump file when trying to restore the database, this is the error I get.
$ mysql -u [user] -p[password] dme < mysqldump.sql
ERROR 1005 (HY000) at line 399: Can't create table 'dme.contacts' (errno: 121)
The restore is error'ing out on any table which has Foreign Key contstraints. If I remove lines after the Primary Key for each table, the entire restore works fine.
My question is, what is causing this and how can I keep my Foreign Keys in the process?
Lowercased.sql example which results in the error (fyi prior to dme.contacts there are multiple tables which are created just fine. Only tables w/ Foreign Keys and Contstraints have issues.)
drop table if exists `contacts`;
/*!40101 set #saved_cs_client = ##character_set_client */;
/*!40101 set character_set_client = utf8 */;
create table `contacts` (
`contactid` int(11) not null auto_increment,
`firstname` varchar(100) default null,
`lastname` varchar(100) not null,
`fullname` varchar(200) not null,
`dob` datetime default null,
`sex` char(1) default null,
`voided` datetime default null,
`vendorid` int(11) not null,
`contacttypeid` int(11) default null,
`comments` varchar(1500) default null,
primary key (`contactid`),
key `fk_contacts_vendors` (`vendorid`),
key `fk_contacts_contacttype` (`contacttypeid`),
constraint `fk_contacts_contacttype` foreign key (`contacttypeid`) references `contacttype` (`contacttypeid`) on delete no action on update no action,
constraint `fk_contacts_vendors` foreign key (`vendorid`) references `vendors` (`vendorid`) on delete no action on update no action
) engine=innodb default charset=utf8;
/*!40101 set character_set_client = #saved_cs_client */;
--
-- dumping data for table `contacts`
--
lock tables `contacts` write;
/*!40000 alter table `contacts` disable keys */;
/*!40000 alter table `contacts` enable keys */;
unlock tables;
Original from the mysqldump.sql (No Error)
--
-- Table structure for table `Contacts`
--
DROP TABLE IF EXISTS `Contacts`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Contacts` (
`ContactId` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(100) DEFAULT NULL,
`LastName` varchar(100) NOT NULL,
`FullName` varchar(200) NOT NULL,
`DOB` datetime DEFAULT NULL,
`Sex` char(1) DEFAULT NULL,
`Voided` datetime DEFAULT NULL,
`VendorID` int(11) NOT NULL,
`ContactTypeID` int(11) DEFAULT NULL,
`Comments` varchar(1500) DEFAULT NULL,
PRIMARY KEY (`ContactId`),
KEY `FK_Contacts_Vendors` (`VendorID`),
KEY `FK_Contacts_ContactType` (`ContactTypeID`),
CONSTRAINT `FK_Contacts_ContactType` FOREIGN KEY (`ContactTypeID`) REFERENCES `ContactType` (`ContactTYpeID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_Contacts_Vendors` FOREIGN KEY (`VendorID`) REFERENCES `Vendors` (`VendorId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `Contacts`
--
LOCK TABLES `Contacts` WRITE;
/*!40000 ALTER TABLE `Contacts` DISABLE KEYS */;
/*!40000 ALTER TABLE `Contacts` ENABLE KEYS */;
UNLOCK TABLES;
Try removing the constrain name. Sample:
constraint `fk_contacts_contacttype` foreign key (`contacttypeid`) ....
To
constraint foreign key (`contacttypeid`) ...
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
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.