Filtering and Deleting in MariaDB: A Guide to the DELETE ... WHERE Clause

2024-07-27

For example, let's say you have a table named customers with columns for id, name, and city. You want to delete all customers from the city "New York". Here's the DELETE statement for that:

DELETE FROM customers
WHERE city = "New York";

In this example:

  • DELETE FROM customers specifies the table to delete from.
  • WHERE city = "New York" is the condition that filters the rows. It will only delete rows where the city column value is "New York".

Important Considerations:

  • Be cautious when deleting rows, especially without a specific WHERE clause. Deleting all rows from a table is faster achieved using TRUNCATE TABLE (assuming no constraints or locks prevent it).
  • While you can reference the same table in the DELETE and WHERE clause, MariaDB doesn't support using a subquery that selects from the same table within the DELETE statement.



DELETE FROM customers
WHERE city = "New York";

This code deletes all customers from the "customers" table where the "city" column value is "New York".

Deleting duplicate entries:

DELETE c1
FROM customers c1
INNER JOIN customers c2 ON c1.id > c2.id  -- This ensures we only delete the higher ID duplicate
WHERE c1.name = c2.name AND c1.email = c2.email;

This code deletes duplicate entries from the "customers" table based on matching "name" and "email" columns. It uses a self-join to compare rows within the same table. The id comparison ensures we only delete the record with the higher ID (assuming IDs are unique and increasing).

DELETE FROM products
WHERE price < 10 OR price > 50;

This code deletes products from the "products" table where the "price" column value is less than 10 or greater than 50.




  • This is a faster way to remove all rows from a table, especially for large datasets.
  • However, unlike DELETE, TRUNCATE TABLE doesn't trigger database triggers or logs (unless configured otherwise).
  • Use this with caution as it's not recoverable.

Temporary Tables and DELETE...SELECT:

  • Create a temporary table to hold the rows you want to keep.
  • Use a SELECT statement to filter the desired rows from the original table into the temporary table.
  • Truncate the original table and then insert all rows from the temporary table back into the original table.

Stored Procedures:

  • For complex deletion logic or repetitive tasks, you can create a stored procedure that encapsulates the deletion logic.
  • This can improve code reusability and maintainability.

Here's a brief comparison:

MethodUse CaseAdvantagesDisadvantages
DELETE ... WHEREDeleting specific rows based on conditionsSimple, efficient for targeted deletionsCan be slow for large datasets, potential for errors
TRUNCATE TABLEQuickly removing all rows from a tableFastest method, good for large datasetsNot recoverable, doesn't trigger triggers/logs
Temporary Tables/DELETE...SELECTComplex row filtering or keeping specific rowsMore flexible filtering, avoids data lossMore complex to write, involves multiple statements
Stored ProceduresEncapsulating complex deletion logic, reusabilityOrganized code, reusable logicRequires additional development effort

mariadb



Understanding "Grant All Privileges on Database" in MySQL/MariaDB

In simple terms, "granting all privileges on a database" in MySQL or MariaDB means giving a user full control over that specific database...


MAMP with MariaDB: Configuration Options

Stands for Macintosh Apache MySQL PHP.It's a local development environment that bundles Apache web server, MySQL database server...


MySQL 5 vs 6 vs MariaDB: Choosing the Right Database Server

The original open-source relational database management system (RDBMS).Widely used and considered the industry standard...


Beyond Backups: Alternative Approaches to MySQL to MariaDB Migration

There are two main approaches depending on your comfort level:Complete Uninstall/Install:Stop the MySQL server. Uninstall MySQL...


MySQL vs MariaDB vs Percona Server vs Drizzle: Choosing the Right Database

Here's an analogy: Imagine MySQL is a popular recipe for a cake.MariaDB would be someone taking that recipe and making a very similar cake...



mariadb

Understanding and Resolving MySQL Error 1153: Example Codes

Common Causes:Large Data Sets: When dealing with large datasets, such as importing a massive CSV file or executing complex queries involving many rows or columns


Speed Up Your Inserts: Multi-Row INSERT vs. Multiple Single INSERTs in MySQL/MariaDB

Reduced Overhead: Sending a single INSERT statement with multiple rows requires less network traffic compared to sending many individual INSERT statements


Example Codes for SELECT * INTO OUTFILE LOCAL

Functionality:This statement exports the results of a MySQL query to a plain text file on the server that's running the MySQL database


MariaDB for Commercial Use: Understanding Licensing and Support Options

Commercial License: Typically refers to a license where you pay a fee to use software for commercial purposes (selling a product that uses the software)


Fixing 'MariaDB Engine Won't Start' Error on Windows

MariaDB: An open-source relational database management system similar to MySQL.Windows: The operating system where MariaDB is installed