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;
Related
After some research on this problem, I found out that something is probably not right in my database structure, so I have tried the "SHOW ENGINE INNODB STATUS" command to find out what is causing the foreign key SQLException.
Here is the result on "lastest foreign key error"
If I'm right, the table that causes the problem is a temporary table, that's why I don't know what to do.
The other questions here are usually like having an unsigned int in one table, and signed int at foreign key, so that causes error, but I can't see anything like that in my script and the script itself works fine in mysql workbench, but the spring application throws exception when using this database.
The strange thing is that I don't have origin_id anywhere in my code, be it the java code or the sql.
Here is the database script:
-- MySQL Script generated by MySQL Workbench
-- Tue Oct 9 20:16:01 2018
-- 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';
-- -----------------------------------------------------
-- DATABASE flights
-- -----------------------------------------------------
DROP DATABASE IF EXISTS `flights` ;
-- -----------------------------------------------------
-- DATABASE flights
-- -----------------------------------------------------
CREATE DATABASE `flights` ;
-- -----------------------------------------------------
-- Schema flights
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `flights` ;
-- -----------------------------------------------------
-- Schema flights
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `flights` DEFAULT CHARACTER SET utf8 ;
USE `flights` ;
-- -----------------------------------------------------
-- Table `flights`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`user` (
`id` REAL NOT NULL,
`username` VARCHAR(50) NULL,
`password` VARCHAR(50) NULL,
`email` TEXT NULL,
`role` VARCHAR(10) NULL DEFAULT "USER",
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `flights`.`airline`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`airline` (
`id` REAL NOT NULL auto_increment,
`name` VARCHAR(100) NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `flights`.`plane`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`plane` (
`id` REAL NOT NULL auto_increment,
`seats` INT NULL,
`airline_id` REAL NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`),
CONSTRAINT `airline_id`
FOREIGN KEY (`airline_id`)
REFERENCES `flights`.`airline` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `airline_id_idx` ON `flights`.`plane` (`airline_id` ASC) VISIBLE;
-- -----------------------------------------------------
-- Table `flights`.`airport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`airport` (
`id` REAL NOT NULL auto_increment,
`name` VARCHAR(100) NULL,
`city` VARCHAR(50) NULL,
`country` VARCHAR(50) NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `flights`.`flight`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`flight` (
`id` REAL NOT NULL auto_increment,
`departure_time` TIMESTAMP NULL,
`arrival_time` TIMESTAMP NULL,
`adult_prise` REAL NULL,
`reduced_prise` REAL NULL,
`plane_id` REAL NULL,
`departure_airport_id` REAL NULL,
`arrival_airport_id` REAL NULL,
`created_at` TIMESTAMP NULL,
`airline_id` REAL NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `airline_id_2`
FOREIGN KEY (`airline_id`)
REFERENCES `flights`.`airline` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `plane_id`
FOREIGN KEY (`plane_id`)
REFERENCES `flights`.`plane` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `departure_airport_id`
FOREIGN KEY (`departure_airport_id`)
REFERENCES `flights`.`airport` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `arrival_airport_id`
FOREIGN KEY (`arrival_airport_id`)
REFERENCES `flights`.`airport` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `plane_id_idx` ON `flights`.`flight` (`plane_id` ASC) VISIBLE;
CREATE INDEX `departure_airport_id_idx` ON `flights`.`flight` (`departure_airport_id` ASC) VISIBLE;
CREATE INDEX `arrival_airport_id_idx` ON `flights`.`flight` (`arrival_airport_id` ASC) VISIBLE;
-- -----------------------------------------------------
-- Table `flights`.`reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`reservation` (
`id` REAL NOT NULL,
`reservation_time` TIMESTAMP NULL,
`adult_ticket` SMALLINT NULL,
`reduced_ticket` SMALLINT NULL,
`user_id` REAL NOT NULL,
`flight_id` REAL NOT NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `flights`.`user` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `flight_id`
FOREIGN KEY (`flight_id`)
REFERENCES `flights`.`flight` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `user_id_idx` ON `flights`.`reservation` (`user_id` ASC) VISIBLE;
CREATE INDEX `flight_id_idx` ON `flights`.`reservation` (`flight_id` ASC) VISIBLE;
USE `flights` ;
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_airline`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_airline` (`id` REAL, `name` VARCHAR(30), `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_airport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_airport` (`id` REAL, `name` VARCHAR(100), `city` VARCHAR(50), `country` VARCHAR(50), `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_flight`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_flight` (`id` REAL, `departure_time` TIMESTAMP, `arrival_time` TIMESTAMP, `adult_prise` REAL, `reduced_prise` REAL, `plane_id` REAL, `departure_airport_id` REAL, `arrival_airport_id` REAL, `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_plane`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_plane` (`id` REAL, `seats` INT, `airline_id` REAL, `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_reservation` (`id` REAL, `reservation_time` TIMESTAMP, `adult_ticket` SMALLINT, `reduced_ticket` SMALLINT, `user_id` REAL, `flight_id` REAL, `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_user` (`id` REAL, `username` VARCHAR(50), `password` VARCHAR(50), `email` TEXT, `role` VARCHAR(50), `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- View `flights`.`v_airline`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_airline`;
USE `flights`;
CREATE OR REPLACE VIEW `v_airline` AS select * from airline;
-- -----------------------------------------------------
-- View `flights`.`v_airport`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_airport`;
USE `flights`;
CREATE OR REPLACE VIEW `v_airport` AS select * from airport;
-- -----------------------------------------------------
-- View `flights`.`v_flight`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_flight`;
USE `flights`;
CREATE OR REPLACE VIEW `v_flight` AS select * from flight;
-- -----------------------------------------------------
-- View `flights`.`v_plane`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_plane`;
USE `flights`;
CREATE OR REPLACE VIEW `v_plane` AS select * from plane;
-- -----------------------------------------------------
-- View `flights`.`v_reservation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_reservation`;
USE `flights`;
CREATE OR REPLACE VIEW `v_reservation` AS select * from reservation;
-- -----------------------------------------------------
-- View `flights`.`v_user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_user`;
USE `flights`;
CREATE OR REPLACE VIEW `v_user` AS select * from user;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
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';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE
utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Diploma`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Diploma` (
`diploma_id` VARCHAR(5) NOT NULL,
`diploma_name` VARCHAR(90) NULL,
PRIMARY KEY (`diploma_id`)
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`School`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`School` (
`school_id` INT(1) NOT NULL,
`school_name` VARCHAR(45) NULL,
PRIMARY KEY (`school_id`)
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Student`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Student` (
`student_number` INT(8) NOT NULL,
`student_id` INT(8) NULL,
`student_name` VARCHAR(45) NULL,
`student_password` VARCHAR(45) NULL,
`student_mobile` INT(8) NULL,
`student_email` VARCHAR(45) NULL,
`diploma_id` VARCHAR(5) NOT NULL,
`school_id` INT(1) NOT NULL,
PRIMARY KEY (`student_number`),
INDEX `fk_Student_Diploma1_idx` (`diploma_id` ASC),
INDEX `fk_Student_School1_idx` (`school_id` ASC),
CONSTRAINT `fk_Student_Diploma1` FOREIGN KEY (`diploma_id`)
REFERENCES `mydb`.`Diploma` (`diploma_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Student_School1` FOREIGN KEY (`school_id`)
REFERENCES `mydb`.`School` (`school_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`OFN`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`OFN` (
`ofn_id` INT(8) NOT NULL,
`ofn_username` VARCHAR(45) NULL,
`ofn_password` VARCHAR(45) NULL,
`ofn_email` VARCHAR(45) NULL,
PRIMARY KEY (`ofn_id`)
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Appointment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Appointment` (
`appointment_id` INT(8) NOT NULL,
`appointment_date` DATE NULL,
`appointment_time` TIME NULL,
`ofn_id` INT(8) NOT NULL,
PRIMARY KEY (`appointment_id`),
INDEX `fk_Appointment_OFN1_idx` (`ofn_id` ASC),
CONSTRAINT `fk_Appointment_OFN1` FOREIGN KEY (`ofn_id`)
REFERENCES `mydb`.`OFN` (`ofn_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Booking`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Booking` (
`student_number` INT(8) NOT NULL,
`appointment_id` INT(8) NOT NULL,
`booking_date` DATE NULL,
`booking_time` TIME NULL,
PRIMARY KEY (`student_number` , `appointment_id`),
INDEX `fk_Student_has_Appointment_Appointment1_idx` (`appointment_id` ASC),
INDEX `fk_Student_has_Appointment_Student1_idx` (`student_number` ASC),
CONSTRAINT `fk_Student_has_Appointment_Student1` FOREIGN KEY
(`student_number`)
REFERENCES `mydb`.`Student` (`student_number`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Student_has_Appointment_Appointment1` FOREIGN KEY (`appointment_id`)
REFERENCES `mydb`.`Appointment` (`appointment_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;
This is the code i was building but it gave an error
Error Code: 1005: Can't create table 'mydb'.'booking'
I tried looking through some of the previous question by people encountering the same problem but i am still stuck at this point.
Any help will be greatly appreciated, thank you.
Somehow it's the foreign key student_number.
I am thinking in these possibilities:
The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this.
One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message - it just won’t create the key.) In Query Browser, you can specify the table type.
One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field.
Also check:
SET FOREIGN_KEY_CHECKS=0;
How should one model multiple members of groups? Let me give an example.
I have people, houses, and household items.
All houses are owned by a single person and a person can own more than one house.
All household items are located in a single house, and are owned by the owner of the house. For my particular situation, I would also like a incremental counter for all household items owned by a given person.
In addition, while all household items share some properties, they also have some properties based on the type of household item they are. Assume there are only a few types of household items such as waterbeds, televisions, and refrigerators. Note that it must also be possible to link a household item to another table (for instance, otherPeopleInterestedInBuyingAHouseholdItem must have a foreign key to household items regardless of the type of household item).
So, houses are the group, and householdItems are members of that group.
How should this be modeled? Two possibilities are below. Is one better than the other given the above rules? I recognize that the first option with houses composite incrementing primary key doesn't play nice with InnoDB, however, can be implemented with triggers/etc which is acceptable. Or maybe a third option which is better?
-- MySQL Script generated by MySQL Workbench
-- 02/16/17 16:01:38
-- Model: New Model Version: 1.0
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
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`people`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`people` (
`idpeople` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`age` INT NOT NULL,
PRIMARY KEY (`idpeople`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houses` (
`idhouses` INT NOT NULL AUTO_INCREMENT,
`people_idpeople` INT NOT NULL,
`address` VARCHAR(45) NOT NULL,
`square_feet` INT NOT NULL,
PRIMARY KEY (`idhouses`),
INDEX `fk_houses_people1_idx` (`people_idpeople` ASC),
CONSTRAINT `fk_houses_people1`
FOREIGN KEY (`people_idpeople`)
REFERENCES `mydb`.`people` (`idpeople`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houseHoldItems`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houseHoldItems` (
`idhouseHoldItems` INT NOT NULL AUTO_INCREMENT,
`houses_idhouses` INT NOT NULL,
`value` DECIMAL(6,2) NOT NULL,
`dateBought` DATETIME NOT NULL,
PRIMARY KEY (`idhouseHoldItems`, `houses_idhouses`),
INDEX `fk_houseHoldItems_houses_idx` (`houses_idhouses` ASC),
CONSTRAINT `fk_houseHoldItems_houses`
FOREIGN KEY (`houses_idhouses`)
REFERENCES `mydb`.`houses` (`idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`waterBeds`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`waterBeds` (
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
`houseHoldItems_houses_idhouses` INT NOT NULL,
`gallonsWaterSize` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`, `houseHoldItems_houses_idhouses`),
CONSTRAINT `fk_waterBeds_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems` , `houseHoldItems_houses_idhouses`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems` , `houses_idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`televisions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`televisions` (
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
`houseHoldItems_houses_idhouses` INT NOT NULL,
`screenSize` INT NOT NULL,
`brandName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`, `houseHoldItems_houses_idhouses`),
CONSTRAINT `fk_televisions_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems` , `houseHoldItems_houses_idhouses`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems` , `houses_idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`refrigerators`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`refrigerators` (
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
`houseHoldItems_houses_idhouses` INT NOT NULL,
`icecubeCapacity` INT NOT NULL,
`color` VARCHAR(45) NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`, `houseHoldItems_houses_idhouses`),
CONSTRAINT `fk_refrigerators_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems` , `houseHoldItems_houses_idhouses`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems` , `houses_idhouses`)
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;
-- MySQL Script generated by MySQL Workbench
-- 02/16/17 16:00:37
-- Model: New Model Version: 1.0
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
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`people`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`people` (
`idpeople` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`age` INT NOT NULL,
PRIMARY KEY (`idpeople`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houses` (
`idhouses` INT NOT NULL AUTO_INCREMENT,
`people_idpeople` INT NOT NULL,
`address` VARCHAR(45) NOT NULL,
`square_feet` INT NOT NULL,
PRIMARY KEY (`idhouses`),
INDEX `fk_houses_people1_idx` (`people_idpeople` ASC),
CONSTRAINT `fk_houses_people1`
FOREIGN KEY (`people_idpeople`)
REFERENCES `mydb`.`people` (`idpeople`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houseHoldItems`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houseHoldItems` (
`idhouseHoldItems` INT NOT NULL AUTO_INCREMENT,
`houses_idhouses` INT NOT NULL,
`uniqueKey` INT NOT NULL,
`value` DECIMAL(6,2) NOT NULL,
`dateBought` DATETIME NOT NULL,
PRIMARY KEY (`idhouseHoldItems`),
INDEX `fk_houseHoldItems_houses1_idx` (`houses_idhouses` ASC),
UNIQUE INDEX `unique_key` (`houses_idhouses` ASC, `uniqueKey` ASC),
CONSTRAINT `fk_houseHoldItems_houses1`
FOREIGN KEY (`houses_idhouses`)
REFERENCES `mydb`.`houses` (`idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`waterBeds`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`waterBeds` (
`gallonsWaterSize` INT NOT NULL,
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`),
CONSTRAINT `fk_waterBeds_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`televisions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`televisions` (
`screenSize` INT NOT NULL,
`brandName` VARCHAR(45) NOT NULL,
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`),
CONSTRAINT `fk_televisions_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`refrigerators`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`refrigerators` (
`icecubeCapacity` INT NOT NULL,
`color` VARCHAR(45) NOT NULL,
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`),
CONSTRAINT `fk_refrigerators_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems`)
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;
Prefixing column names with the tablename gets tedious and unnecessary.
EAV is a bad way to go, but it is better than having separate tables for refrigerators and televisions. Follow the tag I added. The bottom line: have columns for columns you need to manipulate in MySQL throw the rest into a single JSON column.
For "many:many" tables, see my tips.
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.
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.