Learning of SQL Day 5

 

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:

  1. Creating a Table:

    • The CREATE TABLE statement is used to create a new table with specified columns and data types.

sql
-- 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
);
  1. Checking Existing Tables:

    • Use the SHOW TABLES statement to list all tables in the current database.

sql
-- Show all tables in the current database
SHOW TABLES;
  1. Dropping a Table:

    • The DROP TABLE statement is used to delete a table from the database.

sql
-- Drop the table named Courses
DROP TABLE Courses;

Practice Exercise:

  1. Create a table named Books with the following columns:

    • BookID (INT, Primary Key)

    • Title (VARCHAR)

    • Author (VARCHAR)

    • PublishedYear (INT)

  2. Verify the table creation by listing all tables.

  3. Drop the Books table.

  4. 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.

Post a Comment

0 Comments