SQL Commands: DDL DML
SQL Commands: DDL DML
SQL Commands
SQL commands are instructions. It is used to communicate with the database.
It is also used to perform specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users.
DDL DML
DDL stands for data DDL stands for data
definition language. manipulation language.
It is used to create the It is used to manipulate
database schema. database.
It is not classified further.
It is futher classified as
procedural and non-
procedural dml’s
Create,alter,drop,truncate. Select,insert,update,delete
.etc …etc
SQL statement can’t be SQL statement can be
rollback rollback.
DDL command affect the Command affect one or
entire database. more records in a table.
DDL commands-
Data Definition Language (DDL) commands are used to create,
manipulate, and modify objects in Snowflake, such as users, virtual
warehouses, databases, schemas, tables, views, columns, functions,
and stored procedures.
Aim:
To study about various DDL commands and implement them
on the database.
CREATE –
Purpose:
To create new tables , views and databases in SQL.
Syntax:
OUTPUT-
ALTER- The Alter command in DDL can change or
modify the database structure.
Purpose:
To Add , delete , modify columns in an existing
table.
Syntax:
ADD: alter table <table name> add(<new col> <datatype> (<size>));
Code-
alter table stud add(m1 int(3),m2 int(3));
insert into stud values(11,'k',100,50,50);
OUTPUT-
DROP- Executing the Drop command in DDL can
remove databases and tables.
Dropping a column from a table-
Syntax:
Alter table <tablename> drop column <col>;
Code-
mysql>alter table stud drop column m1;
OUTPUT-
Destroying table-
Syntax:
Drop table <tablename>;
Code-
mysql>drop table stud;
OUTPUT-
Truncate -When executed, the Truncate command
in DDL can remove or delete all rows from a specific
table and clear or free the table, respectively.
Syntax-
truncate table<table name>;
Code-
mysql>truncate table stud;
mysql>desc stud;
OUTPUT-
DML COMMANDS-
Data Manipulation Language or DML represents a collection
of programming languages explicitly used to make changes in
the database, Using the INSERT, SELECT, UPDATE and
Delete commands.
Aim:
To study about various DML commands and implement them
on the database.
INSERT –The insert query command in SQL
provides a way to add new rows of information or data
inside a specific database.
Syntax-
Insert into table_name values (value1, value2 ,……value n);
Code-
insert intoem values(23344,'hyungsik','director',29,500000);
select * from em;
OUTPUT-
DELETE -Delete command provides a way to delete a
single column or multiple columns from a table’s
specific row.
Syntax-
Delete from table_name where condition;
CODE-
delete from em where dept='cgo';
Code-
select name,dept from em;
OUTPUT-
Ascending and descending order
This command is used to arrange the table in a ascending or
descending order based on one column.
Syntax-
Ascending order-
Select * from <tablename> order by <colname>;
Descending order-
Select * from <tablename> order by <colname> desc;
Code-
select * from em order by salary desc;
select * from em order by salary;
OUTPUT-
UPDATE-The Update command provides a way to
make changes/update or modify the values present in a
table’s column.
Syntax-
Update <tablename> set
column1=value1,column2=value2…..where [condition];
Code-
update em set name='joongi' , age='39' where id=92843
OUTPUT-
EXERCISE QUESTIONS-
1.Create the 4 tables mentioned above
a. Check if the table is created successfully (tab table)
Table Name: Product_Info
Column Name Purpose
Maker Name of the maker/manufacturer of computers/peripherals
Model_No The unique identifier for each product manufactured
Type Indicates the type of the product – PC for personal computers, LP for Laptop and PR
for printers
ANSWER -
create table product_info(makername varchar(10),model_no
char(10),printerstype varchar(5));
desc product_info;
Table Name: PC
Column Name Purpose
Model_No The unique identifier for each PC
Speed Clock Speed of the PC , RAM RAM in MB
HD The hard-disk capacity of the PC in GB
CD The speed of the CD Drive , Price Price of the PC
ANSWER -
create table pc(model_no char(5) unique , speed int(3) , ram_mb int(2), hd_gb
int (4), cd int(4),price int(10));
desc pc;
ANSWER -
create table laptop(model_no char(5) unique , speed int(3) , ram_mb int(2),
hd_gb int (4), screensize int(4),price int(10));
desc laptop;
Table Name: Printer
Column Name Purpose
Model_No The unique identifier for each Laptop
Color Flag indicating whether it is a color printer or a black and white printer
Type Line, InkJet or Laser
Price Price of the printer
ANSWER -
create table printer(model_no char(5) unique,color varchar(5),type char(5),price
int(5));
desc printer;
2. Insert the following tuples in the table ProductInfo
MAKER MODEL TYPE
--------- -------------------- -----------
HCL PC112 PC HCL LP113 LP ZENITH PR114 PR
WIPRO PC122 PC WIPRO LP123 LP WIPRO PR124 PR
IBM PC134 PC HCL LP114 LP IBM PC132 PC
IBM LP133 LP IBM PR134 PR
Select and display the table after inserting by using Select statement
ANSWER -
insert product_info values('HCL','PC112','pc');
insert product_info values('HCL','LP113','LP');
insert product_info values('ZENITH', 'PR114','PR');
insert product_info values('WIPRO','PC122','pc');
insert product_info values('WIPRO','PR124','PR');
insert product_info values('IBM','PC134','pc');
insert product_info values('HCL','LP114','LP');
insert product_info values('IBM','PC132','pc');
insert product_info values(' IBM ','LP133','LP');
insert product_info values(' IBM ','PR134','PR');
3.Insert the following tuples in the table pc
MODEL SPEED RAM HD CD PRICE
-------------------- --------- --------- --------- --------- ---------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
Select and display the table after inserting by using Select statement
ANSWER -
insert pc values('pc112',2,256,60,52,40000);
insert pc values('pc122',2,256,60,48,42000);
insert pc values('pc132',1,128,100,68,50000);
insert pc values('pc134',1,512,60,68,80000);
select * from pc;
4. Insert the following tuples in the table LAPTOP
MODEL SPEED RAM HD SCREEN PRICE
------------------------------------------------------------------------
lp113 1 64 40 14 59000
lp123 2 128 60 16 72000
lp133 2 256 80 17 100000
lp114 2 128 40 17 45000
Select and display the table after inserting by using Select statement
ANSWER -
insert laptop values('ip113',1,64,40,14,59000);
insert laptop values('ip123',2,168,60,16,72000);
insert laptop values('ip133',2,256,80,17,100000);
insert laptop values('ip114',2,128,40,17,45000);
select * from laptop;
5. Insert the following tuples in the table PRINTER
MODEL COLO TYPE PRICE
---------------------------------------------------
Pr114 true ink 17000
pr124 false dot 12000
pr134 true laser 17000
Select and display the table after inserting by using Select statement
ANSWER -
insert printer values('pr114',true,'ink',17000);
insert printer values('pr124',false,'dot',12000);
insert printer values('pr134',true,'laser',17000);
select * from printer;
6. Insert a tuple such that the model value is PC100, it is
manufactured by HCL and has a speed of 3 GHz, RAM of 256 MB, HD of
40 GB and CD of speed 52x. The price is 50000.
Show the updated table
ANSWER -
insert pc values('pc100',3,256,40,52,50000);
ANSWER -
update product_info set makername='IBM' where makername='HCL';
9. For each PC double the amount of RAM and add 10 gb to the HDD.
ANSWER -
update pc set ram_mb=ram_mb*2;
update pc set hd_gb=hd_gb+10;
10. For the printer whose modelno=pr124 subtract Rs. 4000 from the
price.
ANSWER -
update printer set price=price-4000 where model_no="pr124";