DBMS2
DBMS2
Theory:
Data Definition Language (DDL
What is DDL?
DDL, or Data Definition Language, is a fundamental subset of SQL used to manage the structure
of database objects like tables, views, indexes, constraints, and schemas. Unlike DML (Data
Manipulation Language), which focuses on adding, modifying, and retrieving data, DDL
commands deal exclusively with defining the blueprint or framework for data storage and
organization.
Key DDL Operations:
CREATE: Used to establish new database objects (e.g., CREATE DATABASE,
CREATE TABLE).
ALTER: Employed to modify the structure of existing objects (e.g., ALTER TABLE,
ALTER VIEW).
RENAME: Allows changing the names of existing objects (e.g., RENAME TABLE,
RENAME VIEW).
DROP: Removes objects permanently from the database (e.g., DROP TABLE, DROP
VIEW).
Creating a New Database
Before working with DDL commands for specific objects, you'll typically need a database to
store them. This database serves as a container for all database objects, including tables, views,
stored procedures, and more.
CREATE DATABASE database_name;
DDL Operations on Tables
Tables are the primary data storage units in a relational database. DDL commands are often used
to manage their structure.
1. Creating a Table:
The CREATE TABLE command is used to define a new table, specifying its column names and
corresponding data types. Here's an example:
CREATE TABLE Students (
Enroll_No VARCHAR(10) PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Branch VARCHAR(20),
Marks INTEGER
);
2. Altering a Table:
The ALTER TABLE command allows you to modify the structure of an existing table. It can be
used to:
Add new columns
Modify existing column data types
Change constraints (e.g., ADD, MODIFY, DROP)
ALTER TABLE table_name
ADD column_name datatype,
MODIFY column_name datatype;
3. Renaming a Table:
The RENAME TABLE command is used to change the name of an existing table.
RENAME TABLE old_table_name TO new_table_name;
4. Dropping a Table:
The DROP TABLE command removes a table permanently from the database. This is an
irreversible action, so use it with caution.
DROP TABLE table_name;
Code:
Conclusion:
In this series of DDL operations, we:
1. Created a Table: The Employees table was established to hold key employee details,
ensuring the structure supports various data types.
2. Altered the Table: We added an Email column, demonstrating how to modify an existing
table to include additional relevant information.
3. Renamed the Table: The table was renamed to Staff, which may better reflect its
purpose within the organization.
4. Dropped the Table: Finally, the Staff table was removed from the database, showcasing
the ability to clean up and manage database structures effectively.