You are on page 1of 41

DATABASE MANAGEMENT SYSTEM

Practical File

BACHELOR OF TECHNOLOGY
(ELECTRONICS AND COMMUNICATION ENGINEERING)

Submitted by: -
DEEPANSHU MEHALAWAT
00413204920
ECE-3

Submitted to: -
Mrs. Deeblena Mitra

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?

• SQL stands for Structured Query Language


• SQL lets you access and manipulate databases
• SQL became a standard of the American National Standards Institute (ANSI) in 1986,
and of the International Organization for Standardization (ISO) in 1987

WHAT CAN SQL DO?

• SQL can execute queries against a database


• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views

SQL IS A STANDARD - BUT....

• 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.

USING SQL IN YOUR WEB SITE

To build a web site that shows data from a database, you will need:

• An RDBMS database program (i.e. MS Access, SQL Server, MySQL)


• To use a server-side scripting language, like PHP or ASP
• To use SQL to get the data you want
• To use HTML / CSS to style the page
→RDBMS (Relational Database Management System)

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;

SQL is not case sensitive; Select is same as select.

SEMICOLON after SQL statements?

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.

Most important SQL commands: -

• SELECT - extracts data from a database


• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
EXPERIMENT-2
AIM- 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’.

THEORY-

 THE SQL CREATE DATABASE STATEMENT


The CREATE DATABASE statement is used to create a database.
Syntax:CREATE DATABASE dbname;
Example:CREATE DATABASE my_db;
 THE SQL CREATE TABLE STATEMENT
The CREATE TABLE statement is used to create a table in adatabase.Tables are
organized into rows and columns; and each table must have a name.
Syntax:CREATE TABLE table_name
(column_name1 data_type(size),
column_name2 data_type(size)
, column_name3 data_type(size),....);
Example:CREATE TABLE Persons
(PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255));
 S QL USE STATEMENT
THE
The USE statement is used to select any existing database in SQL schema.When you
have multiple databases in your SQL Schema, then before starting your operation, you
would need to select a database where all the operations would be performed.
Syntax: USE dbname;
Example: USE my_db;
 THE SQL INSERT INTO STATEMENT
The INSERT INTO statement is used to insert new records in a table. It is possible to write
the INSERT INTO statement in two forms. The first form does not specify the column
names where the data will be inserted, only their values:
Syntax:INSERT INTO table_name
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name
(column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Example:INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
QUERIES-
1. create database college;
2. use college; create table emp (ename varchar(40), ecity varchar(40), salary float,
enumber int, eaddress varchar(50), deptname varchar(30)); insert into emp
values('Ram', 'Delhi', 85000, 10, 'Subhash Nagar', 'IT'); insert into emp
values('Shyam', 'Delhi', 80000, 11, 'Hari Nagar', 'CS'); insert into emp
values('Sita', 'Delhi', 85300, 12, 'Ashok Nagar', 'HR'); insert into emp
values('Gita', 'Delhi', 82000, 13, 'Patel Nagar', 'HR');
insert into emp values('Rita', 'Delhi', 83000, 14, 'Shyam Nagar', 'IT');
3. create table company (cName varchar(20), cCity varchar(40), empnumber int,
no_of_emp int); insert into company values('Infosys', 'Delhi', 10, 500); insert into
company values('TCS', 'Delhi', 11, 600); insert into company values('Wipro',
'Delhi', 12, 450); insert into company values('TCS', 'Gurugaon', 13, 550); insert
into company values('Infosys', 'Bangalore', 14, 580);

OUTPUTS-
AIM
EXPERIMENT-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’. 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-
AIM
EXPERIMENT-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.

THEORY-

 ALTER: used to alter the contexts of table


Syntax: alter table tablename add new volume datatype; Eg:
alter table student add age int;
 DELETE: used to delete
Syntax : delete from tablename where condition
Eg: delete from student where rollno = 1

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-
AIM
EXPERIMENT-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’.
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.
AIM
---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-
AIM

EXPERIMENT-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.
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-
AIM

EXPERIMENT-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.
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-
AIM
EXPERIMENT-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’.
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-

1. alter table emp alter column enumber


SET NOT NULL; alter table emp
add constraint g1 primary key(enumber);
select * from emp;
2. alter table company add constraint
g2 foreign key(empnumber)
AIM
references emp(enumber); select *
from company;
3. alter table company add constraint
g3 unique(cname); select * from
company;
4. alter table company alter column
ccity set default 'Delhi'; select * from
company;
5. OUTPUTS-
AIM-
EXPERIMENT-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’.
THEORY-
 RENAME TABLE: RENAME keyword is used to change table name.
Syntax:ALTER TABLE <table-name>
RENAME <new-table-name>;
Example:ALTER TABLE emp
RENAME empl;
 RENAME COLUMN: CHANGE keyword is used to change column name
Syntax: ALTER TABLE <table-name>
CHANGE <old-col-name> <new-table-name> <datatypesize(n)>;
Example:ALTER TABLE emp
CHANGE eno empno int(4);
QUERIES-
1. rename table emp to emp1; select
* from emp1;
2. alter table emp1 change ename
empname varchar(30); select *
from emp1;
OUTPUTS-
AIM-
EXPERIMENT-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’.
THEORY-An SQL JOIN clause is used to combine rows from two or more tables, based on a
common field between them.
 INNER JOIN:The INNER JOIN keyword selects all rows from both tables as long
as there is a match between the columns in both tables.
Syntax: SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Example: SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER
BY Customers.CustomerName;
 LEFT JOIN:The LEFT JOIN keyword returns all rows from the left table (table1),
with the matching rows in the right table (table2). The result is NULL in the right side
when there is no match.
Syntax: SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
Example: SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER
BY Customers.CustomerName;
 RIGHT JOIN: The RIGHT JOIN keyword returns all rows from the right table
(table2), with the matching rows in the left table (table1). The result is NULL in the
left side when there is no match.
Syntax: SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
Example: SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;

QUERIES-
AIM-
1. select emp1.*, company.* from
emp1 cross join company;
2. select emp1.empname from
emp1 inner join company on
emp1.enumber =
company.empnumber where
cname = 'TCS';
OUTPUTS-
EXPERIMENT-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.
THEORY-
 UNION OPERATOR :The UNION operator is used to combine the result-set of two
or more SELECT statements. With each SELECT statement within the UNION must
have the same number of columns. The columns must also have similar data types.
Also, the columns in each SELECT statement must be in the same order. The UNION
operator selects only distinct values by default. To allow duplicate values, use the
ALL keyword with UNION.
Syntax:SELECT column_name(s)
FROM table1
UNION SELECT column_name(s)
FROM table2;
Example: SELECT City
FROM Customers
UNION SELECT City
FROM Suppliers
ORDER BY City; QUERIES-
1. select enumber from emp1
where ecity = 'Delhi'
union select empnumber from
company where ccity =
'Gurugaon';
2. select enumber from emp1
where ecity = 'Delhi' and
enumber not in (select
empnumber from company
where ccity = 'Gurugaon');
OUTPUTS-
AIM-
EXPERIMENT-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.
THEORY-
 SCALER FUNCTIONS (MATHEMATICAL FUNCTIONS):
• POWER – power() returns a value of a number raised to the power of another
number.
Syntax– select power(value,value) as alias name;
Eg – select power(100,0.5) as pow;

• SQUARE – It is used to square the number in the column.


syntax – select square(attribute) from table;
Eg – select square(marks) from student;
 STRING FUNCTIONS:
• SUBSTRING – It extracts a substring from a string.
syntax – select substring(expression,start,length);
Eg – select substring(‘abcdef’, 2, 3);

• REVERSE/UPPER/LOWER – Reverse is used to reverse the attribute


pushed, upper is used to transform the attribute pushed to uppercase and lower
is used to make attribute lowercase letters.
syntax – select reverse/upper/lower(colname) from table name;
Eg – select reverse(sname) from student;

• CONCATENATION – It is used to concatenate two columns of table.


syntax – select concat (col1,col2) from table name;
Eg – select concat(sname,branch) as stu from student;
QUERIES-
1. select reverse(empname) from emp1;
2. select upper(ccity) from company;
3. select concat(empname, ecity) from emp1;
OUTPUTS-
AIM-
EXPERIMENT-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.
THEORY-
 VIEW: a view is a virtual table based on the result-set of an SQL statement. A view
contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
 CREATING VIEW :
Syntax: CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Example: CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No
 UPDATING A VIEW: You can update a view by using the following syntax:
Syntax: CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Example: CREATE OR REPLACE VIEW [Current Product List] AS
SELECT ProductID,ProductName,Category
FROM Products
WHERE Discontinued=No
 DROPPING A VIEW: You can delete a view with the DROP VIEW command.
Syntax: DROP VIEW view_name
Example: DROP VIEW one_emp; QUERIES-
1. create view v1 as (select
empname, ecity from emp1);
select * from v1;
2. update v1 set ecity =
'Chennai' where empname =
'Ram'; select * from v1;
3. create view view2 as select *
from emp1, company
where emp1.enumber = company.empnumber;
select * from view2;
AIM-
4. update view2 set salary =
salary + 1000 where
deptname = 'IT'; select * from
view2;
OUTPUTS-
To study the commands involving indexes and execute the following queries:
EXPERIMENT-14
AIM-
• 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.
THEORY-
Indexes are used to retrieve data from the database very fast. The users cannot see the
indexes, they are just used to speed up searches/queries.
 CREATE INDEX:Creates an index on a table. Duplicate values are allowed.
Syntax: CREATE INDEX index_name
ON table_name ( column1 , column2 , ...);
Example: CREATE INDEX idx_lastname
ON Persons (LastName);
 DROP INDEX:The DROP INDEX statement is used to delete an index in a table.
Syntax: ALTER TABLE table_name
DROP INDEX index_name ;
Example: ALTER TABLE Persons
DROP INDEX idx_lastname;
QUERIES-
1. create index i1 on emp1(empname);
2. create index i2 on company(cname, ccity);
3. alter table company drop index i2;
OUTPUTS-

Introduction to PL-SQL.
THEORY-
EXPERIMENT-15
AIM-
PL/SQL is a block structured language that enables developers to combine the power of SQL
with procedural statements. All the statements of a block are passed to oracle engine all at
once which increases processing speed and decreases the traffic.
DISADVANTAGES OF SQL:
 SQL doesn’t provide the programmers with a technique of condition checking,
looping and branching.
 SQL statements are passed to Oracle engine one at a time which increases traffic and
decreases speed.
 SQL has no facility of error checking during manipulation of data.
FEATURES OF PL/SQL:
 PL/SQL is basically a procedural language, which provides the functionality of
decision making, iteration and many more features of procedural programming
languages.
 PL/SQL can execute a number of queries in one block using single command.
 One can create a PL/SQL unit such as procedures, functions, packages, triggers, and
types, which are stored in the database for reuse by applications.
 PL/SQL provides a feature to handle the exception which occurs in PL/SQL block
known as exception handling block.
 Applications written in PL/SQL are portable to computer hardware or operating
system where Oracle is operational.
 PL/SQL Offers extensive error checking.
DIFFERENCES BETWEEN SQL AND PL/SQL:
SQL-
 SQL is a single query that is used to perform DML and DDL operations.
 It is declarative, that defines what needs to be done, rather than how things need to be
done.
 Execute as a single statement.
 Mainly used to manipulate data.
 Cannot contain PL/SQL code in it. PL/SQL-
 PL/SQL is a block of codes that used to write the entire program blocks/procedure/
function, etc.
 PL/SQL is procedural that defines how the things needs to be done.
 Execute as a whole block.
 Mainly used to create an application.
 It is an extension of SQL, so it can contain SQL inside it.
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.
EXPERIMENT-16
AIM-
THEORY-
 CONDITIONAL STATEMENTS:
syntax - if condition 1 then
statement to be executed if condition is true
else if condition 2 then
statement to be executed if condition 2 is true
else
statement to be executed if condition 1 & 2 is false
endif.
 CASE STATEMENT: It selects one sequence of statements to execute.
syntax – case expression
when value 1 then
statement to be executed when expression = value 1;
when value 2 then
statement to be executed when expression = value 2;
.
.
.
else
statement to be executed when no value match
end case;
QUERIES-
1. delimiter//
create procedure g()
begin
select avg(salary), if (avg(salary)< 10000, ‘ Increase salary’, ‘ OK’) as decision from
emp1;
end;//
delimiter;
call p;
2. select designation,
case
when designation = ‘Project Head’ then 1
when designation = ‘Computer Science’ then 2
else 3
end as designation
from emp1;
OUTPUTS-
EXPERIMENT-17
AIM- 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.
THEORY-
 PROCEDURE:It is a stored program that we can pass parent into it but does not
return a value.
syntax – Create procedure procedure_name (parameter datatype)
begin declaration_section executable_section end;
# Note: To drop procedure we use syntax:
drop procedure procedure_name;
 TRIGGERS:Triggers are stored programs, which are automatically executed or
fixed when some events occurs. Syntax-Create trigger triggername (before/after)
(delete/update/insert) on tablename for each row begin
… variable declaration
… trigger code end;
QUERIES-
1. delimiter// create procedure sol
(num int) begin
select* from emp1 where salary = num; end;
//
delimiter; call
sol (21000);
OUTPUTS-
-X-X-X-X-X-X-X-X-X-X-

You might also like