You are on page 1of 10

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER


ENGINEERING
EXPERIMENT#07

LAB TITLE : SQL DDL Statements

Student Name: BISMAH ASIF Reg. No: 222650


Objective : To know the working of some of the commands of SQL.

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows the
lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: 023-04-2024 Signature: bisma


LAB TITLE: SQL DDL Statements

BISMAH ASIF (222650)

Bachelor of Computer
Engineering Fall-22

Submitted To:
Engr. Atif bajwa
Lab Engineer

Department of Electrical and Computer Engineering


Faculty of Engineering
Air University, Islamabad
LAB#07
SQL and DDl Statement

Lab Objective:
To know the working of some of the commands of SQL.

Introduction to DDL:
The DDL part of SQL permits databases and tables to be created or deleted or modified w.r.t it
Structure. It also define indexes (keys), specify links between tables, and impose constraints between
tables.
The most important DDL statements in SQL are:
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index

The DROP TABLE Statement


The DROP TABLE statement is used to delete a table.

DROP TABLE table_name ;

The DROP DATABASE Statement


The DROP DATABASE statement is used to delete a database.

DROP DATABASE database_name


The TRUNCATE TABLE Statement
What if we only want to delete the data inside the table, and not the table itself?
Then, use the TRUNCATE TABLE statement:

TRUNCATE TABLE table_name

SQL ALTER TABLE Statement


The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

SQL ALTER TABLE Syntax


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype

To delete a column in a table, use the following syntax (notice that some database
systems don't allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name

To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_name


ALTER COLUMN column_name datatype
SQL ALTER TABLE Example
Look at the "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn Sandnes


10

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

Now we want to add a column named "DateOfBirth" in the "Persons" table. We use the
following SQL statement:

ALTER TABLE Persons


ADD DateOfBirth date

Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type
specifies what type of data the column can hold. For a complete reference of all the data types
available in MS Access, MySQL, and SQL Server, go to our complete Data Types reference.
The "Persons" table will now like this:

LastName FirstName Address City DateOfBirth


P_Id
1 Hansen Ola Timoteivn Sandnes
10

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger


LAB TASK 01
Consider the following table:

• Write an SQL statement to convert the above table into following table.

• Write SQL statement(s) to change “Birth_Date” to “Age” with data type Integer.
• Create an Index on the “Customer” table using “First_Name” and “Age”.
• Add a new attribute Supplier_Name into table.
LAB TASK 02
Consider the following table “ Product ” :
• Write an SQL statement to delete the “ProductName” entries from the table.

Output:
LAB TASK 03
Consider the following tables:
Create the above table by keeping their first columns as primary key. After the creation
of the table,
solve the following:
• Write a query to add an attribute, Class to the Student table
• Write a query to change the field for Student_Name from 25 characters to 40 characters
• Write a query to add another column in the Student table with an auto increment field
• Write a query to add another column Department in the Student table. The column
must not
contain any value other than the values COMPUTER or SOFTWARE
• Write a query to change the auto increment field to start from 50
• Write a query to remove the Student table

Code:
Output:

LEARNING OUTCOMES

In this lab, we learned essential DDL commands in SQL for managing


database structure. These commands include renaming columns, changing data
types, adding indexes, and altering tables. Additionally, we performed tasks
like adding new columns, setting constraints, and dropping tables to
demonstrate practical usage of these commands. Understanding and utilizing
these commands effectively are crucial skills for efficient database managent

You might also like