I'm creating a database with mysql and I have a problem with duplicated key, I don't find the error but I know which is the table error, I think the problem is with the FK's in "historiaLaboral" table the SP is this:
Im using mysql workbench I would like to you advising me how improve the model
-- 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 SGD
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema SGD
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SGD` DEFAULT CHARACTER SET utf8 ;
USE `SGD` ;
-- -----------------------------------------------------
-- Table `SGD`.`tipoIdentificación`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`tipoIdentificación` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empresa`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empresa` (
`id` INT NOT NULL,
`razonSocial` VARCHAR(45) NOT NULL,
`NIT` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`direccion` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empleado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empleado` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`id_tipoDocumento` INT NOT NULL,
`numeroDocumento` INT(20) NULL,
`aspiracionSalarial` DOUBLE NOT NULL,
`telefono` INT NOT NULL,
`direccion` VARCHAR(30) NOT NULL,
`id_empresa` INT NOT NULL,
`genero` ENUM('F', 'M') NOT NULL,
`aspirante` BIT(1) NULL,
PRIMARY KEY (`id`),
INDEX `id_tipoDocumento_idx` (`id_tipoDocumento` ASC),
INDEX `id_empresa_idx` (`id_empresa` ASC),
CONSTRAINT `id_tipoDocumento`
FOREIGN KEY (`id_tipoDocumento`)
REFERENCES `SGD`.`tipoIdentificación` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_empresa`
FOREIGN KEY (`id_empresa`)
REFERENCES `SGD`.`empresa` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`rol`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`rol` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`usuario` (
`id` INT NOT NULL AUTO_INCREMENT,
`usuario` VARCHAR(45) NOT NULL,
`contrasena` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`id_empleado` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`historiaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`historiaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_empleado` INT NOT NULL,
`id_usuarioRegistra` INT NOT NULL,
`fechaIngreso` DATE NOT NULL,
`fechaModificacion` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_usuarioRegistra_idx` (`id_usuarioRegistra` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_usuarioRegistra`
FOREIGN KEY (`id_usuarioRegistra`)
REFERENCES `SGD`.`usuario` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`referencia`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`referencia` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`relacion` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`telefono_2` VARCHAR(45) NULL,
`id_HLaboral` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`experienciaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`experienciaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`empresa` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`motivoRetiro` VARCHAR(45) NOT NULL,
`id_HLaboral` INT NOT NULL,
`certificado` BIT(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_Empleado`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`estudios`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`estudios` (
`id` INT NOT NULL AUTO_INCREMENT,
`institución` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`certificado` BIT(1) NOT NULL DEFAULT 0,
`id_HLaboral` INT NOT NULL,
`id_tipoEstudio` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_tipoEstudio` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_tipoEstudio`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`serie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`serie` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`soporte`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`soporte` (
`id` INT NOT NULL,
`papel` BIT(1) NOT NULL,
`digital` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`retencion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`retencion` (
`id` INT NOT NULL AUTO_INCREMENT,
`archivoGestion` INT NOT NULL,
`archivoCentral` INT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`dispFinal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`dispFinal` (
`id` INT NOT NULL AUTO_INCREMENT,
`conservacionTotal` BIT(1) NULL,
`eliminacion` BIT(1) NULL,
`digitalizacion` BIT(1) NULL,
`seleccion` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`TRD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`TRD` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`detalle` VARCHAR(2000) NOT NULL,
`id_soporte` INT NOT NULL,
`id_retencion` INT NOT NULL,
`id_dispFinal` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_serie` ASC),
INDEX `id_soporte_idx` (`id_soporte` ASC),
INDEX `id_retencion_idx` (`id_retencion` ASC),
INDEX `id_dispFinal_idx` (`id_dispFinal` ASC),
CONSTRAINT `id_serie`
FOREIGN KEY (`id_serie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_soporte`
FOREIGN KEY (`id_soporte`)
REFERENCES `SGD`.`soporte` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_retencion`
FOREIGN KEY (`id_retencion`)
REFERENCES `SGD`.`retencion` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_dispFinal`
FOREIGN KEY (`id_dispFinal`)
REFERENCES `SGD`.`dispFinal` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`documento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`documento` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_HLaboral` INT NOT NULL,
`id_TRD` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_HLaboral` ASC),
INDEX `id_TRD_idx` (`id_TRD` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_TRD`
FOREIGN KEY (`id_TRD`)
REFERENCES `SGD`.`TRD` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`ubicacion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`ubicacion` (
`id` INT NOT NULL AUTO_INCREMENT,
`analquel` VARCHAR(45) NOT NULL,
`estante` VARCHAR(45) NOT NULL,
`id_documento` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_documento_idx` (`id_documento` ASC),
CONSTRAINT `id_documento`
FOREIGN KEY (`id_documento`)
REFERENCES `SGD`.`documento` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`subserie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`subserie` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`id_subSerie` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_subSerie` ASC),
CONSTRAINT `id_subSerie`
FOREIGN KEY (`id_subSerie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`permisos`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`permisos` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`acceso` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`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;
I'm based on this model
Thanks for helping me!
Related
I'm forward engineering a database from an ERD that I created in MySqlWorkbench. I have a very basic many to many relationship between the "gyms" table and the "instructors" table, but I'm getting the error above. To me, the error reads that MySql can't find the "gym_id" column in the "gyms" table when it tries to create the many-to-many table.
I don't know why that would be the case.
I've went through the tables to make sure there were no typos and that the data types matched. I also went through the execution script to make sure the "gyms" table was being created before the many-to-many. It was. I can't sniff out what is causing the error. So any help or suggestion is appreciated
This is the error:
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'
SQL Code:
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
The other potentially useful information is:
1. On the many to many table, in the foreign keys section both gym_id and instructor_id are SMALLINTS and NOT NULL,
2. On their respective table the gym_id and instructor_id are autoincrementing primary keys, NOT NULL
3. I pasted the full script below if you want to look.
-- 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 fitness
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema fitness
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `fitness` DEFAULT CHARACTER SET utf8 ;
USE `fitness` ;
-- -----------------------------------------------------
-- Table `fitness`.`fitness_classes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`fitness_classes` (
`fitness_class_id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`description` VARCHAR(500) NOT NULL,
`vacancies` INT NOT NULL,
`start_time` DATETIME NOT NULL,
PRIMARY KEY (`fitness_class_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`categories` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`description` TEXT(1200) NULL,
PRIMARY KEY (`category_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`tags`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`category_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_tags_fitness_classes_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_tags_categories_idx` (`category_id` ASC) VISIBLE,
CONSTRAINT `fk_tags_fitness_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tags_categories`
FOREIGN KEY (`category_id`)
REFERENCES `fitness`.`categories` (`category_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms` (
`gym_id` SMALLINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(55) NOT NULL,
`address` VARCHAR(55) NOT NULL,
`city` VARCHAR(25) NOT NULL,
`state` VARCHAR(25) NOT NULL,
`phone` VARCHAR(10) NOT NULL,
`latitude` DECIMAL(10,8) NULL,
`longitude` DECIMAL(10,8) NULL,
`neighborhood` VARCHAR(45) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`gym_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`instructors` (
`instructor_id` SMALLINT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(55) NOT NULL,
`last_name` VARCHAR(55) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`instructor_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`listings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`listings` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`fitness_class_id` INT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_listings_gym_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_listings_fitness_class_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_listings_instructor_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_listings_gym`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_listings_fitness_class`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_listings_instructor`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`username` VARCHAR(16) NOT NULL,
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `fitness`.`reviews`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`reviews` (
`review_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`rating` TINYINT(1) NOT NULL,
`review_text` TEXT(1200) NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`review_id`),
INDEX `fk_reviews_classes_idx` (`fitness_class_id` ASC) VISIBLE,
CONSTRAINT `fk_reviews_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`bookings` (
`booking_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`user_id` INT NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`class_start_time` DATETIME NOT NULL,
`purchase_date` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`booking_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `fitness`.`users` ()
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fitness_class_id`
FOREIGN KEY ()
REFERENCES `fitness`.`fitness_classes` ()
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 4 years ago.
CREATE SCHEMA IF NOT EXISTS `cap` DEFAULT CHARACTER SET utf8 ;
USE `cap` ;
-- -----------------------------------------------------
-- Table `capstone`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`users` (
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(100) NOT NULL,
`roleid` INT NOT NULL,
`fullname` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
`department` VARCHAR(45) NULL,
PRIMARY KEY (`username`, `roleid`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`capstone`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`capstone` (
`username` VARCHAR(45) NOT NULL,
`capstoneid` INT NOT NULL,
`typeid` INT NOT NULL,
`title` VARCHAR(45) NULL,
`abstract` MEDIUMTEXT NULL,
`plagerismscore` VARCHAR(45) NULL,
`grade` VARCHAR(45) NULL,
PRIMARY KEY (`username`, `capstoneid`, `typeid`),
INDEX `fk_capstone_users_idx` (`username` ASC),
CONSTRAINT `fk_capstone_users`
FOREIGN KEY (`username`)
REFERENCES `cap`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`committee`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`committee` (
`capstoneid` INT NOT NULL,
`username` VARCHAR(45) NOT NULL,
`has_accepted` TINYINT NULL,
`has_declined` TINYINT NULL,
`positionid` INT NOT NULL,
`tracking` TINYINT NULL,
PRIMARY KEY (`capstoneid`, `username`, `positionid`),
CONSTRAINT `fk_committee_capstone`
FOREIGN KEY (`capstoneid`)
REFERENCES `cap`.`capstone` (`capstoneid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`studentdetails`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`studentdetails` (
`username` VARCHAR(45) NOT NULL,
`mastersstart` VARCHAR(45) NULL,
`capstonestart` VARCHAR(45) NULL,
PRIMARY KEY (`username`),
CONSTRAINT `fk_studentdetails_users`
FOREIGN KEY (`username`)
REFERENCES `cap`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`ritcalendar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`ritcalendar` (
`term` INT NOT NULL,
`startdate` VARCHAR(45) NULL,
`adddropdeadline` VARCHAR(45) NULL,
`gradedeadline` VARCHAR(45) NULL,
`enddate` VARCHAR(45) NULL,
PRIMARY KEY (`term`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`statushistory`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`statushistory` (
`capstoneid` INT NOT NULL,
`statusid` INT NOT NULL,
`date` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`capstoneid`, `statusid`),
UNIQUE INDEX `capstoneid_UNIQUE` (`capstoneid` ASC),
CONSTRAINT `fk_statushistory_capstone`
FOREIGN KEY (`capstoneid`)
REFERENCES `cap`.`capstone` (`capstoneid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`status` (
`statusid` INT NOT NULL,
`name` VARCHAR(45) NULL,
`stepcode` VARCHAR(45) NULL,
`description` VARCHAR(255) NULL,
PRIMARY KEY (`statusid`),
CONSTRAINT `fk_status_statushistory`
FOREIGN KEY (`statusid`)
REFERENCES `cap`.`statushistory` (`statusid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`roles` (
`roleid` INT NOT NULL,
`role` VARCHAR(10) NOT NULL,
PRIMARY KEY (`roleid`),
UNIQUE INDEX `role_UNIQUE` (`role` ASC),
CONSTRAINT `fk_roles_users`
FOREIGN KEY (`roleid`)
REFERENCES `cap`.`users` (`roleid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`types` (
`typeid` INT NOT NULL,
`type` VARCHAR(45) NOT NULL,
PRIMARY KEY (`typeid`),
CONSTRAINT `fk_types_capstone`
FOREIGN KEY (`typeid`)
REFERENCES `cap`.`capstone` (`typeid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `capstone`.`positions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cap`.`positions` (
`positionid` INT NOT NULL,
`position` VARCHAR(45) NULL,
PRIMARY KEY (`positionid`),
CONSTRAINT `fk_postions_committee`
FOREIGN KEY (`positionid`)
REFERENCES `cap`.`committee` (`positionid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `cap`.`committee` ( `capstoneid` INT NOT NULL, `username` VARCHAR(45) NOT NULL, `has_accepted` TINYINT NULL, `has_declined` TINYINT NULL, `positionid` INT NOT NULL, `tracking` TINYINT NULL, PRIMARY KEY (`capstoneid`, `username`, `positionid`), CONSTRAINT `fk_committee_capstone` FOREIGN KEY (`capstoneid`) REFERENCES `cap`.`capstone` (`capstoneid`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB Error Code: 1215. Cannot add foreign key constraint 0.031 sec
Can someone explain why I keep getting this error? They are the same datatype...
You should be more specific when you ask a question. You have eight foreign keys, you should at least say which one is causing an error.
Better would be to reduce your question to just the two tables involved in the error: capstone and committee
Here's what I see:
CREATE TABLE IF NOT EXISTS `cap`.`capstone` (
...
PRIMARY KEY (`username`, `capstoneid`, `typeid`),
CREATE TABLE IF NOT EXISTS `cap`.`committee` (
...
FOREIGN KEY (`capstoneid`)
REFERENCES `cap`.`capstone` (`capstoneid`)
You have a FK in committee that is referencing the middle column of the three-column primary key in capstone.
You should always make the FK have the same columns as the primary key it references.
I am trying to run this mysql commands in my H2 database. The DB is set to mysql mode.
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL,
`username` VARCHAR(45) NOT NULL COMMENT ' ',
`email` VARCHAR(320) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `serverprojects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `serverprojects` (
`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`status` TINYINT(1) NULL,
`restorescript_relative_path` VARCHAR(255) NULL,
`backupscript_relative_path` VARCHAR(255) NULL,
`installscript_relative_path` VARCHAR(255) NULL,
`username` VARCHAR(100) NULL,
`password` VARCHAR(100) NULL,
`gradle_relative_path` VARCHAR(255) NULL,
`root_path` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `projects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projects` (
`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`serverprojects_id` INT NOT NULL,
`users_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_projects_serverprojects_idx` (`serverprojects_id` ASC),
INDEX `fk_projects_users1_idx` (`users_id` ASC),
CONSTRAINT `fk_projects_serverprojects`
FOREIGN KEY (`serverprojects_id`)
REFERENCES `serverprojects` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_projects_users1`
FOREIGN KEY (`users_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET FOREIGN_KEY_CHECKS=1;
This produced the following error output
Unbekannter Datentyp: "FK_PROJECTS_SERVERPROJECTS_IDX"
Unknown data type: "FK_PROJECTS_SERVERPROJECTS_IDX"; SQL statement:
-- -----------------------------------------------------
-- Table `projects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projects` (
`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`serverprojects_id` INT NOT NULL,
`users_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_projects_serverprojects_idx` (`serverprojects_id` ASC),
INDEX `fk_projects_users1_idx` (`users_id` ASC),
CONSTRAINT `fk_projects_serverprojects`
FOREIGN KEY (`serverprojects_id`)
REFERENCES `serverprojects` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_projects_users1`
FOREIGN KEY (`users_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB [50004-193] HY004/50004 (Help)
Does anyone know whats going on here ? I think the problem is inside the syntax of INDEX fk_projects_serverprojects_idx (serverprojects_id ASC), but i cant get this to work
I just found out that the problem is this:
INDEX `fk_projects_serverprojects_idx` (`serverprojects_id` ASC),
INDEX `fk_projects_users1_idx` (`users_id` ASC),
in combination with the InnoDB. With InnoCb you are not alowed to do this.
I am trying to import a database created in mySql workbench into phpMyAdmin (WAMP server) , when I try to import it I get error :
MySQL said: Documentation
#1005 - Can't create table '.\mydb\team.frm' (errno: 121)
Below is the executed SQL script
-- MySQL Script generated by MySQL Workbench
-- 03/03/15 13:06:58
-- 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 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`TeamName` VARCHAR(45) NOT NULL,
`GoverningBody` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Competitor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Competitor` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NOT NULL,
`Surname` VARCHAR(45) NOT NULL,
`Age` INT NOT NULL,
`Nationality` VARCHAR(45) NOT NULL,
`Title` VARCHAR(45) NOT NULL,
`ContactNumber` INT NOT NULL,
`team_id` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `TeamID_idx` (`team_id` ASC),
CONSTRAINT `TeamID`
FOREIGN KEY (`team_id`)
REFERENCES `mydb`.`Team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Card`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Card` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`IssueDate` DATE NOT NULL,
`ExpiryDate` DATE NOT NULL,
`Status` ENUM('Active','Expired','Lost') NOT NULL,
`competitor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CompetitorID_idx` (`competitor_id` ASC),
CONSTRAINT `CompetitorID`
FOREIGN KEY (`competitor_id`)
REFERENCES `mydb`.`Competitor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Venue`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Venue` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`Stadium` VARCHAR(45) NOT NULL,
`Location` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Fixture`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Fixture` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`Time` TIME NOT NULL,
`Date` DATE NOT NULL,
`Type` VARCHAR(45) NOT NULL,
`venue_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Authorisation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Authorisation` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`fixture_id` INT NOT NULL,
`card_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `FixtureID_idx` (`fixture_id` ASC),
INDEX `CardID_idx` (`card_id` ASC),
CONSTRAINT `FixtureID`
FOREIGN KEY (`fixture_id`)
REFERENCES `mydb`.`Fixture` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Log` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`card_id` INT NOT NULL,
`venue_id` INT NOT NULL,
`LogTime` TIME NOT NULL,
`LogDate` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `CardID_idx` (`card_id` ASC),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`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;
Making the changes in the comments above worked for me (the DATETIME to INT and making unique constraint and index names). As a safe practice, in our organization we always name our indexes/constraints as (table1Identifier)_(table2Identifier)_(constraintType)...so something like Log_Card_idx or Auth_Fixture_FK. Just a thought.
-- MySQL Script generated by MySQL Workbench
-- 03/03/15 13:06:58
-- 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 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Team`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Team` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`TeamName` VARCHAR(45) NOT NULL,
`GoverningBody` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Competitor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Competitor` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NOT NULL,
`Surname` VARCHAR(45) NOT NULL,
`Age` INT NOT NULL,
`Nationality` VARCHAR(45) NOT NULL,
`Title` VARCHAR(45) NOT NULL,
`ContactNumber` INT NOT NULL,
`team_id` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `TeamID_idx` (`team_id` ASC),
CONSTRAINT `TeamID`
FOREIGN KEY (`team_id`)
REFERENCES `mydb`.`Team` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Card`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Card` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`IssueDate` DATE NOT NULL,
`ExpiryDate` DATE NOT NULL,
`Status` ENUM('Active','Expired','Lost') NOT NULL,
`competitor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CompetitorID_idx` (`competitor_id` ASC),
CONSTRAINT `CompetitorID`
FOREIGN KEY (`competitor_id`)
REFERENCES `mydb`.`Competitor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Venue`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Venue` (
`id` INT NOT NULL,
`referred_as` VARCHAR(45) NULL,
`Stadium` VARCHAR(45) NOT NULL,
`Location` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Fixture`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Fixture` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`Time` TIME NOT NULL,
`Date` DATE NOT NULL,
`Type` VARCHAR(45) NOT NULL,
`venue_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `VenueID_idx` (`venue_id` ASC),
CONSTRAINT `VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `mydb`.`Venue` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Authorisation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Authorisation` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`fixture_id` INT NOT NULL,
`card_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `Auth_FixtureID_idx` (`fixture_id` ASC),
INDEX `Auth_CardID_idx` (`card_id` ASC),
CONSTRAINT `Auth_FixtureID`
FOREIGN KEY (`fixture_id`)
REFERENCES `Fixture` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Auth_CardID`
FOREIGN KEY (`card_id`)
REFERENCES `Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Log` (
`id` INT NOT NULL AUTO_INCREMENT,
`referred_as` VARCHAR(45) NULL,
`card_id` INT NOT NULL,
`venue_id` INT NOT NULL,
`LogTime` TIME NOT NULL,
`LogDate` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `Log_CardID_idx` (`card_id` ASC),
INDEX `Log_VenueID_idx` (`venue_id` ASC),
CONSTRAINT `Log_CardID`
FOREIGN KEY (`card_id`)
REFERENCES `mydb`.`Card` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Log_VenueID`
FOREIGN KEY (`venue_id`)
REFERENCES `Venue` (`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;
I'm trying to create these tables but I'm getting Error 1022 when creating the table cadastro_fornecedor.
I don't know where to start to solve this issue so any kind of help is welcome.
Here is the .sql:
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`.`administrador`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`administrador` (
`id_administrador` INT NOT NULL,
`nome` VARCHAR(45) NULL,
`senha` VARCHAR(45) NULL,
`CPF` INT NULL,
`endereço` VARCHAR(45) NULL,
`data_nascimento` DATE NULL,
PRIMARY KEY (`id_administrador`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_usuario` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data_cadastro` DATETIME NOT NULL,
`id_administrador` INT NULL,
`usuario` INT NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `matricula_idx` (`usuario` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `matricula`
FOREIGN KEY (`usuario`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`usuario_cadastrado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`usuario_cadastrado` (
`matricula` INT NOT NULL,
`nome` VARCHAR(45) NULL,
`endereco` VARCHAR(45) NULL,
`senha` VARCHAR(45) NULL,
`nacionalidade` VARCHAR(45) NULL DEFAULT 'brasileiro',
`naturalidade` VARCHAR(45) NULL,
`sexo` CHAR NULL,
`data_nascimento` DATE NULL,
`CPF` INT NULL,
`nome_pai` VARCHAR(45) NULL,
`nome_mae` VARCHAR(45) NULL,
`telefone_celular` VARCHAR(15) NULL,
`telefone_fixo` VARCHAR(15) NULL,
`id_cadastro` INT NULL,
PRIMARY KEY (`matricula`),
INDEX `id_cadastro_idx` (`id_cadastro` ASC),
CONSTRAINT `id_cadastro`
FOREIGN KEY (`id_cadastro`)
REFERENCES `mydb`.`cadastro_usuario` (`id_cadastro`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`obra`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`obra` (
`ISBN` VARCHAR(20) NOT NULL,
`titulo` VARCHAR(45) NULL,
`autor` VARCHAR(45) NULL,
`ano` YEAR NULL,
`editora` VARCHAR(45) NULL,
`categoria` VARCHAR(45) NULL,
`assunto` VARCHAR(45) NULL,
`descricao` VARCHAR(45) NULL,
PRIMARY KEY (`ISBN`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`fornecedores`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`fornecedores` (
`CNPJ` VARCHAR(45) NOT NULL,
`nome` VARCHAR(45) NOT NULL,
`endereco` VARCHAR(45) NULL,
`telefone` VARCHAR(45) NULL,
PRIMARY KEY (`CNPJ`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`exemplar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`exemplar` (
`codigo_exemplar` INT NOT NULL,
`data_aquisicao` DATE NULL,
`edição` INT NULL,
`volume` INT NULL,
`paginas` INT NULL,
`ISBN` VARCHAR(20) NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`codigo_exemplar`),
INDEX `ISBN_idx` (`ISBN` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `ISBN`
FOREIGN KEY (`ISBN`)
REFERENCES `mydb`.`obra` (`ISBN`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_fornecedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_fornecedor` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data` DATETIME NULL,
`id_administrador` INT NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`emprestimo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`emprestimo` (
`id_emprestimo` INT NOT NULL AUTO_INCREMENT,
`data_emprestimo` DATE NULL,
`data_devolucao_prevista` DATE NULL,
`matricula` INT NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_emprestimo`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`devolucao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`devolucao` (
`id_devolucao` INT NOT NULL,
`data_devolucao` DATE NULL,
`multa` INT NULL,
`matricula` INT NULL,
`id_administrador` INT NULL,
PRIMARY KEY (`id_devolucao`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `id_administrador_idx` (`id_administrador` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`exclusao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`exclusao` (
`id_exclusao` INT NOT NULL AUTO_INCREMENT,
`data_exclusao` DATE NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_exclusao`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`registro`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`registro` (
`id_registro` INT NOT NULL AUTO_INCREMENT,
`data_registro` DATE NULL,
`id_administrador` INT NULL,
`codigo_exemplar` INT NULL,
PRIMARY KEY (`id_registro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `codigo_exemplar_idx` (`codigo_exemplar` ASC),
CONSTRAINT `id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `codigo_exemplar`
FOREIGN KEY (`codigo_exemplar`)
REFERENCES `mydb`.`exemplar` (`codigo_exemplar`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`reserva`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`reserva` (
`id_reserva` INT NOT NULL,
`data_reserva` DATE NULL,
`matricula` INT NULL,
`ISBN` VARCHAR(20) NULL,
PRIMARY KEY (`id_reserva`),
INDEX `matricula_idx` (`matricula` ASC),
INDEX `ISBN_idx` (`ISBN` ASC),
CONSTRAINT `matricula`
FOREIGN KEY (`matricula`)
REFERENCES `mydb`.`usuario_cadastrado` (`matricula`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ISBN`
FOREIGN KEY (`ISBN`)
REFERENCES `mydb`.`obra` (`ISBN`)
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;
I appreciate any kind of help.
Thanks.
You're reusing existing constraint names (id_administrador is already used as a constraint name in the table cadastro_usuario and CNPJ is used in the table exemplar), you'll have to give your constraints unique names (here I renamed them to cf_id_administrador/CF_CNPJ);
-- -----------------------------------------------------
-- Table `mydb`.`cadastro_fornecedor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`cadastro_fornecedor` (
`id_cadastro` INT NOT NULL AUTO_INCREMENT,
`data` DATETIME NULL,
`id_administrador` INT NULL,
`CNPJ` VARCHAR(45) NULL,
PRIMARY KEY (`id_cadastro`),
INDEX `id_administrador_idx` (`id_administrador` ASC),
INDEX `CNPJ_idx` (`CNPJ` ASC),
CONSTRAINT `cf_id_administrador`
FOREIGN KEY (`id_administrador`)
REFERENCES `mydb`.`administrador` (`id_administrador`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `CF_CNPJ`
FOREIGN KEY (`CNPJ`)
REFERENCES `mydb`.`fornecedores` (`CNPJ`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;