Docsity

unit 1 notes of rdbms, Lecture notes of Computer science

Toggle sidebar
bg1
UNIT -I
UNIT I
DATABASE CONCEPTS
1. INTRODUCTION - DATABASE
A database is an organized collection of data. The data are typically
organized to model relevant aspects of reality in a way that supports processes
requiring this information. It is an electronic storage of data. It stores
information and relationship among different things.
Basic terms to examine the structure of a Database
A person, place, event or item is called an entity.
Example: Customer, Transaction, Employee, Library, etc.,
The facts describing an entity are known as data.
Example: For a Customer, custno, custname, address…, etc., are
maintained in a register which describes the characteristics of the entity
Customer.
Each entity can be described by its characteristics, which are known as
attributes.
Example: For a Student, name, regno, class, address etc….
All the related entities are collected together to form an entity set. An
entity set is given a singular noun.
Example: The student entity set contains data about student only. All
related entity in the STUDENT entity set is student. Similarly a company
keeps track all of its employees is an entity set called EMPLOYEES. The
EMPLOYEE entity set does not contain about the company’s customers,
because it would not make any sense.
A database is a collection of entity sets.
Example: A college database may include entities such as student,
faculty, course, term, office, building, exam etc., the entities in a
database are likely to interact with other entities.
The interactions between the entity sets are called relationship.
Example: A faculty member teaches in a building, so the relationship
between FACULTY and BUILDING is teaches.
1.1 Types of Relationships
The database design requires creating various entity sets and
establishing the relationship between the entity sets within the database.
The different database management software packages handle the
creation and use of relationship in different manners. Depending upon
the type on interaction, the relationship is classified in to three
categories.
i)One –to-one relationship:
A one-to-one relationship is written as 1:1 in short form.
It exists between two entity sets X and Y. If an entity set X has only one
matching entity in entity set Y, and vice versa.
Example: A College has one Principal. A Department is lead by one Head.
RDBMS – COMPUTER APPLICATIONSPage 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download unit 1 notes of rdbms and more Lecture notes Computer science in PDF only on Docsity!

UNIT I

DATABASE CONCEPTS

1. INTRODUCTION - DATABASE

A database is an organized collection of data. The data are typically organized to model relevant aspects of reality in a way that supports processes requiring this information. It is an electronic storage of data. It stores information and relationship among different things. Basic terms to examine the structure of a Database  A person, place, event or item is called an entity.  Example: Customer, Transaction, Employee, Library, etc.,  The facts describing an entity are known as data.  Example: For a Customer, custno, custname, address…, etc., are maintained in a register which describes the characteristics of the entity Customer.  Each entity can be described by its characteristics, which are known as attributes.  Example: For a Student, name, regno, class, address etc….  All the related entities are collected together to form an entity set. An entity set is given a singular noun.  Example: The student entity set contains data about student only. All related entity in the STUDENT entity set is student. Similarly a company keeps track all of its employees is an entity set called EMPLOYEES. The EMPLOYEE entity set does not contain about the company’s customers, because it would not make any sense.  A database is a collection of entity sets.  Example: A college database may include entities such as student, faculty, course, term, office, building, exam etc., the entities in a database are likely to interact with other entities.  The interactions between the entity sets are called relationship.  Example: A faculty member teaches in a building, so the relationship between FACULTY and BUILDING is teaches. 1.1 Types of Relationships  The database design requires creating various entity sets and establishing the relationship between the entity sets within the database. The different database management software packages handle the creation and use of relationship in different manners. Depending upon the type on interaction, the relationship is classified in to three categories. i)One –to-one relationship:  A one-to-one relationship is written as 1:1 in short form.  It exists between two entity sets X and Y. If an entity set X has only one matching entity in entity set Y, and vice versa.  Example: A College has one Principal. A Department is lead by one Head.

ii)One-to-Many relationship:  A one-to-one relationship is written as 1:M in short form.  It exits between two entity sets X and Y. If an entity in entity set X has many matching entities in entity set Y, but an entity in entity set Y has only one matching entity in entity set X.  Example: A faculty teaches to Many Students of a class room, but the students had a relationship with one faculty member. iii)Many-to-Many relationship:  It also write in M:N (or) M:M  It exists of two entity set X and Y. If an entity in entity set X has many matching entities inentity set Y and an entity in entity set Y has many matching entities in entity set X.  Example: Many Courses availed for Many Students. To determine the type of relationship, consider the following two questions.

  1. Does an entity in entity set X have more than one matching entity in entity set Y?
  2. Does an entity in entity set Y have more than one matching entity in entity set X?  The answer for both the questions is NO, then the relationship lies in 1:  The answer for the first question is Yes and second question is NO, then the relationship lies in 1:M  The answer for both the questions is Yes, then the relationship lies in M:M (or) M:N 1.2 Database Management System  A DBMS is responsible for maintaining the integrity and security of stored data, and for recovering information if the system fails. A "database management system" (DBMS) is a suite of computer software providing the interface between users and a database or databases. The database system consists of various components. The Components are:  A DBMS software package such as Microsoft Access, Microsoft SQL- Server or Oracle.  Computer Hardware like Personal Computers, Mini computers and Mainframe in a network environment.  Software - Operating System and a Network operating system.  User developed and implemented database(s) – Tables, Data Dictionary, Records and other database objects.  Custom Application such as data entry form, reports, queries, blocks and programs.  Personnel – A Database Administrator (DBA), Database Designer, Analyst, Programmer and End User.

 The number of columns in a table is called the degree. Example: A table has four columns then the degree of the table is 4.  The set of all possible values that a column may have is called the domain of that column.  Two domains are same only if they have the same meaning and use. 1.4.1 Terminology Comparison 1.5 KEY  A key is a minimal set of columns used to uniquely define any row in a table.  A uniquekey or primary key is a key that uniquely defines the characteristics of each row. 1.5.1 Primary Key  When a single column is used as a unique identifier, it is known as primary key.

  1. A primary key cannot allow null values.
  2. Each table can have at most one primary key.
  3. On some RDBMS a primary key automatically generates a clustered table index by default.  For Example:- Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id and Employee_Designation so in this table only one field is there which is used to uniquely identify detail of Employee that is Employee_Id. 1.5.2 Unique Key
  4. A unique key can allow null values.
  5. Each table can have multiple unique keys.
  6. On some RDBMS a unique key automatically generates a non-clustered table index by default.  For Example:-Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id , Employee_Designation and Employee_PhoneNo so in this table except Employee_Id we also have an another field named Employee_PhoneNo which is can also be used to uniquely identify the record and can termed as Unique Key. Relational terminology File system terminology Entity set or table or relation Entity or row or tuple Attribute or column File Record Field

1.5.3 Composite Key  When a combination of columns is used as a unique identifier it is known as a compositeprimary key or composite key.  For Example:-Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id , Employee_Designation, Employee_PANNo and Employee_PhoneNo in this table to build a composite key we combine Employee_Id and Employee_PhoneNo to fetch data from table. 1.5.4.Secondary Key  Sometimes, a more human approach is used to identify or retrieve a row from a table becauseit is not possible to remember primary key values such as the employee number, Book number,department number etc., that time name of the employee, author of the book may be noted with theprimary key. This key is known as Secondary Key. 1.5.5 Surrogate Key  Sometimes none of the column is a candidate for the primary key in a table, so the databasedesigners can be add an extra column as a primary key instead of using composite key.  For example the customer purchases different items in different locations,for this situation we have to maintain historical data. By using surrogate key we can introduce the row in the data warehouse to maintain historical data. 1.5.6 Foreign Key  A column in a table that references a column in another table is known as foreign key.  We have two tables: customers and orders. Each customer has zero or more orders and each order belongs to only one customer.  For Example:- We can have a Employee_Id column in the Company table which is pointing to Employee_Id column in a Employee table where it a primary key. So with the help of foreign key we can easily identify the data from tables. 1.6 INTEGRITY RULES  In RDBMS it is very important that the data underlying the tables be consistent.  Ifconsistency is compromised the data are not usable. This need has led to formulate two integrityrules. 1.6.1 Entity Integrity  No column in a primary key may be null. The primary key provides the means of unique identifier of a row or an entity.

1.7.1.1. Union  The union of two tables results in retrieval of all the rows that are in both tables. The duplicaterows are eliminated from the resulting table.  Basic requirement to perform union operations on two tables:

  1. Both tables must have the same degree.
  2. The domains of the corresponding columns in two tables must be same.  Such tables are said to be union compatible.  Consider an example of two tables named WIPRO and TCS which contains the names of thestudents placed in those companies. TABLE_A = WIPRO TCS 1.7.1. 2. Intersection  The insertion of two tables produces a table with rows that are in both tables. The two tables musthave be union compatible to perform an intersection on them. TABLE_B = WIPRO _ TCS 1.7.1.3. Difference  The difference of two tables procedures a table with rows that are present in the first table but notin the second table. The difference can be performed on union-compatible tables only. TABLE_C = WIPRO – TCS

WIPRO

Reg.no Name course 1123 John BCA 1156 Gobi BCA 5556 Mary CS

TCS

Reg.n o Nam e course 1123 John BCA 1186 Kavin CS 1196 Priya BCA TABLE_A Reg.no Name course 1123 John BCA 1156 Gobi BCA 5556 Mary CS 1186 Kavin CS 1196 Priya BCA TABLE_B Reg.no Name course 1123 John BCA

1.7.1.4. Projection  The projection operation allows creating table based on describable columns from all existingcolumns in a table. The projection operation returns the “vertical slices” of a table.  The projection is indicated by including the table name and a list of described columns TABLE_E = WIPRO(Reg.no, Name) 1.7.1.5. Selection  The selection operations select rows from a table based on a condition or conditions.  The selection operations return “horizontal slices” from a table.  The selection (set) operations to the parts table. TABLE_E = sel(WIPRO: COURSE=’CS’) 1.7.1.6. Product  A product of two tables is a combination everything in both tables.  It is known as a Cartesian product.  If the first table has x rows and the second table has y rows the resulting product has x.y rows.  If the first table has m columns and second table has n columns the resulting product has m*ncolumns. DEPARTMENT EMPLOYEE

TABLE_C

Reg.n o Nam e course 1156 Gobi BCA 5556 Mary CS TABLE_E Reg.no Name 1123 John 1156 Gobi 5556 Mary TABLE_E Reg.no Name course 5556 Mary CS

1.7.1.9. Division  The division operations are the most difficult operation to comprehend.  It is not as simple as division in mathematics.  In relational algebra, it identifiers rows in one table that have a certain relationship to all rows in another table. PROJPARTS PROJ TABLE_I = PROJPARTS / PROJ 1.7.1.10 APPLICATIONS OF RELATIONAL ALGEBRA  Relational algebra is a procedural language in the sense that a user is required to use a series of operation to obtain a certain result. RDBMS – COMPUTER APPLICATIONSPage 10

TABLE_H

DNO DNAME ENO ENAME

PRODUCTIO

N

100 CARTER

20 SUPPLIES 200 ALBERT

30 MARKETING 300 BRENS

PNO

PARTN

O

PN

O

PARTNO

EMPLOYEE

EMPNO

ENAME DEPNO

PROJNO

SALARY

101 CARTER 10 1 25000

102 ALBERT 20 3 37000

103 BREES 30 6 50500

104 GOULD 20 5 23700

105 BARKER 10 7 75000

PROJ

PROJN

O

LOC

CUSTOME

R

1 MIAMI STOCKS

2 ORIANDO ALLEN

3 TRENTON SMITH

CHARLOTT

E

JONES

Referring to the above tables, which employee is working on a project in Miami during the year 2003?  Solution: A = join(PROJ,EMPLOYEE:PROJNO=PROJNO) B=Sel(A:LOC=’MIAMI’) C=B(ENAME)  Alternative solution A=Sel(PROJ:LOC=’MIAMI’) B=Join(EMPLOYEE,A:PROJNO=PROJNO) C=B(ENAME)  Table C will have one entry, Carter, an employee who works on project 1 in Miami. 1.7.2 RELATIONAL CALCULUS  Relational calculus is nonprocedural.  The programmer specifies the data requirement, and the system generates the operations needed to produce a table with the required data.  Syntax:Result = (column list) : Expression  Referring to the DEPT and EMPLOYEE table, find employee names and salary for employees who work in production.  Solution: (r.ENAME, r.SALARY): r in EMPLOYEE and s in DEPARTMENT and s in DEPTNAME = ‘PRODUCTION’ and r.DEPTNO = s.DEPTNO 1.8 DATA MODELING

ENAME SALARY

CARTE

R

BARKE

R

supervises (1,1) (1,1) employs (1,N) (1,1) contains (1.N) (1,N) Fig: Cardinality 1.8.4 Business Rules:  Corporation set rules for minimum and maximum values for cardinality.  A corporate maydecide that a department must have minimum of 10 employees and maximum of 25 employees.  Thenthe cardinality is (10,25). An employee can be part of zero or more than one department and an itemmay not be in any invoice. These types of decisions are known as Business rules. 1.8.5 Optional Relationship:  The relationship becomes optional in such a case; a customer can rent video movies fromvideo rental store. Sometimes, the movie offered by the customer is not available in the store and bysuggestion of salesman the customer can rent other movie instead that one as optional.  The optional relationship is can occur in 1:1, 1:M or M:N relationships. Rents (0,N) (0,N) Fig: Optional Relationship 1.8.6 Composite Entity or Associative Entity:  The decomposition from M:N to 1:M involves a third entity known as composite entity or anassociative entity.  The composite entity is created with the primary key from both tables with M:Nrelationships.  In E-R diagram this entity is represented by a diamond within a rectangle. EMPLOYEE DIVISION INVOICE ITEM DEPARTMENT FACULTY CUSTOMER VIDEO

 Thecomposite entity has a composite primary key with two columns; they are foreign keys referencingother tables.  Here the composite entity INVITEM is created from the tables INVOICE and ITEM.  Theforeign keys are INVNO and ITEMNO together formed as a composite primary for the tableINVITEM. 1.8.6 Weak Entity:  There are entities that cannot exist by themselves. Such entities are known as weak entities,they are represented by double lined rectangles. 1.9 Types of Attributes:

  1. Simple Attribute Attributes that cannot be subdivided.Example: Last name, city
  2. Composite attribute Attribute can be subdivided into atomic form Example: Full name can be subdivided into last name, first name and middle name.
  3. Single Attribute Attribute with a single value (unique value).Example: Employee ID, DOB.
  4. Multivalued Attributes Attribute can be multi-value.Example: Course, Department. 1.10 Dependency  Oracle allows creating a table without a primary key.  It allows defining a primary key for thattable for integrity of data.  Each table has other than primary key columns, they are known as non- keycolumns.

 A dependency diagram in used the show total, partial and transitive dependencies in table.The primary key components are highlighted .they are in bold letters and in boxes with a darker border.  The total and functional dependencies are shown with arrows drawn above the boxes.  The partial andtransitive are shown with arrows at the bottom of the diagram. 1.11 Normalization  In Relational database two phases are used.  Synthesize phase is used to design the databasestructure.  Once entities, columns, domains and keys are defined then each entity is synthesized by creating a table for it.  Analysis phase is used to improve the structure created in synthesis phase.  Aprocess called normalization analyzes the tables created by the synthesis process.  Normalization is a technique to reduce redundancy by decomposition process into split tables.  The splitting is performed carefully, so no information is lost. Unwanted and unnecessary data intables are in lower normal form.  The higher the normal form is lower the redundancy. 1.11.1 Normal Forms  Redundant data can pose to huge problem in database.  First of all someone has to enter thesame data repeatedly.  Second if a change in made in one piece of the data the change has to be madein many places. Example: If the customer ANBU changes the name to BABU in INVOICE tablemake changes in two places.  This redundancy may also lead to anomalies.  A deletion anomaly results when the deletion of information about one entity leads to deletion of information about another entity.

 Eg: If an invoice for customer Gracia is removed, information about item number 4 is also deleted.  An insertion anomaly occurs when the information about an entity cannot be inserted unless the information about another entity is known.  Eg: if the company buys a new item, this information cannot be entered unless an invoice is created for a customer with that new item.  An update anomaly can occur if the item price changes to a new price. The price changes is valid after the change date, but not before the change date. 1.11.1.1 First Normal Form (1NF)  A table is said to be in 1NF, if the following conditions exists:

  1. The primary key is defined. This includes a composite key if a single column cannot be used as a primary key. InvNo and ItemId are defined as the composite primary key.
  2. All non-key columns show functional dependency on the primary key columns. If you know the invoice number and the item number, you can find out all the columns.
  3. The table contains no multivalued columns. In a single-valued column, the intersection of a row and a column returns only one value. In a normalized table, the intersection of a row and a column is a single value.  In 1NF may have redundant data.  It does not show data consistency and integrity. 1.11.1.2 Second Normal Form (2NF)  A table is said to be in 2NF, if the following conditions exists:
  4. All 1NF requirements are fulfilled.
  5. There is no partial dependency.  Partial dependency exists in a table in which non-key columns are partially dependent on part of a composite key.  Suppose a table is in 1NF and does not have a composite key, it is in 2NF, because there is no partial dependency. 1.11.1.3 Third Normal Form (3NF)  A table is said to be in 3NF, if the following conditions exists:
  6. All 2NF requirements are fulfilled.
  7. There is no transitive dependency. 1.11.1.4 Boyce-Codd normal form (BCNF)  A relation is in BCNF if and only if every determinant is a candidate key.  A determinant is an attribute or group of attribute on which some other attribute is fully functionally dependent.  It is based on functional dependencies that take into account all candidate keys in a relation.

1.13 De-normalization  The normalization process split tables in the smaller tables.  These tables are joined through common columns to retrieve information from different tables.  The more tables needs more joins to get the desired information.  In a multi user environment, it is a costly overhead and system performance is affected.  De-normalization is the reverse process.  It reduces the normal form and it increase data redundancy.  In de-normalization the information stored with duplicate data, more storage is required and anomalies and inconsistent data exist.