IT SEM 3
PRACTICAL-1
AIM: Creating And Manipulating Database Objects And
Applying Constraints (DDL).
❖How to Create a Table:
Syntax:
create table table_name(column_1 data_type(size),column_2 data_type(size),
column_3 data_type(size),column_3 data_type(size));
Example:
create table student(id int,name varchar(20),age int,branch varchar(20),city
varchar(20));
❖To Check if the Table is Properly Created, we use Describe
command:
Syntax:
desc table_name; Example:
desc student;
❖To make Changes in the Structure of the Table, we use alter
command.
▪To Add a Column in the Table:
Syntax:
alter table table_name add column_name data_type;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Example:
alter table student add age int;
▪ To Rename the Table:
Syntax:
alter table table_name rename to new_table_name;
▪To Change the Datatype of a Column, we use modify
command:
Syntax:
alter table table_name modify column_name data_type; Example:
alter table student modify name varchar(30);
▪To Rename a Column in the Table:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Syntax:
alter table table_name rename column old_name to new_name;
Example:
alter table student rename column age to s_age;
▪To Drop a Column in the Table:
Syntax:
alter table table_name drop column column_name;
Example:
alter table student drop column s_age;
❖To Delete All the Records from the Table, we use truncate
command:
Syntax:
truncate table table_name; Example:
truncate table student;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
❖To Delete the Table, we use drop command:
Syntax:
drop table table_name; Example:
drop table student;
❖To Create the PRIMARY_KEY on a column Using alter
command:
Syntax:
alter table table_name add primary key (column_name); Example:
alter table student add primary key(id);
❖To Create the FOREIGN_KEY on a column Using alter
command:
Syntax:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
alter table table_name add foreign key (column_name) references
parent_table(column_name);
Example:
alter table parents add foreign key(p_id) references student(id);
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
PRACTICAL-2
AIM: Manipulating Data with Database
Objects (DML).
❖To Fetch Data from the Table, we use
select command:
Syntax:
select * from table_name; Or select column1,column2, .... from
table_name; Example:
select * from student; Or select * from
s_id,s_firstname,s_Lastname from student;
❖To Insert the Data into the Table:
▪To Insert the Data in all the columns:
Syntax:
insert into table_name values(value1,value2,value3, .....);
Example:
insert into student value(1,'margi','it','gandhinagar','3');
insert into student value(2,'poojan','ce','visnagar','3'); insert into
student value(3,'vishu','civil','ahmedabad','2');
❖To Modify the Specific Records/Rows of the Table, we use
update command:
Syntax:
update table_name SET column1 = value1, column2 = value2, ..... where condition;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Example:
update student set name='heer' where s_id=3;
❖To Delete Records/Rows from the Table:
▪ To Delete All Records From the Table:
Syntax:
delete from table_name; Example:
delete from student;
▪To Delete Specific Records/Rows from the Table:
Syantax:
delete from table_name where condition; Example:
delete from student where id=3;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
PRACTICAL-3
AIM: Retrieving, Restricting and Sorting Data (DRL).
❖To Extract only those Records/rows that Matches the
specified conditions, we use where Clause:
Syntax:
select * from table_name where condition;
OR select column1,column2, .... from table_name where condition; Example:
select * from student where id=1;
Or
select name,semester from student where id=2;
▪WHERE clause With AND operator:
Syntax:
select column1, column2, .... from table_name where condition1 and condition2.....;
Example:
select name,semester from student where city='gandhinagar';
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪WHERE clause With OR operator:
Syntax:
select column1, column2, ......from table_name where condition1 or condtion2......;
Example:
select id,name,semester from student where branch='ce';
▪WHERE clause With AND and OR operator:
Syntax: select column1, column2, ......from table_name where condition1 or
condtion2......;
Example:
select name,semester from student where branch='it';
❖To Sort the Result_Table in Ascending or descending
order, we use ORDER BY clause:
▪To Sort in Ascending order:
Syntax:
select column1, column2, ... from table_name order by column1, column2;
Example:
select id,name,address from student order by semester asc;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪To Sort in Descending order:
Syntax:
select column1, column2, ... from table_name order by column1, column2, ... desc;
Example:
select * from student order by semester desc;
❖To Return the No. of Rows that Matches a Specified
Criterion, we USE COUNT() function:
Syntax:
select count(column_name) from table_name; Example:
select count(id) from student;
❖To give the Table or a Column a Temporary Name, we use
ALIASES:
Syntax:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
select column_name(s) as alias_name from table_name;
Example:
select id as enr_no,name,city from student;
❖To Group Rows that have the same values, we use
GROUP BY clause:
Syntax:
select column_name(s) from table_name where condition group by column_name(s)
order by column_name(s);
Example:
select count(semester) as sem,city from student group by address;
❖To Extract only those Records/rows that Matches the
specified conditions, we use HAVING Clause:
Syntax:
select column_name(s) from table_name where condition group by column_name(s)
having condition order by column_name(s);
Example:
select count(id),city from student group by city having count(id)<4;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
PRACTICAL-4
AIM:SQL Single Row Functions.
▪Case Conversion Functions:
▪Upper() function:
Syntax:
select upper(column_name) as alias_name from table_name;
Example:
select upper(name) as name from student;
▪Lower() function:
Syntax:
select lower(column_name) as alias_name from
table_name; Example:
select lower(name) as name from student;
❖String Functions:
▪CONCAT() Function:
Syntax:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
select concat(column1,column2, ......) from table_name;
Example:
select concat(id,name) as info from student;
▪LENGTH() function:
Syntax:
select length(column_name) from table_name;
Example:
select name ,length(name)as len_name from student;
▪SUBSTR() function:
Syntax:
select substr(column_name, index_position, extract_char) from table_name;
Example:
select name ,substr(name,1,3)as sub_string from student;
▪INSTR() function:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Syntax:
select instr(column_name, char_to_search)from
table_name; Example:
select name ,instr(name,'s') from student;
▪LPAD() function:
Syntax:
select lpad(column_name, total_string_length, string_to_pad) from
table_name; Example:
select name,lpad(name,9,'num')as leftpad_num from student;
▪RPAD() function:
Syntax:
select rpad(column_name, total_string_length, string_to_pad)from table_name;
Example:
select name,rpad(name,9,'num')as rightpad_num from student;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪REPLACE() function:
Syntax:
select replace(column_name, substring_to_replace, new_substring)from
table_name; Example:
select name,replace(name,'i','') from student;
❖Date function:
▪Demo table date_time:
Create table datetime(s_no int primary key,cus_name varchar(20),order_datetime
date);
insert into datetime values(1,'vishwa','2024-08-26 23:45:38');
insert into datetime values(2,'het','2023-08-17 03:57:21'); insert into datetime
values(3,'priyal','2021-12-15 21:34:19'); insert into datetime
values(4,'khushi','2022-05-14 01:43:24');
▪CURDATE() function:
Syntax:
select curdate();
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪CURTIME() function:
Syntax:
select curtime();
▪DATE() function:
Syntax:
select date(column_name) from table_name; Example:
select order_datetime,date(order_datetime) as order_date from datetime;
▪EXTRACT() function:
Syntax:
select extract(unit from column_name) from table_name; Example:
select order_datetime,extract(second from order_datetime)as second from datetime;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪DATE ADD() function:
Syntax:
select date_add(column_name, interval value addunit) from table_name;
Example:
select order_datetime,date_add(order_datetime,interval 1 year)as new_date from
datetime;
▪DATEDIFF() function:
Syntax:
select datediff(date1,date2); Example:
select datediff('2024-08-26','2024-08-07') as datediff;
❖Math function:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪POWER() function:
Syntax: select power(number,power);
Example: select power(4,3);
▪LOG() function:
Syntax: select log(number,base);
Example:select log(6,4);
▪ABSOLUTE() function:
Syntax: select abs(number);
Example: select abs(-17);
▪SQUARE_ROOT() function:
Syntax: select sqrt(number); Example:
select sqrt(64);
▪ SINE() function:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Syntax: select sin(degree);
Example: select sin(45);
▪COSINE() function:
Syntax: select cos(degree);
Example: select cos(34);
▪MODULO() function:
Syntax:
select mod(4,8);
▪CEIL() function:
Syntax:
select ceil(55.87);
▪FLOOR() function:
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
Syntax:
select floor(55.87);
▪EXPONENENT() fuction:
Syntax: select exp(7);
▪GREATEST() function:
Syntax:
select greatest(5,26,13,17);
▪LEASR() function:
Syntax:
select least(5,26,13,17);
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪TRUNCATE() function:
Syntax:
select truncate(87.5432,3);
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
PRACTICAL-5
AIM: SQL Multiple Row Functions (Aggregate Function).
❖Demo table employee: create table employee(e_id int,e_name
varchar(20),salary int,e_age int);
insert into employee values(1,'vishwa',50000,19); insert into employee values(2,'het',35000,20);
insert into employee values(3,'priyal',45000,18); insert into employee values(4,'kavya',15000,16);
insert into employee values(5,'khushi',25000,21);
▪SUM() function:
Syntax: select sum(column_name) from table_name; Example: select sum(salary)as total from
employee;
▪MAX() function:
Syntax: select max(column_name) from table_name;
\Example: select max(salary)as max from employee;
▪AVERAGE() function:
Syntax: select avg(column_name) from table_name;
Example: select avg(salary)as avg from employee;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
▪MIN() function:
Syntax: select min(column_name) from table_name;
Example: select min(salary)as min from employee;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
PRACTICAL-6
AIM: Displaying Data from Multiple Tables (Join).
•INNER JOIN:The INNER JOIN selects records that have
matching values in both tables.
Syntax: select column(s)from table1 inner join table2 on table1.column_name =
table2.column_name;
Example: select student.id,student.name,student.branch,employee.salary from student
inner join employee on student.id=employee.e_id;
•LEFT JOIN:The LEFT JOIN returns all records from the left
table (table1), and the matching records from the right table
(table2). The result is 0 records from the right side, if there is
no match.
Syntax: select column(s) from table1left join table2on table1.column_name =
table2.column_name;
Example: select student.id,student.name,employee.e_id from employee inner join
student on student.id=employee.e_id where id>2;
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
•RIGHT JOIN:The RIGHT JOIN returns all records from the
right table (table2), and the matching records from the left
table (table1). The result is 0 records from the left side, if
there is no match.
Syntax: select column(s) from table1 right join table2 on table1.column_name =
table2.column_name;
Example: select student.id,student.name,student.branch,employee.salary from
employee right join student on student.id=employee.e_id;
•FULL JOIN:Returns all rows when there is a match in either
the left table or the right table. If there is no match, null
values are returned for the missing side.
Syntax: select column_name(s) from table1 full outer join table2 on
table1.column_name = table2.column_name;
Example: select student.id,student.name,employee.salary from employee full join
student on student.id=employee.e_id;
NOTe: MYSQL does not support the Full Outer Join, it supports the UNION .
Enroll no.:23BEIT54009 Margi Patel
IT SEM 3
PRACTICAL-7
AIM:Using Commit and Rollback show Transaction ACID Property.
•SET TRANSACTION: Starts a new transaction. Syntax: start transaction; or begin;
•COMMIT: Commits the current transaction, making all changes permanent.
Syntax: COMMIT;
•SAVEPOINT: Creates a savepoint within the transaction, allowing a partial rollback to this
point.
Syntax: SAVEPOINT savepoint_name;
ROLLBACK: Rolls back the current transaction, undoing all changes
made since the last COMMIT or ROLLBACK.
Syntax: ROLLBACK;
•ROLLBACK TO SAVEPOINT: Rolls back the transaction to the specified savepoint, undoing
changes made after the savepoint was set.
Syntax: ROLLBACK;
Example: start transaction;
savepoint s1;
update student set name = 'misha' where id = 2;
select * from student;
commit;
Example: start transaction;
savepoint s2;
update student set name = 'poojan' where id = 2;
Rollback to s2;
Select * from student;
commit;
Enroll no.:23BEIT54009 Margi Patel