Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

2 rdbms


Published on

suresh presentation on RDBMS concepts

Published in: Education, Technology

2 rdbms

  1. 1. SAP EXPO.COM SAP Date: 17th December, 2009 Location: Kolkata
  2. 2. SAP EXPO.COM SAP Index  Introduction to RDBMS  Table Keys  Normalization  SQL  Transaction
  3. 3. SAP EXPO.COM SAP 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
  4. 4. SAP EXPO.COM SAP Architecture
  5. 5. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  6. 6. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  7. 7. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  8. 8. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  9. 9. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  10. 10. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  11. 11. SAP EXPO.COM SAP Codd’s Rules Rule 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.
  12. 12. SAP EXPO.COM SAP Index  Introduction to RDBMS  Table Keys  Normalization  SQL  Transaction
  13. 13. SAP EXPO.COM SAP 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 .
  14. 14. SAP EXPO.COM SAP 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.
  15. 15. SAP EXPO.COM SAP Index  Introduction to RDBMS  Table Keys  Normalization  SQL  Transaction
  16. 16. SAP EXPO.COM SAP 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)
  17. 17. SAP EXPO.COM SAP 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.
  18. 18. SAP EXPO.COM SAP 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.
  19. 19. SAP EXPO.COM SAP 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.
  20. 20. SAP EXPO.COM SAP 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
  21. 21. SAP EXPO.COM SAP 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.
  22. 22. SAP EXPO.COM SAP Index  Introduction to RDBMS  Table Keys  Normalization  SQL  Transaction
  23. 23. SAP EXPO.COM SAP 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.
  24. 24. SAP EXPO.COM SAP 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:-
  25. 25. SAP EXPO.COM SAP 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:-
  26. 26. SAP EXPO.COM SAP 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_master Add constraint pk_emp Primary key(employee_id) ; Create table and then add Primary key:-
  27. 27. SAP EXPO.COM SAP Data Definition Statement – CREATE TABLE create table another_dept as select * from employee; Create table Selecting from other table :-
  28. 28. SAP EXPO.COM SAP 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 :-
  30. 30. SAP EXPO.COM SAP 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 :-
  31. 31. SAP EXPO.COM SAP 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 :-
  32. 32. SAP EXPO.COM SAP 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
  34. 34. SAP EXPO.COM SAP 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’;
  35. 35. SAP EXPO.COM SAP 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’;
  36. 36. SAP EXPO.COM SAP 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%’;
  37. 37. SAP EXPO.COM SAP AGGREGATE FUNCTIONS • SUM • AVG • MAX • MIN • COUNT Group By clause is used with aggregate functions Where selection from more than one columns are made
  38. 38. SAP EXPO.COM SAP 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;
  39. 39. SAP EXPO.COM SAP 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.
  40. 40. SAP EXPO.COM SAP 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;
  41. 41. SAP EXPO.COM SAP 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;
  42. 42. SAP EXPO.COM SAP 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’;
  43. 43. SAP EXPO.COM SAP 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
  44. 44. SAP EXPO.COM SAP 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
  45. 45. SAP EXPO.COM SAP Join Between Two Tables EQUI JOIN SELECT a.ename , b.designation, b.salary from emp_mast a , emp_dept b where a.emp_code = b.emp_code ;
  46. 46. SAP EXPO.COM SAP 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 c where c.emp_code = a.emp_code and c.dept_code = b.dept_code;
  47. 47. SAP EXPO.COM SAP 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
  48. 48. SAP EXPO.COM SAP SELF JOIN SELECT employee = w.ename , manager = m.ename FROM emp_manager w, emp_manager m WHERE w.manager_id = m.empno;
  49. 49. SAP EXPO.COM SAP Outer Join Left Outer Join
  50. 50. SAP EXPO.COM SAP Outer Join Right Outer Join
  51. 51. SAP EXPO.COM SAP Outer Join Full Outer Join
  52. 52. SAP EXPO.COM SAP SUBQUERY Query 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);
  53. 53. SAP EXPO.COM SAP 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
  54. 54. SAP EXPO.COM SAP Explicit Update :- UPDATE table tblname SET colname = value, …………… WHERE search conditions; Example:- UPDATE emp_dtl SET qual = ‘HIGHER SECONDARY’ WHERE emp_code = ‘E17’;
  55. 55. SAP EXPO.COM SAP 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’);
  56. 56. SAP EXPO.COM SAP Updation with Reference to Other Table :- UPDATE emp_sal a FROM desig_mast b SET 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
  57. 57. SAP EXPO.COM SAP 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;
  58. 58. SAP EXPO.COM SAP Index  Introduction to RDBMS  Table Keys  Normalization  SQL  Transaction
  59. 59. SAP EXPO.COM SAP Transactions A 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.
  60. 60. SAP EXPO.COM SAP 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.
  61. 61. SAP EXPO.COM SAP Thank You