In SQL, managing databases and tables is a fundamental skill. This tutorial will cover how to create, modify, and delete databases and tables.
SQL Database and Table Management Tutorial
1. Creating a Database
To create a new database, use the CREATE DATABASE
statement. Here’s an example:
CREATE DATABASE SchoolDB;
This command creates a new database named SchoolDB.
2. Using a Database
To start using a specific database, use the USE
statement:
USE SchoolDB;
This command selects the SchoolDB database for subsequent operations.
3. Creating a Table
To create a table, use the CREATE TABLE
statement. Here’s an example of creating a Students table:
CREATE TABLE Students (
StudentID INT NOT NULL PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
DateOfBirth DATE
);
This command creates a Students table with various fields, including StudentID
as the primary key.
4. Modifying a Table
You can modify an existing table using the ALTER TABLE
statement. For example, to add a new column:
ALTER TABLE Students
ADD Email VARCHAR(100);
This command adds a new column Email
to the Students table.
5. Deleting a Table
To delete a table from a database, use the DROP TABLE
statement. Here’s an example:
DROP TABLE Students;
This command permanently deletes the Students table from the database.
6. Deleting a Database
To delete an entire database, use the DROP DATABASE
statement:
DROP DATABASE SchoolDB;
This command permanently deletes the SchoolDB database along with all its tables and data.
7. Conclusion
SQL database and table management is crucial for maintaining and organizing data effectively. Understanding how to create, modify, and delete databases and tables is essential for database administration.