This is sample query to delete record from multiple tables.
Specify all joined tables to get the list but can specify records in which tables to be deleted.
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
https://dev.mysql.com/doc/refman/8.0/en/delete.html (refer section – Multi-Table Deletes)
To secure the data by encryption at data at database level. This will be full done by database application and won’t affect the application level.
Almost all popular RDBMS provide this feature but maybe with enterprise edition.
- Azure SQL
Example: select * from users where (find_in_set (5, access_string) > 0);
will return position of the key if found in string. else will return 0 for not found or NULL if field is NULL
To update a table with a condition within other table (to join table), you can use this statement
UPDATE table1 a, table2 b
SET a.field6 = value
WHERE a.field1 = b.field1 AND b.field2 = value2
This SQL statement to be used to insert data into a table from another different table.
INSERT INTO table1 (field1)
SELECT table2.field2 FROM table2
WHERE table2.field2 > 100;
Be careful with ambiguous fields. So put the table name in front of the field name.
Also be careful with duplicate data inserted to the table.
You will need this SQL statement to list all duplicate contents in a table
COUNT(email) AS NumOccurrences
GROUP BY email
HAVING ( COUNT(email) > 1 )
If you want to list all duplicate records
SELECT t1.* FROM
( SELECT documentno, COUNT(1) AS dup
GROUP BY documentno
HAVING dup > 1 ) AS t2
WHERE t1.documentno = t2.documentno;
Then how to remove the duplicate records?
1. Create new table and insert clean records (without duplicate)
CREATE TABLE new_table AS
SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];
Note: Be careful, this will create a table without primary key, auto-increment etc. So make sure you update your table after run this statement
2. Drop old table (with duplicates)
I’ve been doing data cleansing of one of my project. Then there was a need to find some abbreviation to be replaced by the real words.
Here I did with mysql statement.
update property set town = replace(town,’tmn’,’taman’)
update TABLENAME set FIELDNAME = replace (FIELDNAME, searchstr, replacestr)