Professional Documents
Culture Documents
Dav Institute of Engineering & Technology, Jalandhar
Dav Institute of Engineering & Technology, Jalandhar
1
INDEX
2
11 To understand the use and working of Sub-Queries. 55-57
DBMS:
A Database Management System is a software program used to manage a database. These programs
enable users to access and modify database.
A DBMS is a complex set of software programs that controls the organization, storage, management, and
retrieval of data in a database.
A DBMS includes four main components, which are: Modeling Language, Data Structures, DB Query
Language and Report Writer, and Transaction Mechanism. Each of these components can be further broken
down into smaller and more specific pieces, but it is the sum of these parts which are combined to create the
management system around the particular database to be utilized.
A database management system, or DBMS, gives the user access to their data and helps them transform
the data into information.
Such database management systems include dBase, Paradox, IMS, and Oracle. These systems allow users
to create, update, and extract information from their databases. Compared to a manual filing system, the
biggest advantages to a computerized database system are speed, accuracy, and accessibility. A
database is a structured collection of data. Data refers to the characteristics of people, things, and events.
SQL is a database computer language designed for the retrieval and management of data in a relational
database. SQL stands for Structured Query Language.
SQL is a computer language for storing, manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database Management
Systems (RDMS) like MySQL, MS Access, Oracle and SQL Server use SQL as their standard database
language.
Applications of SQL:
SQL Commands:
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP. These commands can be classified into the following groups based on
their nature
Languages of DBMS:
1 CREATE
Creates a new table, a view of a table, or other object in the database.
ALTER
2
Modifies an existing database object, such as a table.
RENAME
3
rename an existing database object
DROP
4
Deletes an entire table, a view of a table or other objects in the database.
1 SELECT
Retrieves certain records from one or more tables.
INSERT
2
Creates a record.
UPDATE
3
Modifies records.
DELETE
4
Deletes records.
REVOKE
Takes back privileges granted from user.
1 COMMIT
Commits a Transaction.
ROLLBACK
2
Rollbacks a transaction in case of any error occurs.
SAVEPOINT
3
Sets a save point within a transaction..
Conceptual Level:
o The conceptual schema describes the design of a database at the conceptual level.
Conceptual level is also known as logical level.
o The conceptual schema describes the structure of the whole database.
o The conceptual level describes what data are to be stored in the database and also describes what
relationship exists among those data.
o In the conceptual level, internal details such as an implementation of the data structure are hidden.
o Programmers and database administrators work at this level.
External Level:
o At the external level, a database contains several schemas that sometimes called as subschema. The
subschema is used to describe the different view of the database.
o An external schema is also known as view schema.
o Each view schema describes the database part that a particular user group is interested and hides
the remaining database from that user group.
# Advantages of DBMS:
o Controls database redundancy: It can control data redundancy because it stores all the data in
one single database file and that recorded data is placed in the database.
o Data sharing: In DBMS, the authorized users of an organization can share the data among
multiple users.
o Easily Maintenance: It can be easily maintainable due to the centralized nature of the database
system.
o Backup: It provides backup and recovery subsystems which create automatic backup of
data from hardware and software failures and restores the data if required.
o Multiple user interface: It provides different types of user interfaces like graphical user
interfaces, application program interfaces
# Disadvantages of DBMS:
o Cost of Hardware and Software: It requires a high speed of data processor and large memory size
to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them efficiently.
o Higher impact of failure: Failure is highly impacted the database because in most of the
organization, all the data stored in a single database and if the database is damaged due to electric
failure or database corruption then the data may be lost forever.
Experiment No: 2
Oracle features:
Logical data structure – Oracle uses the logical data structure to store data so that you can interact with
the database without knowing where the data is stored physically.
Partitioning – is a high-performance feature that allows you to divide a large table into different pieces
and store each piece across storage devices.
Memory caching – the memory caching architecture allows you to scale up a very large database that
still can perform at a high speed.
Data Dictionary is a set of internal tables and views that support administer Oracle Database more
effectively.
Backup and recovery – ensure the integrity of the data in case of system failure. Oracle includes a
powerful tool called Recovery Manager (RMAN) – allows DBA to perform cold, hot, and
incremental database backups and point-in-time recoveries.
Clustering – Oracle Real Application Clusters (RAC) – Oracle enables high availability that enables the
system is up and running without interruption of services in case one or more server in a cluster fails.
#Oracle Built-in Data Types:
Following table summarizes Oracle built-in data types.
Types Description
Following are the steps involved in installing the Oracle 18c Express Edition in Window Server:
Step: 1
Download the Oracle Database 18c Express Edition from the Oracle website. It will be in the Zip form, start
by extracting the file.
Step: 2
After Extraction we will have a number of files, we need to run the setup.exe file.
It will check the system before installation.
Step: 3
After system check we will get a window where we are required to click Next.
Step:4
After that we will have to accept the License agreement in order to proceed.
Step: 5
In this step we are required to set the destination address where Oracle will get installed.
Step:6
In this step we need to set the Database Password, After typing the password click Next.
Step: 7
We will have a review window to check the settings.
Step:8
After clicking Install, Oracle will start installing.
Syntax:
CREATE TABLE
table_name( column1 datatype,
column2 datatype, column3
datatype,
.....
columnN datatype
);
Implementation:
The ALTER TABLE command is used to add, delete or modify columns in an existing table. You should
also use the ALTER TABLE command to add and drop various constraints on an existing table
Syntax:
ALTER TABLE - ADD Column
Implementation:
To change the data type of a column in a table, use the following syntax:
Implementation:
Drop Table Statement:
The DROP TABLE statement is used to drop an existing table in a database. This command not only deletes
all the records in the table, but also removes the definition of a table.
Syntax:
DROP TABLE table_name;
Implementation:
Experiment No: 4
SELECT Statement:
A SELECT statement is used as a data retrieval statement i.e. It retrieves information from the database.
Syntax:
SELECT * FROM table_name;
Implementation:
Implementation:
UPDATE Statement:
The UPDATE statement is used to modify the existing records in a table. You can use the WHERE clause
with the UPDATE query to update the selected rows, otherwise all the rows would be affected.
Syntax:
UPDATE table_name SET column=value, column1=value ,…. WHERE condition;
Implementation:
DELETE Statement:
The SQL DELETE Query is used to delete the existing records from a table. We can use WHERE clause.
Syntax:
DELETE FROM Table name WHERE condition;
Implementation:
Experiment No: 5
Aim: To make use of different clauses viz where, group by, having, order by, union, intersection,
set difference.
WHERE CLAUSE:
The WHERE clause is used to specify a condition while fetching the data from a single table or by joining
with multiple tables.
Syntax:
SELECT * FROM Column name WHERE
Condition;
Operators in the WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal
BETWEEN Between a certain range LIKE
Search for a pattern
IN To specify multiple possible values for a column
Implementation:
ORDER BY CLAUSE:
The ORDER BY clause is used to sort the data in ascending or descending order, based on one or more
columns. Some databases sort the query results in an ascending order by default.
Syntax:
SELECT * FROM table_name ORDER BY Condition ASC|DESC;
Implementation:
GROUP BY CLAUSE:
The GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into
groups. This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause.
Syntax:
SELECT column1, column2,….. FROM table_name WHERE conditions
GROUP BY column1, column2;
Implementation:
HAVING CLAUSE:
The HAVING Clause enables you to specify conditions that filter which group results appear in the
results.
Syntax:
SELECT * FROM Column GROUP BY condition HAVING condition;
Implementation:
UNION CLAUSE:
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without
returning any duplicate rows.
Syntax:
SELECT Column_name FROM table1 UNION SELECT Column_name FROM table2;
Implementation:
INTERSECT CLAUSE:
The INTERSECT clause/operator is used to combine two SELECT statements, but returns rows only from
the first SELECT statement that are identical to a row in the second SELECT statement. This means
INTERSECT returns only common rows returned by the two SELECT statements.
Syntax:
SELECT Column_name FROM table1 INTERSECT SELECT Column_name FROM table2;
Implementation:
MINUS CLAUSE:
The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is used to subtract
the result set obtained by first SELECT query from the result set obtained by second SELECT query.
Syntax:
SELECT Column_name FROM table1 MINUS SELECT Column_name FROM table2;
Implementation:
Experiment No: 6
Arithmetic Operations:
SUBTRACTION OPERATION:
It is use to perform subtraction operation on the data items, items include either single column or multiple
columns.
Syntax:
SELECT Column, Column-Column FROM Table;
Implementation:
ADDITION OPERATION:
It is used to perform addition operation on the data items, items include either single column or multiple
columns.
Syntax:
SELECT Column, Column+Column FROM Table;
Implementation:
DIVISION OPERATION:
It is use to perform division of data items.
Syntax:
SELECT Column, Column/2 FROM Table;
Implementation:
MULTIPLICATION OPERATION:
It is use to perform multiplication of data items.
Syntax:
SELECT Column, Column*Column FROM Table;
Implementation:
Logical Operations:
AND:
This operator displays only those records where both the conditions condition1 and condition2 evaluates
to True.
Syntax:
SELECT * FROM Table WHERE Condition AND Condition;
Implementation:
OR:
This operator displays the records where either one of the conditions condition1 and condition2 evaluates
to True. That is, either condition1 is True or condition2 True.
Syntax:
SELECT * FROM Table WHERE Condition OR Condition;
Implementation:
NOT:
The NOT operator displays a record if the condition(s) is NOT TRUE.
Syntax:
SELECT * FROM Table WHERE NOT Condition;
Implementation:
Experiment No: 7
Constraints:
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action, the
action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table level
constraints apply to the whole table.
Syntax:
CREATE TABLE table_name ( column1
datatype constraint, column2 datatype
constraint, column3 datatype constraint,
....
);
The following constraints are commonly used in SQL:
NOT NULL:
By default, a column can hold NULL values. If you do not want a column to have a NULL value, then
you need to define such a constraint on this column specifying that NULL is now not allowed for that
column.
Syntax:
Create table table_name(column_name NOT NULL,…..);
IMPLEMENTATION:
UNIQUE CONSTRAINT:
A unique key is a set of one or more than one fields/columns of a table that uniquely identify a record in a
database table.
Syntax:
Create table table_name(column_name UNIQUE,…);
#IMPLEMENTATION:
PRIMARY KEY:
A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary
keys must contain unique values. A primary key column cannot have NULL values.
Syntax:
Create table table_name(column name NOT NULL PRIMARY KEY,…..);
IMPLEMENTATION:
FOREIGN KEY:
A foreign key is a key used to link two tables together. This is sometimes also called as a referencing key.
A Foreign Key is a column or a combination of columns whose values match a Primary Key in a different
table.
Syntax:
Create table Table1(column1 NOT NULL PRIMARY KEY, column2 varchar(10));
IMPLEMENTATION:
Experiment No: 8
Aggregate Function:-
In database management an aggregate function is a function where the values of multiple rows are
grouped together as input on certain criteria to form a single value of more significant meaning.
Syntax −
SELECT FUNCTION_NAME(Column_Name) FROM Table_Name;
COUNT Function:
The COUNT function is used with SQL SELECT statement and it is very useful to count the number of
rows in a table having enormous data.
Syntax:
Select COUNT(Column) From Table;
IMPLEMENTATION:
SUM Function:
SQL SUM function is used to find out the sum of a field in various records.
Syntax:
Select SUM(Column) From Table;
IMPLEMENTATION:
AVG Function:
It is used to find the Average of a field.
Syntax:
Select AVG(Column) From Table;
IMPLEMENTATION:
MIN Function:
It is used to find the minimum value of a field.
Syntax:
Select MIN(Column) From Table;
IMPLEMENTATION:
MAX Function:
It is used to find the maximum value of a field.
Syntax:
Select MAX(Column) From Table;
IMPLEMENTATION:
VARIANCE Function:
The VARIANCE Function is used to find variance of the column specified as argument.
Syntax:
SELECT VARIANCE(Column) FROM Table;
IMPLEMENTATION:
Numeric Functions:
Numeric Functions are used to perform operations on numbers and return numbers
Syntax :
SELECT FUNCTION NAME (Column_Name) FROM Table_Name; Following
are the numeric functions defined in SQL:
ROUND:
The ROUND function rounds a number to a specified number of decimal places.
Syntax:
Select round(number, no of decimal places needed) from Table;
IMPLEMENTATION:
TRUNC:
Truncates value of a number up to specified decimal places.
Syntax:
Select trunc(number, no of decimal places needed) from Table;
IMPLEMENTATION:
MOD:
It is used to find Remainder of a number divided by another number.
Syntax:
Select MOD(number, number1) from Table;
IMPLEMENTATION:
CEIL:
It is used to find a number greater than or equal to the given number.
Syntax:
Select CEIL(number) from Table;
IMPLEMENTATION:
FLOOR:
It is used to find a number smaller than or equal to the given number.
Syntax:
Select FLOOR(number) from Table;
IMPLEMENTATION:
ABS:
It is used to find the Absolute value of a number.
Syntax:
Select ABS(number) from Table;
IMPLEMENTATION:
Experiment-9
TO_CHAR:
TO_CHAR function is used to typecast a numeric or date input to character type with a format model
(optional).
Syntax:
select TO_CHAR(Column name, '$999999') from table name;
IMPLEMENTATION:
TO_DATE:
The function takes character values as input and returns formatted date equivalent of the same.
Syntax:
select TO_DATE('12 Dec 2020', 'DD MM YYYY') from dual;
IMPLEMENTATION:
NVL:
NVL converts a null value to an actual value. Data types that can be used are date, character and number.
Data type must match with each other i.e. expr1 and expr2 must of same data type.
Syntax:
select NVL(Null value, “Against Null value”) from dual;
IMPLEMENTATION:
UPPER:
The SQL UPPER function converts all the letters in a string into uppercase.
Syntax:
select UPPER(column name) from table name;
IMPLEMENTATION:
LOWER:
The SQL LOWER function converts all the letters in a string into lowercase.
Syntax:
select LOWER(column name) from table name;
IMPLEMENTATION:
INITCAP:
The Oracle INITCAP function sets the first letter of each word in uppercase.
Syntax:
select INITCAP(column name) from table name;
IMPLEMENTATION:
CONCAT:
It is used to concatenate strings.
Syntax:
select CONCAT(column name1, column name2) from table name;
IMPLEMENTATION:
LENGTH:
It is used to find the length of the string.
Syntax:
select LENGTH(column_name) from table_name;
IMPLEMENTATION:
LAPD:
LPAD function is used to padding the left side of a string with a specific set of characters.
Syntax:
select LPAD(column name, length, “padding symbol”) from table name;
IMPLEMENTATION:
RAPD:
RPAD function is used to padding the right side of a string with a specific set of characters.
Syntax:
select RPAD(column name, length, “padding symbol”) from table name;
IMPLEMENTATION:
SUBSTR:
Syntax:
select SUBSTR(column name, start position, End position) from table name;
IMPLEMENTATION:
INSTR:
The INSTR functions search string for substring. The INSTR function returns a numeric value.
Syntax:
select INSTR(column name, “string/character”) from table name;
IMPLEMENTATION:
Experiment-10
Oracle join is used to combine columns from two or more tables based on values of the related columns.
Equi join:
SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables.
An equal sign (=) is used as comparison operator in the where clause to refer equality.
Syntax:
IMPLEMENTATION:
INNER JOIN:
The INNER JOIN statement joins the left table to the right table using the values in the column.
Syntax:
IMPLEMENTATION:
Cross/Cartesian join:
The CROSS JOIN is used to generate a paired combination of each row of the first table with each
row of the second table.
Syntax:
RIGHT JOIN:
The right join or right outer join is a reversed version of the left join. The right join makes a result set that
contains all rows from the right table with the matching rows from the left table. If there is no match, the
left side will have nulls.
Syntax:
IMPLEMENTATION:
LEFT JOIN:
The left join returns all rows from the left table with the matching rows if available from the right table. If
there is no matching row found from the right table, the left join will have null values for the columns of
the right table.
Syntax:
IMPLEMENTATION:
Oracle full outer join or full join returns a result set that contains all rows from both left and right tables,
with the matching rows from both sides where available. If there is no match, the missing side will have
nulls.
Syntax:
IMPLEMENTATION:
Experiment-11
THEORY:
A Sub query or Inner query or a Nested query is a query within another SQL query and embedded
within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the
data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Syntax:
Select * from table where column in (select column from table where column condition);
IMPLEMENTATION:
Subqueries with the INSERT Statement:
Subqueries also can be used with INSERT statements. The INSERT statement uses the data returned
from the subquery to insert into another table. The selected data in the subquery can be modified with any
of the character, date or number functions.
Syntax: Insert into table select * from table where column condition;
IMPLEMENTATION:
Syntax: Update table set column expression where column in (select column from table where
condition);
IMPLEMENTATION:
The subquery can be used in conjunction with the DELETE statement like with any other statements
mentioned above.
Syntax: Delete from table where column in (select column from table where condition);
IMPLEMENTATION:
Experiment-12
Aim: To make use of transaction control statements viz ROLLBACK, COMMIT AND SAVEPOINT.
THEORY:
Transaction Control Language(TCL) commands are used to manage transactions in the database. These
are used to manage the changes made to the data in a table by DML statements. It also allows statements
to be grouped together into logical transactions.
ROLLBACK:
The ROLLBACK command is the transactional command used to undo transactions that have not already
been saved to the database. The ROLLBACK command can only be used to undo transactions since the
last COMMIT or ROLLBACK command was issued.
Syntax:
ROLLBACK;
IMPLEMENTATION:
COMMIT:
The COMMIT command is the transactional command used to save changes invoked by a transaction to
the database. The COMMIT command saves all transactions to the database since the last COMMIT or
ROLLBACK command.
Syntax:
COMMIT;
IMPLEMENTATION:
SAVEPOINT:
The savepoint command is used to temporarily save a transaction so that you can rollback to that point
whenever required.
Syntax:
SAVEPOINT savepoint_name;
IMPLEMENTATION:
Practical-13
Aim: To make Views of a table and to perform Insert, Update, Select and Delete operations on View.
Syntax:
CREATE VIEW view_name AS SELECT column1, column2, ...FROM table_name WHERE condition;
IMPLEMENTATION:
SQL Updating a View:
A view can be updated with the CREATE OR REPLACE VIEW command.
Syntax:
CREATE OR REPLACE VIEW view_name AS SELECT column1, column2, ... FROM table_name
WHERE condition;
IMPLEMENTATION:
Insertion a Views:
It is used to Insert rows in views. We must specify a list of values for a particular row.
Syntax:
IMPLEMENTATION:
Syntax:
DROP VIEW view_name;
IMPLEMENTATION:
Practical-14
THEORY:
INDEXES:
An Index is a database object that is used by the server to find a row in a table Quickly. Indexing a table is an
access strategy, that is, a way to sort and search records in the table. Indexes are essential to improve the
speed with which the records can be located and retrieved from a table.
Indexes are of two types:
1. Simple Index.
2. Composite Index.
1. Simple Index: An Index created on a Single Column of a table is called a simple Index.
Syntax:
CREATE INDEX index_name ON table_name (column_name);
IMPLEMENTATION:
2. Composite Index: An Index Created on more than one column is called a Composite Index.
Syntax:
CREATE INDEX index_name on table_name (column1, column2);
IMPLEMENTATION:
3. Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A unique index does not
allow any duplicate values to be inserted into the table. The basic syntax is as follows.
Syntax:
IMPLEMENTATION: