Dropping Tables and Altering Structures

Tutorial 4 of 5

1. Introduction

In this tutorial, we are going to learn how to drop tables and alter structures in SQL. These are essential tasks that help in maintaining the efficiency and organization of your database.

By the end of this tutorial, you will know how to:
- Drop tables from a database.
- Alter the structure of a database table.

Prerequisites

A basic understanding of SQL, databases, and how to execute SQL queries.

2. Step-by-Step Guide

Dropping Tables

Dropping a table means to delete the table along with all the data it contains. Be careful when dropping tables because the data lost cannot be recovered.

Syntax:

DROP TABLE table_name;

Altering Table Structures

Altering a table structure means making changes to the structure of a table like adding a column, deleting a column, changing the data type of a column, etc.

Syntax to add a column:

ALTER TABLE table_name
ADD column_name datatype;

Syntax to delete a column:

ALTER TABLE table_name
DROP COLUMN column_name;

Syntax to modify data type of a column:

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

3. Code Examples

Example 1: Dropping a Table

Let's consider a table named 'Students'. To drop this table:

DROP TABLE Students;

This will delete the 'Students' table from the database.

Example 2: Adding a Column

Let's say we want to add a new column named 'Email' to the 'Students' table:

ALTER TABLE Students
ADD Email varchar(255);

This will add a new column 'Email' of type 'varchar' with a maximum length of 255 characters.

Example 3: Dropping a Column

To remove the 'Email' column that we just added:

ALTER TABLE Students
DROP COLUMN Email;

This will remove the 'Email' column from the 'Students' table.

Example 4: Modifying a Column

Let's change the datatype of a column named 'Student_ID' from 'int' to 'varchar':

ALTER TABLE Students
MODIFY COLUMN Student_ID varchar(100);

This will change the data type of the 'Student_ID' column to 'varchar' with a maximum length of 100 characters.

4. Summary

In this tutorial, we learned how to drop a table and alter a table structure in SQL, including how to add, drop and modify columns in a table.

Next steps for learning could include understanding how to rename tables and columns, and how to add and drop primary and foreign keys.

Additional resources:
- SQL documentation
- W3Schools SQL tutorial

5. Practice Exercises

  1. Create a table named 'Employees', then add a column named 'Department'. Finally, drop the 'Department' column.
  2. Create a table named 'Orders'. Add a column named 'Order_Date' of type 'varchar'. Then, modify the 'Order_Date' column to type 'date'.

Solutions:
1. sql CREATE TABLE Employees (Employee_ID int, Employee_Name varchar(255)); ALTER TABLE Employees ADD Department varchar(255); ALTER TABLE Employees DROP COLUMN Department;
2. sql CREATE TABLE Orders (Order_ID int, Product_Name varchar(255)); ALTER TABLE Orders ADD Order_Date varchar(255); ALTER TABLE Orders MODIFY COLUMN Order_Date date;

Remember to practice these operations until you're comfortable with them. Happy coding!