Docsity

SQL DDL Commands — CREATE, ALTER, and DROP Statements, Exercises of Database Management Systems (DBMS)

Toggle sidebar
bg1
DATABASE MANAGEMENT SYSTEMS LAB MANUAL
1 | P a g e
SQL – DDL
Topic: CREATE, ALTER, and DROP Statements
1. Objective:
The objectives of this lab are to:
Understand Data Definition Language (DDL) in SQL.
Differentiate DDL commands from commands used to manipulate table data.
Create databases and tables using MySQL.
Use ALTER TABLE to change the structure of an existing table.
Add, modify, and rename columns using appropriate SQL syntax.
Understand the purpose and consequences of DROP DATABASE and DROP TABLE.
Verify database structure changes using SQL commands and MySQL Workbench.
Develop safe database-management practices before performing destructive operations.
2. HARDWARE AND SOFTWARE REQUIRED :
Hardware
PC or laptop
Keyboard and mouse
Software
MySQL Server
MySQL Workbench
Windows, Linux, or macOS
Other Requirements
Access to the course LMS
Lab folder for SQL scripts and screenshots
Basic understanding of databases, tables, and SQL syntax
3. Introduction to Data Definition Language (DDL)
A database stores organized information in structures such as tables. Before storing information, we must
define the structure in which that information will be kept.
For example, a university database may contain tables for students, teachers, courses, and departments.
Each table needs a name and a defined set of columns with suitable data types.
Data Definition Language (DDL) is a category of SQL used to define and modify database structures.
The three main commands covered in this lab are:
Command Purpose
CREATE Creates a database or database object.
ALTER Changes the structure of an existing database object.
DROP Removes a database object.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download SQL DDL Commands — CREATE, ALTER, and DROP Statements and more Exercises Database Management Systems (DBMS) in PDF only on Docsity!

1 | P a g e

SQL – DDL

Topic: CREATE, ALTER, and DROP Statements

1. Objective:

The objectives of this lab are to:  Understand Data Definition Language (DDL) in SQL.  Differentiate DDL commands from commands used to manipulate table data.  Create databases and tables using MySQL.  Use ALTER TABLE to change the structure of an existing table.  Add, modify, and rename columns using appropriate SQL syntax.  Understand the purpose and consequences of DROP DATABASE and DROP TABLE.  Verify database structure changes using SQL commands and MySQL Workbench.  Develop safe database-management practices before performing destructive operations.

2. HARDWARE AND SOFTWARE REQUIRED:

Hardware

 PC or laptop

 Keyboard and mouse

Software

 MySQL Server

 MySQL Workbench

 Windows, Linux, or macOS

Other Requirements

 Access to the course LMS

 Lab folder for SQL scripts and screenshots

 Basic understanding of databases, tables, and SQL syntax

3. Introduction to Data Definition Language (DDL)

A database stores organized information in structures such as tables. Before storing information, we must define the structure in which that information will be kept. For example, a university database may contain tables for students, teachers, courses, and departments. Each table needs a name and a defined set of columns with suitable data types. Data Definition Language (DDL) is a category of SQL used to define and modify database structures. The three main commands covered in this lab are: Command Purpose CREATE (^) Creates a database or database object. ALTER (^) Changes the structure of an existing database object. DROP (^) Removes a database object.

2 | P a g e DDL primarily deals with database structures rather than inserting, updating, or retrieving individual records. 3.1 Database Structure Example Consider a university database: In this example, UniversityDB is the database, while Students and Courses are tables. The columns define the information each table can store.

4. The CREATE Statement The CREATE statement is used to create database objects, such as databases and tables. 4.1 Creating a Database Syntax: CREATE DATABASE database_name; Example: CREATE DATABASE UniversityDB; This statement creates a database named UniversityDB. To display the available databases, use: SHOW DATABASES; To avoid an error if the database already exists, MySQL also supports: CREATE DATABASE IF NOT EXISTS UniversityDB; 4.2 Selecting a Database Before creating a table inside a database, select the appropriate database: USE UniversityDB; The USE statement selects the database for subsequent operations in the current session. It is not itself a DDL command. 4.3 Creating a Table A table consists of columns, and each column has a name and a data type. Syntax: CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype ); Example: CREATE TABLE Students ( Student_ID INT, Student_Name VARCHAR(50), Department VARCHAR(50), Semester INT ); Explanation:  Student_ID INT defines an integer column.  Student_Name VARCHAR(50) stores text of up to 50 characters.

4 | P a g e DROP TABLE Students; This removes the Students table, including its structure and stored records. 6.2 Dropping a Database Syntax: DROP DATABASE database_name; Example: DROP DATABASE UniversityDB; This removes the database and the objects contained in it. 6.3 Safer Syntax for Practice MySQL also supports conditional removal: DROP TABLE IF EXISTS Students; And: DROP DATABASE IF EXISTS UniversityDB; These statements avoid an error when the specified object does not exist. However, they do not protect existing data from deletion when the object does exist. Safety rule: Do not run DROP commands on university, production, or shared databases. Use only the practice objects created for this lab. PRACTICAL ACTIVITIES: Activity 1: Create a Database Objective: Create a database using MySQL Workbench. Procedure

  1. Open MySQL Workbench.
  2. Connect to your MySQL Server.
  3. Open a new SQL query tab.
  4. Execute the following statement: CREATE DATABASE Lab06_DB;
  5. Execute: SHOW DATABASES;
  6. Refresh the Schemas panel.
  7. Confirm that Lab06_DB appears. Deliverable: Screenshot showing the SQL statement and the created database.

5 | P a g e Activity 2: Create a Table Objective: Create a table inside the database. Select the database: USE Lab06_DB; Create the table: CREATE TABLE Employees ( Employee_ID INT, Employee_Name VARCHAR(50), Job_Title VARCHAR(50), Salary DECIMAL(10,2) ); Verify the table: SHOW TABLES; View its structure: DESCRIBE Employees; Deliverable: Screenshot showing the Employees table and its column definitions.

7 | P a g e Activity 4: Modify and Rename a Column Objective: Change the definition and name of existing columns. Modify the job-title column: ALTER TABLE Employees MODIFY Job_Title VARCHAR(100); Rename the job-title column: ALTER TABLE Employees RENAME COLUMN Job_Title TO Designation; Verify the result: DESCRIBE Employees; Deliverable: Screenshot showing the revised column name and data type.

8 | P a g e Activity 5: Remove a Column Objective: Remove a column from the table structure. Execute: ALTER TABLE Employees DROP COLUMN Email; Verify the result: DESCRIBE Employees; Deliverable: Screenshot showing that Email is no longer listed.

10 | P a g e

AFTER DROPPING TABLE:

11 | P a g e Activity 7: Drop a Practice Database Objective: Understand how a database can be removed. Create a separate practice database: CREATE DATABASE TemporaryDB; Verify that it exists: SHOW DATABASES; Remove the practice database: DROP DATABASE TemporaryDB; Verify again: SHOW DATABASES; Deliverable: Screenshots showing the database before and after removal. BEFORE DELETING DATABSE;

13 | P a g e

  1. Select the database.

14 | P a g e

  1. Create the Courses table.
  2. Verify the table using SHOW TABLES.

16 | P a g e

  1. Add a column named Course_Code with an appropriate data type.
  2. Modify the size of the Course_Name column. LIMIT OF COURSE_NAME CHANGED FROM VARCHAR(20) TO VARCHAR(50)

17 | P a g e

  1. Rename Instructor_Name to Faculty_Name. BEFORE:

19 | P a g e

  1. Verify all changes using DESCRIBE Courses.
  2. Remove the Course_Code column.

20 | P a g e

  1. Verify the final table structure. Important: Do not drop CollegeDB or Courses for this task. You will need the completed structure for