Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Database SYSTEMS laB mANUAL 10

Dr. Muhammad Bilal Shahnawaz

NAtional skills university, islamabad


Lab 10: Using DDL queries to manage database objects
Lab Objectives: This lab aims to learn:

 How to create and manage/modify database objects using DDL statements

Data Definition Language (DDL) queries to manage the database

DDL (Data Definition Language) queries are used to define, modify, and delete database objects.
DDL queries are typically executed by database administrators or during the setup phase of an
application to create the necessary database structure. It's important to use DDL queries with
caution, as they can have a significant impact on the database schema and data. Here are some
common DDL queries with examples:

1. SQL CREATE DATABASE is used to create a new SQL database.


Syntax:
CREATE DATABASE databasename

A database TEST will be created and you can use it using USE TEST query.

2. SQL DROP DATABASE is used to drop an existing SQL database.

Syntax:

DROP DATABASE databasename;

The database TEST will be dropped. Again execute USE TEST query to verify it.
NOTE: you can’t drop a database while using it i.e. you have created a database and using it,
then execute query to drop it.

In this scenario, you can switch to another database or simply return to the default database and
after that drop the database.

3. SQL ALTER DATABASE can be used to rename the database.

Syntax:

ALTER DATABASE YourDatabaseName


MODIFY NAME = NewDatabaseName;
Refresh the Databases folder in Object Explorer to see the changes. A database can also be
renamed from Object Explorer just like any folder in a system.

It's important to note that renaming a database should be done with caution, especially in a
production environment. Ensure you have a backup of the database before performing these
steps.

4. SQL BACKUP DATABASE is used in SQL Server to create a full backup of an


existing SQL database.

Syntax:

BACKUP DATABASE databasename


TO DISK = 'filepath';

The differential backup can also be created with a DIFFERENTIAL Statement. A differential
backup only backs up the parts of the database that have changed since the last full database
backup.

Syntax:

BACKUP DATABASE databasename


TO DISK = 'filepath'
WITH DIFFERENTIAL

NOTE: A differential backup reduces the backup time (since only the changes are backed up).

The dropped database can be restored using the RESTORE DATABASE command.

The WITH REPLACE option is used to overwrite an existing database with the same name if it
already exists. The WITH RECOVERY option is used to bring the database online after the
restore.
5. SQL CREATE TABLE is used to create a new table in a database (Discussed in the
previous lab).
Syntax:
CREATE TABLE table_name(
column_name1 datatype constraint,
column_name2 datatype constraint,
column_name3 datatype constraint,
…..
column_nameN datatype constraint,
)

6. SQL DROP TABLE is used to drop an existing table in a database.

Syntax:

DROP TABLE table_name;

For verification, a SELECT statement is executed to view the table.

The DELETE deletes one or more existing records from the table in the database.

The DROP drops the complete table from the database.

The TRUNCATE deletes all the rows from the existing table, leaving the row with the
column names.
7. Rename a Table: A table can be renamed from the object explorer same as to rename a
database. Similarly, to rename a table in SQL Server, sp_rename stored procedure can
be used.
Syntax:
EXEC sp_rename 'OldTableName', 'NewTableName';

A stored procedure is a precompiled collection of one or more SQL statements or procedural


logic statements, which is stored in a database and can be executed by DBMS. Stored procedures
are typically created and stored in the database for reuse and efficiency. The sp_rename stored
procedure in SQL Server is used to rename an existing user-created object in the current
database.

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

The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.

i. ALTER TABLE to ADD Column

Again create a table as created earlier and view the table after inserting the values.

A new column in the existing table can be added using the following syntax.

Syntax:
ALTER TABLE table_name
ADD column_name ;
Try to add a new column Email2 as follows:

Now, Email addresses for the existing records can be added using the UPDATE query.

To add email address for all of the existing records, UPDATE query can be used with SQL
CASE Expression.
ii. ALTER TABLE to DROP a column

A column from the table can be deleted using the following syntax.

ALTER TABLE table_name


DROP COLUMN column_name

The newly added column, Email can be dropped from the table:

iii. ALTER TABLE to RENAME a column


To rename a column in a table, one way is to create a new column, copy the data from an
existing column, and delete the old one, using the ALTER TABLE command in combination
with ADD/DROP commands.

An alternative to renaming a column in the table is to use sp_rename stored procedure.

iv. ALTER TABLE to ALTER/MODIFY datatype/constraints of a column

The datatype of a column can be changed using the following syntax:

Syntax:

ALTER TABLE table_name


ALTER COLUMN column_name datatype

---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------

task: Perform the following using SQL queries:

 Create a database with the name “Testing1” if it doesn’t exist already.

 Use the created database and modify its name to “Testing2”.


===============================================================
===============================================================
======================================================

 Now, delete the database “Testing2” but make sure to keep a backup before deleting
it.
===============================================================
===============================================================
======================================================

 Restore the deleted database as “Testing3” using the backup file.


 Verify the restored database.

===============================================================
===============================================================
======================================================

 Create a table in the database with the name Employee that can save information of
Employees corresponding to the attributes Employee_ID, Employee_name, City,
phone_number, salary, age, and joining_date. Make sure to select the appropriate
data types for the attributes while creating the table.
===============================================================
===============================================================
======================================================

 Insert some records.

===============================================================
===============================================================
======================================================
 Differentiate delete/drop/truncate commands using the created table.
DELETE:

TRUNCATE:
DROP:
 Create another table with the name of testing and insert some records at least 10.
 Change this table name to EmployeesData.
 Now alter the table to add another column named Email.
 Insert emailIDs for the Employees.
 Rename the column email to EmailID.
 Change the datatype of salary so that the user can insert only numeric values.
 Delete the column age.

 Show the final table and the inserted information.

You might also like