MariaDB: Create, rename, and drop a table
In MariaDB, CREATE TABLE creates a table. RENAME TABLE changes its name while preserving the columns and rows. DROP TABLE removes the definition and the rows. This exercise uses one new practice database for the entire sequence.
Create a practice database and table
Connect with an SQL client to a MariaDB server with no production data. Your practice account needs permissions to create a database and table, insert and read rows, rename the table, and drop it. Run the blocks in order in the same session.
CREATE DATABASE table_practice
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE table_practice;
SELECT DATABASE();
Check that SELECT DATABASE() returns table_practice. If CREATE DATABASE says the name already exists, stop. Choose an unused name and substitute it consistently throughout the exercise. Existing databases and tables can stay in place.
CREATE TABLE products_demo (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created_at DATETIME NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO products_demo (id, name) VALUES (1, 'Notebook');
SELECT id, name, created_at FROM products_demo;
The first query returns id 1, the sample product name, and NULL for created_at. The primary key admits neither duplicates nor NULL, and name is a required string of up to 100 characters. Check for an existing table before creating it again. To specify the database regardless of the selected schema, qualify the name as table_practice.products_demo. MariaDB — CREATE TABLE
Inspect the table and its columns
SHOW TABLES;
DESCRIBE products_demo;
SHOW CREATE TABLE products_demo;
SHOW TABLES lists products_demo, and DESCRIBE shows id, name, and created_at. DESC is the short form. SHOW CREATE TABLE shows the key, engine, and charset as SQL. Its formatting can vary with server settings. Back up the rows separately. MariaDB — DESCRIBE MariaDB — SHOW CREATE TABLE
Check what IF NOT EXISTS does
CREATE TABLE IF NOT EXISTS products_demo (ignored_column INT);
SHOW WARNINGS;
DESCRIBE products_demo;
Try a different column definition with the same table name and IF NOT EXISTS. Under the default settings, the immediate SHOW WARNINGS returns a note that the table already exists. DESCRIBE still shows the original three columns. It does not show ignored_column.
To change columns, plan the relevant ALTER TABLE operation. CREATE OR REPLACE TABLE can discard the existing contents, so use it only when replacement is intended. Inspect the actual definition when you need to verify the schema.
Rename the table and restore its original name
Check that products_archive is free, then rename the table. The table list shows products_archive, and the same row is still there. For a real deployment, review queries, views, and jobs that use the old name, and check grants made directly on the table.
RENAME TABLE products_demo TO products_archive;
SHOW TABLES;
SELECT id, name FROM products_archive;
If the original name is still free, reverse the rename to restore it. RENAME TABLE requires ALTER and DROP on the old table and CREATE and INSERT on the new name. It may wait for a metadata lock held by another session. MariaDB — RENAME TABLE
RENAME TABLE products_archive TO products_demo;
SELECT id, name FROM products_demo;
Drop the practice table and understand recovery
Check SELECT DATABASE() before dropping anything. The drop statement names the practice database explicitly. If you chose a different practice name, update that qualifier too. For a table with data you intend to keep, verify the backup and restore procedure before removal.
DROP TABLE table_practice.products_demo;
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'table_practice' AND TABLE_NAME = 'products_demo';
The final query returns no rows when products_demo is gone. Repeating the create and insert examples recreates the sample row. IF EXISTS suppresses the error for an absent table, but an existing table named by mistake can still be dropped. If a foreign key blocks the operation, inspect the dependency first. MariaDB — DROP TABLE
The CREATE, RENAME, and DROP statements here affect ordinary tables and trigger an implicit commit. ROLLBACK cannot restore the dropped table. Verify a recovery method before doing this with real data. MariaDB — Statements that cause an implicit commit




