Can't write duplicate key in MySQL - mysql

Executing SQL script in server
ERROR: Error 1022: Can't write; duplicate key in table 'dependent'
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`DEPENDENT`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`DEPENDENT` (
`Essn` CHAR(9) NOT NULL,
`Dependent_name` VARCHAR(45) NOT NULL,
`Sex` CHAR NULL,
`Bdate` DATE NULL,
`Relationship` VARCHAR(45) NULL,
PRIMARY KEY (`Dependent_name`, `Essn`),
CONSTRAINT `Essn`
FOREIGN KEY (`Essn`)
REFERENCES `mydb`.`EMPLOYEE` (`Ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch`enter code here`
Am getting this error. Can some one help me on this. Thanks

hi you have primary key defined on the Dependent_name if you try to insert same name 2 times you will get error So you have to define other primary key if you expect duplicate name a common approach id to use auto increment column as primary key but you can define composite keys as well for primary key

Related

how can i make my database er diagram to code?

I've made this ER diagram for my college project but i have to use forward engineering to see SQL code. MySQL version is 8.0.27
when i try it, it come up with error like below.
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'editor_ismi' for constraint 'fk_editor_ismi' in the referenced table 'editor'
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`kitap`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`kitap` (
`isbn` VARCHAR(45) NOT NULL,
`editor_ismi` VARCHAR(45) NULL,
`yazar_ismi` VARCHAR(45) NULL,
`yazar_adres` VARCHAR(45) NULL,
`kitap_yili` INT NOT NULL,
`kitap_baslik` VARCHAR(45) NOT NULL,
`kitap_fiyat` VARCHAR(45) NOT NULL,
PRIMARY KEY (`isbn`),
INDEX `fk_editor_ismi_idx` (`editor_ismi` ASC) VISIBLE,
INDEX `fk_yazar_ismi_idx` (`yazar_ismi` ASC) VISIBLE,
INDEX `fk_yazar_adres_idx` (`yazar_adres` ASC) VISIBLE,
CONSTRAINT `fk_editor_ismi`
FOREIGN KEY (`editor_ismi`)
REFERENCES `mydb`.`editor` (`editor_ismi`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_yazar_ismi`
FOREIGN KEY (`yazar_ismi`)
REFERENCES `mydb`.`Yazar` (`yazar_ismi`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_yazar_adres`
FOREIGN KEY (`yazar_adres`)
REFERENCES `mydb`.`Yazar` (`yazar_ismi`)
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
screenshot of my er diagram

how to fix Error in EER diagram to create tables?

when I wanted to generate tables, I got this error I don't know how to fix it. Is anyone face to this problem and how you solve it?
thanks
CREATE TABLE IF NOT EXISTS `tb_inv_detail` (
`inv_id_fk` INT UNSIGNED NOT NULL,
`part_id_fk` INT UNSIGNED NOT NULL,
`qty` DECIMAL(12,2) NOT NULL,
`tb_detailed` VARCHAR(50) NULL,
`tb_inv_detailcol` VARCHAR(45) NULL,
PRIMARY KEY (`inv_id_fk`),
INDEX `fk_tb_inv_detail_tb_parts1_idx` (`part_id_fk` ASC) VISIBLE,
CONSTRAINT `fk_tb_inv_detail_tb_parts1`
FOREIGN KEY (`part_id_fk`)
REFERENCES `tb_parts` (`part_id_pk`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_inv_detail_tb_invoice1`
FOREIGN KEY (`inv_id_fk`)
REFERENCES `tb_invoice` (`inv_id_pk`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 25 succeeded, 1 failed
Also this screenshot of my EER diagram:
Well the obvious code which is missing from what you posted above is the DDL for the two tables referred to by the foreign keys defined in tb_inv_detail. These tables are:
tb_parts
tb_invoice
These tables must be defined first, with correctly named primary key columns to match the foreign keys in your table above.
CREATE TABLE tb_parts (
part_id_pk INT UNSIGNED NOT NULL,
PRIMARY KEY (part_id_pk), -- referred to by part_id_fk
...
)
CREATE TABLE tb_invoice (
inv_id_pk INT UNSIGNED NOT NULL,
PRIMARY KEY (inv_id_pk), -- referred to by inv_id_fk
...
)

why i Cannot add foreign key constraint in MySQL Workbench

i have customer table with nid_c,nama_customer, and more field ..
second table I have kendaraan with nopol,nid_c,nama_customer, and more field ..
I try make relation between this table..
I want update data nid_c and nama_customer on kendaraan table when I update customer table.
I got error message here.
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `BengkelBiru`.`kendaraan`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `BengkelBiru`.`kendaraan` (
`NOPOL` VARCHAR(12) NOT NULL,
`NID_C` VARCHAR(7) NULL DEFAULT NULL,
`NAMA_CUSTOMER` VARCHAR(25) NULL DEFAULT NULL,
`MERK` VARCHAR(15) NULL DEFAULT NULL,
`TYPE` VARCHAR(25) NULL DEFAULT NULL,
`CC` VARCHAR(4) NULL DEFAULT NULL,
`TAHUN` VARCHAR(4) NULL DEFAULT NULL,
`WARNA` VARCHAR(10) NULL DEFAULT NULL,
`STATUS` VARCHAR(7) NULL DEFAULT NULL,
PRIMARY KEY (`NOPOL`),
INDEX `pkk_idx` (`NAMA_CUSTOMER` ASC, `NID_C` ASC),
CONSTRAINT `FK_NID_C`
FOREIGN KEY (`NAMA_CUSTOMER` , `NID_C`)
REFERENCES `BengkelBiru`.`customer` (`NID_C` , `NID_C`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
SQL script execution finished: statements: 14 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Your problem is on one or both of these lines:
FOREIGN KEY (`NAMA_CUSTOMER` , `NID_C`)
REFERENCES `BengkelBiru`.`customer` (`NID_C` , `NID_C`)
^^^^^^^ Looks wrong s/b NAMA_CUSTOMER
I think you want this line:
REFERENCES `BengkelBiru`.`customer` (`NID_C` , `NID_C`)
to be
REFERENCES `BengkelBiru`.`customer` (`NAMA_CUSTOMER`, `NID_C`)
Why are you referring to NID_C twice in the reference? I say this because you define the foreign key as:
FOREIGN KEY (`NAMA_CUSTOMER` , `NID_C`)
and your descriptions at the top shows customer having NID_C and NAMA_CUSTOMER as columns.
However, fundamentally, why do you have Nama_customer in the kendaraan (vehicle) table at all? This doesn't seem to be 3rd normal form. You've repeated the customer name in a second table; which isn't part of the Customer's PK. Now, this may be acceptable if you want to keep the name of the customer at the time the entry is made into kendaraan; but since you're making it part of the FK... and doing cascade update/delete... it's very odd.
So maybe you just want:
FOREIGN KEY (`NID_C`)
REFERENCES `BengkelBiru`.`customer` (`NID_C`)
Assuming the Primary Key of Customer is NID_C
I dont think you can declare both at the same time. Try doing them separately.
CONSTRAINT `FK_NAMA_CUSTOMER`
FOREIGN KEY (`NAMA_CUSTOMER`)
REFERENCES `BengkelBiru`.`customer` (`NID_C`)
CONSTRAINT `FK_NID_C`
FOREIGN KEY (`NID_C`)
REFERENCES `BengkelBiru`.`customer` (`NID_C`)
There could be any possible scenario :-
1.Columns in the parent tables Can be INT UNSIGNED?
2.Data type in both tables should be same.
3.You are trying to reference a nonexistent key on the target table. Make sure that it is a key on the other table (it can be a primary or unique key).
Foregin Key Constaints

MYSQL Workbench - ERROR: Error 1215: Cannot add foreign key constraint

I am trying to forward engineer a database on MySQL Workbench, but I continually am getting this error message. I feel as though it is most likely an issue with the structure of my relationships, but I cannot find a discrepancy.
Here is an image of the schema diagram.
Here is the error message I am receiving:
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `bturpin`.`THREAD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bturpin`.`THREAD` (
`id` INT NOT NULL AUTO_INCREMENT,
`answer` DOUBLE NULL,
`comment` VARCHAR(255) NULL,
`test_subcategory_name` VARCHAR(45) NULL,
`RATING_id` INT NOT NULL,
`RATING_INSTRUCTOR_id` VARCHAR(30) NOT NULL,
`RATING_INSTRUCTOR_LESSON_title` VARCHAR(45) NOT NULL,
`RATING_INSTRUCTOR_LESSON_COURSE_num` VARCHAR(15) NOT NULL,
`RATING_RATER_id` VARCHAR(30) NOT NULL,
PRIMARY KEY (`id`, `RATING_id`, `RATING_INSTRUCTOR_id`, `RATING_INSTRUCTOR_LESSON_title`, `RATING_INSTRUCTOR_LESSON_COURSE_num`, `RATING_RATER_id`),
INDEX `fk_THREAD_RATING1_idx` (`RATING_id` ASC, `RATING_INSTRUCTOR_id` ASC, `RATING_INSTRUCTOR_LESSON_title` ASC, `RATING_INSTRUCTOR_LESSON_COURSE_num` ASC, `RATING_RATER_id` ASC),
CONSTRAINT `fk_THREAD_RATING1`
FOREIGN KEY (`RATING_id` , `RATING_INSTRUCTOR_id` , `RATING_INSTRUCTOR_LESSON_title` , `RATING_INSTRUCTOR_LESSON_COURSE_num` , `RATING_RATER_id`)
REFERENCES `bturpin`.`RATING` (`id` , `INSTRUCTOR_id` , `INSTRUCTOR_LESSON_title` , `INSTRUCTOR_LESSON_COURSE_num` , `RATER_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 14 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Try forwarding in this order :
Manually create the fields which must have a foreign key constraint in MySQL WB
Forward engineer to your DB
Manually create the foreign key constraints in MySQL WB
Forward engineer angain
You may also consider using only integer foreign key.
This seems to be a MySQL Workbench issue with foreign key on non integer key.
Source : Error 1215: Cannot add foreign key constraint

Mysql error # 1215

I have been using mySQL to create a database but when I try to forward engineer my EER Diagram the database keeps sending me back the same error I have tried multiple fixes does anyone see what the problem is? The error code is 1215
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`Employee`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Employee` (
`EID` INT NOT NULL,
`Fname` VARCHAR(45) NOT NULL,
`Lname` VARCHAR(45) NOT NULL,
`AddressID` INT NOT NULL,
`PayLevel` FLOAT NOT NULL,
`Jobtitle` VARCHAR(45) NOT NULL,
`Date of Employment` DATE NOT NULL,
PRIMARY KEY (`EID`),
CONSTRAINT `fk_Employee_Store1`
FOREIGN KEY (`EID`)
REFERENCES `mydb`.`Store` (`EID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
This is the parent code, multiple times I have tried switching the relationships however employee9the top code) should be a child of Store.
-- -----------------------------------------------------
-- Table `mydb`.`Store`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Store` (
`SID` INT NOT NULL,
`StoreName` VARCHAR(45) NULL,
`AddressID` INT NULL,
`EID` INT NOT NULL,
`CID` INT NULL,
`MID` INT NULL,
PRIMARY KEY (`SID`, `EID`),
INDEX `fk_Store_Employee1_idx` (`EID` ASC),
CONSTRAINT `fk_Store_Employee1`
FOREIGN KEY (`EID`)
REFERENCES `mydb`.`Employee` (`EID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Does anyone see the problem, Btw I have checked the type's multiple times they are equivalent.
you are trying to create a foreign key in store from employee and another one in employee from store.
if this is a parent/child relation then only the primary key from the parent that is used in the child.
if you have n<->n relation then you need a new table that holds both foreign keys.
if I understand, here you need to have a new table (work) with (SID, EID) where both point to their respective tables (store and employee). Also creation order is important (parents first then child tables).
You have a foreign key constraint operating in both directions. When you're creating the tables the first to be created will fail because the second doesn't exist. I'm not even sure that MySQL will accept a circular reference like this. You should probably remove the foreign key constraint applied to mydb.store
However, if this is essential you can ask MySQL to ignore the foreign key checks while you create the table. Just execute
SET foreign_key_checks = 0;
before you create the tables and
SET foreign_key_checks = 1;
after you finish