DBMS3
DBMS3
DBMS3
System
COURSE CODE- BCA1203
COURSE CREDIT: 2
2 HOURS A WEEK
Lesson : 1
Evaluation Pattern
2
Query Languages
• A query language is a language in which a user requests information from
the database. .
• Relational query languages use relational algebra to break the user requests
and instruct the DBMS to execute the requests
• Categorized into-
– Procedural - the user instructs the system to perform a sequence of operations on
the database to compute the desired result.
– Non-procedural- the user describes the desired information without giving a
specific procedure for obtaining that information.
• “Pure” languages:
– Relational Algebra- procedural
– Tuple Relational Calculus- non-procedural
– Domain Relational Calculus- non-procedural
• Pure languages form underlying basis of query languages that people use
Query Languages
Procedural Language: In procedural languages, the program code is written as a
sequence of instructions.
User has to specify “what to do” and also “how to do” (step by step procedure).
These instructions are executed in the sequential order.
These instructions are written to solve specific problems.
Examples of Procedural languages: FORTRAN, COBOL, ALGOL, BASIC, C and Pascal.
Output:
Roll Name Department Fees Team
6 EE Assistant Professor
6 EE Assistant Professor
5 CSE
6 EE
Union Operation
• Notation: r ∪ s
• Defined as:
• r ∪ s = {t | t ∈ r or t ∈ s}
• For r ∪ s to be valid.
• 1. r, s must have the same arity (same number of attributes)
• 2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
column of s)
Example
1.SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
Set Difference Operation
• Notation: r – s
• This is a binary operator.
• This operator creates a new relation with tuples that are in one
relation but not in other relation.
• It is denoted by ‘-‘symbol.
• Defined as:
• r – s = {t | t ∈ r and t ∉ s}
• Set differences must be taken between compatible relations.
– r and s must have the same arity
– attribute domains of r and s must be compatible
Consider a relation Student(FIRST, LAST) and Faculty(FIRSTN, LASTN) given
below :
First Last
FirstN LastN
Aisha Arora
Raj Kumar
Bikash Dutta
Honey Chand
Makku Singh
Makku Singh
Raju Chopra
First Last
Aisha Arora
Bikash Dutta
Raju Chopra
Cartesian-Product Operation
• Notation: r x s
• Allows to combine attributes from two relations.
• Defined as:
• r x s = {t q | t ∈ r and q ∈ s}
• Assume that attributes of r(R) and s(S) are disjoint. (That is,
R ∩ S = ∅).
• If attributes of r(R) and s(S) are not disjoint, then renaming
must be used.
Example
Consider two relations STUDENT(SNO, FNAME, LNAME) and DETAIL(ROLLNO,
AGE) below:
On applying CROSS PRODUCT on STUDENT and DETAIL:
STUDENT ✕ DETAILS
SNO FNAME LNAME ROLLNO AGE
1 Vishal Singh 5 18
2 Niya Patel 9 21
1 Vishal Singh 5 18
1 Vishal Singh 9 21
2 Niya Patel 5 18
2 Niya Patel 9 21
Rename Operation
• Allows us to give the name, and therefore to refer to, the results of relational-
algebra expressions.
• Allows us to refer to a relation by more than one name.
• Example:
• ρ x (E)
• returns the result of expression E under the name X
• It is denoted by Greek letter rho(ρ).
• This is a unary operator used to rename the tables and columns of a relation.
• When we perform self join operation, we have to differentiate two same
tables. In such case rename operator on tables comes into picture
Example
Query to rename the relation Student as Male Student and
the attributes of Student – RollNo, SName as (Sno, Name).
Sno Name
2600 Ronny
2655 Raja
Set-Intersection
• This operation is a binary operation. It results in a relation with tuples that are
in both the relations. It is denoted by ‘∩’
• R∩S
• Where R and S are the relations. It picks all the tuples that are present in both R
and S, and results it in a new relation.
• DESIGN_EMPLOYEE – (DESIGN_EMPLOYEE – TESTING_EMPLOYEE)
• It first filters only those employees who are only design employees-(104,
Kathy)
Exam p le
Find all the customers whose account is in the bank and have taken out
a loan. Borrowe
Depositor
r
ID Name ID Name
1 Raj 2 Vinayak
2 Vinayak 3 Raj
3 Priya 5 Priya
∏Name(Depositor) ∩ ∏Name(Borrower)
Raj
Vinayak
Example Queries
• Find all loans of over $1200
σamount > 1200 (loan)
• Find the loan number for each loan of an amount greater than $1200
∏loan-number (σamount > 1200 (loan))
• Find the names of all customers who have a loan, an account, or both, from
the bank
∏customer-name (borrower) ∪ ∏customer-name (depositor)
• Find the names of all customers who have a loan not an account at bank.
∏customer-name (borrower) ∩ ∏customer-name (depositor)
Modification of the Database
• The content of the database may be modified using the
following operations:
– Deletion
– Insertion
– Updating
• All these operations are expressed using the assignment
operator.
Deletion
• If you have a key defined for the relation, then you can meet
the unique row requirement.
• Example relation in 1NF (note that key attributes are
underlined):
• STOCKS (Company, Symbol, Headquarters, Date, Close_Price)
Second Normal Form (2NF)
• This violates the rule for 2NF in that a part of our key key determines a non key attribute.
• Another name for this is a Partial key dependency. Symbol is only a “part” of the key and
it determines a non-key attribute.
• Also, consider the insertion and deletion anomalies.
Second Normal Form (2NF)
• One Solution: Split this up into two new relations:
• COMPANY (Company, Symbol, Headquarters)
• STOCK_PRICES (Symbol, Date, Close_Price)
• At this point we have two new relations in our relational
model. The original “STOCKS” relation we started with is
removed form the model.
• Sample data and functional dependencies for the two new
relations:
• COMPANY Relation:
Third Normal Form (3NF)
• A relation is in 3NF if the relation is in 2NF and all
determinants of non-key attributes are candidate keys
• That is, for any functional dependency: X → Y, where Y is a
non-key attribute (or a set of non-key attributes), X is a
candidate key.
• A relation in 3NF will not have any transitive dependencies of
non-key attribute on a candidate key through another non-key
attribute.(If one non-key -> non-key attribute)
Second Normal Form (2NF)
• STOCK_PRICES relation:
• FD1: Symbol, Date → Close Price
•Symbol •Date ClosePrice
So, Definition of 1NF (each one has well defined unique keys) and 2NF (no
partial key dependencies).
Third Normal Form (3NF)
• The functional dependencies we can see are:
• FD1: Symbol → Company
• FD2: Company → Headquarters
• so therefore:
• Transitive dependency: Symbol → Headquarters
• This is a transitive dependency.
Third Normal Form (3NF)
• What happens if we remove Oracle?
• We loose information about 2 different facts.
• The solution again is to split this relation up into two new
relations:
• STOCK_SYMBOLS(Company, Symbol)
• COMPANY_HEADQUARTERS(Company, Headquarters)
Third Normal Form (3NF) example :-
• Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP
dependent on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY)
transitively dependent on super key(EMP_ID). It violates the rule of third
normal form.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
Integrity
• Integrity of database means correctness of data stored in the
database.
• Constraints are set of defined rules which are used to maintain
integrity of database.
• Integrity constraints guard against accidental damage to the
database, by ensuring that authorized changes to the database
do not result in a loss of data consistency.
Domain Constraints
• Domain constraints are the most elementary form of integrity constraint.
• Domain constraints can be defined as the definition of a valid set of values for an
attribute.
• They test values inserted in the database, and test queries to ensure that the
comparisons make sense.
• Implementation: Data types and derived data types
• Domain Constraint = data type + Constraints (NOT NULL / UNIQUE / PRIMARY KEY /
FOREIGN KEY / CHECK / DEFAULT)
Referential Integrity
• Based on foreign key concept
• Referential integrity is a property of data which, when satisfied,
requires every value of one attribute (column) of a relation
(table) to exist as a value of another attribute in a different (or
the same) relation (table).
• Foreign key is an attribute or a set of attributes whose values
correspond to primary key of another table.
• Master table within relation is one which contains primary key
• Detail table in a relation is one which contains foreign key.
Referential Integrity
•
Physical level
– Physical access to computers allows destruction of data by intruders; traditional lock-and-key security is needed.
– Computers must also be protected from floods, fire, etc.
•
Human level
– Users must be screened to ensure that an authorized users do not give access to intruders.
– Users should be trained on password selection and secrecy.
Authorization
• Forms of authorization on parts of the database:
• Read authorization - allows reading, but not modification of
data.
• Insert authorization - allows insertion of new data, but not
modification of existing data.
• Update authorization - allows modification, but not deletion of
data.
• Delete authorization - allows deletion of data.
Authorization
• Forms of authorization to modify the database schema:
•
Index authorization - allows creation and deletion of indices.
• Resources authorization - allows creation of new relations.
• Alteration authorization - allows addition or deletion of
attributes in a relation.
• Drop authorization - allows deletion of relations.
Authorization and Views
• Users can be given authorization on views, without being
given any authorization on the relations used in the view
definition.
• Ability of views to hide data serves both to simplify usage of
the system and to enhance security by allowing users access
only to data they need for their job.
• A combination or relational-level security and view-level
security can be used to limit a user’s access to precisely the
data that user needs.
GRANTING PRIVILEGES
The grant statement is used to confer authorization
grant <privilege list>
on <relation name or view name> to <user list>
<user list> is:
a user-id public, which allows all valid users the privilege granted
A role