Date post: | 22-May-2015 |
Category: |
Education |
Upload: | sureshreddy-nallapareddy |
View: | 952 times |
Download: | 1 times |
SAP EXPO.COM
SAP EXPO.COM
Date: 17th December, 2009
Location: Kolkata
SAP EXPO.COM
SAP EXPO.COM
Index Introduction to RDBMS
Table Keys
Normalization
SQL
Transaction
SAP EXPO.COM
SAP EXPO.COM
Introduction to RDBMS
•Relational Database Management System is the most preferred system for Storing and managing information.
•Data is stored in the form of tables and the relationship among the data is also stored in the form of tables.
•Must adhere to Codd’s rules
SAP EXPO.COM
SAP EXPO.COM
Architecture
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 1 – Information Rule :-
All information is explicitly and logically represented in exactly one way – by data values in tables.
If an item of data does not reside somewhere in a table in the database, then it does not exist.
Rule 2 – Rule of Guaranteed Access :-
Every item of data must be logically addressable by resorting to a combination of table name, primary key value and a column name.
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 3 – The Systematic Treatment of Null Values :-
The RDBMS handles records that have unknown or inapplicable values in a pre-defined fashion.
The RDBMS distinguishes between zeros, blanks and nulls in the records hand handles such values in a consistent manner that produces correct answers, comparisons and calculations. Through the set of rules for handling nulls, users can distinguish results of the
queries that involve nulls, zeros and blanks.
Rule 4 – The Database Description Rule :-
The description of a database and in its contents are database tables and therefore can be queried on-line via the data manipulation language.
There must be a Data Dictionary within the RDBMS that is constructed of tables and/or views that can be examined using SQL.
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 5 – The Comprehensive Sub-language rule :-
A RDBMS may support several languages. But at least one of them should allow user to do all of the following:
•Define tables and views
•Query and update the data
•Set integrity constraints
•Set authorizations
•Define transactions.
RDBMS must be completely manageable through its own dialect of SQL.
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 6 – The View Updating Rule :-
All views that can be updated in theory, can also be updated by the system.
Data consistency is ensured since the changes made in the view are transmitted to the base
table and vice-versa. .
Rule 7 – High Level Insert, Update and Delete Rule
The RDBMS supports insertions, updation and deletion at a table level.
This means that data can be retrieved from a relational 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.
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 8 – The Physical Independence Rule :-
The execution of adhoc requests and application programs is not affected by changes in the physical data access and storage methods.
Database administrators can make changes to the physical access and storage method which improve performance and do not require changes in the application programs or requests. Here the user specified what he wants an need not worry about how the data is obtained. Rule 9 – Logical Data Interdependence Rule:-
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.
The database can change and grow to reflect changes in reality without requiring the user intervention or changes in the applications. For example, adding attribute or column to the base table should not disrupt the programs or the interactive command that have no use for
the new attribute.
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 10 – Integrity Independence Rule :-
Integrity constraints must be specified separately from application programs and stored in the catalog. It must be possible to change such constraints as and when appropriate without unnecessarily affecting existing applications.
The following Integrity rules(Constraints) should apply to every RDBMS :-
No component of a primary key can have missing values – this is the basic rule of Entity Integrity.
For each distinct foreign key value there must exist a matching primary key value in the same domain. Conformation to this rule ensures what is called Referential integrity.
SAP EXPO.COM
SAP EXPO.COM
Codd’s RulesRule 11 – Distribution Rule :-
Application programs and adhoc requests are not affected by change in the distribution of physical data.
Improved systems reliability since application programs will work even if the programs and
data are moved in different sites.
Rule 12 – 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.
SAP EXPO.COM
SAP EXPO.COM
Index Introduction to RDBMS
Table Keys
Normalization
SQL
Transaction
SAP EXPO.COM
SAP EXPO.COM
Table Keys
Unique Key : Contains one or more columns with non-repeating values in a table . Might consider null values.
Primary Key : Contains one/more columns with non-repeating values in a table. Does not consider null values.
Composite Key : A key formed by combining at least two or more columns is called composite key .
SAP EXPO.COM
SAP EXPO.COM
Table Keys
Foreign Key : A foreign key is a field in a relational table that matches the primary key column of another table. The foreign key can be used to
cross-reference tables.
SAP EXPO.COM
SAP EXPO.COM
Index Introduction to RDBMS
Table Keys
Normalization
SQL
Transaction
SAP EXPO.COM
SAP EXPO.COM
Normalization
Systematic way of ensuring that a database structure is suitable for general-purpose querying and free of certain undesirable characteristics—insertion, update, and deletion anomalies—that could lead to a loss of data integrity .
First Normal Form(1NF)
Second Normal Form(2NF)
Third Normal Form(3NF)
Byce-Code Normal Form(BCNF)
Forth Normal Form(4NF)
Fifth Normal Form(5NF)
Sixth Normal Form(6NF)
SAP EXPO.COM
SAP EXPO.COM
First Normal Form ( 1NF)
An entity is in First Normal Form (1NF) when all tables are two-
dimensional with no repeating groups.
Eliminate duplicative columns from the same table.
Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).
The only attribute values permitted by 1 NF are single atomic (or indivisible) values. If there is no value present for the column, then it should be denoted as null.
SAP EXPO.COM
SAP EXPO.COM
Second Normal Form ( 2NF)
A row is in second normal form if, and only if, it is in first normal form and every non-key attribute is fully dependent on the key.
SAP EXPO.COM
SAP EXPO.COM
Third Normal Form ( 3NF)
A row is in third normal form if, and only if, it is in second normal form and every non-key attribute is non-transitively dependent on the primary key.
SAP EXPO.COM
SAP EXPO.COM
Fourth Normal Form ( 4NF)
An entity is in fourth normal form if, and only if, it is in third normal form and no entity can have more than a single one-to-many relationship within an entity if the one-to-many attributes are independent of each other
SAP EXPO.COM
SAP EXPO.COM
Fifth Normal Form ( 5NF)
A table is in fifth normal form (5NF) or Project-Join Normal Form (PJNF) if it is in 4NF and it cannot have a lossless decomposition into any number of smaller tables.
SAP EXPO.COM
SAP EXPO.COM
Index Introduction to RDBMS
Table Keys
Normalization
SQL
Transaction
SAP EXPO.COM
SAP EXPO.COM
Structured Query Language(SQL)• Common language to
almost any RDBMS.
- Used to create tables, inserting and updating records, deleting
records,creating view,Maintaining database tables etc.
SAP EXPO.COM
SAP EXPO.COM
Data Definition Language(DDL):-
create table,view etc.
Data Query Language(DQL):-
selection of records
Data Manipulation Language(DML):-
Updation, deletion of records
Features of SQL:-
SAP EXPO.COM
SAP EXPO.COM
Data Definition Statement – CREATE TABLE
create table employee_history ( employee_id number(6) not null, salary number(8,2), hire_date date default sysdate, termination_date date, termination_desc varchar2(4000), constraint emphistory_pk primary key (employee_id, hire_date) );
Create table with Primary key:-
SAP EXPO.COM
SAP EXPO.COM
Data Definition Statement – CREATE TABLE
create table employee_master ( employee_id number(6) not null, empname varchar2(50) not null, age number(2) not null , sex varchar2(1) not null ) ;
Alter table employee_masterAdd constraint pk_empPrimary key(employee_id) ;
Create table and then add Primary key:-
SAP EXPO.COM
SAP EXPO.COM
Data Definition Statement – CREATE TABLE
create table another_dept as select * from employee;
Create table Selecting from other table :-
SAP EXPO.COM
SAP EXPO.COM
Data Manipulation Statement – Insert Records into table
INSERT INTO tbl1(col1,col2,col3,col4) VALUES(val1,val2,val3,val4), (val11,val12,val13,val14);
Inserting Multiple Records:-
INSERT into california_authors (au_id, au_lname, au_fname) SELECT au_id, au_lname, au_fname FROM authors WHERE State = 'CA'
Inserting using SELECT :-
SAP EXPO.COM
SAP EXPO.COM
SELECTION OF RECORDS
SAP EXPO.COM
SAP EXPO.COM
SELECT COL1,COL2,..,..,
FROM tbl1………
WHERE ……………..
AND ……………..
OR ………………
GROUP BY ………..
HAVING …….
The Red syntaxes are compulsory. Blue are used as and when required.
Basic Syntax :-
SAP EXPO.COM
SAP EXPO.COM
Select * from tbl1
Where col1 = “ABC”
And …………
Contains selection criteria
Type of Selection:-
Select all Records :-
SELECT * FROM TABL1 ;
SELECT COL1 , COL2 FROM TABL1;
Selecting Few records :-
SAP EXPO.COM
SAP EXPO.COM
Selection of few columns:-
Select emp_code,emp_name from emp_mast;
Renaming Columns:-
Select emp_code, name=emp_name from emp_mast;
Arithmetic Calculations in Selection:-
Select basic, da=1.2*basic from salary_mast;
SELECT…continued
SAP EXPO.COM
SAP EXPO.COM
OPERATORS
AND
OR
IN
BETWEEN
NOT IN
DISTINCT
LIKE
SAP EXPO.COM
SAP EXPO.COM
AND :
Used to incorporate more than one selection criteria
Select emp_code, city_code
FROM emp_dtl
WHERE city_code=‘C01’
AND desig_code=‘A03’;
OR:-
SELECT emp_code FROM emp_dtl
WHERE city_code=‘C01’
OR city_code=‘C02’;
SAP EXPO.COM
SAP EXPO.COM
IN :
Used to put more than one value for a column
SELECT emp_code FROM emp_dtl
WHERE city_code in (‘C01’,’C02’);
BETWEEN:
Used to specify a range in selection criteria
SELECT emp_code, dep_code
from emp_joining
WHERE doj BETWEEN ‘1/1/2009’ AND 31/12/2009’;
SAP EXPO.COM
SAP EXPO.COM
NOT IN:
Used to exclude some criteria from selection
Select emp_code from emp_dtl
where city_code not in (‘C01’,’C02’);
DISTINCT:
To select single occurrence of some value in columns
select distinct city_code
from emp_dtl;
LIKE: Used for pattern matching
select empname from emp_mast
where name like ‘S%’;
SAP EXPO.COM
SAP EXPO.COM
AGGREGATE FUNCTIONS• SUM
• AVG
• MAX
• MIN
• COUNT
Group By clause is used with aggregate functionsWhere selection from more than one columns are made
SAP EXPO.COM
SAP EXPO.COM
SUM:Totals numeric,float and money values
SELECT SUM(bonus)
FROM performance
WHERE year=2008;
SELECT emp_code,
sum(bonus)
from performance
WHERE year=2008
GROUP BY emp_code;
SAP EXPO.COM
SAP EXPO.COM
Having Clause Syntax/Example
SELECT material , Totamt = sum(storage_amt) from store_dtl GROUP BY material HAVING SUM( storage_amt ) > 9000.
Basic form ... HAVING condition.
Used in a SELECT command, it allows you to use a logical condition for the groups in a GROUP BY clause.
SAP EXPO.COM
SAP EXPO.COM
AVG:Finds out average value in a column of selected records
select avg(bonus) from performance
where emp_code=‘E01’;
select month, year, bonus=avg(bonus)
from performance
group by month,year;
SAP EXPO.COM
SAP EXPO.COM
MAX and MIN: Max returns maximum of the values in a column of selected records and MIN returns the minimum
select max(bonus) from performance;
select emp_code, min(bonus) from performance
where year=2008
group by emp_code;
COUNT:-
Count no of values in table or column.
select emp_code, count(*) from performance
where year=2008
Group by emp_code;
SAP EXPO.COM
SAP EXPO.COM
Select count(name) from emp_mast
Where emp_name like ‘S%’;
Select count(*) from emp_dtl;
Select count(emp_code) from emp_dtl
Where city_code=‘C01’;
SAP EXPO.COM
SAP EXPO.COM
Sorting By Primary Key :
To sort the selection set in ascending order by the primary key, use the following:
SELECT <lines> * ... ORDER BY PRIMARY KEY.
This sorting method is only possible if asterisk (*) in the SELECT clause is used to select all columns.
Sorting By Any Number of Columns
SELECT ... ...ORDER BY <s1> [ASCENDING|DESCENDING] <s2> [ASCENDING|DESCENDING] ...
Sorting records
SAP EXPO.COM
SAP EXPO.COM
Used to retrieve records from more than one table using matching conditions.
Equi join(Inner join)
Self Join
Outer join(left and right join)
Using JOINS
SAP EXPO.COM
SAP EXPO.COM
Join Between Two Tables
EQUI JOIN
SELECT a.ename , b.designation, b.salary from emp_mast a , emp_dept bwhere a.emp_code = b.emp_code ;
SAP EXPO.COM
SAP EXPO.COM
Between more than two tables
EQUI JOIN…continued
SELECT a.ename , b.dept_name, c.designation, c.salary from emp_mast a , dept_mast b, emp_dept cwhere c.emp_code = a.emp_code and c.dept_code = b.dept_code;
SAP EXPO.COM
SAP EXPO.COM
SELF JOIN Sometimes the records to be selected remains in a single table,but it cannot be
viewed by simple selection.For that, a record need to be broken into more than
one fragments.In that case, a single table is hypothetically broken into more
than one fragments, and each fragment is treated as a single table and joins are
established between them.
Need information on Employee Name and Manager Name
SAP EXPO.COM
SAP EXPO.COM
SELF JOIN
SELECT employee = w.ename , manager = m.ename FROM emp_manager w, emp_manager m WHERE w.manager_id = m.empno;
SAP EXPO.COM
SAP EXPO.COM
Outer Join
Left Outer Join
SAP EXPO.COM
SAP EXPO.COM
Outer Join
Right Outer Join
SAP EXPO.COM
SAP EXPO.COM
Outer Join
Full Outer Join
SAP EXPO.COM
SAP EXPO.COM
SUBQUERYQuery involving subqueries contain more than one query where output of one query becomes input of other query.
select * from emp_dtl
where desig_code in (
select desig_code from salary_mast
where total>10000);
SAP EXPO.COM
SAP EXPO.COM
Updating a table can be as following:-
1. Update the table with input values from outside.
2. Update a table by its own.
3. Update a table by another table.
Updating Table Entries
SAP EXPO.COM
SAP EXPO.COM
Explicit Update :-
UPDATE table tblname
SET colname = value,
……………
WHERE search conditions;
Example:- UPDATE emp_dtl
SET qual = ‘HIGHER SECONDARY’
WHERE emp_code = ‘E17’;
SAP EXPO.COM
SAP EXPO.COM
Implicit Update:-
Update salary_mast
Set basic=1.2*basic,
special=1.2 * special,
conveyance=1.2 * conveyance,
medical=1.2* medical,
total=1.2* total
WHERE desig_code in (‘A04’,’A05’);
SAP EXPO.COM
SAP EXPO.COM
Updation with Reference to Other Table :-
UPDATE emp_sal a
FROM desig_mast b
SET salary=b.total
WHERE a.desig_code=b.desig_code;
UPDATE emp_sal a
FROM performance b
SET performance=b.bonus
WHERE a.emp_code=b.emp_code
AND a.month_code=b.month_code
AND a.year_code=b.year_code AND a.month_code=12
AND a.year_code=2000
SAP EXPO.COM
SAP EXPO.COM
Deletion of Records :-
DELETE FROM tablename
WHERE
search_conditions;
Example:-
DELETE FROM performance
WHERE emp_code=‘E01’
AND month_code=12
AND year_code=2000;
SAP EXPO.COM
SAP EXPO.COM
Index Introduction to RDBMS
Table Keys
Normalization
SQL
Transaction
SAP EXPO.COM
SAP EXPO.COM
TransactionsA database transaction is a sequence of SQL statements that the database system treats as a unit. A transaction brings the database from one consistent state to another.
SAP EXPO.COM
SAP EXPO.COM
Transactions
A transaction is always completed by a COMMIT or ROLLBACK.
When a transaction is successfully concluded with a COMMIT, all of
the data changes are retained.
If a transaction is ended with a ROLLBACK or terminated in any
other way, the database system reverses all the data changes made
during the transaction.
SAP EXPO.COM
SAP EXPO.COM
Thank You