Day 5: Creating and Dropping Tables
Introduction:
Tables are fundamental building blocks in SQL databases. Today, we'll focus on creating and dropping tables.
Key Concepts:
Table Creation: Setting up a new table to store data.
Table Deletion: Removing an existing table from the database.
SQL Commands:
Creating a Table:
The
CREATE TABLEstatement is used to create a new table with specified columns and data types.
-- Create a table named Courses with columns for CourseID, CourseName, and Credits
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(255) NOT NULL,
Credits INT
);
Checking Existing Tables:
Use the
SHOW TABLESstatement to list all tables in the current database.
-- Show all tables in the current database
SHOW TABLES;
Dropping a Table:
The
DROP TABLEstatement is used to delete a table from the database.
-- Drop the table named Courses
DROP TABLE Courses;
Practice Exercise:
Create a table named Books with the following columns:
BookID(INT, Primary Key)Title(VARCHAR)Author(VARCHAR)PublishedYear(INT)
Verify the table creation by listing all tables.
Drop the Books table.
Verify the table deletion by listing all tables again.
Important Tips:
Use meaningful column names to make your tables self-explanatory.
Always ensure you have backups before dropping tables to avoid data loss.
Mastering the creation and management of tables will significantly enhance your database skills.
0 Comments