Rdbms Unit II
Rdbms Unit II
Rdbms Unit II
Relational data model is the primary data model, which is used widely around the world for data
storage and processing. This model is simple and it has all the properties and capabilities
required to process data with storage efficiency.
Concepts
Tables − In relational data model, relations are saved in the format of Tables. This format stores
the relation among entities. A table has rows and columns, where rows represents records and
columns represent the attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system represents relation
instance. Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and
their names.
Relation key − Each row has one or more attributes, known as relation key, which can identify
the row in the relation (table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope, known as attribute
domain.
• Key constraints
• Domain constraints
• Referential integrity constraints
1) Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there are more than
one such minimal subsets, these are called candidate keys.
• in a relation with a key attribute, no two tuples can have identical values for key
attributes.
2)Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a positive
integer. The same constraints have been tried to employ on the attributes of a relation. Every
attribute is bound to have a specific range of values. For example, age cannot be less than zero
and telephone numbers cannot contain a digit outside 0-9.
Referential integrity constraint states that if a relation refers to a key attribute of a different or
same relation, then that key element must exist.
THE RELATIONAL DATABASE STRUCTURE
A modern database, on the other hand, alleviates the problem of multiple updates of individual
files. The database enables the user to perform a single update across multiple files
simultaneously. A database is a collection of organized files that are electronically stored. The
files in a database are referred to as tables.
We come in contact with databases every day. Some examples of databases include ATMs,
computer-based card-catalog systems at a library, and numerous Internet features including order
forms and catalogs of merchandise.
The most popular and widely implemented type of database is called a relational database.
A relational database is a collection of two or more tables related by key values.
Tables
We refer to the tables in a database as two-dimensional files that contain rows (records) and
columns (fields). The reason we say that tables are two-dimensional is because the rows of a
table run horizontally and the columns run vertically, hence two dimensions. Take a look
at Figure 1-1.
SELECT (σ)
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 operation selects tuples that satisfy a given predicate.
σp(r)
σ is the predicate
p is prepositional logic
Example 1
Example 2
Output - Selects tuples from Tutorials where the topic is 'Database' and 'author' is guru99.
Example 3
Output - Selects tuples from Customers where sales is greater than 50000
Projection(π)
The projection eliminates all attributes of the input relation but those mentioned in the projection
list. The projection method defines a relation that contains a vertical subset of Relation.This
helps to extract the values of specified attributes to eliminates duplicate values. (pi) The symbol
used to choose attributes from a relation. This operation helps you to keep specific columns from
a relation and discards the other columns.
Example of Projection:
1 Google Active
2 Amazon Active
3 Apple Inactive
4 Alibaba Active
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active
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:
Example
Table A Table B
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
- Symbol denotes it. The result of A - B, is a relation which includes all tuples that are in A but
not in B.
Example
A-B
Table A - B
column 1 column 2
1 2
Intersection
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.
Example:
A∩B
Table A ∩ B
column 1 column 2
1 1
Cartesian product(X)
This type of operation is helpful to merge columns from two relations. Generally, a Cartesian
product is never a meaningful operation when it performs alone. However, it becomes
meaningful when it is followed by other operations.
σ column 2 = '1' (A X B)
Output – The above example shows all rows from relation A and B whose column 2 has value 1
σ column 2 = '1' (A X B)
column 1 column 2
1 1
1 1
JOIN operation also allows joining variously related tuples from different relations.
Types of JOIN:
Inner Joins:
• Theta join
• EQUI join
• Natural join
Outer join:
I . Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included, while the rest
are excluded. Let's study various types of Inner Joins:
1) Theta Join:
The general case of JOIN operation is called a Theta join. It is denoted by symbol θ
Example
A ⋈θ B
Theta join can use any conditions in the selection criteria.
For example:
column 1 column 2
1 2
2) EQUI join:
When a theta join uses only equivalence condition, it becomes a equi join.
For example:
column 1 column 2
1 1
EQUI join is the most difficult operations to implement efficiently in an RDBMS and one reason
why RDBMS have essential performance problems.
Example:
C D
2 4 2 8
3 9 3 18
C⋈D
C⋈D
2 4 4
3 9 9
In an outer join, along with tuples that satisfy the matching criteria, we also include some or all
tuples that do not match the criteria.
In the left outer join, operation allows keeping all tuple in the left relation. However, if there is
no matching tuple is found in right relation, then the attributes of right relation in the join result
are filled with null values.
A B
2 4 2 8
3 9 3 18
4 16 5 75
A B
A⋈B
2 4 4
3 9 9
4 16 -
2 4 8
3 9 18
4 16 -
5 - 75
In the right outer join, operation allows keeping all tuple in the right relation. However, if there is
no matching tuple is found in the left relation, then the attributes of the left relation in the join
result are filled with null values.
A B
A⋈B
2 8 4
3 18 9
5 75 -
In a full outer join, all tuples from both relations are included in the result, irrespective of the
matching condition.
A B
Summary
Operation Purpose
Select(𝛔) The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition
Projection(π) The projection eliminates all attributes of the input relation but those
mentioned in the projection list.
Union Operation(∪) UNION is symbolized by symbol. It includes all tuples that are in tables A
or in B.
Set Difference(-) - Symbol denotes it. The result of A - B, is a relation which includes all
tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation consisting of a set of all tuple that are in
both A and B.
Cartesian Product(X) Cartesian operation is helpful to merge columns from two relations.
Inner Join Inner join, includes only those tuples that satisfy the matching criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is denoted by
symbol θ.
EQUI Join When a theta join uses only equivalence condition, it becomes a equi join.
Natural Join(⋈) Natural join can only be performed if there is a common attribute
(column) between the relations.
Outer Join In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( ) In the left outer join, operation allows keeping all tuple in the left relation.
Right Outer join( ) In the right outer join, operation allows keeping all tuple in the right
relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in the result
irrespective of the matching condition.
Division
• Notation: r ÷ s
The result of r ÷ s is a relation on schema R − S = (A1, . . . , Am) • Suited for queries that include
the phrase “for all”. The result of the division operator consists of the set of tuples from r defined
over the attributes R − S that match the combination of every tuple in s.
r ÷ s := {t | t ∈ πR−S(r) ∧ ∀u ∈ s: tu ∈ r}
Assignment Operation
■ The assignment operation (←) provides a convenient way to express complex queries.
■ Example: Write r ÷ s as
temp1← ∏RS (r )
● The result to the right of the ← is assigned to the relation variable on the left of the ←.
Null values
In populating a database with data objects, it is not uncommon that some of these objects may
not be completely known. For example, in capturing new customer information through forms
that customers are requested to fill, some fields may have been left blank (some customers may
take exception to revealing their age or phone numbers!). In these cases, rather than not have any
information at all, we can still record those that we know about. But what value do we insert into
the unknown fields of data objects? Leaving a field blank is not good enough as it can be
interpreted as an empty string which may be a valid value for some domains. We need a value
that denotes 'unknown' and that cannot be confused with valid domain values.
It is here that the Null value is used. We can think of it as a special value different from any other
value from any attribute domain. At the same time, we may think of it as belonging to every
attribute domain in the database, ie. it may appear as a value for any attribute and not violate any
type constraints. Syntactically, different DBMSs may use different symbols to denote null
values. For our purposes, we will use the symbol '?'.
How do null values affect relational operations? All relational operations involve comparing
values in tuples, including Projection (which involves comparison of result tuples for duplicates).
The key to answering this question is in how we evaluate boolean operations involving null
values. Thus, for example, what does "? > 5" evaluate to? The unknown value could be greater
than 5. But then again, it may not be. That is, the value of the boolean expression cannot be
determined on the basis of available information. So perhaps we should consider the result of the
comparison as unknown as well?
Unfortunately, if we did this, the relational operations we've discussed cease to be well-defined!
They all rely on comparisons evaluating categorically to one of two values: TRUE or FALSE.
For example, if the above comparison ("? > 5") was generated in the process of selection, we
would not know whether to include or exclude the associated tuple in the result if we were to
admit a third value (UNKNOWN). If we wanted to do that, we must go back and redefine all
these operations based on some form of three-valued logic.
To avoid this problem, most systems that allow null values simply interpret any comparison
involving them as FALSE. The rationale is that even though they could be true, they are not
demonstrably true on the basis of what is known. That is, the result of any relational operation
conservatively includes only tuples that demonstrably satisfy conditions of the operation.
Adopting this convention, all the operations defined previously still hold without any
amendment. Some implications on the outcome of each operation are considered below.
For the Select operation, an unknown value cannot identify a tuple. This is illustrated in Figure
6-6 which shows two Select operations applied to the relation R. Note that between the two
operations, the selection criteria ranges over the entire domain of the attribute I2. One would
expect therefore, that any tuple in R1 would either be in the result of the first or the second. This
is not the case, however, as the second tuple in R1 (<b,?>) is not selected in either operation-the
unknown value in it falsifies the selection criteria of both operations!
For Projection, tuples containing null values that are otherwise identical are not considered to be
duplicates. This is because the comparison "? = ?", by the above convention, evaluates to
FALSE. This leads to the situation as illustrated in Figure 6-7 below. The reader should note
from this example that the symbol '?', while it denotes some value much like a mathematical
variable, is quite unlike the latter in that it's occurrences do not always denote the same value.
Thus "? = ?" is not demonstrably true and therefore considered FALSE.
In a Join operation, tuples having null values under the common attributes are not concatenated.
This is illustrated in Figure 6-8 ("?=1", "1=?" and "?=?" are all FALSE).
In Division, the occurrence of even one null value in the divisor means that the result will be an
empty relation, as any value in the dividend's common attribute(s) will fail when matched with it.
This is illustrated in Figure 6-9 below. Note, however, that this is not necessarily the case if only
the dividend contains null values under the common attribute(s) - division may still be successful
on tuples not containing null values.
In set operations, because tuples are treated as a single unit in comparisons, a single rule applies:
tuples otherwise identical but containing null values are considered to be different (as was the
case for Projection above). Figure 6-10 illustrates this for each set operation. Note that because
of the occurrence of null values, the tuples in R2 are not considered duplicates of R1's tuples.
Thus their union simply collects tuples from both relations; subtracting R2 from R1 simply
results in R1; and their intersection is empty.
1. Procedural Language - Those Languages which clearly define how to get the required
results from the Database are called Procedural Language. Relational algebra is a
Procedural Language.
2. Declarative Language - Those Language that only cares about What to get from the
database without getting into how to get the results are called Declarative
Language. Relational Calculus is a Declarative Language.
So Relational Calculus is a Declarative Language that uses Predicate Logic or First-Order Logic
to determine the results from Database.
Tuple Relational Calculus in DBMS uses a tuple variable (t) that goes to each row of the table
and checks if the predicate is true or false for the given row. Depending on the given predicate
condition, it returns the row or part of the row.
{t \| P(t)}
Where t is the tuple variable that runs over every Row, and P(t) is the predicate logic expression
or condition.
Let's take an example of a Customer Database and try to see how TRC expressions work.
Customer Table
Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
Workflow of query - The tuple variable "t" will go through every tuple of the Customer table.
Each row will check whether the Cust_Zipcode is 12345 or not and only return those rows that
satisfies the Predicate expression condition.
The TRC expression above can be read as "Return all the tuple which belongs to the
Customer Table and whose Zipcode is equal to 12345."
1 Rohit 12345
4. Amit 12345
Example 2: Write a TRC query to get the customer id of all the Customers.
Customer_id
Domain Relational Calculus uses domain Variables to get the column values required from the
database based on the predicate expression or condition.
{<x1,x2,x3,x4...> \| P(x1,x2,x3,x4...)}
where,
<x1,x2,x3,x4...> are domain variables used to get the column values required,
and P(x1,x2,x3...) is predicate expression or condition.
Let's take the example of Customer Database and try to understand DRC queries with some
examples.
Customer Table
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345
Example 1: Write a DRC query to get the data of all customers with Zip code 12345.
Workflow of Query: In the above query x1,x2,x3 (ordered) refers to the attribute or column
which we need in the result, and the predicate condition is that the first two domain variables x1
and x2 should be present while matching the condition for each row and the third domain
variable x3 should be equal to 12345.
Example 2: Write a DRC query to get the customer id of all the customer.
Customer_id
The basic structure of an SQL query consists of three clauses: select, from, and
where. The query takes as its input the relations listed in the from clause, operates on
them as specified in the where and select clauses, and then produces a relation as the
result.
Let us consider a simple query using our Faculty table, “Find the names of all instructors.
select FNAME from Faculty;
1 JISY 1
2 SANTHY 1
3 SWETHA 2
DEPT
CS
EC
SQL allows us to use the keyword all to specify explicitly that duplicates are not removed:
The select clause may also contain arithmetic expressions involving the operators +, −, ∗,
and / operating on constants or attributes of tuples. For example,the query returns a
relation that is the same as the Faculty relation, except that the attribute salary is
multiplied by 1.1.
SQL allows the use of the logical connectives and, or, and not in the where clause. The
operands of the logical connectives can be expressions involving the comparison
operators <, <=, >, >=, =, and <>.
2.4.2.1 Retrieve CID, Address and amount from relation CUSTOMER and ORDER
whose name= jisy
SELECT CUSTOMER.CID, Addr, AMOUNT FROM CUSTOMER,
ORDER WHERECUSTOMERS.CID = ORDERS.CID and NAME=’Jisy’;
2.4.2.2 Retrieve customer id, name, Address and amount from relation CUSTOMER AND
ORDER
SELECT CUSTOMER.CID, NAME, Addr, AMOUNT FROM
CUSTOMER, ORDER WHERE CUSTOMERS.CID =
ORDERS.CID;
Join
Select CID, NAME, Addr, AMOUNT from CUSTOMER Natural join ORDER
Select CID, NAME, Addr, AMOUNT from CUSTOMER Inner join ORDER onCUST
SQL aliases are used to give a table, or a column in a table, a temporary name.
Eg:
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%_%' Finds any values that start with "a" and are at
least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" andends
with "o"
String Operations
SQL specifies strings by enclosing them in single quotes, for example,
’Computer’. The SQL standard specifies that the equality operation on strings is case
sensitive; as a result the expression “ ’computer’ = ’Computer’ ” evaluates to false.
SQL also permits a variety of functions on character strings, such as
concatenating (using “ ||”), extracting substrings, finding the length of strings, converting
strings to uppercase (using the function upper(s) where s is a string) and lowercase
(using the function lower(s)), removing spaces at the end of the string (using trim(s)).
Pattern matching can be performed on strings, using the operator like. We describe
patterns by using two special characters:
SQL ORDER BY
The ORDER BY statement in sql is used to sort the fetched data in either ascending or
descending according to one or more columns. By default ORDER BY sorts the data in
ascending order.
SELECT column1, column2, ...FROM table_name ORDER
BY column1,column2, ... ASC/DESC;
NAME
Vishnu
Manju
Jisy
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are
included.
1 2 100
2 1 250
3 4 300
The SQL operations union, intersect, and except operate on relations and
correspond to the mathematical set-theory operations ∪, ∩, and −. Consider two tables
First and Second,
SET operators are special type of operators which are used to combine the result of two queries.
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
ID Name ID Name
1 JISY 3 SWETHA
2 SANTHY 2 SANTHY
ID Name
1 JISY
2 SANTHY
3 SWETH
A
2)UNION ALL:This operation is similar to Union. But it also shows the duplicate rows.
SELECT * FROM First UNION ALL SELECT * FROM Second;
ID Name
1 JISY
2 SANTHY
3 SWETH
A
2 SANTHY
ID Name
2 SANTHY
4)MINUS/ EXCEPT:
It combines the result of two SELECT statements. Minus operator is used to display
the rows which are present in the first query but absent in the second query.
SELECT * FROM First Except SELECT * FROM Second;
ID Name
1 JISY
ID Name
3 SWETH
A
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Syntax
1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT()
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:
10
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;
Output:
Output:
Output:
Com1 5
Com2 3
Com3 2
Output:
Com1 5
Com2 3
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Syntax
1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )
Example: SUM()
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Output:
670
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3;
Output:
320
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3
4. GROUP BY COMPANY;
Output:
Com1 150
Com2 170
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax
1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
Example:
1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax
1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )
Example:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function determines
the smallest value of all selected values of a column.
Syntax
1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Example:
1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;
Output:
10
In general, aggregate functions treat nulls according to the following rule: All
aggregate functions except count (*) ignore null values in their input collection. As a
result of null values being ignored, the collection of values may be empty. The count of
an empty collection is defined to be 0, and all other aggregate operations return a value
of null when applied on an empty collection. A Boolean data type that can take values
true, false, and unknown.
In nested queries, a query is written inside a query. The result of inner query is used in execution
of outer query. We will use STUDENT, COURSE, STUDENT_COURSE tables for
understanding nested queries.
STUDENT
STUDENT_COURSE
COURSE
S_ID C_ID
C_ID C_NAME
S1 C1
C1 DSA
S1 C3
C2 Programming
S2 C1
C3 DBMS
S3 C2
S4 C2
S4 C3
query, but the result of inner query is used in execution of outer query. Various operators like IN,
NOT IN, ANY, ALL etc are used in writing independent nested queries.
IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’ or ‘DBMS’, we can write
it with the help of independent nested query and IN operator. From COURSE table, we can find
out C_ID for C_NAME ‘DSA’ or DBMS’ and we can use these C_IDs for finding S_IDs
from STUDENT_COURSE TABLE.
The inner query will return a set with members C1 and C3 and outer query will return
those S_IDs for which C_ID is equal to any member of set (C1 and C3 in this case). So, it will
return S1, S2 and S4.
Note: If we want to find out names of STUDENTs who have either enrolled in ‘DSA’ or
‘DBMS’, it can be done as:
Select S_NAME from STUDENT where S_ID IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));
NOT IN: If we want to find out S_IDs of STUDENTs who have neither enrolled in ‘DSA’ nor
in ‘DBMS’, it can be done as:
Select S_ID from STUDENT where S_ID NOT IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));
The innermost query will return a set with members C1 and C3. Second inner query will return
those S_IDs for which C_ID is equal to any member of set (C1 and C3 in this case) which are
S1, S2 and S4. The outermost query will return those S_IDs where S_ID is not a member of set
(S1, S2 and S4). So it will return S3.
• Co-related Nested Queries: In co-related nested queries, the output of inner query depends on
the row which is being currently executed in outer query. e.g.; If we want to find
out S_NAME of STUDENTs who are enrolled in C_ID ‘C1’, it can be done with the help of co-
related nested query as:
Select S_NAME from STUDENT S where EXISTS
( select * from STUDENT_COURSE SC where S.S_ID=SC.S_ID and SC.C_ID=’C1’);
For each row of STUDENT S, it will find the rows from STUDENT_COURSE where
S.S_ID = SC.S_ID and SC.C_ID=’C1’. If for a S_ID from STUDENT S, atleast a row exists
in STUDENT_COURSE SC with C_ID=’C1’, then inner query will return true and
corresponding S_ID will be returned as output.
Student_Marks
1. Creating view
Syntax:
CREATE VIEW view_name AS SELECT column1, column2.....
FROM table_name WHERE condition;Creating View from a single table
Query:
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
2.
3.
4.
Query:
CREATE VIEW MarksView AS
SELECT Student_Detail.NAME, Student_Detail.ADDRESS,
Student_Marks.MARKS FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;
To display data of
View MarksView:
SELECT * FROM
MarksView;
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
Syntax
1. DROP VIEW view_name;
Example:
Uses of a View :
A good database should contain views due to the given reasons:
1. Restricting data access –
Views provide an additional level of table security by restricting
access to a predetermined set of rows and columns of a table.
2. Hiding data complexity –
A view can hide the complexity that exists in a multiple table join.Simplify
commands for the user –
Views allows the user to select information from multiple tables
without requiring the users to actually know how to perform a join.
3. Store complex queries –
Views can be used to store complex queries.
4. Rename Columns –
Views can also be used to rename the columns without affecting
the base tables provided the number of columns in view must
match the number of columns specified in select statement. Thus,
renaming helps to to hide the names of the columns of the base
tables.
5. Multiple view facility –
Different views can be created on the same table for different users.
3. Materialized view : The materialized view is view like a physical table which is
used mainly for performance management.
What is Complex view in SQL with real example?
In this section I would like to explain about complex view in SQL with real life example.
1. Complex view is view which uses multiple data together and create the snapshot of
the data.
2. Relation between table : The relation between multiple table is must to create the
complex views.
3. Complex view is nothing but the view which has been created with multiple joins,
group by statements or set operators to fetch the complex data from multiple tables.
4. The complex views are used to fetch the complex operations to fetch the complex
data from multiple table.
Example:
where e.Customer_code=d.customer_code
Group by item_category;
In SQL, The Group By statement is used for organizing similar data into groups. The data is
further organized with the help of equivalent function. It means, if different rows in a precise
column have the same values, it will arrange those rows in a group.
o The SELECT statement is used with the GROUP BY clause in the SQL query.
o WHERE clause is placed before the GROUP BY clause in SQL.
o ORDER BY clause is placed after the GROUP BY clause in SQL.
Syntax:
Sample Table:
Employee
1 John 24 25000
2 Nick 22 22000
3 Amara 25 15000
4 Nick 22 22000
5 John 24 25000
Student
C language 2 John
C language 2 Ginny
C language 2 Jasmeen
C language 3 Nick
C language 3 Amara
Java 1 Sifa
Java 1 dolly
Example:
Group By single column: Group By single column is used to place all the rows with the same
value. These values are of that specified column in one group. It signifies that all rows will put
an equal amount through a single column, which is of one appropriate column in one group.
NAME SALARY
John 50000
Nick 44000
Amara 15000
In the output, the rows which hold duplicate NAME are grouped under a similar NAME, and
their corresponding SALARY is the sum of the SALARY of the duplicate rows.
o Groups based on several columns: A group of some columns are GROUP BY column
1, column2, etc. Here, we are placing all rows in a group with the similar values of
both column 1 and column 2.
Output:
C language 2 3
C language 3 2
Java 1 2
In the above output, the student with similar SUBJECT and YEAR are grouped in the same
place. The students who have only one thing in common belongs to different groups. For
example, if the NAME is same and the YEAR is different.
Now, we have to group the table according to more than one column or two columns.
WHERE clause is used for deciding purpose. It is used to place conditions on the columns to
determine the part of the last result-set of the group. Here, we are not required to use the
combined functions like COUNT (), SUM (), etc. with the WHERE clause. After that, we need
to use a HAVING clause.
Example:
3. HAVING SUM(SALARY)>23000;
Output:
Name SUM(SALARY)
John 50000
According to the above output, only one name in the NAME column has been listed in the result
because there is only one data in the database whose sum of salary is more than 50000.
Points:
o The GROUP BY Clause is used to group the rows, which have the same values.
o The SELECT statement in SQL is used with the GROUP BY clause.
o In the Group BY clause, the SELECT statement can use constants, aggregate
functions, expressions, and column names.
o The GROUP BY Clause is called when the HAVING clause is used to reduce the results.
1) INSERT Statement
The INSERT Statement adds one or more rows to a table. It has two formats:
The VALUES Clause in the INSERT Statement provides a set of values to place in
the columns of a new row. It has the following general format:
Before After
P4 NULL Brown
S1 P1 NULL S1 P1 NULL
S2 P1 200 S2 P1 200
S3 P2 200 S3 P2 200
S2 P3 500
2) UPDATE Statement
The UPDATE statement modifies columns in selected table rows. It has the
following general format:
The optional WHERE Clause has the same format as in the SELECT Statement.
The set-list contains assignments of new values for selected columns. See SET
Clause.
The SET Clause expressions and WHERE Clause predicate can contain subqueries.
SET Clause
The SET Clause in the UPDATE Statement updates (assigns new value to) columns
in the selected table rows. It has the following general format:
Before After
S1 P1 NULL S1 P1 NULL
=>
S2 P1 200 S2 P1 220
S3 P1 1000 S3 P1 1020
S3 P2 200 S3 P2 220
Before After
DELETE Statement
The DELETE Statement removes selected rows from a table. It has the following
general format:
The optional WHERE Clause has the same format as in the SELECT Statement.
The WHERE Clause predicate can contain subqueries.
DELETE Examples
DELETE FROM sp WHERE pno = 'P1'
Before After
sno pno qty sno pno qty
S1 P1 NULL S3 P2 200
S2 P1 200 =>
S3 P1 1000
S3 P2 200
Before After
P3 Dongle Green
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.
PROJECT
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as long as the
condition is satisfied. It returns the combination of all rows from both the tables where the
condition satisfies.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values from the right
table. If there is no matching join value, it will return NULL.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and the
matched values from the left table. If there is no matching in both tables, it will return NULL.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables
have all the records from both tables. It puts NULL on the place of matches not found.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
The DDL Commands in Structured Query Language are used to create and modify the schema of
the database and its objects. The syntax of DDL commands is predefined for describing the data.
The commands of Data Definition Language deal with how the data should exist in the database.
1. CREATE Command
2. DROP Command
3. ALTER Command
4. TRUNCATE Command
5. RENAME Command
CREATE Command
CREATE is a DDL command used to create databases, tables, triggers and other database
objects.
Example 1: This example describes how to create a new database using the CREATE DDL
command.
Suppose, you want to create a Books database in the SQL database. To do this, you have to write
the following DDL Command:
Example 2: This example describes how to create a new table using the CREATE DDL
command.
2. (
3. column_Name1 data_type ( size of the column ) ,
4. column_Name2 data_type ( size of the column) ,
5. column_Name3 data_type ( size of the column) ,
6. ...
7. column_NameN data_type ( size of the column )
8. ) ;
Suppose, you want to create a Student table with five columns in the SQL database. To do this,
you have to write the following DDL command:
Example 3: This example describes how to create a new index using the CREATE DDL
command.
Suppose, you want to create an index on the combination of the City and State field of
the Student table. For this, we have to use the following DDL command:
Example 4: This example describes how to create a trigger in the SQL database using the
DDL CREATE command.
DROP Command
DROP is a DDL command used to delete/remove the database objects from the SQL database.
We can easily remove the entire table, view, or index from the database using this DDL
command.
Example 1: This example describes how to remove a database from the SQL database.
Suppose, you want to delete the Books database from the SQL database. To do this, you have to
write the following DDL command:
Example 2: This example describes how to remove the existing table from the SQL
database.
Suppose, you want to delete the Student table from the SQL database. To do this, you have to
write the following DDL command:
Example 3: This example describes how to remove the existing index from the SQL
database.
Suppose, you want to delete the index_city from the SQL database. To do this, you have to write
the following DDL command:
ALTER Command
ALTER is a DDL command which changes or modifies the existing structure of the database,
and it also changes the schema of database objects.
We can also add and drop constraints of the table using the ALTER command.
Example 1: This example shows how to add a new field to the existing table.
Suppose, you want to add the 'Father's_Name' column in the existing Student table. To do this,
you have to write the following DDL command:
Example 2: This example describes how to remove the existing column from the table.
Suppose, you want to remove the Age and Marks column from the existing Student table. To do
this, you have to write the following DDL command:
Example 3: This example describes how to modify the existing column of the existing table.
Suppose, you want to change the character size of the Last_Namefield of the Student table. To
do this, you have to write the following DDL command:
TRUNCATE Command
TRUNCATE is another DDL command which deletes or removes all the records from the table.
This command also removes the space allocated for storing the table records.
Example
Suppose, you want to delete the record of the Student table. To do this, you have to write the
following TRUNCATE DDL command:
The above query successfully removed all the records from the student table. Let's verify it by
using the following SELECT statement:
RENAME Command
RENAME is a DDL command which is used to change the name of the database table.
Example
This query changes the name of the table from Student to Student_Details.
Connection to DB
This is the first step while writing a query in high level languages. First connection to the DB
that we are accessing needs to be established. This can be done using the keyword CONNECT.
But it has to precede with ‘EXEC SQL’ to indicate that it is a SQL statement.
Since the compilation is done at run-time, the system will know how to access the database at
run-time only. So, no proper planning for execution and optimization can be done previously.
This will reduce the performance of the system. Also, if you are taking the database query
from the user at run-time, then there are possibilities that the users might enter some wrong
queries and this is very dangerous because here you are dealing with lots and lots of data.
Note: Most of the functions described in this lesson are specific to Oracle'sversions.
Types of Functions
In general, functions in Oracle SQL are divided into five groups, the first group being the
topic of this chapter, namely, single row functions. Other function types are aggregate
functions, which create groups of rows; analytic functions, which also group but allow in-
depth data analysis; object reference functions, allowing access to special object pointers;
and finally user-defined functions, such as those you create using a programming language
such as PL/SQL.
Single Row Functions— Single row functions can be used to execute an operation
on each row of a query. In other words, a single row function can be used to execute
the same operation for every row a query retrieves.
Aggregate Functions— These functions summarize repeating groups in a row set
into distinct groups, aggregating repetitive values into items such as sums or
averages.
Analytic Functions— Unlike aggregates, which summarize repetitions into unique
items, analytics create subset summaries within aggregates.
Object Reference Functions— These functions use pointers to reference values.
Commonly, object reference functions either reference objects or dereference
values from objects.
User-Defined Functions— Custom functions can be built using PL/SQL, allowing
extension of the large library of Oracle SQL built-infunctionality. PL/SQL is
beyond the scope of this book.
Single Row Functions
Now let's focus on the subject matter of this chapter. Recall that a single row function was
defined as a function that can be used to execute an operation oneach row of a query.
Let's start this journey by discovering the different classifications for single row functions:
String Functions These functions take a string as a parameterand
return a number or a string.
functions. Believe it or not, sometimes the plethora of available Oracle built-in functions
will not suffice. Creating your own functions can be done using PL/SQL.
CHARACTER FUNCTIONS
Character functions operate on values of character class datatype, i.e., Char, Varchar2,
Varchar etc. These functions can return either character class datatype or number classs
datatype based on the operation performed on the input data. Length of a value returned by
these functions is limited upto 4000 bytes for varchar2 datatype and 2000 bytes for char
datatype. If a function returns a value that exceedes the length limit, Oracle automatically
truncate the value before returning the result. Some of the SQL built-in character functions
are given in the following table.
ECT ASCII('Ç')FROM
DUAL;
K
L A
R
SELECT ENAME, CONCAT(ENAME, JOB), LENGTH(ename),
INSTR(ename, 'A' )
FROM EMP
WHERE SUBSTR(job ,1 , 5) = UPPER('saLEs');
INSTR('MISSISSIPPI','S',5,2)
7
FROM emp;
LPAD(ENAME,20,'*')
***************SMITH
***************ALLEN
14 rows selected.
RPAD(ENAME,20,'*')
SMITH***************
ALLEN***************
14 rows selected.
LT
RI
bbccxXXyyzzaaabbbccc
M
RTRIM
aaaaabbccxXXyyzzaaabbbccc
SELECT ENAME
FROM EMP
WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE');
EN
A
SMITH
M
E
RASC- DEPARTMENT COMPUTER SCIENCE AND APPLICATION-N. MANIMOZHI 62
RELATIONAL DATABASE MANAGEMENT SYSTEM UNIT-II
A
DEF l
t
Function Result
UPPER('SQLCourse') COURSE
LOWER, UPPER, and INITCAP are the three case conversion functions.
• LOWER Converts mıxed case ör uppercass character string lo
louercase
• UPPER Converts mısed case ör louercasc clıaracter string to
uppcrcase
Display the employee number, name, and department number foremployee Blake.
The WHERE clause of the first SQL statement specifies the employee name as ' blake.'
Since all the data in the EMP tabls is stored in uppercase. the name ' blake' does not ffind
a match in the EMP table and as a result no rowsare selected.
The WHERE clause of the second SQL statement specifies that the employee name in the
EMP table be converted to lowercase and then be compared to 'blake ' . Since both the
names are in lowercase now, a match is found and one row is selected. The WHERE
clause can be rewritten in the following manner to produce the same result:
Manipulate character strings
Function Result
LENGTH( 'String' ) 6
CONCAT. SUBSTR, LENGTH, INSTR. and LPAD are îhe five charactermanipulation
functions covered in this lesson.
CONCAT Joins values together (Yon are limited to using two parameterswith
CONCAT.)
SUBSTR Extracts a string of determined length LENGTH
Shows the length of a string as a numeric valueINSTR
Finds numerîc position of a named character LPAD
Pads the character value right-justified
Note: RPAD character manipulation function pads the character value lelt-justified.
Example:
The slide example displays ernployee name and job joined together, length of the
employee name, and the numeric position of the letter A in the employee name, forall
employees who are in sales.
Example
Write a SQL statement to display the data for those employees whosenames end
with an N .
A database represents an essential corporate resource that should be properly secured using
appropriate controls. Multi-user database system must provide a database security and
authorization subsystem to enforce limits on individual and group access rights and privileges.
Database security and integrity is about protecting the database from being inconsistent and
being disrupted. We can also call it database misuse. Database security encompasses hardware,
software, people and data. Database misuse could be Intentional or accidental, where accidental
misuse is easier to cope with than intentional misuse.
Likewise, even though there are various threats that could be categorized in this group,
intentional misuse could be:
Most systems implement good Database Integrity to protect the system from accidental misuse
while there are many computer-based measures to protect the system from intentional misuse,
which is termed as Database Security measures.
• Legal, ethical and social issues regarding the right to access information
• Physical control
• Policy issues regarding privacy of individual level at enterprise and national level
• Operational consideration on the techniques used (password, etc)
• System level security including operating system and hardware control Security levels
and security policies in enterprise level
Database security is the mechanisms that protect the database against intentional or
accidental threats. Threat is any situation or event, whether intentional or accidental, that may
adversely affect a system and consequently the organization. A threat may be caused by a
situation or event involving a person, action, or circumstance that is likely to bring harm to an
organization. The harm to an organization may be tangible or intangible:
• Wire-tapping
• Illegal entry by hacker
• Blackmail
• Creating ‗trapdoor ‘into system
• Theft of data, programs, and equipment
• Failure of security mechanisms, giving greater access than normal
• Staff shortages or strikes
• Inadequate staff training
• Viewing and disclosing unauthorized data
• Electronic interference and radiation
• Data corruption owing to power loss or surge
• Fire (electrical fault, lightning strike, arson), flood, bomb
• Physical damage to equipment
• Breaking cables or disconnection of cables
• Introduction of viruses