













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














1 | P a g e
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.
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
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
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
14 | P a g e
16 | P a g e
17 | P a g e
19 | P a g e
20 | P a g e