0% found this document useful (0 votes)
4 views23 pages

Lecture 10 - SQL - Data Definition Language

This document provides an overview of Data Definition Language (DDL) in SQL, covering its importance, key commands, and syntax for creating, modifying, and managing database structures. It emphasizes the role of DDL in ensuring data integrity, defining constraints, and facilitating effective database management. Additionally, it includes examples of DDL commands such as CREATE, ALTER, DROP, and TRUNCATE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Lecture 10 - SQL - Data Definition Language

This document provides an overview of Data Definition Language (DDL) in SQL, covering its importance, key commands, and syntax for creating, modifying, and managing database structures. It emphasizes the role of DDL in ensuring data integrity, defining constraints, and facilitating effective database management. Additionally, it includes examples of DDL commands such as CREATE, ALTER, DROP, and TRUNCATE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Database Systems

School of Systems & Technology -


SST
Learning Objectives

In this chapter you will learn:

Understand DDL Concepts: Gain a foundational understanding of Data Definition Language and its role in
database management.

Learn Key DDL Commands: Study the syntax and usage of essential DDL commands such as CREATE, ALTER,
DROP, and TRUNCATE.

Database Schema Creation: Develop the skills to create and define database schemas, including tables,
indexes, and constraints.

Modify Database Structures: Learn how to modify existing database objects using ALTER commands to adapt to
changing requirements.

Ensure Data Integrity: Understand how to enforce data integrity through primary keys, foreign keys, and other
constraints.

Manage Database Objects: Acquire techniques for managing and organizing database objects effectively,
including renaming and commenting.

2
What is SQL-Data Definition Language?

 Data Definition Language (DDL) is a subset of SQL (Structured Query Language)


used to define and manage database structures.

 It consists of commands that create, modify, and remove database objects


such as tables, indexes, and schemas.

 DDL is essential for setting up and organizing the data in a database, laying
the foundation for data manipulation and querying.

3
Importance of DDL

 Database Schema Creation: DDL commands define the database schema, which is the
skeleton structure that represents the logical view of the entire database.

 Data Integrity and Constraints: DDL allows the definition of constraints (such as primary
keys, foreign keys, and unique constraints) that ensure data integrity and enforce business rules.

 Database Management: DDL provides the tools necessary for database administrators to
manage and organize database structures efficiently.

 Documentation: Using comments and descriptive naming conventions in DDL can help document
the database structure, making it easier to understand and maintain.

4
5
DDL Commands

6
SQL DDL Commands Syntax

 CREATE TABLE table_name (column1 data_type, column2 data_type, ...);

 DROP TABLE table_name;

 ALTER TABLE table_name ADD COLUMN column_name data_type;

 TRUNCATE TABLE table_name;

 RENAME TABLE old_table_name TO new_table_name;

7
CREATE / DROP Database Command

 The CREATE DATABASE statement is used to create a new SQL database

 The DROP DATABASE statement is used to drop an existing SQL database.

8
CREATE Table Command

9
ALTER/DROP/TRUNCATE/RENAME Table Commands

 ALTER: This command modifies existing database objects. It


can be used to add, delete, or modify columns in a table.

 DROP: This command deletes existing database objects. It


removes the object and all the data it contains.

 TRUNCATE: This command deletes all rows from a table, but


the table structure remains intact.

 RENAME: Changes the name of a database object.

10
Common ALTER Commands

 Add Column: ALTER TABLE table_name ADD COLUMN column_name column_type;

 Modify Column: ALTER TABLE table_name MODIFY COLUMN column_name new_type; or ALTER TABLE
table_name ALTER COLUMN column_name TYPE new_type;

 Drop Column: ALTER TABLE table_name DROP COLUMN column_name;

 Rename Column: ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

 Add Constraint: ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_type (columns);

 Drop Constraint: ALTER TABLE table_name DROP CONSTRAINT constraint_name;

 Rename Table: ALTER TABLE old_name RENAME TO new_name;

11
ALTER command examples

12
CREATE view & CREATE index

CREATE VIEW employee_details AS


SELECT employee_id, first_name, last_name, department, salary
FROM employees;

CREATE INDEX idx_employee_last_name


ON employees(last_name);

13
DROP Command

 The SQL DDL (Data Definition Language) DROP command is used to delete database
objects such as tables, databases, indexes, views, triggers, and procedures.

 DROP TABLE employees;

 DROP DATABASE company_db;

 DROP INDEX idx_employee_name ON employees;

 DROP VIEW employee_view;

14
TRUNCATE Command

 The SQL DDL (Data Definition Language) TRUNCATE command is used to delete all rows from a table,
effectively resetting it to its empty state, but without removing the table structure.

 Unlike the DELETE command, TRUNCATE is usually faster and uses fewer system and transaction log
resources because it does not generate individual row delete statements.

 However, it also cannot be rolled back if not used within a transaction.

 TRUNCATE TABLE employees;

 TRUNCATE TABLE sales.monthly_reports;

 TRUNCATE TABLE customers, orders, order_details;

15
RENAME Command

 The SQL DDL (Data Definition Language) RENAME command is used to change the name of a database
object.

 RENAME TABLE old_table_name TO new_table_name;

 RENAME TABLE employees TO staff;

 ALTER TABLE employees RENAME COLUMN last_name TO surname;

 ALTER INDEX idx_employee_name RENAME TO idx_staff_name;

 RENAME TABLE employee_view TO staff_view;

16
Transaction Control Language (TCL)

17
Class Practice Session (DML)

18
Class Practice Session (DML)

19
Class Practice Session (DML)

20
Class Practice Session (DDL)

21
Class Practice Session (DDL)

22
Thankyou
Any Queries?

23

You might also like