0% found this document useful (0 votes)
40 views

RDBMS Concepts

The document discusses database concepts like data models, database management systems, and the advantages they provide over flat file systems by centralizing control and eliminating issues like data redundancy, inconsistency, and lack of sharing. It also covers database structures, normalization to remove data anomalies, and relational database management systems based on Codd's relational model and rules.

Uploaded by

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

RDBMS Concepts

The document discusses database concepts like data models, database management systems, and the advantages they provide over flat file systems by centralizing control and eliminating issues like data redundancy, inconsistency, and lack of sharing. It also covers database structures, normalization to remove data anomalies, and relational database management systems based on Codd's relational model and rules.

Uploaded by

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

RDBMS Concepts

Data?

Information?

.
Initial Data Storage Methods

Data is represented in one or more flat files

Flat file is nothing but electronic representation of


cardboard file.

Every business group has its own set of files

.
Disadvantages of Flat File Systems
No centralized control.
Data Redundancy
Data Inconsistency
Data can not be shared
Standards can not be enforced
Security issues
Integrity can not be maintained
Data dependence
Database Management Systems

A system whose overall purpose is to


record and maintain information

A database is a repository for stored


data and programs to manipulate it.
Advantages of DBMS
Centralized control.
No Data Redundancy
Data Consistency
Data can be shared
Standards can be enforced
Security can be enforced
Integrity can be maintained
Data independence
Data Models
A data model is a collection of concepts
for describing data
A Schema is a description of a particular
collection of data using the given data
model
The relational model is the most widely
used model today
Example University Database
Conceptual Schema
Students(sid: string, name: string, login: string,
age: integer, gpa:real)
Courses(cid: string, cname:string, credits:integer)
Physical Schema
Relations stored as unordered files.
Index on first column of Students.
External Schema (View):
Course_info(cid:string,enrollment:integer)
Data Independence
Applications insulated from how data is
structured and stored
Logical Data Independence: Protection
from changes in logical structure of
data
Physical Data Independence: Protection
from changes in physical structure of
data
Types of DBMS

Hierarchical

Network

Relational
Structure of a DBMS
Types of DBMS

Hierarchical

Network

Relational
Example Data
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris

P# PNAME COLOR WEIGHT CITY


P1 Nut Red 12 London
P2 Bolt Green 17 Paris
P3 Screw Blue 17 Rome
P4 Screw Red 14 London

S# P# QTY
S1 P1 300
S1 P2 200
S1 P3 400
S2 P1 300
S2 P2 400
S3 P2 200
Hierarchical DBMS
Data is represented by a tree structure
P1 Nut Red 12 London P2 Bolt Green 17 Paris

S2 Jones 10 Paris 300 S3 Blake 30 Paris 200


S1 Smith 20 London 300 S2 Jones 10 Paris 400
S1 Smith 20 London 200

P3 Screw Blue 17 Rome P4 Screw Red 14 London

S1 Smith 20 London 400


Hierarchical DBMS (Contd.)
Can not handle Many-Many relations

Can not reflect all real life situations

Anomalies in insert, delete and update


operations.
Network DBMS
Data is represented by records and
pointers
Addresses Many-Many relations
Insert,delete,update operations possible
Complex in design
Relational DBMS
Based on Relational Mathematics
principles
Data is represented in terms of rows
and columns of a table
Addresses all types of relations
Easy to design
No anomalies for insert/delete/update
Relational Terminology
Tuple (Row)
Attribute (Column)
Relation (Table)

Integrity Constraints
Primary Key
Alternate Key
Foreign Key
Normalization
Normalization
Normalization - process of removing
data redundancy by decomposing
relations in a Database.

De normalization - carefully introduced


redundancy to improve query
performance.
Normalization through
decomposition
The decomposition approach starts with
one relation and the relation is
decomposed into more number of
relations to remove insert, delete and
update anomalies.

1NF, 2NF, 3NF and BCNF can be


achieved by this approach.
Introduction to Databases.

A database is a collection of Data (Information). Examples of databases, which we use


in our daily life, is an Attendance Register, Telephone Directory, Muster Rule.

Database Management System(DBMS): A database management system is a


collection of programs written to manage a database. That is, it acts as a interface
between user and database.

.
Un normalized Form
A relation is said to be in Un normalized
Form (0NF) if the values of any of its
attributes are non-atomic. In other
words more than one value is
associated with each instance of the
attribute.
Un normalized Relation
S# PQ
P# QTY

S1 P1 300
P2 200
P3 400
P4 200

S2 P1 300
P2 400

S3 P2 200
Introduction to RDBMS

A Database Management System based on Relational Data Model


is known as Relational Database Management System (RDBMS).

The father of Relational Data Model was Dr. E.F. CODD. He


developed the relational data model by taking the concept from
Relational Algebra in June-1970.

Relational Data Model is nothing but 12 Rules which are named


after Codd as Codd Rules. According to Codd a package can be
called as RDBMS only if it satisfies the Codd Rules

.
CODDS RULES

Information Rule: All information in a relational database including table names,


column names are represented by values in tables

Guaranteed Access Rule: Every piece of data in a relational database, can be


accessed by using combination of a table name, a primary key value that identifies the
row and column name which identified a cell

Systematic Treatment of Nulls Rule: The RDBMS handles records that have
unknown or inapplicable values in a pre-defined fashion

View Updating Rule: Any view that is theoretically updateable can be updated using
the RDBMS. Data consistency is ensured since the changes made in the view are
transmitted to the base table and vice-versa

High-Level Insert, Update and Delete: The RDBMS supports insertions, updation
and deletion at a table level. .
Physical Data Independence: The execution of adhoc requests and application programs is
not affected by changes in the physical data access and storage methods

Logical Data Independence: Logical changes in tables and views such adding/deleting
columns or changing fields lengths need not necessitate modifications in the programs or in
the format of adhoc requests.

Integrity Independence: Like table/view definition, integrity constraints are stored in the
on-line catalog and can therefore be changed without necessitating changes in the application
programs

Distribution Independence: Application programs and adhoc requests are not affected by
change in the distribution of physical data

No subversion Rule: If the RDBMS has a language that accesses the information of a record
at a time, this language should not be used to bypass the integrity constraints. This is
necessary for data integrity

.
Oracle Datatypes

Fixed-length character data of length size bytes or Fixed for every row in the table (with trailing
Char characters blanks); maximum size is 2000 bytes per row

VARCHAR2 Variable-length character data Variable for each row, up to 4000 bytes per row

NCHAR Fixed-length Unicode character 2000 bytes per row

NVARCHAR2 Variable-length Unicode character data The upper limit is 4000 bytes per row

CLOB Single-byte character data Up to 232 - 1 bytes, or 4 gigabytes.

NCLOB Unicode national character set (NCHAR) data. Up to 232 - 1 bytes, or 4 gigabytes.

LONG Variable-length character data 2 gigabytes

.
NUMBER (p, s) Variable-length numeric dataVariable-length The maximum space required for
numeric data. Maximum precision p and/or scale a given column is 21 bytes per
s is 38. row

DATE Fixed-length date and time data Fixed at 7 bytes for each row

INTERVAL YEAR A period of time, represented as years and Fixed at 5 bytes.


(precision) TO months.
MONTH

INTERVAL DAY A period of time, represented as days, hours, Fixed at 11 bytes.


(precision) TO minutes, and seconds.
SECOND (precision)
BLOB Unstructured binary data 4 gigabytes

BFILE Binary data stored in an external file 4 gigabytes

.
Data Definition Language

A Data Definition Language or Data Description Language (DDL) is a computer


language for defining data structures.

CREATE statements
A CREATE statement in SQL creates an object inside of a relational database management
system (RDBMS). Like table, index, or stored query

CREATE TABLE statement


CREATE [TEMPORARY] TABLE [table name] ( [column definitions] ) [table parameters].

Column definition: [column name] [data type] {NULL | NOT NULL} {column options}
Primary key definition: PRIMARY KEY ( [comma separated column list] )
CONSTRAINTS: {CONSTRAINT} [constraint definition]

CREATE TABLE employees ( id number(10,2) , first_name CHAR(50) NULL,


last_name CHAR(75) NOT NULL, dateofbirth DATE NULL );
.
DROP statements

A DROP statement in SQL removes an object from a relational database


management system (RDBMS).
Like tables,views,synanyms,indexes

DROP TABLE employees;

ALTER statements
Alter - To modify an existing database object

ALTER TABLE sink ADD bubbles INTEGER;

ALTER TABLE sink DROP COLUMN bubbles;

.
Renaming a table

ALTER TABLE table_name


RENAME TO new_table_name;

Modifying column(s) in a table

ALTER TABLE supplier MODIFY ( supplier_name varchar2(100) not


null, city varchar2(75)

Rename the colums

ALTER TABLE supplier


RENAME COLUMN supplier_name to sname;
.
Integrity Constraints

Data integrity allows to define certain data quality requirements that the data
in the database needs to meet

Not Null
A column in a table can be specified not null. It's not possible to insert a null
in such a column.

create table ri_not_null ( a number not null, b number null, c number );


insert into ri_not_null values ( 1, null, null);

insert into ri_not_null values ( 2, 3, 4);

insert into ri_not_null values (null, 5, 6);

.
Unique Key

The unique constraint doesn't allow duplicate values in a


column.

create table ri_unique ( a number unique, b number );

insert into ri_unique values (4, 5);


insert into ri_unique values (4, 5);

create table ri_3 ( a number, b number, c number, constraint


uq_ri_3 unique (a) );

.
Composite Unique Key

A unique constraint can be extended over multiple columns:

create table ri_3 ( a number, b number, c number, constraint


uq_ri_3 unique (a,b) );

Adding the Adding unique constrain

alter table ri_unique add contraint uq_ri_b unique (b);

Doping the Unique constariaint

Alter table drop contraint ri_unique


.
Primary Key

On a technical level, a primary key combines a unique and


a not null constraint

a table can have at most one primary key

create table ri_primary_key ( a number primary key, b


number );

create table ri_primary_key_1 ( a number, b number, c


number, constraint pk_name primary key (a, b) );
.
Foreign Key

A foreign key constraint (also called referential integrity


constraint) on a column ensures that the value in that column
is found in the primary key of another table.

CREATE TABLE supplier ( supplier_id numeric(10) not null, supplier_name


varchar2(50) not null, contact_name varchar2(50), CONSTRAINT
supplier_pk PRIMARY KEY (supplier_id) );

CREATE TABLE products ( product_id numeric(10) not null, supplier_id


numeric(10) not null, CONSTRAINT fk_supplier FOREIGN KEY
(supplier_id) REFERENCES supplier(supplier_id) );

.
Foreign key with more than one field

CREATE TABLE supplier


( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
CONSTRAINT fk_supplier_comp
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name) );
.
Check

A check constraint allows to state a minimum requirement


for the value in a column

create table ri_check_1 ( a number check (a between 0


and 100), b number );

create table ri_check_2 ( begin_ number, end_ number,


value_ number, check (begin_ < end_) );

.
Disabling Constraints

create table foo (bar number, baz number, unique (bar, baz));

alter table foo disable unique (bar, baz);

create table foo (bar number, baz number, constraint uq_foo


unique (bar, baz));

alter table foo disable constraint uq_foo;

.
TRUNCATE - remove all records from a table,
including all spaces allocated for the records are
removed

Truncate table employee;

.
Data Manipulation language (DML)

Data Manipulation Language (DML) statements are used for managing


data within schema objects. Some examples:

SELECT - retrieve data from the a database

INSERT - insert data into a table

UPDATE - updates existing data within a table

DELETE - deletes all records from a table, the space for the records remain

MERGE - UPSERT operation (insert or update)

CALL - call a PL/SQL or Java subprogram


.
Examples tables for DML

create table emp(empno number(4),


ename varchar2(30),
job char(10),
mgr number(4),
hiredate date,sal number(7,2),
deptno number(2));

.
Examples tables for DML

create table dept(deptno number(2),dname


varchar2(20),
loc varchar2(20));

create table salgrade(grade number(1),losal


number(4),hisal number(4));

.
Insert statement

insert into emp values( 7369, 'SMITH', 'CLERK', 7902 ,'17-DEC-80' ,800, 20);

insert into emp values(7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-81', 1600, 30);

insert into emp values(7521, 'WARD', 'SALESMAN', 7698, '22-FEB-81', 1250, 30);

insert into emp values(7698, 'BLAKE', 'MANAGER', '01-MAY-81', 3850, 30);

insert into emp values(7902, 'FORD', 'ANALYST', 7566, '03-DEC-81', 3000, 10);

.
dept

insert into dept values(10, 'STORE' ,'CHICAGO');

insert into dept values(20, 'RESEARCH' ,'DALLAS');

insert into dept values(30, 'SALES', 'NEW YORK');

insert into dept values(40, 'MARKETING', 'BOSTON');

.
SALGRADE

insert into SALGRADE values(1, 700, 1200);

insert into SALGRADE values(2, 1201, 1400);

insert into SALGRADE values(3, 1401, 2000);

insert into SALGRADE values(4, 2001, 3000);

insert into SALGRADE values(5, 3001, 9999);

.
.
Select Statement

select [distinct] <column(s)>


from <table>
[ where <condition> ]
[ order by <column(s) [asc|desc]> ]

.
Selecting Columns

select LOC, DEPTNO from DEPT;

select from EMP;

Arithmetic operators

select ENAME, DEPTNO, SAL 1.55 from EMP;

.
Sorting

select ENAME, DEPTNO, HIREDATE from EMP;


from EMP order by DEPTNO [asc];

select ENAME, DEPTNO, HIREDATE from EMP;


from EMP order by DEPTNO [asc], HIREDATE desc;

.
Filtering the Data

select JOB, SAL from EMP where MGR = 7698;

Operators
=, != or <>,<, >,<=, =>

select JOB, SAL from EMP where MGR <>7698;

select JOB, SAL from EMP where MGR >= 7698;

.
Set Conditions:

Set Conditions: <column> [not] in (<list of values>)

select from DEPT where DEPTNO in (20,30);

Null value: <column> is [not] null,

select from EMP where MGR is not null

Domain conditions: <column> [not] between <lower bound> and


<upper bound>

select EMPNO, ENAME, SAL from EMP where SAL between 1500
and 2500; .
select JOB, SAL from EMP
where (MGR = 7698 or MGR = 7566) and SAL > 1500;

Select * from emp where ename like M%

Seleect * from emp where enam lime %M

.
String functions

string || string Concatenates string values


INITCAP(string) Converts a string to initial capital letters
LENGTH(string) Returns the number of characters in a string
LOWER(string) Converts a string to all lowercase characters
SUBSTR(string,starting Extracts a portion of a string
value,number of
characters)
UPPER(string) Converts a string to all uppercase characters
LPAD Add characters to the left of a string until a fixed number
is reached
LTRIM LTRIM removed characters from the left of a string if they
are equal to the specified string. Example:
ltrim('aaaaaabc','a') = 'bc'
RTRIM RTRIM removed characters from the right of a string if
. equal to the specified string.
they are
Date Fuctions

ADD_MONTHS Adds the specified number of months to the date value


LAST_DAY(date) Returns the last day of the month that contains the date
MONTHS_ Returns the difference between two dates
BETWEEN(date1,date2)
NEXT_DAY(date,day Returns the date of the first day of the specified name
name) that is later than the date supplied
ROUND (datetime, Returns the date-time rounded to the unit specified by
format) the format,
SYSDATE Returns the current date-time from the server where the
database is located
TRUNC(datetime) Removes the time component from a date-time value

.
Conversion functions

TO_CHAR(date,format) Converts a date to a string in the specified format

TO_CHAR(number, Converts a number to a string in the specified format


format)

TO_DATE(string,format) Converts a string to a date using the specified format

TO_NUMBER Converts a string to a number using the optional


(string, format) format if specified

.
Group Functions

AVG(expression) Returns the average of the values in a set of rows

COUNT(expression) Returns the number of rows in the set

MAX(expression) Returns the largest value from a set of rows

MIN(expression) Returns the smallest value from a set of rows

SUM(expression) Adds the value for all rows in the query or for all rows
with the same values for columns listed in the GROUP BY
clause

.
Numeric functions

ABS(number) Removes the sign, if any, returning a positive value


GREATEST(value1, Returns the largest of the values in the list
value2, )

LEAST(value1, Returns the smallest of the values in the list


value2, )

ROUND(number, Rounds a value to the specified number of decimal


decimal places) places

TRUNC(number, Cuts off a value at the specified number of decimal


decimal places) places
TRUNC(123.456,2) returns 123.45
TRUNC(234567.00,-3) returns 234000
.
Updates

For modifying attribute values of (some) tuples in a table

update <table> set


<column i> = <expression i>, . . . , <column j> =
<expression j>
[where <condition>];

update EMP set


JOB = MANAGER, DEPTNO = 20, SAL = SAL +1000
where ENAME = JONES;

.
update EMP set SAL = SAL 1.15 where DEPTNO in (10,30);

update EMP set SAL = (select min(SAL) from EMP


where JOB = MANAGER)
where JOB = SALESMAN and DEPTNO = 20;

.
Deletions

delete from <table> [where <condition>];

delete from PROJECT where PEND < sysdate;

.
TCL command?

Commit

Rollback

Savepoint

.
Joining Relations

Comparisons in the where clause are used to combine rows


from the tables listed in the from clause.

select ENAME, E.DEPTNO, DNAME from EMP E, DEPT D


where E.DEPTNO = D.DEPTNO and JOB = SALESMAN;

select ENAME, DNAME, PNAME from EMP E, DEPT D,


PROJECT Pwhere E.EMPNO = P.MGR
and D.DEPTNO = E.DEPTNO;

.
Self Join

List the names of all employees together with the name of


their manager

select E1.ENAME, E2.ENAME


from EMP E1, EMP E2
where E1.MGR = E2.EMPNO;

.
Sub Query

List the name and salary of employees of the department


20 who are leading a project that started before December
31, 1990:

select ENAME, SAL from EMP


where EMPNO in
(select PMGR from PROJECT
where PSTART < 31-DEC-90)
and DEPTNO =20;

.
List all employees who are working in a department located
in BOSTON:

select from EMP where DEPTNO in (select DEPTNO from


DEPT where LOC = BOSTON);

Retrieve all employees who are working in department 10


and who earn at least as much as any (i.e., at least one)
employee working in department 30:

select from EMP where SAL >= any(select SAL from EMP
where DEPTNO = 30) and DEPTNO = 10;
.
List all departments that have no employees:

select from DEPT


where not exists
(select from EMP
where DEPTNO = DEPT.DEPTNO);

.
Set operators

union [all] returns a table consisting of all rows either


appearing in the result of <query1> or in the result of
<query 2>. Duplicates are automatically eliminated unless
the clause all is used.

intersect returns all rows that appear in both results <query


1> and <query 2>.

minus returns those rows that appear in the result of


<query 1> but not in the result of <query 2>.

.
Views

create [or replace] view <view-name> [(<column(s)>)] as


<select-statement>

The following view contains the name, job title and the
annual salary of employeesworking in the department 20:
create view DEPT20 as

select ENAME, JOB, SAL12 ANNUAL SALARY from EMP


where DEPTNO = 20;

.
DECODE

Select deptno,decode(deptno, 10,accounts,


20,marketing,
30,hra,stores)
as dept_name from dept

.
Nvl function

The NVL and NVL2 functions allow you to test an


expression to see whether it is NULL

Select nvl(ename,no name) as name from emp;

.
CASE

A searched CASE expression evaluates a number of conditions


and returns a result determined by which condition is true

CASE
WHEN C1 THEN R1
WHEN C2 THEN R2
...
WHEN CN THEN RN
ELSE RD
END

.
SELECT TO_CHAR(order_dt, 'Q') sales_quarter,
SUM(sale_price) tot_sales FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-
YYYY)AND order_dt < TO_DATE('01-JAN-2002','DD-MON-
YYYY') GROUP BY TO_CHAR(order_dt, 'Q)ORDER BY 1;

S TOT_SALES
- ----------
1 9739328
2 10379833
3 9703114
4 9772633
.
SELECT
SUM(DECODE(TO_CHAR(order_dt, 'Q'), '1', sale_price, 0)) Q_1,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '2', sale_price, 0)) Q_2,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '3', sale_price, 0)) Q_3,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '4', sale_price, 0)) Q_4
FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-YYYY')
AND order_dt < TO_DATE('01-JAN-2002','DD-MON-YYYY');

Q_1 Q_2 Q_3 Q_4


---------- ---------- ---------- ----------
9739328 103798 33 970311 4 9772633

.
SELECT LNAME, EMP_ID, MANAGER_EMP_ID FROM EMPLOYEE
START WITH MANAGER_EMP_ID IS NULL CONNECT BY PRIOR
EMP_ID = MANAGER_EMP_ID;

LNAME EMP_ID MANAGER_EMP_ID


-------------------- ---------- --------------
KING 7839
JONES 7566 7839
SCOTT 7788 7566
ADAMS 7876 7788
FORD 7902 7566
SMITH 7369 7902
BLAKE 7698 7839
ALLEN 7499 7698
.

You might also like