there is a existing table A. suppose i want to add all or specific(column) values of an existing table A to table B using foreign key, how do i do it in MySQL?
and if there is any new insert or update in table A it should automatically insert into table B also..
To automatically update Table B from changes in Table A would require triggers that MySQL supports but phpMyAdmin does not. If instead you're looking to insert rows into Table B from Table A on an ad-hoc basis then that's simple
INSERT INTO TABLEA (COL1, COL2, COL3)
SELECT FROM TABLEB (COL1, COL2, COL3)
WHERE (SELECT COUNT(*) FROM TABLEA WHERE TABLEA.COL1 = TABLEB.COL1) = 0
The above SQL does a simple copy from TableB into TableA. The WHERE clause ensures only records which don't already exist are inserted.
You can use insert/update trigger for that.
Why should you want to do that?
Why don't you just reference the data in table A using the forign key in table B you mentioned?
insert into tableb(columns) select columns from tablea
Related
I have a table called leads with duplicate records
Leads:
*account_id
*campaign_id
I want to remove all the duplicate account_id where campaign_id equal to "51"
For example, if account_id = 1991 appears two times in the table then remove the one with campaign_id = "51" and keep the other one.
You could use a delete join:
DELETE t1
FROM yourTable t1
INNER JOIN yourTable t2
ON t2.account_id = t1.account_id AND
t2.campaign_id <> 51
WHERE
t1.campaign_id = 51;
There's no problem to delete from a table provided that:
You use the correct syntax.
You have done a backup of the table BEFORE you do any deleting.
However, I would suggest a different method:
Create a new table based on the existing table:
CREATE TABLE mytable_new LIKE mytable;
Add unique constraint (or PRIMARY KEY) on column(s) you don't want to have duplicates:
ALTER TABLE mytable_new ADD UNIQUE(column1,[column2]);
Note: if you want to identify a combination of two (or more) columns as unique, place all the column names in the UNIQUE() separated by comma. Maybe in your case, the constraint would be UNIQUE(account_id, campaign_id).
Insert data from original table to new table:
INSERT IGNORE INTO mytable_new SELECT * FROM mytable;
Note: the IGNORE will insert only non-duplicate values that match with the UNIQUE() constraint. If you have an app that runs a MySQL INSERT query to the table, you have to update the query by adding IGNORE.
Check data consistency and once you're satisfied, rename both tables:
RENAME TABLE mytable TO mytable_old;
RENAME TABLE mytable_new TO mytable;
The best thing about this is that in case that if you see anything wrong with the new table, you still have the original table.
Changing the name of the tables only take less than a second, the probable issue here is that it might take a while to do the INSERT IGNORE if you have a large data.
Demo fiddle
DELETE t1
FROM yourTable t1
INNER JOIN yourTable t2
ON t2.account_id = t1.account_id AND
t2.campaign_id <> 51
WHERE
t1.campaign_id = 51;
Trying to remove duplicate based on three column like query will not find duplicate based on single column it will concatenate three column then remove duplicate rows based on the merge column.
I would appreciate if someone can share an easiest way of achieving this. I know this is not an appropraiote way but tried and its not working
Select concat(col1, col2, col3,) as newCol distinct newcCol from Table2
I know how to remove the table duplicate based on multiple columns using Excel VBA but do not know how to achieve this using mysql
Sub DelDupl()
Range("A1").CurrentRegion.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
End Sub
Table name is Table2 in Mysql
enter image description here
Sample Data
CREATE TABLE Table2(
col1 INT,
col2 varchar(10),
col3 INT,
col4 varchar(10),
col5 varchar(10),
col6 varchar(10),
col7 varchar(10));
INSERT INTO Table2 (col1,col2,col3,col4,col5,col6,col7)
VALUES ('1','A','123456','data1','data1','data1','data1'),
('2','B','78910','data2','data2','data2','data2'),
('3','C','45698','data3','data3','data3','data3'),
('1','A','123456','data1','data1','data1','data1'),
('2','B','78910','data2','data2','data2','data2'),
('3','C','45698','data3','data3','data3','data3'),
('4','D','85969','data5','data5','data5','data5');
The problem is there is no way to establish which to keep so I suggest you set up a staging table with a compound key, load it , truncate your existing table and load it from the staging table
How about this:
SELECT *
FROM table2 AS t
GROUP BY t.col1, t.col2, t.col3;
GROUP BY is normally used for aggregating functions(count, sum, max, etc), but will do the job for your purpose.
Edit:
Ok, since you actually need to delete duplicates, thats a bit more complicated, but it's possible. First we need to somehow discriminate duplicate rows, so we will temporary add a primary key. Then execute delete statement, while joining the table on itself to find duplicated rows. And lastly drop the primary key column we added.
add primary key column, we need some discrimination for duplicates
ALTER TABLE `table2` ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY;
join table on itself to get duplicates and delete them
DELETE t1
FROM `table2` t1
INNER JOIN `table2` t2
ON t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3
WHERE t1.id < t2.id;
drop the primary key column
ALTER TABLE `table2` DROP COLUMN id;
As a note, the join is done on the columns which define duplication(col1, col2, col3) in your case. You can execute all 3 queries at once, just be sure you really need this data gone.
I am trying to copy data from one table to another with an additional value.
from table1 I need to copy two column(field) values such as ERNO and ENAME to table2.
Also need to update or add ECNO(column or field).
Note: I am using MySQL.
Not only one field ECNO, also need to add more field while copying data from one table to another.
ECNO field is int datatype.
Follwing query I have used for that. But it doesn't work
INSERT INTO TABLE2 (ECNO, ERNO, ENAME) values (1, select ERNO, ENAME from TABLE1)
Any Suggestion how to do this in proper way.
Use INSERT INTO...SELECT syntax:
INSERT INTO TABLE2 (ECNO, ERNO, ENAME)
SELECT 1, ERNO, ENAME
FROM TABLE1
Use like this:
INSERT INTO tbl2 (col1, col2.....)
SELECT tbl1.col1,tbl1.col2
FROM tbl
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
I usually copy data into tabelB from table A daily by the following query:
REPLACE INTO tableB (id,col1,col2,col3) SELECT id,col1,col2,col3 FROM tableA WHERE date='somedate'
Here REPLACE is used as if the script is being run double for a particular date by mistake, it will not copy the tableA two times into tableB. But problem is, tableB has two unique key index (id and col1). So if REPLACE is executed more than once, index is deleted and recreated by this REPLACE command. To avoid this thing, I want to use:
INSERT INTO tableB (id,col1,col2,col3) VALUES (val1,val2,val3,val4) ON DUPLICATE KEY UPDATE col2=VALUES(col2),col3=VALUES(col3)
But as I am copying the tableA here, I cant use the above INSERT........ON DUPLICATE KEY UPDATE query, I have to use SELECT command to copy data from tableA.
So how to modify INSERT INTO ........... ON DUPLICATE KEY UPDATE query, so that it can copy the tableA and at the same time if execute the script more than once, it just updates the data.
Regards
Try this INSERT query -
INSERT INTO tableB (id, col1, col2, col3)
SELECT id, col1, col2, col3 FROM tableA WHERE date = 'somedate'
ON DUPLICATE KEY UPDATE col2 = VALUES(col2), col3 = VALUES(col3);