How to add columns from one MySQL table to another without duplicates? - mysql

There are two tables. It is necessary to insert two fields from one to the other so that duplicates do not appear. I tried those methods that are described for
USING INSERT IGNORE
Using replace
USING INSERT ... on duplicate key update
But I didn't succeed. For example, it ignore duplicate and write these:
REPLACE INTO user_favorites
(user_id, partner_id)
SELECT id, partner_id FROM users
How to do it?
1 table
create table local.users
(
id int auto_increment,
name varchar(255) null,
email varchar(255) null,
password varchar(255) null,
partner_id int null,
constraint users_email_unique
unique (email),
constraint users_id_uindex
unique (id)
)
alter table local.users
add primary key (id);
2 table
create table local.user_favorites
(
id int auto_increment,
user_id int null,
partner_id int null,
constraint user_favorites_id_uindex
unique (id),
constraint user_favorites_partners_id_fk
foreign key (partner_id) references local.partners (id)
on update cascade on delete cascade,
constraint user_favorites_users_id_fk
foreign key (user_id) references local.users (id)
on update cascade on delete cascade
);
alter table local.user_favorites
add primary key (id);

insert ignore and insert ... on duplicate key and replace all detect duplicates by whatever unique key constraints you have. Right now, your only unique constraint in user_favorites is the primary key id, which obviously doesn't help.
Add a unique constraint on user_id and partner_id:
alter table local.user_favorites add unique (user_id,partner_id);
If that fails, you already have duplicates that you will need to clean up first.
Then do any of the things you tried to add the rows from users.

You can first add empty columns and then update corresponding values by join operation. Like this:
ALTER TABLE user_favorites ADD COLUMN
name VARCHAR(255) NULL,
email VARCHAR(255) NULL,
password VARCHAR(255) NULL;
CONSTRAINT users_email_unique UNIQUE(email);
UPDATE user_favorites tb1
INNER JOIN users tb2 ON tb1.user_id = tb2.id
AND tb1.partner_id = tb2.partner_id
SET tb1.name = tb2.name
tb1.email = tb2.email
tb1.password = tb2.password;
Reference here: https://www.tutorialspoint.com/can-we-add-a-column-to-a-table-from-another-table-in-mysql

Related

MySQL Error Code 1215: “Cannot add foreign key constraint”

I keep getting this error when attempting to create a table with SQL.
I have these two tables:
I'm using PHPMyAdmin and it won't allow me to use M_id as a foreign key which references Employee Table primary key E_id.
Anyone able to see what's wrong with my code?
Thanks!
Foreign key definitions have to exactly match the primary key columns to which they refer. In this case, you defined Department.M_id to a be a nullable integer column, while EMPLOYEE.E_id is integer not nullable. Try making M_id not nullable:
CREATE TABLE Department (
D_name VARCHAR(100) NOT NULL,
D_id INT NOT NULL,
M_id INT NOT NULL DEFAULT 0000,
...
FOREIGN KEY (M_id) REFERENCES EMPLOYEE(E_id)
ON DELETE SET DEFAULT ON UPDATE CASCADE
)
Your code has multiple errors:
varchar() length is too long.
You have a forward reference for a foreign key constraint.
SET DEFAULT doesn't really work.
You want something like this:
CREATE TABLE employees (
employee_id int not null primary key,
Job_type VARCHAR(100),
Ssn INT NOT NULL,
Salary DECIMAL NOT NULL,
Address VARCHAR(500) NOT NULL,
First_name VARCHAR(50) NOT NULL,
M_initial CHAR(1),
Last_name VARCHAR(50) NOT NULL,
E_end_date DATE,
E_start_date DATE NOT NULL,
department_id INT NOT NULL,
Super_id INT,
FOREIGN KEY (Super_id) REFERENCES employees(employee_id) ON DELETE SET NULL ON UPDATE CASCADE,
UNIQUE (Ssn)
);
CREATE TABLE departments (
department_id int primary key,
D_name VARCHAR(100) NOT NULL,
D_id INT NOT NULL,
M_id INT DEFAULT 0000,
Manager_start_date DATE NOT NULL,
Manager_end_date DATE,
Report VARCHAR(8000),
Num_of_employees INT NOT NULL,
FOREIGN KEY (M_id) REFERENCES employees(employee_id) ON DELETE SET NULL ON UPDATE CASCADE,
UNIQUE (D_name)
);
ALTER TABLE employees ADD CONSTRAINT FOREIGN KEY (department_id) REFERENCES departments(department_id)
ON DELETE CASCADE ON UPDATE CASCADE;
I also changed a few other things:
The table names are plural.
The primary keys are the singular form followed by "_id".
Foreign keys and primary keys have the same name.
The primary key is the first column in the table.
Here is a db<>fiddle showing that this works.
I will not question your design, though it looks problematic.
However - You cannot reference a table which doesn't exist yet (REFERENCES Department(D_id)). You should either remove the FOREIGN KEY constraints from the CREATE statements and add them afterwards in ALTER TABLE statements.
Example:
CREATE TABLE EMPLOYEE (...);
CREATE TABLE Department (...);
ALTER TABLE EMPLOYEE
ADD FOREIGN KEY (D_id)
REFERENCES Department(D_id)
ON DELETE CASCADE
ON UPDATE CASCADE
;
Demo
Or temporarily disable foreign key checks:
SET FOREIGN_KEY_CHECKS = 0;
CREATE TABLE EMPLOYEE (...);
CREATE TABLE Department (...);
SET FOREIGN_KEY_CHECKS = 1;
Demo
You can also not use ON DELETE SET DEFAULT. InnoDB doesn't support it. You need to change it to ON DELETE SET NULL. If you want that behavior, you will need to implement it either in your application code or in a trigger.
I would also use TEXT as data type instead of VARCHAR(30000).

Delete, upon foreign key constraint update

MySQL offers the following:
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;
Does MySQL offer something similar for DELETE which would attempt to delete and upon foreign key constraint, update the record in the table which was attempted to be deleted?
For instance...
DELETE FROM table1 WHERE idtable1 = 123;
IF(foreign key constraint) { //pseudo code...
UPDATE table1 SET deleted=1 WHERE idtable1 = 123;
}
CREATE TABLE IF NOT EXISTS table1 (
idtable1 INT NOT NULL,
data VARCHAR(45) NULL,
deleted TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY (idtable1))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS table2 (
idtable2 INT NOT NULL,
table1_idtable1 INT NOT NULL,
data VARCHAR(45) NULL,
INDEX fk_table2_table1_idx (table1_idtable1 ASC),
CONSTRAINT fk_table2_table1
FOREIGN KEY (table1_idtable1)
REFERENCES table1 (idtable1)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
If I understand correctly, you want a cascading foreign constraint on delete and/or update:
CREATE TABLE IF NOT EXISTS table2 (
idtable2 INT NOT NULL,
table1_idtable1 INT NOT NULL,
data VARCHAR(45) NULL,
INDEX fk_table2_table1_idx (table1_idtable1 ASC),
CONSTRAINT fk_table2_table1
FOREIGN KEY (table1_idtable1)
REFERENCES table1 (idtable1)
ON DELETE CASCADE
ON UPDATE CASCADE
);
This will delete the row in table2 when the corresponding row in table1 is deleted.
You can read about the different types of foreign key constraints in the documentation.

MySql - Product Variants Table (Wide Table) - Unique with NULLs

I have a products table, and a product_variants table (one-to-many).
The product_variants table has the following structure:
CREATE TABLE product_variants (
id int(11) NOT NULL AUTO_INCREMENT,
id_product int(11) NOT NULL,
id_colourSet int(11) DEFAULT NULL,
id_size int(11) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY UNIQUE (id_product,id_colourSet,id_size),
KEY idx_prod (id_product),
KEY idx_colourSet (id_colourSet),
KEY idx_size (id_size),
CONSTRAINT fk_df_product_variants_id_colurSet FOREIGN KEY (id_colourSet) REFERENCES df_colour_sets (id_colourSet) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_df_product_variants_id_product FOREIGN KEY (id_product) REFERENCES df_products (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_df_product_variants_id_size FOREIGN KEY (id_size) REFERENCES df_sizes (id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB
The options are known at compile-time. Each option is foreign-keyed to a dedicated table, and the unique key is the combination of all options.
I then insert products with an "ON DUPLICATE KEY UPDATE ..." statement, and if a variant already exists the query will use an existing variant.
The problem is that certain products do not have a color, nor a size. In this case the unique constraint fails and I insert lots of almost-empty rows in the product_variants table.
In order to solve this problem I am creating a "NULL" value for each option (e.g. "NO_COLOR", "NO_SIZE") in the respective option tables, and using that as the default value for the option columns in the product_variants table.
Would this be the recommended solution? Is there a better way of structuring this data? I would really like to avoid an EAV design.
Thank you
Designating a magic value that means "missing value" is not the right solution in almost every case. That's what NULL is for.
It's also not clear how "NO_COLOR" is used for an integer. I guess it would map to the value 0, which is typically not used in an auto-increment column.
You can create another column to be a hash of the three unique key columns, defaulted to '' to avoid null problems. Then put a unique constraint on that hash.
CREATE TABLE product_variants (
id int(11) NOT NULL AUTO_INCREMENT,
id_product int(11) NOT NULL,
id_colourSet int(11) DEFAULT NULL,
id_size int(11) DEFAULT NULL,
option_hash binary(16) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (option_hash),
KEY idx_prod (id_product),
KEY idx_colourSet (id_colourSet),
KEY idx_size (id_size),
CONSTRAINT fk_df_product_variants_id_colurSet FOREIGN KEY (id_colourSet) REFERENCES df_colour_sets (id_colourSet) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_df_product_variants_id_product FOREIGN KEY (id_product) REFERENCES df_products (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_df_product_variants_id_size FOREIGN KEY (id_size) REFERENCES df_sizes (id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
CREATE TRIGGER product_variants_ins BEFORE INSERT ON product_variants
FOR EACH ROW SET option_hash = UNHEX(MD5(CONCAT_WS('|',
COALESCE(id_product, ''),
COALESCE(id_colourSet, ''),
COALESCE(id_size, ''))));
CREATE TRIGGER product_variants_upd BEFORE UPDATE ON product_variants
FOR EACH ROW SET option_hash = UNHEX(MD5(CONCAT_WS('|',
COALESCE(id_product, ''),
COALESCE(id_colourSet, ''),
COALESCE(id_size, ''))));

Add foreign key constraint if it is not existing

I want to create a foreign key from 1 table, but only if it does not exist.
Tables are created like that:
CREATE TABLE
IF NOT EXISTS PEs (
id INT(20) AUTO_INCREMENT PRIMARY KEY,
Name varchar(20),
Message varchar(30),
CoordsX double(9,6) SIGNED,
CoordsY double(9,6) SIGNED,
CoordsZ double(9,6) SIGNED,
Status smallint(1) DEFAULT 1,
world varchar(20)
) ENGINE = InnoDB;
CREATE TABLE
IF NOT EXISTS`rh_pe`.`attributes` (
`toid` INT(20) NOT NULL,
`Kommentar` VARCHAR(60) NOT NULL,
`Aktion` varchar(10) NOT NULL,
`Person1` INT NOT NULL,
`Person2` INT
) ENGINE = InnoDB;
The Foreign key should be like so:
ALTER TABLE `attributes`
ADD CONSTRAINT `Const`
FOREIGN KEY (`toid`) REFERENCES `pes`(`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT;
To create the foreign key, I tried the following two options:
IF NOT EXISTS(
ALTER TABLE `attributes`
ADD CONSTRAINT `Const`
FOREIGN KEY (`toid`) REFERENCES `pes`(`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
);
and
ALTER TABLE `attributes`
ADD CONSTRAINT `Const`
FOREIGN KEY
IF NOT EXISTS (`toid`) REFERENCES `pes`(`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
But none of them work.
Any Ideas on how I could create the constraint only if it does not exist?
Both of your table examples have the same name, so I suposed that your second table name is "pes" as you mention in your constraint examples. This one should work:
IF NOT EXISTS (SELECT * FROM sys.objects o WHERE o.object_id = object_id(N'`rh_pe`.`Const`') AND OBJECTPROPERTY(o.object_id, N'IsForeignKey') = 1)
BEGIN
ALTER TABLE `rh_pe`.`attributes` ADD CONSTRAINT `Const` FOREIGN KEY (`toid`) REFERENCES `rh_pe`.`pes`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
END
I haven't used the "if (not) exists" clausule for this but you can find a similar question here: If Foreign Key Not Exist Then Add Foreign Key Constraint(Or Drop a Foreign Key Constraint If Exist) without using Name?

How to add on delete cascade option in mysql?

I created two tables students and orders and I added a foreign key constraint to the order table but I forgot to add on delete cascade option to this table.
table STUDENTS:
CREATE TABLE STUDENTS (
ID varchar(50) NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
)
Table ORDERS
CREATE TABLE Orders
(O_Id int NOT NULL PRIMAY KEY,
Order_No int NOT NULL,ID varchar(50))
Add foreign key to "orders":
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID)
I tried this attempt :
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID) ON DELETE CASCADE
Thanks.
You have a typo in the Order table. You have PRIMAY where it should be PRIMARY.
After correcting this, I tried creating the tables and all statements worked fine, including the last one.