Removing DEFINER from MySQL < 5.7 dump files

Problem

You are using the dump files from MySQL (earlier version than 5.7 – that has -skip-definer option in mysqlpump), that outputs the DEFINER keyword in Functions and Views, which causes en error when trying to import in a different database that the users do not exist.

Solution

Use the following in your database dump to remove these keywords:

For FUNCTIONS

sed -i 's/DEFINER=\S*\sFUNCTION/FUNCTION/' mysql_dump

For VIEWS

sed -i '/50013\sDEFINER/d' mysql_dump

Adding missing timestamp column in MySQL table

Problem

You have a MySQL table that does not have any timestamp information (creating, updating) and you want to add a column that automatically adds a timestamp every time the record is created.

Solution

Add the column for timestamp that creates a timestamp every time a new record is created (INSERT) by using the following:

ALTER TABLE table_name ADD created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP

More information here

Find the table sizes in MySQL database

Problem

You would like to have a list with all the tables in an MySQL database, sorted with their sizes (actual table and indices).

Solution

You could run the following query to give you a list of all the tables sorted by size:

SELECT
  table_schema AS database_name,
  table_name AS table_name,
  round(((data_length + index_length) / 1024 / 1024), 2) AS size_mb
FROM information_schema.TABLES
ORDER BY (data_length + index_length) DESC;

Take from the answer here

Exporting SQL structure commands from MySQL Workbench

Problem

You would like to export the structure of some specific MySQL tables using MySQL Workbench (ie to create same tables in another database).

Solution

Go to the table name, right click, select ‘Copy To Clipboard’, then ‘Create Statement’ and finally go to a new file or editor and paste the command from the clipboard.
Just have in mind that you may need to delete the AUTO_INCREMENT from the SQL.
Solution was described here

Using a primary key not named id with Ecto and Mysql

Problem

You would like to use a legacy or a database you don’t have control over, with Phoenix, but the primary key of the table it is not named ‘id’.

** (Mariaex.Error) (1054): Unknown column 'p0.id' in 'field list'

Solution

You can define the primary key using the @primary_key as in:

 @primary_key {:id_other_name, :integer, []}
  schema "db_table" do
    field :description, :string
    ...
  end

Taken from here