A Free sample background from © 2006 By Default! R.SARAVANA KUMAR S.NAVEEN Relational Database Model
A Free sample background from © 2006 By Default! RELATIONAL MODEL Basically concerned with three things Data structure Data Integrity Data Manipulation A relation is defined as table with columns and rows. Data can be stored in form of a two dimensional table. Row will be the records Columns will be the attributes. Tuple – it a row of a relation. Degree of Relation – No of attributes it contains. Cardinality – No of tuples it contains.
A Free sample background from © 2006 By Default! Each attribute of a relation has a name The set of allowed values for each attribute is called the domain of the attribute The special value null is a member of every domain The current values (relation instance) of a relation are specified by a table Jones Smith Curry Lindsay customer_name Main North Park customer_street Harrison Rye Pittsfield customer_city attributes (or columns) tuples (or rows)
A Free sample background from © 2006 By Default! Database A database consists of multiple relations Information about an enterprise is broken up into parts, with each relation storing one part of the information account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers Storing all information as a single relation such as bank(account_number, balance, customer_name,..) results in repetition of information (e.g., two customers own an account) the need for null values (e.g., represent a customer without an account)
A Free sample background from © 2006 By Default! keys A key is a set of one or more attributes that uniquely identify each tuple in a relation. Keys can be classified as: Candidate key Super key Primary Key
A Free sample background from © 2006 By Default! Candidate Key (CK) When more than one or group of attributes serve as a unique identifier, they are each called as candidate key. Super Key (SK) an attribute or set of attributes that uniquely identifies a tuple within a relation. Primary Key (PK) It is defined as the candidate key that is selected to identify tuples uniquely within the relation. candidate Key(Ck) = Super key(Sk) - primary key(PK)
A Free sample background from © 2006 By Default! Foreign Key A foreign key is an attributes or combination of attributes in relation R2 whose values must match the primary key values in R1.In referring to the R1 tuple
A Free sample background from © 2006 By Default! Properties of primary key Stable : The value of a primary key must not change or should not become Null throughout the life of an entity. Minimal: The primary key should be composed of the minimum number of fields that endures the occurrence are unique. Definitive: A value must exist for every record at the time of creation. Accessible: Anyone who wants create, read or delete a record must be able to see the primary key value.
A Free sample background from © 2006 By Default! Relational integrity rule There are two important integrity rules: Entity integrity rule. In a base relation, the value of attribute of a primary key cannot be null. We should not be able to insert a tuple into a table with a NULL for primary key. If a user tries to do so then RDBMS reports an error message. Referential integrity rule. If a foreign key exists in a relation, either the foreign key value must match a candidate key value of some tuple in its home relation. The master table or the parent table which contains the linking column must be a primary key. The linking column in the transaction table or the child table reference the parent table. This column in the child table which reference a primary key of the parent table is called as a foreign key.
A Free sample background from © 2006 By Default! CODD’S RULES E.F. Codd formulated thirteen rules for RDBMS. 1.0-single foundation rule 2.Information rule. 3.Guaranteed Access 4.Systematic treatment of Null 5.Active online Catalogue 6.Comprehensive Data Sublanguage 7.View updation rule 8.High Level global insert, update & delete 9.Physical Data independence. 10.Logical Data independence 11.Integrity independence 12.Distribution independence 13.Non subversion rule.
A Free sample background from © 2006 By Default! Relational Algebra It is a procedure language with operations that is performed on one or more existing relation to derive result ( another) relations without changing the original relations. It has two categories 1. Common set operation a.Union b. Intersection c. Difference d. Product 2. Native relational operations a. Selection b. Projection c. Join d. Division
A Free sample background from © 2006 By Default! UNION ( U ) S.idNameAgeDept 02Anbu18CSE 12Gopi19ECE 42Ravi17ECE 52Karan20IT S.idNameAgeDept 10Rahul21IT 42Ravi17ECE 02Anbu18CSE 36Saran20CIVIL S.idNameAgeDept 10Rahul21IT 42Ravi17ECE 02Anbu18CSE 36Saran20CIVIL 12Gopi19ECE 52Karan20IT Combine two relation into single table & Eliminates duplicate records
A Free sample background from © 2006 By Default! INTERSECTION ( ∩ ) S.idNameAgeDept 02Anbu18CSE 12Gopi19ECE 42Ravi17ECE 52Karan20IT S.idNameAgeDept 10Rahul21IT 42Ravi17ECE 02Anbu18CSE 36Saran20CIVIL S.idNameAgeDept 42Ravi17ECE 02Anbu18CSE An intersect operator is used to get common records from two relations.
A Free sample background from © 2006 By Default! Difference ( -- ) S.idNameAgeDept 02Anbu18CSE 12Gopi19ECE 42Ravi17EEE 52Karan20IT Tuples that are in one relation but are not in another. The expression ( r – s ) produces a relation containing those tuples in r but not in s. S.idNameAgeDept 10Rahul21IT 42Ravi17ECE 02Anbu18CSE 36Saran20CIVIL S.idNameAgeDept 12Gopi19ECE 52Karan20IT
A Free sample background from © 2006 By Default! Cartesian Product ( Χ ) F.I d F.NameQualif.. 01SakthivelM.E 03AnandhanB.E 06SrinivasanM.S R.NS.Namesemester 01 AJIN.P. VARGHESE II 39 SOURAV DUTTA II 19 MORATANCH.N II A set of all possible records, that means combine two relations.
A Free sample background from © 2006 By Default! F.IdF.NameQualif..R.NS.Namesemester 01SakthivelM.E01 AJIN.P. VARGHESE II 01SakthivelM.E39 SOURAV DUTTA II 01SakthivelM.E19 MORATANCH.N II 03AnandhanB.E01 AJIN.P. VARGHESE II 03AnandhanB.E39 SOURAV DUTTA II 03AnandhanB.E19 MORATANCH.N II 06SrinivasanM.S01 AJIN.P. VARGHESE II 06SrinivasanM.S39 SOURAV DUTTA II 06SrinivasanM.S19 MORATANCH.N II
A Free sample background from © 2006 By Default! Selection( σ ) Native Relational Operations : Selection( σ ) Selects a subset of rows from relation (horizonta l). σ age < 30 ( emp) Emp_idEnameAgesalary 202Ramakrishnan Vel Murugan Mirthula Nirmala Ravikumar Emp_idEnameAgesalary 458Mirthula Nirmala
A Free sample background from © 2006 By Default! Projection ( π ) Retains only wanted columns from relation (vertical). Π age, salary ( emp) Emp_idEnameAgesalary 202Ramakrishnan Vel Murugan Mirthula Nirmala Ravikumar Agesalary
A Free sample background from © 2006 By Default! Division A / B contains all x tuples such that for every y tuple in B, there is an xy tuple in A. A B1 B2 B3 A/B1 A/B2A/B3
A Free sample background from © 2006 By Default! Join ( ) Retrieving data from multiple tables using a single SQL statement Natural join In which this duplication of column values is eliminated by taking a projection of the table which includes only one of the duplicated columns. left outer join take all tuples in the left relation that did not match with any tuple in the right relation, pads the tuples that tuple with null values for all other attributes from the right relation Right Outer join it pads tuple from the right relation that did not match any from that left relation with nulls Full outer join
A Free sample background from © 2006 By Default! EMBEDDED SQL
A Free sample background from © 2006 By Default! Embedded SQL Embedded SQLs are SQL statements included in the programming language. The programming language in which the SQL statements are included is called the host language. Some of the host languages are C,COBOL,Pascal,FORTRAN.
A Free sample background from © 2006 By Default! This embedded SQL source code is submitted to an SQL precompiler,which processes the SQL statements. Variables of the host language can be referenced in the embedded SQL statements thus allowing the values calculated by the program to be used by the SQL statements.
A Free sample background from © 2006 By Default! The host language variables are used by the embedded SQL statements to receive the result of the SQL queries thus allowing the programming language to process the retrieved values.
A Free sample background from © 2006 By Default! EMBEDDED SQL FEATURES The embedded SQL statement appears in the host language. Embedded SQL statements are prefixed by a delimiter EXEC SQL – so that they can be distinguished from the host language statements
A Free sample background from © 2006 By Default! Every embedded SQL statements is terminated with a delimiter. In COBOL it is END-EXEC.In C,Pascal it is semicolon. SQL statements can include reference to host variable.Such reference must be prefixed with a colon(:). Host variables and SQL columns can have the same name.
A Free sample background from © 2006 By Default! ADVATAGE OF EMBEDDED SQL The mixing of SQL statements with the programming language statements is an efficient way of merging the strength of two programming environments.
A Free sample background from © 2006 By Default! A simple COBOL Program. IDENTIFICATION DIVISION. PROGRAM ID EMBDSQL. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER IBM OBJECT-COMPUTER IBM * DATA DIVISION. WORKING-STORAGE SECTION. *Host variables for receiving the values retrieved using the SQL 01 WS-EMP-DETAILS 05 WS_EMPNO PIC 9(3). 05 WS_NAMEPIC X (30). 05 WS_DEPTID PIC X(2). EXEC SQL
A Free sample background from © 2006 By Default! INCLUDE SQLCA END_EXEC. * PROCEDURE DIVISION. * *Initialize the working storage variables *The Embedded SQL statements that retrieve the values. EXEC SQL SELECT EMPNO,NAME,DEPTNO INTO:WS_EMPNO,:WS_NAME,:WS_DEPTID FROM EMPLOYEE WHERE EMPNO=100 END-EXEC
A Free sample background from © 2006 By Default! * COBOL code to display the values retrieved. DISPLAY “Employee NO. :”,WS_EMPNO. DISPLAY “Employee Name:”, WS_NAME. DISPLAY “Department ID.:”WS_DEPT. STOP RUN
A Free sample background from © 2006 By Default! CURSOR IN EMBEDDED SQL Cursor declaration Open cursor statement Fetch statement Close statement
A Free sample background from © 2006 By Default! DYNAMIC SQL Allows programs to construct and submit SQL queries at run time. Example of the use of dynamic SQL from within a C program. char * sqlprog = “update account set balance = balance * 1.05 where acc_no = ?” EXEC SQL prepare dynprog from :sqlprog; char acc [10] = “101”; EXEC SQL execute dynprog using :acc;
A Free sample background from © 2006 By Default! The dynamic SQL program contains acc_no= ?, which is a place holder for a value that is provided when the SQL program is executed.