0% found this document useful (0 votes)
94 views13 pages

RDBMS Notes

The document discusses various relational database concepts like relational algebra operations, data types, date functions, indexes, and join queries. Relational algebra operations covered include selection, projection, union, intersection, and rename. Data types, date functions, indexes, and different types of join queries such as inner, outer, and cross joins are also explained.

Uploaded by

AMAAN SHAIKH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views13 pages

RDBMS Notes

The document discusses various relational database concepts like relational algebra operations, data types, date functions, indexes, and join queries. Relational algebra operations covered include selection, projection, union, intersection, and rename. Data types, date functions, indexes, and different types of join queries such as inner, outer, and cross joins are also explained.

Uploaded by

AMAAN SHAIKH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Unit 1.2 Relational operations Algebra (select, project, union, intersection,


rename)
• RELATIONAL ALGEBRA is a widely used procedural query language.
• It uses various operations to perform this action.
• SQL Relational algebra query operations are performed recursively on a
relation.
• The output of these operations is a new relation, which might be formed
from one or more input relations.
SELECT (σ) (Selection ---> to select the rows of table)
• The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition.
• Sigma(σ)Symbol denotes it.
• It is used as an expression to choose tuples which meet the selection
condition.
• Select operator selects tuples that satisfy a given predicate.
Example : σp(r)
σ is the predicate
r stands for relation which is the name of the table
p is prepositional logic

σ sales > 50000 (Customers)


Output - Selects tuples from Customers
where sales is greater than 50000
Projection(π) (Projection --> for selecting the columns of table)
• Projection means choosing which columns (or expressions) the query
shall return.
• (pi) symbol is used to choose attributes from a relation. This operator
helps you to keep specific columns from a relation and discards the other
columns.

6
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Projection Example :
CustomerID CustomerName Status CustomerName Status

1 Google Active
Google Active
2 Amazon Active

3 Apple Inactive Amazon Active

4 Alibaba Active
Apple Inactive

Π CustomerName, (Customers) Alibaba Active


Status

Union operation (υ)


• UNION is symbolized by ∪ symbol. It includes all tuples that are in tables
A or in B. It also eliminates duplicate tuples. So, set A UNION set B would
be expressed as: A ∪ B
• It is used to combine the result of two or more SQL SELECT queries.
• In the union operation, all the number of datatype and columns must be
same in both the tables on which UNION operation is being applied.
• The union operation eliminates the duplicate rows from its result set.
Example: SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

7
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Intersection

• An intersection is defined by the symbol ∩


• A∩B
• Defines a relation consisting of a set of all tuple that are in both A and B.
However, A and B must be union-compatible.
• It is used to combine two SELECT statements.
• The Intersect operation returns the common rows from both the SELECT
statements.
• In the Intersect operation, the number of datatype and columns must be
the same.
• It has no duplicates and it arranges the data in ascending order by
default
Example: SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;

8
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Rename (ρ)
• Rename is a unary operation used for renaming attributes of a relation.
• ρ (a/b)R will rename the attribute 'b' of relation by 'a'.
• The RENAME operation is used to rename the output of a relation.

ρ X
(R)
where the symbol ‘ρ’ is used to denote the RENAME operator and R is the result
of the sequence of operation or expression which is saved with the name X.
E.G We can use the rename operator to rename STUDENT relation to
STUDENT1.
ρ(STUDENT1, STUDENT)

9
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Unit-2 Advanced SQL


2.1 Data types (NUMBER, CHAR, VARCHAR, VARCHAR2, CLOB, NCLOB, LONG,
DATE, RAW, LONGROW)
2.2 ROWID pseudo column & DUAL table
2.3 DATE Functions (SYSDATE, SYSTIMESTAMP, TO_CHAR, TRUNC, ROUND,
NEXT_DAY, LAST_DAY, MONTHS_BETWEEN, ADD_MONTHS)
2.4 Concepts of Index (Create, drop)
2.5 Join Queries
2.5.1 Inner Join
2.5.2 Outer Join (Left, Right, Full)
2.5.3 Cross Join
2.6 Sub Queries with(Insert, update and Delete)
2.7 Nested queries

2.1 Data types


Data types are used to represent the nature of the data that can be stored in the database
table.

For example, in a particular column of a table, if we want to store a string type of data then
we will have to declare a string data type of this column.

10
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

ROWID pseudocolumn
• For each row in the database, the ROWID pseudocolumn returns the address of
the row.
• a rowid value uniquely identifies a row in the database.
• Values of the rowid pseudocolumn have the datatype rowid.
• They are the fastest way to access a single row.
• They can show you how the rows in a table are stored.
• They are unique identifiers for rows in a table.
• You should not use rowid as the primary key of a table.
• If you delete and reinsert a row with the Import and Export utilities, for example,
then its rowid may change. If you delete a row, then Oracle may reassign its rowid
to a new row inserted later.
Example:
Selects the address of all rows that contain data for employees in department 20.

SELECT ROWID, last_name


FROM employees
WHERE department_id = 20;

11
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)
Dual table
• It is a table that is automatically created by Oracle Database along with
the data dictionary.
• DUAL is in the schema of the user SYS but is accessible by the name DUAL
to all users.
• Dual is a table that is created by Oracle together with data dictionary. It
consists of exactly one column named “dummy”, and one record. The
value of that record is X.
• It has one column, DUMMY, defined to be VARCHAR2(1), and contains
one row with a value X.
Example :
SELECT * FROM dual;
select sum(10,20,30) from dual;

DATE Functions

12
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Concepts of Index (Create, drop)


 Indexes are used to retrieve data from the database more quickly than
otherwise.
 The users cannot see the indexes, they are just used to speed up
searches/queries.
Syntax: (Creating Index)
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Creates a unique index on a table. Duplicate values are not allowed
CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);

Example:
creates an index named "idx_Ename" on the “EName" column in the
“Employee" table
CREATE INDEX idx_Ename
ON employee(EName);
CREATE INDEX idx_Dname
ON Dept (Dno,DName);
Drop Index (delete Index)
DROP INDEX index_name;
Join Queries
• The SQL Joins clause is used to combine records from two or more tables
in a database. A JOIN is a means for combining fields from two tables by
using values common to each.
Example:
SELECT ID, NAME, AGE, AMOUNT
FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

13
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

There are different types of joins available in SQL :


• INNER JOIN − returns rows when there is a match in both tables.
• OUTER JOIN (Left, Right ,Full)
• LEFT JOIN − returns all rows from the left table, even if there are no
matches in the right table.
• RIGHT JOIN − returns all rows from the right table, even if there are
no matches in the left table.
• FULL JOIN − returns rows when there is a match in one of the tables.
• SELF JOIN − is used to join a table to itself as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
• CARTESIAN JOIN − returns the Cartesian product of the sets of records
from the two or more joined tables.

Inner join
• The INNER JOIN keyword selects all rows from
both the tables as long as the condition
satisfies.
Example :
SELECT table1.column1,table1.column2, table2.column1,....
FROM table1 INNER JOIN table2
ON table1.matching_column = table2.matching_column;

LEFT join
This join returns all the rows of the table on the left side of
the join and matching rows for the table on the right side
of join. The rows for which there is no matching row on
right side, the result-set will contain null. LEFT JOIN is also
known as LEFT OUTER JOIN.

14
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)
Example:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

RIGHT Join
RIGHT JOIN is similar to LEFT JOIN. This join returns all the
rows of the table on the right side of the join and matching
rows for the table on the left side of join. The rows for
which there is no matching row on left side, the result-set
will contain null. RIGHT JOIN is also known as RIGHT OUTER
JOIN.

Example :

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

FULL Join
FULL JOIN creates the result-set by combining
result of both LEFT JOIN and RIGHT JOIN. The
result-set will contain all the rows from both the
tables. The rows for which there is no matching,
the result-set will contain NULL values.

Example:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 FULL JOIN table2
ON table1.matching_column = table2.matching_column;

15
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Cross join
• When each row of first table is combined with each row from the second
table, known as Cartesian join or cross join. In general words we can say
that SQL CROSS JOIN returns the Cartesian product of the sets of rows
from the joined table.
We can specify a CROSS JOIN in two ways:
• Using the JOIN syntax.
• The table in the FROM clause without using a WHERE clause.

SELECT * FROM [TABLE1] CROSS JOIN [TABLE2]


OR
SELECT * FROM [ TABLE1] , [TABLE2]
Sub Query
• A Subquery 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.
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause, unless
multiple columns are in the main query for the subquery to compare its
selected columns.
• An ORDER BY command cannot be used in a subquery, although the main
query can use an ORDER BY. The GROUP BY command can be used to
perform the same function as the ORDER BY in a subquery.
• Subqueries that return more than one row can only be used with multiple
value operators such as the IN operator.
• The BETWEEN operator cannot be used with a subquery. However, the
BETWEEN operator can be used within the subquery.
16
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)

Sub query with select


SELECT column_name [, column_name ] FROM table1 [, table2 ]
WHERE column_name OPERATOR (SELECT column_name ,
column_name ] FROM table1 [, table2 ] [WHERE])
Example :
1. SELECT * FROM CUSTOMER
WHERE ID IN (SELECT ID FROM CUSTOMER WHERE
SALARY > 4500)
2. Display all the details of customer whose salary is more than
average salary.
Select * from customer
Where salary > ( select avg(salary) from customer)

Sub query with insert


The INSERT statement uses the data returned from the subquery to insert into
another table.
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ] [ WHERE VALUE OPERATOR ]
Example: Insert records into customer_detail table from customers table
INSERT INTO CUSTOMER_detail
SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID FROM CUSTOMERS where city =‘vapi’)

17
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana
FYBCA SEM II RELATIONAL DATABASE MANAGEMENT SYSTEM CONCEPT (205)
Sub query with update
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ]

Example :
UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );

Update customer
Set salary= salary *0.25
Where age in (80,45,56,66,69,78,89)
Sub query with delete
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ]

Example:
DELETE FROM CUSTOMERS
WHERE salary >
(SELECT salary FROM CUSTOMERS_BKP WHERE age>50);

18
ROFEL, SHRI G.M BILAKHIA COLLEGE OF APPLIED SCIENCES,VAPI Asst.prof Tanvi Rana

You might also like