Delete data from database (MariaDB) with packageJS
As a packageJS developer, there would be times where you may need to delete data from a database and might need guidance hence the reason for the blog. Here's how to delete data from the database.
To clear or delete data from your MariaDB database, you can use the DELETE
statement to remove specific rows or the TRUNCATE
statement to remove all rows from a table. Here's how to use the DELETE statement.
Steps:
Make sure you're executing commands within the mariaDB terminal or mysql terminal interface first. To do this:
-> Run this command in the terminal
mariadb -u your_username -p
Replace
your_username
with your actual MariaDB username.Enter your MariaDB password when prompted.
Once you are in the MariaDB command-line interface, run this command
SHOW DATABASES;
to see your database information.Choose the database you want to work with and use the
USE
statement to select itUSE your_database_name;
. Replaceyour_database_name
with the actual name of the database you want to work with. The database name is mostly a 24 character with letters and numbers.Once you have selected a database, you can run queries on the tables within that database like so
SHOW TABLES;
. This query would show you the tables in your database.To be sure of the table you're deleting, I'd advise to see the content of the tables that way you aren't deleting the wrong table or row in the table. Run this query to do that:
SELECT * FROM d0cb23f79a6b9674
.Replace
d0cb23f79a6b9674
with the actual name of the table you're interested in. This query will retrieve all rows and columns from the specified table and you should have something like this in the terminal ๐๐พNOTE: In this example:
SELECT
: Specifies the columns to be retrieved.FROM
: Specifies the table (your_table
) from which to retrieve data.WHERE
: Filters the results based on specified conditions (condition1
andcondition2
).
Remember, the order of the clauses is important.
Now that you've seen the content of the table, I'd like to delete a specific row in the table where the
service_message
contains "we take care of leaky air conditioning" and theservice_title
is "Air conditioning," you can use theDELETE
statement with aWHERE
clause. Here's how you can do it:DELETE FROM d0cb23f79a6b9674 WHERE `service_message.=` LIKE '%we take care of leaky air conditioning%' AND `service_title.=` LIKE '%Air conditioning%';
The use of
%
withLIKE
in SQL is a wildcard character. It allows for a more flexible search and using this pattern(%
) will check for any string as "we take care of leaky air conditioning" at any position within theservice_message
column and do the needful.To be sure the data was deleted successfully, you can run the query below and you should see "empty set".
SELECT * FROM d0cb23f79a6b9674;
Please make sure to use backticks around column names with special characters.
Et Voila! The query executed successfully, and no rows were affected ๐. This is how to delete a table, rows and column in a database. This process can be replicated for any deletion process you might want to do in the future.
Until next time, ciao!!!