DBMS Practical File
DBMS Practical File
Practical File
BACHELOR OF TECHNOLOGY
(ELECTRONICS AND COMMUNICATION ENGINEERING)
Submitted by: -
Sneha Soniwal
02176802820
ECE-3
Submitted to: -
Prof. Rashmi Arora
DEPARTMENT OF ECE
GURU TEGH BAHADUR INSTITUTE OF TECHNOLOGY
(AFFILIATED TO GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY, DELHI)
NEW DELHI – 110064
INDEX
1. Introduction to SQL.
2. To study Basic SQL commands (create database, create table, use, drop, insert) and execute the
following queries using these commands:
Create a database named ‘Employee’.
Use the database ‘Employee’ and create a table ‘Emp’ with attributes
‘ename’,’ecity’,’salary’,’enumber’,’eaddress’,’depttname’.
Create another table ‘Company’ with attributes ‘cname’, ccity’,’empnumber’ in the
database ‘Employee’.
3. To study the viewing commands (select, update) and execute the following queries using these
commands:
Find the names of all employees who live in Delhi.
Increase the salary of all employees by Rs. 5,000.
Find the company names where the number of employees is greater than 10,000.
Change the Company City to Gurgaon where the Company name is ‘TCS’.
4. To study the commands to modify the structure of table (alter, delete) and execute the following
queries using these commands:
Add an attribute named ‘Designation’ to the table ‘Emp’.
Modify the table ‘Emp’, Change the datatype of ‘salary’ attribute to float.
Drop the attribute ‘depttname’ from the table ‘emp’.
Delete the entries from the table ‘Company’ where the number of employees are less than
500.
5. To study the commands that involve compound conditions (and, or, in , not in, between , not
between , like , not like) and execute the following queries using these commands:
Find the names of all employees who live in ‘Gurgaon’ and whose salary is between Rs.
20,000 and Rs. 30,000.
Find the names of all employees whose names begin with either letter ‘A’ or ‘B’.
Find the company names where the company city is ‘Delhi’ and the number of employees
is not between 5000 and 10,000.
Find the names of all companies that do not end with letter ‘A’.
6. To study the aggregate functions (sum, count, max, min, average) and execute the following
queries using these commands:
Find the sum and average of salaries of all employees in computer science department.
Find the number of all employees who live in Delhi.
Find the maximum and the minimum salary in the HR department.
7. To study the grouping commands (group by, order by) and execute the following queries using
these commands:
List all employee names in descending order.
Find number of employees in each department where number of employees is greater
than 5.
List all the department names where average salary of a department is Rs.10,000.
8. To study the commands involving data constraints and execute the following queries using these
commands:
Alter table ‘Emp’ and make ‘enumber’ as the primary key.
Alter table ‘Company’ and add the foreign key constraint.
Add a check constraint in the table ‘Emp’ such that salary has the value between 0 and
Rs.1,00,000.
Alter table ‘Company’ and add unique constraint to column cname.
Add a default constraint to column ccity of table company with the value ‘Delhi’.
9. To study the commands for aliasing and renaming and execute the following queries using these
commands:
Rename the name of database to ‘Employee1’.
Rename the name of table ‘Emp’ to ‘Emp1’.
Change the name of the attribute ‘ename’ to ‘empname’.
10. To study the commands for joins (cross join, inner join, outer join) and execute the following
queries using these commands:
Retrieve the complete record of an employee and its company from both the table using
joins.
List all the employees working in the company ‘TCS’.
11. To study the various set operations and execute the following queries using these commands:
List the enumber of all employees who live in Delhi and whose company is in Gurgaon
or if both conditions are true.
List the enumber of all employees who live in Delhi but whose company is not in
Gurgaon.
12. To study the various scalar functions and string functions ( power, square, substring, reverse,
upper, lower, concatenation) and execute the following queries using these commands:
Reverse the names of all employees.
Change the names of company cities to uppercase.
Concatenate name and city of the employee.
13. To study the commands for views and execute the following queries using these commands:
Create a view having ename and ecity.
In the above view change the ecity to ‘Delhi’ where ename is ‘John’.
Create a view having attributes from both the tables.
Update the above view and increase the salary of all employees of IT department by
Rs.1000.
14. To study the commands involving indexes and execute the following queries:
Create an index with attribute ename on the table employee.
Create a composite index with attributes cname and ccity on table company.
Drop all indexes created on table company.
15. Introduction to PL-SQL.
16. To study the conditional controls and case statement in PL-SQL and execute the following
queries:
Calculate the average salary from table ‘Emp’ and print increase the salary if the average
salary is less than 10,000.
Print the deptno from the employee table using the case statement if the deptname is
‘Technical’ then deptno is 1, if the deptname is ‘HR’ then the deptno is 2 else deptno is 3.
17. To study procedures and triggers in PL-SQL and execute the following queries:
Create a procedure on table employee to display the details of employee to display the
details of employees by providing them value of salaries during execution.
Create a trigger on table company for deletion where the whole table is displayed when
delete operation is performed.
EXPERIMENT-1
AIM- Introduction to SQL.
THEORY-
WHAT IS SQL?
Although SQL is an ANSI/ISO standard, there are different versions of the SQL
language.
However, to be compliant with the ANSI standard, they all support at least the major
commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar
manner.
To build a web site that shows data from a database, you will need:
It is the basis of SQL and for all modern database systems such as Ms SOL server, IBM DB2
,oracle, MySQL and MS Access. The data in RDBMS is stored in database objects called tables.
A table is a collection of related data entries and it consists of column and rows.
Database Tables: A database more often contains one or more tables. Each table is identified by
a name. Table contains records with data.
SQL statements: Most of the actions you need to perform on a database done with SQL
statements following SQL statement select all the records is the “customer” table.
Select*from customer;
Some database requires a semicolon at the end of each SQL statement. Semicolon is standard
way to separate each SQL statement in database systems that allow more than 1 SQL statement
to be executed in same call to the server. But MS SQL servers executes query with or without the
semicolon.
THEORY-
OUTPUTS-
EXPERIMENT-3
AIM- To study the viewing commands (select, update) and execute the following queries
using these commands:
Find the names of all employees who live in Delhi.
Increase the salary of all employees by Rs. 5,000.
Find the company names where the number of employees is greater than 10,000.
Change the Company City to Gurgaon where the Company name is ‘TCS’.
THEORY-
SELECT: is used to view some or all specific details from a table specified.
Syntax: select *from tablename ;
Eg: select*from student.
UPDATE: is used to update the specific contents of the table. It is used to change and
alter table Values.
Syntax: where tablename set colname! = expression!
Where condition
Eg: update student set marks = marks +2
Where roll no.= 5
QUERIES-
1. select ename from emp
where ecity = 'Delhi';
2. update emp
set salary = salary + 5000;
select * from emp;
3. select cname from company
where no_of_emp > 500;
4. update company
set ccity = 'Gurugaon'
where cname = 'Wipro';
select * from company;
OUTPUTS-
EXPERIMENT-4
AIM- To study the commands to modify the structure of table (alter, delete) and execute the
following queries using these commands:
Add an attribute named ‘Designation’ to the table ‘Emp’.
Modify the table ‘Emp’, Change the datatype of ‘salary’ attribute to float.
Drop the attribute ‘depttname’ from the table ‘emp’.
Delete the entries from the table ‘Company’ where the number of employees
are less than 500.
THEORY-
QUERIES-
1. Alter table emp
Add designation varchar(30)
2. alter table emp
alter column salary type float;
select * from emp;
3. alter table emp
drop column designation;
select * from emp
4. delete from company
where no_of_emp < 500;
select * from company;
OUTPUTS-
EXPERIMENT-5
AIM- To study the commands that involve compound conditions (and, or, in , not in, between
, not between , like , not like) and execute the following queries using these commands:
Find the names of all employees who live in ‘Gurgaon’ and whose salary is
between Rs. 20,000 and Rs. 30,000.
Find the names of all employees whose names begin with either letter ‘A’ or ‘B’.
Find the company names where the company city is ‘Delhi’ and the number of
employees is not between 5000 and 10,000.
Find the names of all companies that do not end with letter ‘A’.
THEORY-
And/OR: The AND and OR operators are used to filter records based on more than
one condition.
--- the AND operator displays a record if all the condition separated by AND are true
Syntax: select* from table_name
Where <condition1> AND <condition2>;
Eg: select * from student
Where sname = ‘XYZ’ and roll = 10;
--- the OR operator displays a record if any of the conditions separated by OR is
TRUE.
Syntax: select * from tablename
Where<condition1> OR <condition2>;
Eg: select * from student
Where sname = ‘ABC’ OR roll = 1;
IN/NOT IN: the IN operator allows to specify multiple values is a WHERE clause. It
is a shorthand for multiple OR condition. NOT IN is its complement.
Syntax: select * from tablename
Where colname IN/NOT IN (‘value1’,’value2’);
Eg: -select * from student
Where sname in (‘ABC’,’XYZ’)
-select * from student
Where roll not in (1,3);
BETWEEN/NOT BETWEEN: The BETWEEN operator select values within a
given range. The values can be numbers, text or dates. The BETWEEN operator is
inclusive which means begin and end value is included. The NOT BETWEEN
operator select values outside the range.
Syntax: select * from table_name
Where colname BETWEEN/NOT BETWEEN
<lower limit> and <upper limit>;
Eg: select * from student
Where roll between 11 and 20;
LIKE/NOT LIKE: The like operator is used in a WHERE clause to search for a
specified pattern in a column. To get records that doesn’t match the like pattern we
use NOT LIKE. They are two wild cards often used in conjunction with the LIKE
operator:
--- the percent sign (%) represents zero, one or multiple character.
---the underscore ( _ ) represents one , single character.
Syntax : select * from tablename
Where colname like/not like <pattern>
Eg: -select * from student
Where sname like ’A%’;
-select * from student
Where sname like ‘A_C’;
(Name like ABC, ADC, AAC, etc)
QUERIES-
1. select ename from emp
where ecity = 'Delhi' and salary between 87000 and 93000;
2. select ename from emp
where ename like 'R%' or ename like 'G%';
3. select cname from company
where ccity = 'Delhi' and no_of_emp not between 550 and 600;
4. select cname from company
where cname not like '%o';
OUTPUTS-
EXPERIMENT-6
AIM- To study the aggregate functions (sum, count, max, min, average) and execute the
following queries using these commands:
Find the sum and average of salaries of all employees in computer science department.
Find the number of all employees who live in Delhi.
Find the maximum and the minimum salary in the HR department.
THEORY-
1. SUM (): the SUM () function returns the total sum of a numeric column.
Syntax: Select sum(marks) from students where roll between 1 and 10;
Eg: select sum(marks) from students where roll between 1 and 10;
2. COUNT (): the COUNT () function returns the number of rows that matches a
specified criterion.
Syntax: select count (col_name) from table_name where <condition>
Eg: select count (roll) from student;
3. MAX (): the MAX () function returns the longest values of the selected column.
Syntax: select max(colname) from table_name where<condition>;
Eg: select max(marks) from student;
4. MIN (): the MIN () function returns the smallest value of the selected column
Syntax: select min(col_name) from tablename where<condition>;
Eg: select min(marks) from student;
5. AVERAGE (): the AVG () function returns the average value of a numeric column.
Syntax: select avg(colname) from table_name where <condition>
Eg: select avg(marks) from student where roll between 1 and 20;
QUERIES-
1. select sum(salary), avg(salary) from emp
where deptname = 'IT';
2. select count(enumber) from emp
where ecity = 'Delhi';
3. select max(salary), min(salary) from emp
where deptname = 'HR';
OUTPUTS-
EXPERIMENT-7
AIM- To study the grouping commands (group by, order by) and execute the following
queries using these commands:
List all employee names in descending order.
Find number of employees in each department where number of employees is
greater than 5.
List all the department names where average salary of a department is
Rs.10,000.
THEORY-
ORDER BY:The ORDER BY keyword sorts the records in ascending order by
default. To sort the records in a descending order, you can use the DESC keyword.
SYNTAX: SELECT column_name , column_name
FROM table_name
ORDER BY column_name ASC|DESC,
column_name ASC|DESC;
Example: SELECT * FROM Customers
ORDER BY Country ASC,
CustomerName DESC;
GROUP BY:The GROUP BY statement is used to group the result-set by one or
more columns.
SYNTAX: SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example:SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
QUERIES-
1. select ename from emp
order by ename desc;
2. select deptname, count(enumber) from emp
group by deptname
having count(enumber) > 1;
3. select deptname from emp
group by deptname
having avg(salary) > 87000;
OUTPUTS-
EXPERIMENT-8
AIM- To study the commands involving data constraints and execute the following queries
using these commands:
Alter table ‘Emp’ and make ‘enumber’ as the primary key.
Alter table ‘Company’ and add the foreign key constraint.
Add a check constraint in the table ‘Emp’ such that salary has the value
between 0 and Rs.1,00,000.
Alter table ‘Company’ and add unique constraint to column cname.
Add a default constraint to column ccity of table company with the value
‘Delhi’.
THEORY-
PRIMARY KEY CONSTRAINT: The PRIMARY KEY CONSTRAINT uniquely
identifies each record in a table primary. Keys must contain unique values and cannot
contain null value.
SYNTAX - alter table tablename add constraint
consname primary key (colname);
Eg: alter table student add constraint
C1 primary key (roll);
FOREIGN KEY CONSTRAINT: A FOREIGN KEY CONSTRAINT specifies that
the key can only contain values that are in the referred primary key
SYNTAX: alter table tablename1 add constraint consname
foreign key ( colname) references tablename (primary key of table2);
Eg:alter table orders
add constraints C2 foreign key(Person ID)
references Persons (Person ID);
UNIQUE KEY: The UNIQUE constraint prevents two records from having identical
values in a column
SYNTAX: alter table tablename add constraint
constraint_name unique (colname);
Eg : alter table student add constraint
C3 unique (adm no);
DEFAULT CONSTRAINT: The default constraint is used to fill a column with a
default and fixed value
SYNTAX:alter table tablename alter column
Set default value;
Eg: alter table student alter column age set default 20;
QUERIES-
-X-X-X-X-X-X-X-X-X-X-