List and Drop Table
π Listing All Tables in a Databaseβ
A database schema contains metadata about all objects in the database, including tables, views, and indexes. If you want to see only the tables in your database, you can query the schema for objects of type table.
Suppose you have a database with the following tables:
- SQL Table
- SQL Code
- Output
| Table Name |
|--------------|
| past |
| events |
| crew |
SELECT name
FROM sqlite_schema
WHERE type = 'table';
| name |
|---|
| past |
| events |
| crew |
β Tip: Querying the schema is a reliable way to list all tables in your SQLite database.
π― Example: Listing Table Names Onlyβ
To list all tables in your database, you only need the name column from the schema. Use a SELECT statement to retrieve just the table names.
- Sample Data
- SQL Code
| name | entry | attendees |
|---|---|---|
| Pride Party | 10 | 79 |
| Classical Day | 15 | 76 |
| Wine tasting | 8 | 43 |
SELECT name
FROM sqlite_schema
WHERE type = 'table';
To get only the table names, select the
namecolumn fromsqlite_schemawheretypeis'table'.
ποΈ Deleting a Table from the Databaseβ
Once you have listed all tables, you may want to remove an entire table from your database. You can do this using the DROP TABLE statement. For example, to delete the past_events table:
- Sample Data
- SQL Code
- Output
| name | entry | attendees |
|---|---|---|
| Pride Party | 10 | 79 |
| Classical Day | 15 | 76 |
| Wine tasting | 8 | 43 |
DROP TABLE past_events;
SELECT name FROM sqlite_schema WHERE type = 'table';
| name |
|---|
| events |
| crew |
β οΈ Caution: Dropping a table will permanently delete the table and all its data. Make sure you have backups if needed.
β What You Have Learnedβ
In this module, you learned how to manage tables in your SQL database, including:
-
Listing Tables
Querying the database schema to view all tables present in your database. -
Dropping Tables
Using theDROP TABLEstatement to permanently remove tables and their data. -
Best Practices
Always verify before dropping tables, as this action cannot be undone, and ensure you have backups if needed.
These skills are essential for maintaining and organizing your database as your project requirements change.
π Quiz: Test Your Knowledgeβ
1. Now that we have the list of tables, let's delete the entire past_events table with the DROP TABLE query.β
Here is the original past_events table:
| name | entry | attendees |
|---|---|---|
| Pride Party | 10 | 79 |
| Classical Day | 15 | 76 |
| Wine tasting | 8 | 43 |
Answer
- Use the
DROP TABLEstatement to remove the entire table:
DROP TABLE past_events;
SELECT name FROM sqlite_schema WHERE type = 'table';
2. Which of the following statements is true for a schema?β
Q Tap the correct answer
- A schema contains only table information.
- A schema contains information about a database.
Answer
- A schema contains information about a database, including tables, views, indexes, and other objects.
3. Where is the list of tables stored in your database?β
Q Tap the correct answer
- In the data rows of a table
- In the schema
Answer
- In the schema , The list of tables is stored in the database schema, which contains metadata about all objects in the database.