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

LAB 2 FAMILIRIZATION WITH DDL COMMAND

Theory
DDL Stands for "Data Definition Language." A DDL is a language used to define data
structures and modify data. For example, DDL commands can be used to add, remove, or
modify tables within in a database. DDLs used in database applications are considered a
subset of SQL, the Structured Query Language. However, a DDL may also define other types
of data, such as XML.
Some commands under DDL are:
 CREATE
 ALTER
 DROP
 RENAME
 TRUNCATE
CREATE
“Create” is used to create database and table in SQL.
Syntax: 1. CREATE DATABSE database_name;
2. CREATE TABLE table_name
(column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
………..);
ALTER
“ALTER” is use to add, drop and modify column in a table.
Syntax: 1. ALTER TABLE table_name ADD (column_name_1 datatype
column_name_2 datatype
……. );
2. ALTER TABLE table_name DROP COLUMN column_name;
3. ALTER TABLE table_name MODIFY column_name datatype;
DROP
It is used to delete both the structure and record stored in the table.
Syntax: 1. DROP DATABASE databasename;
2. DROP TABLE table_name;
RENAME
It is used to rename table in database.
Syntax: 1. RENAME TABLE table_name to new_table_name;
TRUNCATE
It is used delete all the row in table.
Question:
1.
ID address sales product Day
1 Ktm 1000 6 Sun
2 Ktm 2000 5 Mon
3 Ktm 2500 10 Tue
4 Pkr 2000 12 Wed
5 Cht 3000 13 Thu
6 Bkt 1500 4 Fri
7 Patan 5000 9 Sat
8 Jhapa 6000 12 sun
Write SQL for the following:
a.Create table for shop
Ans-> CREATE TABLE pasal (id int PRIMARY KEY, address
varchar (30), sales int, product int, day varchar (10));

b.Rename Pasal to shop


Ans->  ALTER TABLE pasal RENAME shop;
c.Insert all values
Ans-> INSERT INTO `shop` (`id`, `address`, `sales`, `product`, `day`) VALUES
(1,"ktm",1000,6,"sun"), (2,"ktm",2000,5,"mon"), (3,"ktm",2500,10,"tue"),
(4,"pkr",2000,12,"wed"), (5,"cht",3000,13,"thu"), (6,"bkt",1500,4,"fri"),
(7,"patan",5000,9,"sat"), (8,"jhapa",6000,12,"sun");
Output->

a. Find the max sales


Ans->SELECT MAX(sales) AS maximum_sale FROM shop;
Output->

b. Select avg product of the shop


Ans->SELECT AVG(product)FROM shop;
Output->

c. Select the min sales.


Ans->SELECT MIN(sales)FROM shop;
Output->
2. id name address Gender
1 Ishwor Sunapati male
2 Nitesh Kirtipur male
3 Buddha Mahalaxmi male
a. Create above table
Ans-> CREATE TABLE labmates (id int PRIMARY KEY,name
varchar (30), address varchar (30), gender varchar (8));
Output->

b. Add column phone_no and salary


Ans-> ALTER TABLE labmates ADD (phone_no int, salary bigint);

Output->

c. Insert data
Ans-> INSERT INTO `labmates`(`id`, `name`, `address`, `gender`,
`phone_no`, `salary`) VALUES
(1,"Ishwor","Sunapati","male",988884654,100000),
(2,"Nitesh","Kritpur","male",9803504728,200000),
(3,"Buddha","Mahalaxmi","male",9812356445,30000);
Output->

d. Drop column gender


Ans-> ALTER TABLE labmates DROP gender;
Output->
e. truncate above table.
Ans-> TRUNCATE TABLE labmates;

Output->

f. Rename table
Ans-> ALTER TABLE labmates RENAME bcalabmates;

Output->

Conclusion:
We learn about the DDL command i.e create,drop,truncate,rename,alter and use those
command to modifiy the structure of the database and table. We learn to deal with
descriptions of the database schema and is used to create and modify the structure of database
objects in the database.

You might also like