Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Unit-1 Introduction of Relational model

• RDBMS
o RDBMS stands for Relational Database Management System.

o A Relational database management system (RDBMS) is a database


management system (DBMS) that is based on the relational model as
introduced by E. F. Codd.

o Data is represented in terms of tuples (rows) in RDBMS.

o Relational database is most commonly used database. It contains number of


tables and each table has its own primary key.

o Due to a collection of organized set of tables, data can be accessed easily in


RDBMS.

o Examples of RDBMS

 MS SQL Server, IBM DB2,PosgreSQL, ORACLE, My-SQL and


Microsoft Access are based on RDBMS.

1.1 Codd’s Rules

History of E. F. Codd 12 Rules

 Dr. E. F. Codd is an IBM researcher.

 who first developed the relational data model in 1970.

 Dr E.F.Codd, also known to the world as the ‘Father of Database


Management Systems’

 In 1985, He had propounded 12 rules which are in-fact 13 in number.


The rules are numbered from zero to twelve.

 It has provided a guideline for the design of all relational database


systems.

 Dr. Codd has used the term guideline because till date no commercial
relational database system fully conforms to all 12 rules.

 It Just remain a goal for relational database design.


Rule 0 : The Foundation rule

• This rule states that any database system should have characteristics as
relational, as a database and as a management system to be RDBMS.

Rule 1 : The information rule


• All data should be presented in table form.

Eg: CLIENT_MASTER table represent following:

TABLE: CLIENT_MASTER

CREATE TABLE CLIENT_MASTER

( CLIENTNO VARCHAR2(6) PRIMARY KEY,

NAME VARCHAR2(20) NOT NULL,

ADDRESS VARCHAR2(30),

CITY VARCHAR2(15),

PINCODE NUMBER(8),

STATE VARCHAR2(15),

BALDUE NUMBER(10,2) );

STRUCTURE

Name Null? Type

---------------------- -------- ----

CLIENTNO NOT NULL VARCHAR2(6)

NAME NOT NULL VARCHAR2(20)

ADDRESS1 VARCHAR2(30)

CITY VARCHAR2(15)

PINCODE NUMBER(8)

STATE VARCHAR2(15)
BALDUE NUMBER(10,2)

Rule 2: Guaranteed Access Rules


• All data should be accessible without any ambiguity.

• This can be accomplished through a combination of the table name, Primary Key, and
column name.

• EXAMPLE:

Emp_info Emp_Salary_info

EmpId Name EmpId Salary

91 John 91 20000

92 Thomas 92 40000

Rule 3: Systematic Treatment of Null Values


• A field should be allowed to remain empty.

• This involves the support of a null value, which is distinct from an empty string or
number with a value of zero.

• Of course, this can’t apply to primary key. In addition, most database implementations
support the concept of a not- null field constraint that prevents null values in a specific
table column.

• EXAMPLE:

Emp Id Name City Mobile No

20BCA01 Harshil Chikhli 9725142752

20BCA02 Megh Navsari -

Rule 4: Dynamic On-Line Catalog based on the Relational Model


• A relational database must provide access to its structure through the same
tools that are used to access the data.

• EXAMPLE:

– This is usually accomplished by storing the structure definition within


special system tables(Data Dictionary).

Rule 5: Comprehensive Data Sublanguage Rule

• The Relational database must support at least one clearly defined language
that includes functionality for data definition, data manipulation, data
integrity and database transaction control.

1. Data Definition

2. View Definition

3. Data Manipulation

4. Integrate Constraints

5. Authorization

6. Transaction Control

• All commercial relational database use forms of standard SQL


(i.e. Structure QueryLanguage) as their supported comprehensive language.

Rule 6: View Updating Rule

• Data can be presented in different logical combination called views.

• Each view should support the same full range of data manipulation that has
direct access to a table available.

• In practice, providing update and delete access to logical view is difficult


and not fully supported by any current database.

• All views those are theoretically updatable by the system.

• This rule is not really implemented yet any available.


Rule 7: High – Level Insert, Update and Delete

• Data can be retrieved from a relation database in sets constructed of data


from multiple rows and/or multiple tables.

• This rule states that insert, update, and delete operations should be supported
for any retrievable set rather than just for a single row in a single table.

EXAMPLE:

Suppose if we need to change ID then it will reflect everywhere automatic.

Rule 8: Physical Data independence

• The user is isolated from the physical method of storing and retrieving
information from the database.

• Changes can be made to the underlying architecture (hardware, disk storage


methods) without affecting how the user accesses it.

Rule 9: Logical Data Independence

• How data is viewed should not be changed when the logical structure
(table’s structure) of the database changes.

• This rule is particularly difficult to satisfy.

• Most databases rely on strong ties between the data viewed and the actual
structure of the underlying tables.

EXAMPLE:

In this rule we want to retrieve any ID, when we retrieve Data without ID that
time we can not satisfy this rule.

Rule 10: Integrity Independence

• The database language (like SQL) should support constraints on user input
that maintain database integrity.

• This rule is not fully implemented by most major vendors.


• At a minimum, all databases do preserve two constraints through SQL.

• All databases do preserved 2 constraints through SQL.

– 1. Primary Key column can not have NULL & Must be UNIQUE.

– 2. Foreign Key is define in one table any value in it must exists as


a primary

key in another table.

• Integrity constraints specific to a particular relation database must be


definable in the relational data sub-language & storable in a catalog
not in the application program.

Rule 11: Distribution independence

• A user should be totally unaware of whether or not the database is


distributed (whether parts of database exist in multiple locations) make this
rule difficult to implement.

• A variety of reasons make this rule difficult to implement.

EXAMPLE:

The database can be located at the user server or at any other network. The
end user should not be able to know about the database servers. He should be able
to get the records as if he is pulling the records locally. Even if the database is
located in different servers, the accessibility time should be comparatively less.

Rule 12: Non subversion Rule

• There should be no way to modify the database structure other than through
the multiple row database language (like SQL).

• Most databases today support administrative tools that allow some direct
manipulation of the data structure.

EXAMPLE:
If a relational system supports a low level (Single record at a time) language
that low level language can not be used to sub word or by pass the integrity rules or
constraints expressed in the higher level (multiple record at a time) relational
language.

1.2 Relational Operations Algebra

Relational Algebra
Relational Algebra is procedural query language, which takes Relation as input and generate
relation as output. Relational algebra mainly provides theoretical foundation for relational
databases and SQL. It uses operators to perform queries.
Projection (π)(PI)
 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 eliminate duplicate values.
 (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.
Example :
R
(A B C)
----------
1 2 4
2 2 3
3 2 3
4 3 4

π (BC)

Output

B C
-----
2 4
2 3
3 4

Note: By Default projection removes duplicate data.


Example of Projection:

Consider the following table

CustomerID CustomerName Status

1 Google Active

2 Amazon Active

3 Apple Inactive

4 Alibaba Active

5 Amazon Active

Here, the projection of CustomerName and status will give

Π CustomerName, Status (Customers)

CustomerName Status

Google Active

Amazon Active

Apple Inactive

Alibaba Active

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 operator selects tuples that satisfy a given predicate.

σp(r)
σ is the predicate

r stands for relation which is the name of the table

p is prepositional logic

σ(c>3)R
will select the tuples which have c more than 3.

Note: selection operator only selects the required tuples but does not display them. For
displaying, data projection operator is used.
For the above selected tuples, to display we need to use projection also.
π (σ (c>3)R ) will show following tuples.
A B C
-------
1 2 4
4 3 4

Example 1

σ Status=”Active” (Customers)

Output – Selects tuples from Customers where Status is ‘Active’.

Example 2

σ CustomerName=”Amazon” and Status=”Active” (Customers)

Output – Selects tuples from Customers where CustomerName is ”Amazon” and Status is
‘Active’.

Example 3

σ CustomerId>3(Customers)
Output – Selects tuples from Customers where CustomerId is greater than 3.

Rename (ρ)(Rho)
 Rename is a unary operation used for renaming attributes of a relation.
 ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’.

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:

The result <- A ∪ B

For a union operation to be valid, the following conditions must hold –

 R and S must be the same number of attributes.


 Attribute domains need to be compatible.
 Duplicate tuples should be automatically removed.

Example

Consider the following tables.

Table A Table B

column 1 column 2 column 1 column 2

1 1 1 1

1 2 1 3

A ∪ B gives

Table A ∪ B

column 1 column 2

1 1

1 2

1 3

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.

Visual Definition
of Intersection
Example:

A∩B
Table A ∩ B
column 1 column 2
1 1

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.

 The attribute name of A has to match with the attribute name in B.


 The two-operand relations A and B should be either compatible or Union compatible.
 It should be defined relation consisting of the tuples that are in relation A, but not in B.

Example

A-B
Table A – B

column 1 column 2
1 2

1.3 Transactional Control Language


PL/SQL Transactions
The series of one or more SQL statement that are logically related or a series of
operations performed on Oracle table data is termed as a Transaction.

A Transaction is a group of events that occurs between any of the following


events:

1. Connecting to Oracle
2. Disconnecting from Oracle
3. Committing changes to the database table
4. Rollback

Closing Transactions

A transaction can be closed by using either a COMMIT or a ROLLBACK


statement. By using these statements, table data can be changed or all the changes made
to the table data undone.

Using COMMIT

A COMMIT ends the current transaction and makes permanent any changes made
during the transaction. All transaction locks acquired on tables are released.

Syntax:

COMMIT;

Using ROLLBACK

A ROLLBACK does exactly the opposite of COMMIT. It ends the transaction but
undoes any changes made during the transaction. All transaction locks acquired on tables
are released.
Syntax:

ROLLBACK [TO [SAVEPOINT] savepoint_name];

SAVEPOINT – Is optional and is used to rollback a transaction partially, as far as

the specified savepoint

SAVEPOINTNAME – Is a savepoint created during the current transaction.

Creating a Savepoint
Savepoint marks and saves the current point in the processing of a transaction.

When a savepoint is used with a rollback statement parts of a transaction can be


undone.

Syntax:

SAVEPOINT savepoint_name;

 Rollback Operation performed without the SAVEPOINT clause.

1. Ends the transaction.


2. Undoes all the changes in the current transactions.
3. Erases all savepoints in that transaction.
4. Release the transactional locks.

 Rollback Operation performed with the TO SAVEPOINT clause.

1. A predetermined portion of the transaction is rollback.


2. Retains the save point rollback to, but loses those created after the
named savepoint.
3. Releases all transactional locks that were acquired since the savepoint
was taken.

Exercise:

Consider Product_master and Product_requisite tables:

Product_master

Column name Data Size Attributes


types

Prod_id Number 4 P.K

Prod_name Varchar2 20 Not null

Bal_stock Number 3

Product_requisite

Column name Data Size Attributes


types

Prod_id Number 4 F.K

Dept_no Number 20 F.K reference from the Dept table

Qty Number 3

Update the Bal_stock in the Product_master table for each requisite in the Product_requisite table.
Based on the prod_id, decrease the Bal_stock in the Product_master table by the quantity
in the Product_requisite table. The updation is completed only when the corresponding
record in the Product_requisite table is deleted.

Write a PL/SQL block of code based on the conditions.

1. If at any given point, the value in Bal_stock becomes negative, the entire update
operation for all prod_id should be rollback.

2. If at any given point, the value in Bal_stock becomes negative, the update
operation for that particular prod_id should rollback.
1.4 Data Control language
1.4 Data Control language

It stands for Data Control Language.

• It is a sub language of SQL. It is used to control data from being access.

• It is used to give or take back privileges(Permission) to other user to access data.

Grant

Objects that are created by the user are owned and controlled by that user.

• If a user wishes to access any of the objects belonging to another user, the owner of the object
will have to give permission to access to that user.

• This is called Granting of Privileges.

• Oracle allows to give all privileges of object. Privileges means some operations to be perform
on the object.

Privileges are:

1. Insert - Allow the Grantee to add records to the table with the INSERT command.

2. Update - Allow the Grantee to modify records to the table with the UPDATE command.

3. Delete- Allow the Grantee to remove records to the table with the DELETE command.

4. Select- Allow the Grantee to display records to the table with the SELECT command.
5. Index- Allow the Grantee to create an index on the table with the CREATE INDEX command.

6. Alter- Allow the Grantee to change the table definition with the ALTER TABLE command.

To give all privileges to specific user. •

Syntax:

GRANT object privileges ON Tablename TO User_name [WITH GRANT OPTION];

With Grant Option allows the grantee to in turn grant object privileges to other user.

Example:

GRANT ALL ON SYBCA TO SMITH;

In this example Smith get all the data manipulation permissions on the table SYBCA.

To give specific privileges to specific user.

Syntax:

GRANT Insert, update, delete, select, index,alter ON Tablename TO User_name WITH


GRANT OPTION;

Example:

GRANT Insert, Delete, Select ON SYBCA TO SMITH;

In this example Smith get permissions only for display, remove and stored new data in the table
SYBCA.

Example:

Give the user Harsh all data manipulation privileges on the table SYBCA along with the option
to further grant permission on the SYBCA table to other users.(Owner is Harshil).

GRANT ALL ON SYBCA TO HARSH WITH GRANT OPTION;

In against Harsh can give permission to other user to access SYBCA table.

For Example:

GRANT ALL ON HARSHIL.SYBCA TO MEGH;

Here Megh get the permission of all privileges on harshil’s table SYBCA by Harsh.
To access Table belonging to another user.

Syntax:

Ownername.Objectname

To view table records: Example:

SELECT * FROM HARSHIL.SYBCA;

To Insert record:

Insert into harshil.sybca values(15,’JIYA’,11-OCT-2002’,’NAVSARI’);

REVOKE

• Privileges once given can be denied to a user using the REVOKE command.

• The object owner can revoke privileges granted to another user.

• A user of an object who is not the owner, but has been granted the GRANT privilege, has a
power to REVOKE the privileges from a grantee.

SYNTAX:

REVOKE

OBJECT PRIVILEGES

ON OBJECTNAME

FROM USERNAME;

Example:

REVOKE

ALL

ON SYBCA

FROM SMITH;

Here Owner take back all permission from user smith.

You might also like