DBMS LAB-1
DBMS LAB-1
AND TECHNOLOGY
MODINAGAR
The database management system (DBMS) is the software that interacts with end users,
applications, and the database itself to capture and analyse the data. The DBMS software
additionally encompasses the core facilities provided to administer the database. The sum total
of the database, the DBMS and the associated applications can be referred to as a "database
system". Often the term "database" is also used to loosely refer to any of the DBMS, the
database system or an application associated with the database.
A database has broad searching functionality. For example, a sales department could quickly
search for and find all sales personnel who had achieved a certain amount of sales over a
particular time period.
A database can update records in bulk – even millions or more records. This would be useful,
for example, if you wanted to add new columns or apply a data patch of some sort.
If the database is relational, which most databases are, it can cross-reference records in
different tables. This means that you can create relationships between tables. For instance, if
you linked a Customers table with an Orders table, you could find all purchase orders from the
Orders table that a single customer from the Customers table ever processed, or further refine it
to return only those orders processed in a particular time period – or almost any type of
combination you could imagine.
A database can perform complex aggregate calculations across multiple tables. For example,
you could list expenses across multiple retail outlets, including all possible sub-totals, and then
a final total.
A database can enforce consistency and data integrity, which means that it can avoid
duplication and ensure data accuracy through its design and a series of constraints.
Structure Query Language(SQL) is a database query language used for storing and
managing data in Relational DBMS. SQL was the first commercial language introduced for
E.F Codd's Relational model of database. Today almost all RDBMS(MySql, Oracle, Infomix,
Sybase, MS Access) use SQL as the standard database query language. SQL is used to
perform all types of data operations in RDBMS.On the surface, a database might seem much
like a spread sheet; it has data arranged in columns and rows. But that is where the similarity
ends because a database is far more powerful.
Experiment 1
AIM: Write the queries for Data Manipulation and Data definition Language.
Description :-
a. INSERT
b. UPDATE
c. DELETE
d. SELECT
DML COMMANDS:
SYNTAX:
INSERT Statement:
Single Row into a Table: INSERT INTO table – name [column- identifier-comma-list)] VALUES
(column-valuecomma-list);
Multiple Row into a Table: insert into <table name> values (&col1, &col2, ….);
a. CREATE
b. DROP
c. TRUNCATE
d. RENAME
e. ALTER
DDL COMMANDS:
SYNTAX:
ALTER:
Q1. Write a query to create a table employee with empno, ename, designation, and
salary.
VARCHAR2 (10),
Table created.
Q2. Write a query for create a table from an existing table with all the fields.
Table created.
Q3. Write a Query to Alter the column EMPNO NUMBER(4) TO EMPNO NUMBER(6).
Table deleted.
DML QUERIES:
Q3. Write a query to insert the records in to employee using substitution method.
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY');
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
1 row created.
SQL> /
1 row updated.
Description :-
• Arithmetic operators
• Comparison operators
• Logical operators
Pre-Requisite Data:
CUSTOMER TABLE
Q1. Write a query to find the salary of a person where age is <= 26 and salary >= 25000
from customer table.
SQL>SELECT * FROM CUSTOMERS WHERE AGE <= 26 AND SALARY >= 25000;
Output:
2 rows selected.
Q2. Write a query to find the salary of a person where age is <= 26 or salary > =33000
from customer table.
Output:
5 rows selected.
Q3.Write a query to find the name of customer whose name is like “Ku%”.
SQL>SELECT * FROM CUSTOMERS WHERE NAME LIKE 'Ku%';
Output:
1 row selected.
Q4. Write a query to find the customer details using “IN” and “Between” operator where
age can be 25 or 27.
Output:
2 rows selected.
Experiment No.3
AIM: Write SQL query using group by function.
Description :-
Q1: What are the appropriate data types accepted by GROUP BY functions?
GROUP BY Syntax:
ORDER BY column_name(s);
QUERY:
Output:
COUNT(ID) ADDRESS
2 Delhi
1 Mumbai
1 Kolkata
1 Hyderabad
1 Chennai
1 Noida
6 rows selected.
Experiment No.4
AIM:Write SQL queries for group functions.
Description :-
Column inventory;
ofColumn inventory;
Column inventory;
inventory;
To use a group function in a SQL query, list the function name followed by umericcolumn
name within parentheses. AVG averages the column, COUNT counts the numberof items,
MAX returns maximum number of the column, and MIN returns minimumnumber of the
column .The following is query to retrieve total price, average price, maximum price, and
minimum price from the table “product” assuming the product table has the
followingvalues.
QUERY:
PRODUCT TABLE
SUM(PRICE)
870
This statement will returns the total amount for the column price which is 870.
Avg(price)
217.50
This statement will returns the average amount for the column price which is 870/4 or
217.50
Max(price)
300
This statement will returns the maximum amount for the column price which is 300.
Experiment No.5
AIM:Write SQL queries for sub queries, nested queries.
Types
1. Sub queries that return several valuesSub queries can also return more than one value.
Such results should be made usealong with the operators in and any.
2. Multiple queries
Here more than one sub query is used. These multiple sub queries are combined bymeans of
„and‟ & „or‟ keywords
A sub query is evaluated once for the entire parent statement whereas a correlatedSub query is
evaluated once per row processed by the parent statement.
The purpose of a join concept is to combine data spread across tables. A join isactually
performed by the „where‟ clause which combines specified rows of tables.Syntax; select
columns from table1, table2 where logical expression;
Types of Joins 1.Simple Join 2.Self Join 3. Outer Join 4. Inner Join 1.
Simple Join
Table aliases are used to make multiple table queries shorted and more readable. Wegive an
alias name to the table in the „from‟ clause and use it instead of the namethroughout the query.
Self join: Joining of a table to itself is known as self-join. It joins one row in a tableto another. It
can compare each row of the table to itself and also with other rows ofthe same table.
Outer Join: It extends the result of a simple join. An outer join returns all the rowsreturned by
simple join as well as those rows from one table that do not match anyrow from the table. The
symbol (+) represents outer joins.
Inner join: Inner join returns the matching rows from the tables that are beingjoined
Queries:
EMPLOYEE TABLE
1 Mathi AP 1 30000
4 Karthik AP 1 35000
Q1. Display all employee names and salary whose salary is greater than minimum salary of
the company and job title starts with „A‟.
SQL>select ename,sal from emp where sal>(select min(sal) from emp where job like 'A%');
Output:
ENAME SALARY
Arjun 32000
Gugan 40000
Karthik 35000
3 rows selected.
Experiment No.6
AIM: Write programme by the use of PL/SQL.
Description :-
The PL/SQL programming language was developed by Oracle Corporation in the late
1980s as procedural extension language for SQL and the Oracle relational database. PL/SQL
has the following features −
Query:
DECLARE
a number (2) := 21;
BEGIN
IF (a = b) then
ELSE
END IF;
IF (a < b) then
ELSE
END IF;
IF ( a> b ) THEN
ELSE
END IF;
END;
/
Output:
Experiment No.7
AIM:Write SQL queries to create views.
Description :-
A view is nothing more than a SQL statement that is stored in the database with an
associated name. A view is actually a composition of a table in the form of a predefined SQL
query.
A view can contain all rows of a table or select rows from a table. A view can be created from
one or many tables which depends on the written SQL query to create a view.
Views, which are a type of virtual tables allow users to do the following −
➢ Structure data in a way that users or classes of users find natural or intuitive.
➢ Restrict access to the data in such a way that a user can see and (sometimes) modify
exactly what they need and no more.
➢ Summarize data from various tables which can be used to generate reports.
Syntax:
SELECT column1,column2,....
FROM table_name
WHERE condition;
Query:
Q1. Write a SQL query to create a view of customer table created in PRACTICAL no 1.
Output:
ID NAME ADDRESS
1 Akshay Delhi
2 Manish Mumbai
3 Kushagra Kolkata
4 Mukesh Hyderabad
5 Himanshu Chennai
6 Neeraj Noida
7 Nishant Delhi
Experiment No.8
AIM:Write an SQL query to implement JOINS.
Description :-
A SQL join clause combines columns from one or more tables in a relational database.
It creates a set that can be saved as a table or used as it is. A JOIN is a means for combining
columns from one (self-table) or more tables by using values common to each. ANSI-standard
SQL specifies five types of JOIN: INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER
and CROSS. As a special case, a table (base table, view, or joined table) can JOIN to itself in a
self-join.
A programmer declares a JOIN statement to identify rows for joining. If the evaluated predicate
is true, the combined row is then produced in the expected format, a row set or a temporary
table.
QUERIES:
EMPLOYEE TABLE
1 Mathi AP 1 30000
4 Karthik AP 1 35000
DEPARTMENT TABLE
2 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Q1. Display the employee details, departments that the departments are same in both the
emp and dept.
YORK
YORK
Experiment No.9
AIM: Write a query for extracting data from more than one table.
Query:
EMPLOYEE TABLE
1 Mathi AP 1 30000
4 Karthik AP 1 35000
DEPARTMENT TABLE
2 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Q1. Write a query to extract empno, ename, salary, dname and location from employee
and department table where empno = deptno without using joins.
2 rows selected.
Q2. Write a query to extract ename, salary and location from employee and department
table where is like 30, 40.
Output:
No rows Selected.
Experiment No.10
AIM:Write a query to understand the concepts for ROLL BACK, COMMIT & CHECK
POINTS.
Description :-
Commit command
Rollback command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
rollback to savepoint-name;
Savepoint command
Savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
savepointsavepoint-name;
QUERY:
Q1. Write a query to implement the save point.
Savepoint created.
Rollback complete.