RDBMS Notes
RDBMS Notes
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
4 Alibaba Active
Apple Inactive
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
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)
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.
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)
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)
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.
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