Lazy people works hard
Keep in mind that this is my first post in english, please if you see something written bad, let me know leaving a comment. well here we go.
some days ago I was very bored and tired at my job enough to do nothing that day, however I remembered that I would had to do one task, I needed to compare two databases to know what tables there was into the first one but not in the other one (each database had lots of tables), I’m a very very lazy person when I have to do humdrum tasks and that was definitely. I wouldn’t had wanted to acomplish that task reviewing table by table. I started to think how to do my task in a diferent way, suddenly an idea through across my mind, I would be able to list the name of all tables from each database then I could put all those names into a table containing only the names and making a SQL statement to group the names I would get group’s name, groups having only one element would be the result that I had been searching, but it all still was lot of work for my laziness, therefore I had to find out another way to solve my problem otherwise I would end doing it by hand and thought to myself “never mind, I never going to do that by hand”. It was the time when I remembered that Unix systems has lots of tools to manage text files maybe I could use something tools. Suddenly I remembered a command line tool called diff, the diff command compare two text files (actually it can be binary files) and show their differences, unfortunatly diff doesn’t do the work exactly as easy as I would like, by example having two files called db_one.txt and db_two.txt whit the name of the tables:
db_one.txt
table1 table2 table3 table4 table7 table9 table10
db_two.txt
table1 table2 table3 table4 table5 table6 table7 table8
Using diff command we would obtain this result:
[espartano@ ~/pruebas/diff]$ diff db_one.txt db_two.txt 4a5,6 table5 table6 6,7c8 table9 table10 --- table8 [espartano@ ~/pruebas/diff]$
but it doesn’t enough good for me, I would have liked a command more straightforward than diff (maybe with patch but I not remembered it in those times), well I had to continue searching a command that just make my work in a easy way, then was when I found the blessed command comm, yes, comm going to solve all my problem because as man page says:
utility comm reads file1 and file2, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files.
having the same two files from the last time:
[espartano@ ~/pruebas/diff]$ comm db_one.txt db_two.txt table1 table2 table3 table4 table5 table6 table7 table8 table9 table10 [espartano@ ~/pruebas/diff]$
voiala this is the result that I was finding. At those point I wasn’t tired nor bored
because I learned a couple new things, but I think that I worked more than what was do it by hand.
Only if you insist, this is the way which I would have done with SQL:
[espartano@ ~/pruebas/diff]$ cat db_two.txt >> db_one.txt [espartano@ ~/pruebas/diff]$ cat db_one.txt table1 table2 table3 table4 table7 table9 table10 table1 table2 table3 table4 table5 table6 table7 table8 [espartano@ ~/pruebas/diff]$ cat db_one.txt | sed -e s/^/"insert into tablas (nombre) values ('"/g > insert.sql [espartano@ ~/pruebas/diff]$ cat insert.sql insert into tablas (nombre) values ('table1 insert into tablas (nombre) values ('table2 insert into tablas (nombre) values ('table3 insert into tablas (nombre) values ('table4 insert into tablas (nombre) values ('table7 insert into tablas (nombre) values ('table9 insert into tablas (nombre) values ('table10 insert into tablas (nombre) values (' insert into tablas (nombre) values ('table1 insert into tablas (nombre) values ('table2 insert into tablas (nombre) values ('table3 insert into tablas (nombre) values ('table4 insert into tablas (nombre) values ('table5 insert into tablas (nombre) values ('table6 insert into tablas (nombre) values ('table7 insert into tablas (nombre) values ('table8 insert into tablas (nombre) values (' [espartano@ ~/pruebas/diff]$ cat insert.sql | sed -e s/$/"');"/g > completed_inserts.sql [espartano@ ~/pruebas/diff]$ cat completed_inserts.sql insert into tablas (nombre) values ('table1'); insert into tablas (nombre) values ('table2'); insert into tablas (nombre) values ('table3'); insert into tablas (nombre) values ('table4'); insert into tablas (nombre) values ('table7'); insert into tablas (nombre) values ('table9'); insert into tablas (nombre) values ('table10'); insert into tablas (nombre) values (''); insert into tablas (nombre) values ('table1'); insert into tablas (nombre) values ('table2'); insert into tablas (nombre) values ('table3'); insert into tablas (nombre) values ('table4'); insert into tablas (nombre) values ('table5'); insert into tablas (nombre) values ('table6'); insert into tablas (nombre) values ('table7'); insert into tablas (nombre) values ('table8'); insert into tablas (nombre) values ('');
mysql>; CREATE DATABASE prueba; Query OK, 1 row affected (0.13 sec) mysql> USE prueba; DATABASE changed mysql> CREATE TABLE tablas (nombre varchar(50)); Query OK, 0 rows affected (0.42 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table1'); Query OK, 1 row affected (0.11 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table2'); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table3'); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table4'); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table7'); Query OK, 1 row affected (0.02 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table9'); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table10') Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO tablas (nombre) VALUES (''); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table1'); Query OK, 1 row affected (0.06 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table2'); Query OK, 1 row affected (0.02 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table3'); Query OK, 1 row affected (0.02 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table4'); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table5'); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table6'); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table7'); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO tablas (nombre) VALUES ('table8'); Query OK, 1 row affected (0.02 sec) mysql> INSERT INTO tablas (nombre) VALUES (''); Query OK, 1 row affected (0.03 sec)
then
mysql> SELECT nombre FROM tablas GROUP BY nombre HAVING count(nombre) = 1; +---------+ | nombre | +---------+ | table10 | | table5 | | table6 | | table8 | | table9 | +---------+ 5 rows IN SET (0.02 sec)