Category: MySQL

mysqldump: how to exclude or include tables

mysqldump is a command-line tool used for creating database backups in MySQL. By default, mysqldump includes all tables of the specified database when creating the dump. In some cases, it is useful to exclude some of the tables or even include only some of them. For me, this helped to exclude one of the biggest tables to reduce the backup execution time and file size.

Exclude a single table

To exclude a single table, the argument --ignore-table followed by the table name is used:

mysqldump my_database --ignore-table=my_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of all tables in the database my_database except my_table.

Exclude multiple tables

To exclude multiple tables, simply add the argument --ignore-table for all the tables you want to exclude:

mysqldump my_database --ignore-table=my_table --ignore-table=another_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of all tables in the database my_database except my_table and another_table.

Include a single table

To include only a single table into the dump, add the name of the table to the command:

mysqldump my_database my_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of the table my_table from the database my_database.

Include multiple tables

To include multiple tables into the dump, add the names of the tables to the command:

mysqldump my_database my_table another_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of the tables my_table and another_table from the database my_database.

A complete documentation of mysqldump is available at mysql.com.

Photo by benjamin lehman on Unsplash.

 

MySQL error: Cannot truncate a table referenced in a foreign key constraint

By default, you cannot TRUNCATE a table that has foreign key constraints applied on it. This is to keep the data consistent over multiple tables that are linked by constraints.

Solution 1

The fastest way to clean the table is to disable FOREIGN_KEY_CHECKS:

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE `table1`;
TRUNCATE `table2`;
SET FOREIGN_KEY_CHECKS = 1;

Keep in mind, that disabling FOREIGN_KEY_CHECKS might result in inconsistent data with foreign key values that do not exists.

Solution 2

To remove all rows, you can also go the long way:

  • Remove all foreign key constraints
  • Run TRUNCATE on the table(s)
  • Manually delete all rows without reference
  • Recreate the foreign key constraints

Photo by Jan Antonin Kolar on Unsplash

 

MySQL: Tabellen in andere Datenbank kopieren

Mit nachstehenden MySQL-Befehlen lässt sich eine Tabelle recht einfach in eine andere Datenbank kopieren. In diesem Beispiel werden die Daten von db1 in db2 kopiert.

Zunächst muss die neue Tabelle (mit gleicher Struktur) erstellt werden:

CREATE TABLE db2.newTable LIKE db1.oldTable

Danach können die Daten in die neue Tabelle kopiert werden:

ALTER TABLE db2.newTable DISABLE KEYS
INSERT INTO db2.newTable SELECT * FROM db1.oldTable
ALTER TABLE db2.newTable ENABLE KEYS

MySQL-Variablen für Spaltennamen (und andere Identifier) verwenden

Variablen lassen sich in MySQL hervorragend für Strings, Zahlenwerte oder auch binäre Daten verwenden. Möchte man diese Variablen jedoch zum Adressieren von Spalten, Tabellen oder Datenbanken verwenden, dann erfordert dies ein etwas anderes Vorgehen. Dieser Artikel gibt eine kurze Zusammenfassung, wie man MySQL-Variablen in Abfragen einsetzt.

MySQL: INSERT … ON DUPLICATE KEY UPDATE …

MySQL bietet die Möglichkeit, beim Ausführen eines INSERT INTO ein UPDATE auszuführen, falls es beim Einfügen des Datensatzes zu einem dublicate key kommt. Dies lässt sich mit folgender Eingabe erzielen:

INSERT INTO <table> (<field1>, <field2>) VALUES (<value1>, <value2>)
ON DUPLICATE KEY UPDATE <field2> = <field2> + 1;

Sollte es bei der Ausführung der INSERT-Anweisung zu einem doppelten Eintrag in einer als PRIMARY KEY oder UNIQUE KEY definierten Spalte kommen, dann wendet MySQL die UPDATE-Anweisung auf den Datensatz an, der zu dem doppelten Inhalten führt.

Spalte einer MySQL-Tabelle verschieben

Die Spalte einer MySQL-Tabelle lässt sich folgendermaßen an eine andere Position verschieben:

ALTER TABLE <tabelle> MODIFY <spaltenname> tinyint(1) DEFAULT '0'
AFTER <spaltenname_vor_neuer_position>

Wichtig ist hierbei auch die Angabe der Spaltendefinitionen. Eingaben wie “tinyint(1) DEFAULT ‘0’” müssen also den Definitionen der Spalte entsprechen und beim Verschieben mit angegeben werden.