Export schema/ data out from mysql
To export schema and/or data, you can use mysqldump command:
mysqldump -u [username] -p[password] -d [schema_name] > [filename].sql
- -d means no data (just gives me the schema).
- -B is needed for multiple schema output
- -h (hostname)
Export data out from postgresql
However, if you want to export sql result set to csv in postgresql, you can consider to use COPY functionality.
COPY ( select statement ) TO STDOUT WITH CSV
COPY stock FROM ‘mydir/Stock.csv’;
Run sql script using mysql command
To run the scripts as input, we can use the following command:
mysql [schema_name] -u [username] -p[password] < [filename].sql
SQL Tips
There are times we want to put logic in SQL but not writing store procedure. Here are some of using functions that may get you there:
- Conditional statement – CASE WHEN xxx THEN abc WHEN yyy THEN bbc …ELSE ccc END
UPDATE Account SET Sales_Location__c =
CASE WHEN Sales_Country__c != ” THEN Sales_Country__c WHEN Country__c != ” THEN Country__c
ELSE ‘–’ END
- COALESCE (input1, input2,….) – This function takes in as many parameter as you want and return you the first non-NULL parameter. Suppose we have a table A having 3 columns FullName, CompleteName and DisplayName. Any of these columns can contain null values. Now we want to select the DisplayName from this table, but if it is null, then return FullName, if that is also null then return CompleteName. We can easily perform the same in one select statement as: (COALESCE vs ISNULL)
SELECT COALESCE(DisplayName, FullName, CompleteName) From A
ETL
In mysql, you can export a table from db1 and import it to db2 remotely. For example, in db2 host, you can issue the following command:
/usr/bin/mysqldump – -force – -compress – -opt -u [username] -p[password] -h [hostname] db1 [table] | /usr/bin/mysql -u [username] -p[password] -D db2
0 Comments.