Errors Adding Foreign Keys (MySQL) (Error Code 1215) - mysql

I am trying to add a foreign key between two different sets of tables, the first set is customer and shopping_cart. I tried looking at the other posts regarding this error but I couldn't get it to work after looking at them.
CREATE TABLE `usale`.`customer` (
CREATE TABLE `usale`.`customer` (
`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`)),
INDEX `fk_customer_student1_idx` (`student_student_id` ASC,`student_school_id` ASC),
INDEX `fk_customer_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
CONSTRAINT `fk_customer_student1`
FOREIGN KEY (`student_student_id`)
REFERENCES `usale`.`student` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart` (`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE customer ADD CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I'm getting the error: "ERROR: Cannot add foreign key constraint
Error Code: 1215"
For the second set of tables they are named shopping_cart and shopping_cart_item
CREATE TABLE IF NOT EXISTS `usale`.`shopping_cart_item` (
`shopping_cart_item_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
`inventory_item_inventory_item_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_student_id` BIGINT NOT NULL,
`inventory_item_inventory_type` VARCHAR(45) NOT NULL,
`inventory_item_student_student_id` BIGINT NOT NULL,
`inventory_item_student_customer_id` BIGINT NOT NULL,
`inventory_item_student_school_id` BIGINT NOT NULL,
`inventory_item_edition_edition_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_edition_author_id` BIGINT NOT NULL,
PRIMARY KEY (`shopping_cart_item_id`, `shopping_cart_cart_id`, `inventory_item_inventory_item_id`, `inventory_item_student_id`, `inventory_item_inventory_type`, `inventory_item_student_student_id`, `inventory_item_student_customer_id`, `inventory_item_student_school_id`, `inventory_item_edition_edition_id`, `inventory_item_edition_author_id`),
INDEX `fk_shopping_cart_items_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
INDEX `fk_shopping_cart_item_inventory_item1_idx` (`inventory_item_inventory_item_id` ASC, `inventory_item_student_id` ASC, `inventory_item_inventory_type` ASC, `inventory_item_student_student_id` ASC, `inventory_item_student_customer_id` ASC, `inventory_item_student_school_id` ASC, `inventory_item_edition_edition_id` ASC, `inventory_item_edition_author_id` ASC))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE shopping_cart_item ADD CONSTRAINT `fk_shopping_cart_items_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Once again I am getting error 1215: "ERROR: Cannot add foreign key constraint
Error Code: 1215"

Related

MySQL "Foreign key constraint is incorrectly formed"

I have the follwing SQL commands:
CREATE TABLE IF NOT EXISTS `users`
(
`id` INTEGER NOT NULL auto_increment,
`username` VARCHAR(255) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`root` TINYINT(1) DEFAULT 0,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
UNIQUE `username_unique` (`username`),
PRIMARY KEY (`id`)
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `domains`
(
`id` INTEGER NOT NULL auto_increment,
`domain` VARCHAR(255) NOT NULL UNIQUE,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
`userid` INTEGER,
UNIQUE `domain_unique` (`domain`),
PRIMARY KEY (`id`),
FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE SET NULL ON
UPDATE CASCADE
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `aliases`
(
`id` INTEGER NOT NULL auto_increment,
`source_username` VARCHAR(255) NOT NULL,
`source_domain` VARCHAR(255) NOT NULL,
`destination_username` VARCHAR(255) NOT NULL,
`destination_domain` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`source_domain`) REFERENCES `domains` (`domain`)
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `accounts`
(
`id` INTEGER NOT NULL auto_increment,
`username` VARCHAR(255) NOT NULL,
`domain` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`quota` INTEGER NOT NULL DEFAULT 500,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`sendonly` TINYINT(1) NOT NULL DEFAULT 0,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`domain`) REFERENCES `domains` (`domain`)
)
engine=innodb;
if i try to run these i get the message:
"Foreign key constraint is incorrectly formed"
for the tables Alias and Accounts.
Its not possible to use primary keys in alias and accounts
Im using MariaDB 10.2
I hope somebody can tell me what is wrong with these statements.
You need to add a key/index to the domain field of the domains table, in order for it to be used as Foreign Key by another table.
If the table is already created and you want to add an index, use this.
CREATE INDEX domain ON domains(domain);
OR add the index while creating the table -
CREATE TABLE IF NOT EXISTS `domains`
(
`id` INTEGER NOT NULL auto_increment,
`domain` VARCHAR(255) NOT NULL UNIQUE,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
`userid` INTEGER,
UNIQUE `domain_unique` (`domain`),
PRIMARY KEY (`id`),
INDEX domain (domain),
FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE SET NULL ON
UPDATE CASCADE
)
engine=innodb;
For further reading (official documentation) regarding Foreign Key Contraint Errors on MariaDB this URL
You define an integer primary key. Use it:
CREATE TABLE IF NOT EXISTS `aliases`
(
`id` INTEGER NOT NULL auto_increment,
`source_username` VARCHAR(255) NOT NULL,
`source_domain_id` INTEGER NOT NULL,
`destination_username` VARCHAR(255) NOT NULL,
`destination_domain` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`source_domain_id`) REFERENCES `domains` (`id`)
)
engine=innodb;

having trouble with my database using workbench

I am creating a test database to train with and am running into this problem -
"Schema Creation Failed: Can't create table 'db_2_5b129.tbluserassignment' (errno: 150): "
The code for the specific table is -
-- Table tblUserAssignment
CREATE TABLE IF NOT EXISTS `tblUserAssignment` (
`assignment_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`supervisor_id` INT NOT NULL,
`position_id` INT NOT NULL,
`department_id` INT NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`date_added` DATE NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`assignment_id`),
INDEX `fk_tblUserAssignment_tblUserPhone1_idx` (`user_id` ASC),
INDEX `fk_tblUserAssignment_tblUserPositions1_idx` (`position_id` ASC),
CONSTRAINT `fk_tblUserAssignment_tblUserPhone1`
FOREIGN KEY (`user_id`)
REFERENCES `tblUserPhone` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tblUserAssignment_tblUserPositions1`
FOREIGN KEY (`position_id`)
REFERENCES `tblUserPositions` (`position_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Can someone look it over and point me in the right direction?
problem is in differences between forign fields type
tblUserPositions.position_id INT UNSIGNED NOT NULL AUTO_INCREMEN
and
tblUserAssignment.position_id INT NOT NULL,`
EDIT 1
see this Use Composite Primary Key as Foreign Key
EDIT 2
this one tested & works fine
CREATE TABLE `tbluserphone` (
`contact_information_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(11) NOT NULL,
`phone_number` VARCHAR(20) NOT NULL,
`phone_type_id` INT(11) NOT NULL,
`date_added` DATETIME NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`contact_information_id`, `user_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
CREATE TABLE `tbluserassignment` (
`assignment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`contact_information_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(11) NOT NULL,
`supervisor_id` INT(11) NOT NULL,
`position_id` INT(10) UNSIGNED NOT NULL,
`department_id` INT(11) NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`date_added` DATE NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`assignment_id`),
INDEX `fk_tblUserAssignment_tblUserPhone1_idx` (`user_id`),
INDEX `fk_tblUserAssignment_tblUserPositions1_idx` (`position_id`),
INDEX `contact` (`contact_information_id`),
CONSTRAINT `FK_tbluserassignment_tbluserpositions`
FOREIGN KEY (`position_id`)
REFERENCES `tbluserpositions` (`position_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT tbluserassignment_fkz1
FOREIGN KEY (contact_information_id, user_id)
REFERENCES tbluserphone (contact_information_id, user_id)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
(contact_information_id, user_id) - fields should goes same way, like in primary key difinition

ERROR: Error 1067: Invalid default value for ON UPDATE CURRENT_TIMESTAMP

I run the script from Workbench. Here is the complete 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,ALLOW_INVALID_DATES';
-- Schema mydb
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- Table mydb.categories
DROP TABLE IF EXISTS `mydb`.`categories` ;
CREATE TABLE IF NOT EXISTS `mydb`.`categories` (
`categories_id` INT(5) UNSIGNED NOT NULL,
`categories_name` VARCHAR(32) NOT NULL,
`categories_image` VARCHAR(64) NULL,
`parent_id` INT(5) UNSIGNED NOT NULL,
`sort_order` INT(3) NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
`last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`categories_id`),
INDEX `fk_categories_categories1_idx` (`parent_id` ASC),
CONSTRAINT `fk_categories_categories1`
FOREIGN KEY (`parent_id`)
REFERENCES `mydb`.`categories` (`categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.manufacturers
DROP TABLE IF EXISTS `mydb`.`manufacturers` ;
CREATE TABLE IF NOT EXISTS `mydb`.`manufacturers` (
`manufacturers_id` INT(5) UNSIGNED NOT NULL,
`manufacturers_name` VARCHAR(32) NOT NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
PRIMARY KEY (`manufacturers_id`))
ENGINE = InnoDB;
-- Table mydb.products
DROP TABLE IF EXISTS `mydb`.`products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products` (
`products_id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`products_model` VARCHAR(20) NULL,
`products_price` DECIMAL(10,2) UNSIGNED NULL,
`products_weight` DECIMAL(4,2) UNSIGNED NULL,
`manufacturers_id` INT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`products_id`),
INDEX `fk_products_manufacturers1_idx` (`manufacturers_id` ASC),
CONSTRAINT `manufacturers_id`
FOREIGN KEY (`manufacturers_id`)
REFERENCES `mydb`.`manufacturers` (`manufacturers_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.categories_has_products
DROP TABLE IF EXISTS `mydb`.`categories_has_products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`categories_has_products` (
`categories_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`categories_id`, `products_id`),
INDEX `fk_categories_has_products_products_idx` (`products_id` ASC),
INDEX `fk_categories_has_products_categories_idx` (`categories_id` ASC),
CONSTRAINT `categories_id`
FOREIGN KEY (`categories_id`)
REFERENCES `mydb`.`categories` (`categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `products_id`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.products_description
DROP TABLE IF EXISTS `mydb`.`products_description` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_description` (
`products_id` INT(5) UNSIGNED NOT NULL,
`products_name` VARCHAR(64) NOT NULL,
`products_description` TEXT NULL,
`products_url` VARCHAR(255) NULL,
`products_viewed` INT(5) UNSIGNED NULL,
PRIMARY KEY (`products_id`),
UNIQUE INDEX `products_name_UNIQUE` (`products_name` ASC),
CONSTRAINT `products_id`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.customers
DROP TABLE IF EXISTS `mydb`.`customers` ;
CREATE TABLE IF NOT EXISTS `mydb`.`customers` (
`customers_id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`customers_gender` CHAR(1) NULL,
`customers_firstname` VARCHAR(32) NOT NULL,
`customers_lastname` VARCHAR(32) NULL,
`customers_dob` DATE NULL,
`customers_email_address` VARCHAR(96) NULL,
`customers_default_address_id` INT(5) UNSIGNED NULL,
`customers_telephone` VARCHAR(32) NULL,
`customers_fax` VARCHAR(32) NULL,
`customers_password` VARCHAR(40) NULL,
`customers_newsletter` CHAR(1) NULL,
`customers_info_date_of_last_logon` DATETIME NOT NULL,
`customers_info_number_of_logons` INT(5) UNSIGNED NOT NULL,
`customers_info_date_account_created` TIMESTAMP NOT NULL DEFAULT 0,
`customers_info_date_account_last_modified` TIMESTAMP NOT NULL,
PRIMARY KEY (`customers_id`))
ENGINE = InnoDB;
-- Table mydb.reviews
DROP TABLE IF EXISTS `mydb`.`reviews` ;
CREATE TABLE IF NOT EXISTS `mydb`.`reviews` (
`reviews_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`customers_id` INT(5) UNSIGNED NOT NULL,
`customers_name` VARCHAR(64) NULL,
`reviews_rating` INT(1) UNSIGNED NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
`last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`reviews_read` INT(5) UNSIGNED NULL,
`reviews_text` TEXT NULL,
PRIMARY KEY (`reviews_id`),
INDEX `fk_reviews_products1_idx` (`products_id` ASC),
INDEX `fk_reviews_customers1_idx` (`customers_id` ASC),
CONSTRAINT `fk_reviews_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_reviews_customers1`
FOREIGN KEY (`customers_id`)
REFERENCES `mydb`.`customers` (`customers_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.specials
DROP TABLE IF EXISTS `mydb`.`specials` ;
CREATE TABLE IF NOT EXISTS `mydb`.`specials` (
`specials_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`specials_new_products_price` DECIMAL(10,2) UNSIGNED NULL,
`specials_date_added` TIMESTAMP NOT NULL DEFAULT 0,
`specials_last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`specials_id`),
INDEX `fk_specials_products1_idx` (`products_id` ASC),
CONSTRAINT `fk_specials_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.orders
DROP TABLE IF EXISTS `mydb`.`orders` ;
CREATE TABLE IF NOT EXISTS `mydb`.`orders` (
`orders_id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`customers_id` INT(5) UNSIGNED NOT NULL,
`customers_street_address` VARCHAR(64) NOT NULL,
`customers_suburb` VARCHAR(32) NULL,
`customers_city` VARCHAR(32) NOT NULL,
`customers_postcode` VARCHAR(10) NULL,
`customers_state` VARCHAR(32) NULL,
`customers_country` VARCHAR(32) NULL,
`customers_telephone` VARCHAR(32) NULL,
`customers_email_address` VARCHAR(96) NULL,
`delivery_name` VARCHAR(64) NULL,
`delivery_street_address` VARCHAR(64) NULL,
`delivery_suburb` VARCHAR(32) NULL,
`delivery_city` VARCHAR(32) NULL,
`delivery_postcode` VARCHAR(10) NULL,
`delivery_state` VARCHAR(32) NULL,
`delivery_country` VARCHAR(32) NULL,
`payment_method` VARCHAR(12) NULL,
`cc_type` VARCHAR(20) NULL,
`cc_owner` VARCHAR(64) NULL,
`cc_number` VARCHAR(32) NULL,
`cc_expires` VARCHAR(4) NULL,
`last_modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_purchased` TIMESTAMP NOT NULL DEFAULT 0,
`shipping_cost` DECIMAL(10,2) UNSIGNED NULL,
`shipping_method` VARCHAR(32) NULL,
`orders_status` VARCHAR(10) NULL,
`orders_date_finished` DATETIME NULL,
`comments` TEXT NULL,
`currency` VARCHAR(3) NULL,
`currency_value` DECIMAL(16,6) NULL,
PRIMARY KEY (`orders_id`),
INDEX `fk_orders_customers1_idx` (`customers_id` ASC),
CONSTRAINT `fk_orders_customers1`
FOREIGN KEY (`customers_id`)
REFERENCES `mydb`.`customers` (`customers_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.ordered_products
DROP TABLE IF EXISTS `mydb`.`ordered_products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`ordered_products` (
`orders_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`products_size_id` TINYINT UNSIGNED NOT NULL,
`products_color_id` TINYINT UNSIGNED NOT NULL,
`products_price` DECIMAL(10,2) UNSIGNED NOT NULL,
`quantity` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`orders_id`, `products_id`, `products_size_id`, `products_color_id`),
INDEX `fk_orders_has_products_products1_idx` (`products_id` ASC),
INDEX `fk_orders_has_products_orders1_idx` (`orders_id` ASC),
CONSTRAINT `fk_orders_has_products_orders1`
FOREIGN KEY (`orders_id`)
REFERENCES `mydb`.`orders` (`orders_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_orders_has_products_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.ordered_products
DROP TABLE IF EXISTS `mydb`.`ordered_products` ;
CREATE TABLE IF NOT EXISTS `mydb`.`ordered_products` (
`orders_id` INT(5) UNSIGNED NOT NULL,
`products_id` INT(5) UNSIGNED NOT NULL,
`products_size_id` TINYINT UNSIGNED NOT NULL,
`products_color_id` TINYINT UNSIGNED NOT NULL,
`products_price` DECIMAL(10,2) UNSIGNED NOT NULL,
`quantity` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`orders_id`, `products_id`, `products_size_id`, `products_color_id`),
INDEX `fk_orders_has_products_products1_idx` (`products_id` ASC),
INDEX `fk_orders_has_products_orders1_idx` (`orders_id` ASC),
CONSTRAINT `fk_orders_has_products_orders1`
FOREIGN KEY (`orders_id`)
REFERENCES `mydb`.`orders` (`orders_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_orders_has_products_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.products_size
DROP TABLE IF EXISTS `mydb`.`products_size` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_size` (
`products_size_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`products_size_name` VARCHAR(15) NOT NULL,
PRIMARY KEY (`products_size_id`))
ENGINE = InnoDB;
-- Table mydb.products_color
DROP TABLE IF EXISTS `mydb`.`products_color` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_color` (
`products_color_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`products_color_name` VARCHAR(20) NOT NULL,
PRIMARY KEY (`products_color_id`))
ENGINE = InnoDB;
-- Table mydb.products_attributes
DROP TABLE IF EXISTS `mydb`.`products_attributes` ;
CREATE TABLE IF NOT EXISTS `mydb`.`products_attributes` (
`products_id` INT(5) UNSIGNED NOT NULL,
`products_size_id` TINYINT UNSIGNED NOT NULL,
`products_color_id` TINYINT UNSIGNED NOT NULL,
`products_quantity` INT(4) UNSIGNED NOT NULL,
`products_image` VARCHAR(64) NULL,
`products_date_added` TIMESTAMP NOT NULL DEFAULT 0,
`products_last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`products_date_available` DATETIME NULL,
`products_status` TINYINT UNSIGNED NULL,
PRIMARY KEY (`products_id`, `products_size_id`, `products_color_id`),
INDEX `fk_products_attributes_products_sizes1_idx` (`products_size_id` ASC),
INDEX `fk_products_attributes_products_colors1_idx` (`products_color_id` ASC),
CONSTRAINT `fk_products_attributes_products1`
FOREIGN KEY (`products_id`)
REFERENCES `mydb`.`products` (`products_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_products_attributes_products_sizes1`
FOREIGN KEY (`products_size_id`)
REFERENCES `mydb`.`products_size` (`products_size_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_products_attributes_products_colors1`
FOREIGN KEY (`products_color_id`)
REFERENCES `mydb`.`products_color` (`products_color_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 here is the complete error:
Executing SQL script in server
ERROR: Error 1067: Invalid default value for 'last_modified'
SQL Code:
CREATE TABLE IF NOT EXISTS `mydb`.`categories` (
`categories_id` INT(5) UNSIGNED NOT NULL,
`categories_name` VARCHAR(32) NOT NULL,
`categories_image` VARCHAR(64) NULL,
`parent_id` INT(5) UNSIGNED NOT NULL,
`sort_order` INT(3) NULL,
`date_added` TIMESTAMP NOT NULL DEFAULT 0,
`last_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`categories_id`),
INDEX `fk_categories_categories1_idx` (`parent_id` ASC),
CONSTRAINT `fk_categories_categories1`
FOREIGN KEY (`parent_id`)
REFERENCES `mydb`.`categories` (`categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
It is because of the SQL_MODE you are setting.
TRADITIONAL and ALLOW_INVALID_DATES restricts the TIMESTAMP type columns for not being set with a default value.
By defining any of the following should work on TIMESTAMP type columns.
DEFAULT 0
DEFAULT CURRENT_TIMESTAMP
Alternately, by just setting the SQL_MODE to ALLOW_INVALID_DATES would need no changes to your script.
Others:
Constraint names MUST be unique. Table products_description defines Constraint products_id but the same name was already used in table categories_has_products.
Maintain unique constraint names.
Refer to:
Important SQL Modes
TRADITIONAL
ALLOW_INVALID_DATES
Change the SQL_MODE like below:
SET GLOBAL sql_mode = 'ALLOW_INVALID_DATES’;
SET SESSION sql_mode = 'ALLOW_INVALID_DATES';
Since last_modified cannot be null by your definition, you have to provide a default value as well:
`last_modified` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
I had the same problem and too many dates to change. I set this at the top of my .sql script and all was well.
SET sql_mode = 'ALLOW_INVALID_DATES';
I`m not sure if its correct but I set on "my sql settings -> sql-mode as none or something like this (the other beyond "sql-mode -> user-mode") solved my problem
So in my case ubuntu 16.04 instance with lemp
i had to change the mysql time stamp so log into mysql and execute below commands
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row on CONSTRAINT `fk_users_has_ratings_businesses1

I'm getting this error when i try to insert new ratings
Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`yuldi`.`users_ratings`, CONSTRAINT `fk_users_has_ratings_businesses1` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
below database table structure with some foreign keys. i'm confused with adding multiple primary and foreign keys. please help me to sort out this issue
CREATE TABLE IF NOT EXISTS `yuldi`.`businesses` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`state` VARCHAR(100) NOT NULL,
`slug` VARCHAR(250) NOT NULL,
`city` VARCHAR(100) NOT NULL,
`suburb` VARCHAR(100) NOT NULL,
`business_name` VARCHAR(100) NOT NULL,
`business_address` VARCHAR(250) NOT NULL,
`business_postal` VARCHAR(10) NOT NULL,
`business_postal_id` INT(11) NOT NULL,
`business_phone` VARCHAR(50) NOT NULL,
`business_phone1` VARCHAR(50) NOT NULL,
`business_phone2` VARCHAR(50) NOT NULL,
`business_email` VARCHAR(100) NOT NULL,
`business_website` VARCHAR(200) NOT NULL,
`business_details` VARCHAR(5000) NOT NULL,
`business_openinghours` VARCHAR(200) NOT NULL,
`business_service` VARCHAR(200) NOT NULL,
`business_addtionalinfo` VARCHAR(200) NOT NULL,
`business_lat` FLOAT(10,6) NOT NULL,
`business_lng` FLOAT(10,6) NOT NULL,
`identity` VARCHAR(100) NOT NULL,
`status` INT(11) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 232
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `yuldi`.`ratings` (
`id` VARCHAR(36) NOT NULL,
`model` VARCHAR(255) NOT NULL,
`value` FLOAT(8,4) NULL DEFAULT '0.0000',
`comment` TEXT NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `UNIQUE_RATING` (`model` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `yuldi`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_email` VARCHAR(255) NULL DEFAULT NULL,
`user_password` CHAR(100) NULL DEFAULT NULL,
`user_name` VARCHAR(255) NULL DEFAULT NULL,
`user_code` VARCHAR(255) NULL DEFAULT NULL,
`user_status` TINYINT(4) NULL DEFAULT '0',
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
`ip_address` VARCHAR(15) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 14
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `yuldi`.`users_ratings` (
`user_id` INT(11) NOT NULL,
`rating_id` VARCHAR(36) NOT NULL,
`business_id` INT(11) NOT NULL,
PRIMARY KEY (`user_id`, `rating_id`, `business_id`),
INDEX `fk_users_has_ratings_ratings1_idx` (`rating_id` ASC),
INDEX `fk_users_has_ratings_users1_idx` (`user_id` ASC),
INDEX `fk_users_has_ratings_businesses1_idx` (`business_id` ASC),
CONSTRAINT `fk_users_has_ratings_businesses1`
FOREIGN KEY (`business_id`)
REFERENCES `yuldi`.`businesses` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_ratings_ratings1`
FOREIGN KEY (`rating_id`)
REFERENCES `yuldi`.`ratings` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_ratings_users1`
FOREIGN KEY (`user_id`)
REFERENCES `yuldi`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
The problem is coming from this:
CONSTRAINT `fk_users_has_ratings_businesses1`
FOREIGN KEY (`business_id`)
REFERENCES `yuldi`.`businesses` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
The record you're trying to insert / update - probably user_ratings - is foreign keyed to the businesses table; you can't create a user_ratings record without a business_id, and that business_id must actually exist as an id in the businesses table.

cannot add second FK : got error can't create table'.jobstatus\#sql-32c_12f2f.frm' (errno:150)

CREATE TABLE `job` (
`jobId` int(11) NOT NULL auto_increment,
`jobcode` varchar(25) default NULL,
`jobname` varchar(255) default NULL,
`location` varchar(255) default NULL,
`budget` int(10) unsigned default NULL,
`year_type` varchar(100) default NULL,
`worklineId` int(11) default NULL,
PRIMARY KEY (`jobId`),
KEY `NewIndex` (`worklineId`),
FOREIGN KEY (`worklineId`) REFERENCES `workline` (`worklineId`)
) TYPE=InnoDB;
CREATE TABLE `subjob` (
`subjobId` int(11) NOT NULL auto_increment,
`subjobcode` varchar(25) default NULL,
`subjobname` varchar(255) default NULL,
`subjobbudget` int(11) unsigned default NULL,
`jobgoal_date` date default '0000-00-00',
`jobId` int(11) default NULL,
PRIMARY KEY (`subjobId`),
KEY `NewIndex` (`jobId`),
FOREIGN KEY (`jobId`) REFERENCES `job` (`jobId`)
) TYPE=InnoDB;
CREATE TABLE `contract` (
`contractId` int(11) NOT NULL auto_increment,
`contractcode` varchar(25) default NULL,
`price` int(11) unsigned default NULL,
`contractprice` int(11) unsigned default NULL,
`company` varchar(50) default NULL,
`signdate` date default '0000-00-00',
`begindate` date default '0000-00-00',
`enddateplan` date default '0000-00-00',
`note` text,
PRIMARY KEY (`contractId`)
) TYPE=InnoDB;
CREATE TABLE `subjob_contract` (
`subjobcontractId` int(11) NOT NULL auto_increment,
`status` varchar(11) default NULL,
`contractId` int(11) default NULL,
`subjobId` int(11) default NULL,
PRIMARY KEY (`subjobcontractId`),
KEY `NewIndex` (`contractId`),
KEY `NewIndex2` (`subjobId`),
FOREIGN KEY (`contractId`) REFERENCES `contract` (`contractId`)
) TYPE=InnoDB
I m using mysql front 3.2 to manage database,I can add first fk but when i add second fk i got an error following this :
sql execution error #1005. response from the database: can't create table'.jobstatus#sql-32c_12f2f.frm' (errno:150). i already define the new index for fk subjobId reference to subjob table what could be the possibility of this error? thank you
Check the datatype and size of the subjobId column on primary table and referenced table. both must be same than it will allow you to create foreign key.
Answer is: You can not refer that column/table which is not created yet. Try to execute tables having foreign keys after the referenced tables.
Obviously you should have consistency in datatypes of foreign key and referenced column as well
Correct Execution Demo. Also You should use Engine=InnoDB instead of Type=InnoDB