SlideShare a Scribd company logo
UNIT-I
Chapter-I : DATABASE SYSTEMS
Data: Data consists of raw facts, which the computer stores and reads. Data can consist of
letters, numbers, sounds or images etc. that have some meaning in the user environment. Data
are the raw material from which information is generated.
Information: When data has been processed to give it more meaning, it is called as
information.
Database: An organized collection of logically related data usually designed to meet the
information needs of multiple users in an organization.
Database Management System: (DBMS) is a software tool used to define, create, maintain
and provide controlled access to the database.
DBMS software stores data structures, relationship between those structures and the access
paths to those structures in a central location.
Q) How the data is organized within a database?
Ans: To help you visualize how a database stores data, think about a typical address book.
Fields: Each field contains a specific type of information such as first name, last name, phone
number, email etc…
Records: Record is a collection of related fields.
Ex: All information about one person in an address book.
Tables: A complete collection of records makes a table
Ex: Contacts table
FirstName LastName Company Address City State Pincode
Record1
Record2
Q) Why the database is important?
Ans: If you keep list of all your business customers in a database, you can
• You can sort the customers by pincode.
• Create a simple onscreen entry form that even your technically unskilled employee
can use successfully
You can manipulate data in almost anyway you want.
Files, File Systems & Problems With File System Data Management.
Manual filing system works well when the number of items stored is quite small and they are
only needed to be stored and retrieved.
A manual filing system crashes when cross referencing and processing of information in the
files is carried out.
Limitations or disadvantages of File Processing Systems.
Program data dependence: File descriptions are stored within each program that access a
given file.
In the invoicing system program access both the inventory pricing file and the customer
master file.
Page 1
Therefore this program contains a detailed description for both these files.In the below figure
both the customer master file is contained in both the order filing system and invoicing
system. Suppose it is decided to change the customer address field length in the records in
customer master file from 20 to 30 characters. For this, each related program have to be
modified.
Duplication of data or Redundancy of data: in the below figure, order filing system
contains the inventory master file, the invoicing system contains inventory pricing file.
Inventory master, inventory pricing file contains product descriptions and quantity. There is
duplication of data which requires additional storage space.
Orders Department Accounting Department
Inconsistent data: The redundancy in storing the same data multiple times leads to data
inconsistency when an update is applied to some of files but not to other.
Limited data sharing: In the file processing system, users have little opportunity to shared
data outside their applications.
Lengthy development times: Developing an application by using the file systems is very
skilled activity. The programmers has to write many programs for supporting file opening,
file closing and iterative logic for representing operations, this is very lengthy process.
Incompatible file formats: Since the structure of files is embedded in application, the
structure is dependent on application programming languages.
Ex: structure of file generated by COBOL is different from ‘C’ programming language.
The application programmer has to develop software to convert the files to some common
format for processing. This may be time consuming and expensive.
Fixed Queries: Any query or report needed by organization has to be developed by the
application programmer.
Lack of security: All users could see all data and no security and authorization subsystem.
No recovery and back up system:Data could be lost in case of hardware or software failure.
All the data is stored in disk files and accessed according to access methods (sequential,
direct etc..) provided by file system and chosen by application programmer.
Page 2
Progra
m A
Progra
m
B
Progra
m
C
Order File System
Customer
Master File
Inventory
Master File
Back Order
File
Progra
m A
Progra
m
B
Invoicing System
Inventory
Pricing File
Customer
Master File
Order
Programs
Accounting
Programs
Payroll
Programs
DBMS
Database
Customer master data
Inventory master data
Employee master data
Back order data
Q: What is a database system and What are the advantages of database systems?
Database System: Database and DBMS software together is called a database system.
Program data independence: DBMS allows certain types of changes to the structure of the
database without affecting the stored data and the existing application.
Improved data sharing: The DBMS helps create an environment in which end users have
better access to more data and better managed data.
Improved data security: The DBA uses security and authorization subsystem provided by
DBMS to create accounts and to specify account restrictions. The DBMS will enforce these
restrictions automatically.
Better Data Integration: DBMS promotes and enforces integrity rules, thus minimizing
data redundancy and maximizing data consistency.
Minimized data inconsistency: Data inconsistency is also reduced in a properly designed
database as such a database doesn’t allow different versions of same data in different places.
Ex: company’s sales department stores salesman name as ‘Bill Brown’ and the same person
name is stored as ‘William G Brown’ in company’s HR department.
Improved Data access: A query is a question or specified request issued to DBMS for data
manipulation. Example of query language is SQL
An Adhoc query is a spur of the moment question. The DBMS sends back an answer ( called
the query result set) to the application
For Ex: How many of our cutomer have balances of Rs. 3000 or more?
The DBMS gives quick answers to adhoc queries.
Improved Decision Making: Better managed data and improved data access makes it
possible to generate better quality information on which better decisions are based.
Increased end- user productivity: The availability of data and tools that transform data into
information allows end user to make quick decisions that can make the difference between
success and failure in global economy.
Page 3
Database system environment:
Database system environment is made up of five major parts. They are hardware, software,
people, procedures and data.
Hardware: Hardware refers to all of the system’s physical devices.
For ex: computers (micro computers, servers etc..), storage devices, printers, networking
devices (hubs, switches etc…) and other devices ( ATMS, ID readers etc..)
Software: Three types softwares are needed to make the database system function fully.
1. Operating system software: manages all hardware components and makes it
possible for all other software to run on the computers. Ex: UNIX, Microsoft
windows.
2. DBMS Software: manages the data within the database system.
Ex: SQL server, Oracle, DB2, My SQL
3. Application programs and utility software: are most commonly used to access data
found within the database to generate reports, tabulations and other information for
decision making. For Ex: All DBMS vendors provide GUI’s to create database
structures, control database access and monitor database operations.
4. People includes all users of database system. On the basis of their job functions, five
types of users can be identified.
1. System administrators: looks after database system general operations.
2. Database administrators (DBA) manages the DBMS and ensures that the
database is functioning properly.
3. Database Designers or Database architects design database structure. The
determination of what data are to be entered into the database and how the data are to
be organized is an important part of database designer’s job.
4. System analysts and programmers design and implement the application
programs. They design and create the data entry screens, reports and procedures
through which end users access and manipulate the database’s data.
5. End users are the people who use the application programs to run the
organisation’s daily operations. For Ex: clerks, managers, supervisors and directors.
High level end users uses the information obtained from the database to make
decisions.
5. Procedures: Procedures play an important role in a company. They enforce the
standards by which the business is conducted within the organization and with
customers. Procedures are also used to ensure that there is an organized way to
monitor and audit both the data that enter the database and the information generated
through the use of that data.
6. Data: are the raw materials from which information is generated. Data covers the
collection of facts stored in the database.
DBMS Functions:
DBMS performs several functions that guarantee the integrity and consistency of the data in
database. They are
1. Data dictionary management: The DBMS stores definitions of the data elements
and their relationships in a data dictionary. The DBMS provide data abstractions and
it removes structural and data dependency from the system.
2. Data Storage management: The DBMS creates and manages the complex structures
required for data storage, thus you need not define and program the physical data
characteristics. It also provide storage for on-screen definitions, report definitions,
data validation etc..
3. Data transformation and presentation: The DBMS must manage the data in proper
format for each country while entering dates, names etc... must not allow different
Page 4
versions of same data in different places. Ex: company’s sales department stores
salesman name as ‘Bill Brown’ and the same person name is stored as ‘William G
Brown’ in company’s HR department
4. Security Management: DBMS creates a security system that enforces user security
and data privacy. Security rules determine which users can access the database, which
data items each user can access? , which data operations (read, add, delete or modify)
the user can perform. This is important in multi user database system.
5. Multi user access control: The DBMS uses sophisticated algorithms to ensure that
multiple users can access the database at the same time.
6. Back up and recovery management: DBMS provide special utilities that allow
DBA to perform back up and restore procedures. Recovery management deals with
the recovery of database after a failure, such as bad sector in disk or power failure.
7. Data integrity management: DBMS promotes and enforces integrity rules, thus
minimizing data redundancy and maximizing data consistency.
8. Data access languages and application programming interfaces: The DBMS
provide access through a query language. A query language is a non procedural
language that lets the user specify what must be done without having to specify how it
is to be done. Example of query language is SQL
9. Database communication interface: DBMS accepts end user requests from multiple,
different network environments.
Disadvantages of DBMS
1. Increased Costs: Database system requires hardware, software and highly skilled
people. The cost of maintaining these.
2. Management Complexities: The database system hold important data that are
accessed from multiple sources, security issues may occur.
3. Frequent Updates: must perform frequent updates an d apply latest patches and
security measures to all components. These increases personnel training costs.
4. Vendor Dependence: due to heavy investment in technology and personnel training,
companies do not change database vendors. As a result, vendors donot offer pricing
point advantages to existing customers.
5. Frequent Updates / Replacement Cycles: DBMS vendors frequently upgrade their
products by adding new functionality i.e, upgrade versions of software. Some of these
versions require hardware upgrades and training to users costs money.
Page 5
UNIT-I Chapter-II
Data modeling and data models
Q: What is a data model?
Ans: Data model is blue print containing all the instructions to build a database that will meet
all the end –user requirements.
This blue print contains both text descriptions in plain, unambiguous language and clear
useful diagrams depicting the main data elements.
Q: Explain The importance of data models?
Ans:Data models are communication tool that enables interaction among the designer, the
application programmer and end user.
Data models are used to represent real world data and how the different degrees of data
abstraction enables data modeling.
Ex: a house blue print is an abstraction; you cannot live in a blue print, Similarly the data
model is an abstraction, you cannot draw the required data out of the data model. As you
cannot build a perfect house without blue print, you cannot create a good database without
creating an appropriate data model.
Q:What are Business Rules?
Ans: Business rule is a description of policy, procedure within a specific organization.
Properly written business rules are used to define entities, attributes, relationships and
constraints.
Example1: Consider 2 business rules
• A customer may generate many invoices.
• An invoice is generated by only one customer.
These business rules establish 2 entities (CUSTOMER and INVOICE) and a 1:M
relationship.
Example 2: A business rule is as follows
• A training session cannot be scheduled for <10 employees or for >30 employees
This rule establishes a constraint (not <10 employees or for >30 employees) , two entities
(EMPLOYEE and TRAINING) and a relationship between these entities.
Q: How to Discover Business Rules
Ans: The main sources of business rules are
• company managers,
• policy makers,
• department managers and
• written documentation such as a company’s procedures, standards or operation
manuals,
• direct interviews with end users.
The process of identifying and documenting business rules is essential to database design for
several reasons.
• They help standardize the company’s view of data
• They allow designer to develop relationship participation rules and constraints and to
create an accurate data model.
Q: Why not all business rules can be modeled?
Ans: For ex: No pilot can fly more than 10 hours within 24- hour period .
Such a business rule can be enforced by application software and not by database design.
Page 6
Q: Explain about hierarchical model?
Ans: Its structure is represented by an upside – down tree.
The hierarchical structure contains levels or segments.
Within the hierarchy, the top layer (also called root) is the parent of the segment directly
beneath it.
Advantages:
1. It promotes data sharing.
2. Parent child relationship promotes conceptual simplicity and data integrity.
3. Database security is provided and enforced by DBMS.
4. It is efficient with 1:M relationships.
Disadvantages:
• Complex to implement and difficult to manage as it requires knowledge of physical
data storage characteristics.
• Can implement only 1:M relationships. So it has implementation limitations.
• No standards.
• No DDL and DML language in the DBMS.
• Lacks structural independence. Changes in structure require changes in all
application programs.
• No adhoc queries
• Access paths predefined
This technology is best applied when conceptual model also resembles a tree and most data
accesses begin with the same root file.
Q: Explain about network model?
Ans: Network model allows a record to have more than one parent.
Advantages:
• It can handle M:N and multi parent relationship types.
• Data access is more flexible
• There are standards defined to implement this model.
• It includes DDL and DML commands in DBMS
Disadvantages:
• Little data independence.
• Lacks structural independence. Changes in structure require changes in all
application programs.
• No adhoc queries
• Access paths predefined
Q: What is CODASYL and DBTG?
Ans: To help establish database standards, the conference on data systems languages
(CODASYL) created Database Task Group (DBTG) in late 1960s.
The final DBTG report contained specifications for 3 crucial database components.
The schema is the conceptual organization of the entire database as viewed by DBA
The subschema defines the portion of the database as seen by the application programs.
The application programs invoke the subschema required to access the appropriate database
file.
A data management language that defines the environment in which data can be managed.
Page 7
Q: Explain about The Relational Model ?
Ans: Here tables are called as “Relations”
Rows are called “Tuples” and column names as “attributes”.
Every attribute has a domain. A domain is set of permissible values that can be given to an
attribute.
A common attribute existing in any two tables creates a relationship between the tables.
It supports relationship types (1:1, 1: M or M: N)
The RDBMS manages all the physical details, while the user sees the relational database as
collection of tables. (it enables you to view data logically rather than physically.)
The RDBMS uses SQL to translate user queries into instructions for retrieving the required
data. The SQL engine executes all queries.
Advantages
• Promotes data and structural independence.
• Tabular view improves conceptual simplicity.
• Adhoc query capability is based on SQL
• RDBMS isolates end user from physical level details.
Disadvantages:
• RDBMS requires substantial hardware and software overhead.
• Conceptual simplicity gives untrained people the tools to use good system poorly.
• It may produce islands of information problems as individuals and departments can
easily develop their own applications.
Q: Explain about The Entity Relationship Model?
Ans: ER models are normally represented in an entity relationship diagram (ERD)
The ER model is based on the following components:
Entity: Entity is anything about which data are to be collected and stored
Attribute: Attributes are characteristics of entities.
Relationship:A relationship is an association between entities.
Advantages:
• Visual modeling yields conceptual simplicity.
• Visual representation makes it an effective communication tool.
• It can be integrated with dominant relational model.
Disadvantages
• There is limited constraint representation.
• There is limited relationship representation.
• There is no data manipulation language.
Q: Explain the various notations used with ERDs ?
Ans: The various notations used with ERDs are
• The chen notation favors conceptual simplicity.
• The crow’s foot notation favors implementation – oriented approach.
• The UML notation can be used for both conceptual and implementation modeling.
Q: Explain about Object Oriented model?
Ans: In this model both the data and their relationships are contained in a single structure
known as an Object.
Page 8
Object includes information about relationships between facts within the object and
relationships with other objects.
The OODM is the basis of OODBMS
The OODM is said to be semantic data model because semantic indicated meaning.
The object oriented data model is based on the following components
• An object is an abstraction of a real-world entity.
• Attributes describe the properties of an object.
• Objects that share similar characteristics are grouped in classes.
• A class is a collection of similar objects with shared structure (attributes) and
behaviour (methods) (where as entities do not have methods)
• Classes are organized in class hierarchy (which represents an upside – down tree in
which each class has only one parent)
• Inheritance is the ability of an object within class hierarchy to inherit the attributes
and methods of the classes above it.
Object oriented data models are depicted using UML diagrams.
Advantages:
• Semantic content is added
• Visual representation includes semantic content.
• Inheritance promotes data integrity.
Disadvantages:
• No widely accepted standard.
• It is a complex navigational system.
• There is a steep learning curve.
• High system overhead slows transaction.
Q) Distinguish between Logical and Physical data independence.
Logical Data Independence:
Logical data independence is the ability to modify the conceptual schema without having
alteration in external schemas or application programs. Alterations in the conceptual schema
may include addition or deletion of fresh entities, attributes or relationships and should be
possible without having alteration to existing external schemas or having to rewrite
application programs.
Physical Data Independence:
Physical data independence is the ability to modify the internal schema without having
alteration to the conceptual schemas or application programs. Alteration in the internal
schema might include.
* Using new storage devices.
* Using different data structures.
* Switching from one access method to another.
* Using different file organizations or storage structures.
* Modifying indexes.
Page 9
Explain about the Conceptual, Internal and external and Physical Model
(Or)
Explain about different levels of data abstraction
(Or)
Explain about three schema architecture.
Ans:
The Conceptual Model
1. The conceptual model represents a global view of the organization’s data as viewed by all
end-users.
2. It describes all entities and their attributes, the relationships among these entities and the
constraints on these relationships.
3. The conceptual model forms the basis for the conceptual schema - a description of the
database structure.
4. The conceptual model is independent of both software (DBMS and OS) and hardware.
5. The E-R model is the most widely used to represent conceptual model
The Internal Model
Page 10
1. The internal model adapts the conceptual model to a specific DBMS (e.g., hierarchical,
network, and relational).
2. The internal model is software-dependent but hardware-independent.
3. Development of the internal model is especially important to hierarchical and network
database models.
The External Model
1. The external model is the end user’s/ applications programmer’s view (local view) of the
database .
2. It is concerned about a specific business operation.
3. It is implemented through the CREATE VIEW command in SQL.
Benefits of the external model
• Application program development is simplified because the programmer does not have to
be concerned about data not relevant to his/her application.
• Communication with the end-user is simplified.
• Identification of data required to support each business operation is simplified.
• Access control and security can be easily implemented.
Page 11
The Physical Model
• The physical model operates at the lowest level of abstraction, describing the way data
is stored on storage media such as disks or tapes.
• It requires the definition of
physical storage devices and
the access methods required to reach the data.
• The physical model is both software and hardware-dependent.
Page 12
UNIT-I
Chapter-III The Relational Database model
Explain characteristics of relational table?
1. A table is perceived as a two-dimensional structure composed of rows and columns.
2. Each table row (tuple) represents a single entity occurrence within the entity set.
3. Each table column represents an attribute, and each column has a distinct name.
4. Each row/column intersection represent a single data value.
5. All values in a column must conform to the same data format.
6. Each column has a specific range of values known as the domain of that attribute.
Example: The domain for the gender attribute consists of only two possibilities: M or F.
The domain for a company’s date of hire attribute consists of all dates (from start up date to
current date)
Attribute may share a domain.
For ex: a student address and a professor address share the same domain of all possible
addresses.
7. The order of rows and columns is immaterial to the DBMS
8. Each table must have an attribute or a combination of attributes that uniquely identifies
each row. Ex: Roll_No in the STUDENT table
What are data types support by most DBMS?
Ans: The different data types are
1. Numeric: Numeric data are data on which you can perform arithmetic operations.
2. Character: Character data or text data or string data can contain any character, symbol or
digit not intended for mathematical manipulations.
3. Date: Date attributes contain calendar dates stored in special format known as the julian
date format.
Logical: Logical data can have only a true or false (yes or no) condition.
What is data dictionary?
Ans: The data dictionary provides detailed descriptions of all tables and so contains all of
attributes names,characteristics and structure of each table in the system.
What is system catalog?
Ans: it is a detailed system data dictionary that describes all objects within the database,
including data about table names, table’s creator etc..
The system catalog is a system – created database whose tables store the user created
database characteristics and content. These tables can be queried just like user-defined table.
Explain about indexes in relational database?
Ans: An index is composed of an index key and a set of pointers. An index can be used to
retrieve data more efficiently. When you define a table’s primary key, the DBMS
automatically creates a unique index on the primary key columns.
Page 13
What is meant by functional dependence?
Ans: The attribute B is functional dependent on A
if each value in column A determines one and only one value in column B.
Ex:
What is composite key?
Ans: A key may be composed of more than one attribute. Such a multi- attribute key is known
as a composite key.
What is meant by fully functional dependency?
Ans: If attribute B is functionally dependent on a composite key A but not on any subset of
that composite key, the attribute B is fully functionally dependent on A.
Explain about various keys used in relational database model?
Key Type Definition Example
Super key An attribute (or combination of attributes)
that uniquely identifies each row in a
table.
In STUDENT table, the super key could
be any of the following:
STU_NUM
STU_NUM, STU_LNAME
Candidate
key
A minimal (irreducible) super key is a
candidate key.
A super key that does not contain a subset
of attributes that is itself a super key.
STU_NUM,STU_LNAME is a super
key, but it is not a candidate key
because STU_NUM by itself can
uniquely identifies each row in the
STUDENT table.
Primary
key
A candidate key is selected as a primary
key. It cannot contain NULL values
If employee’s PAN number has been
included as one of the attribute in the
EMPLOYEE table. EMP_NUM and
EMP_PAN are both candidate keys
because both uniquely identifies each
employee. Selection of EMP_NUM as
primary key would be designer’s
choice.
Secondary
key
An attribute or combination of attributes
used strictly for data retrieval purposes
Most of the time if I need city wise
customers list from CUSTOMER table,
I can place a secondary key on
CUS_CITY column to get a speed reply.
Foreign
key
An attribute in one table whose values must either match the primary key in another
table or be null.
Q: What is a constraint? Write short notes on integrity constraints/ rules with example?
Ans: A constraint is a restriction placed upon the data values that can be stored in a column
or columns of a table.
Integrity Constraint are of 2 types
1. Entity integrity constraint
2. Referential integrity constraint
Entity integrity : All primary key entries are unique and no part of a primary key may be null.
Referential integrity: A foreign key may have either a null entry, as long as it is not part of its
tables primary key or an entry that matches the primary key value in a table to which it is
related. (Every non- null foreign key value must reference an existing primary key value.)
Page 14
Example: Table name: AGENT
Primary key: AGENT_CODE Foreign Key: none
AGENT_CODE AGENT_FNAME AGENT_PHONE
A01 ANU 2475258
A02 RAM 2465258
Table Name: CUSTOMER
Primary Key: CUS_CODE and Foreign Key: AGENT_CODE
CUS_CODE CUS_FNAME AGENT_CODE
C01 SWATHI NULL
C02 DOLLY A01
C03 RAMA A01
Here the customer swathi is not assigned a agent yet, hence the agent code is NULL.
No entry in agent code column in customer table has invalid entry as they reference a valid
entry A01 which is anu’s agent code.
Also primary keys of both tables contain null values and has unique values.
Relational set operators or relational algebra
Relational algebra is set of basic operations used to manipulate the data in relational model.
These operations can be classified into two categories:
1. Basic set operations: These are
When two or more tables share
• the same number of columns and
• the columns have the same names and
• the columns share the same (or compatible) domains
the Two tables are said to be union-compatible.
UNION: union combines all rows from two tables, excluding duplicate rows. The two tables
must be union- compatible.
Example: R3=R1U R2
R1 R2
Page 15
Binary operations
UNION
INTERSECTION
SET DIFFERENCE
CARTESIAN PRODUCT
Relational operations
SELECT
PROJECT
JOIN
DIVISION
Fname
A1
A2
A3
A4
A7
yields
UNION
Intersect: Intersect yields only the rows that appear in both tables.
The tables must be union –compatible to yield valid results.
Page 16
Fname
A1
A2
A3
A4
Fname
A1
A7
A2
A4
Example:
R1 R2 R3=R1 n R2
yields
INTERSECT
Difference: Difference yields all rows in one table that are not found in the other table. The
tables must be union-compatible. Example:
R1 R2 R1-R2 =R3
yields
DIFFERENCE
Cartesian Product: yields all possible pairs of rows from two tables.
R3=R1X R2
R1 R2
yields
Page 17
Fname
A1
A2
A4
Fname
A1
A2
A3
A4
Fname
A1
A7
A2
A4
Fname
A3
Fname
A1
A2
A3
A4
Fname
A1
A7
A2
A4
Course Fname
C1 A1
C1 A2
C1 A3
C2 A1
C2 A2
C2 A3
Course
C1
C2
Fname
A1
A2
A3
Select: Also known as RESTRICT
Yields values for all rows found in a table that satisfy a given condition.
PRODUCT
SELECT only price < $10 yields
Project: yields all values for selected attributes. Project yield a vertical subset of a table.
PRODUCT
PROJECT Price yields
Join: A join is used to combine rows from multiple tables.
Natural Join links tables by selecting only the rows with common values in common
columns. A natural join is a result of a three-stage process.
1.a PRODUCT of the tables is created.
2. a SELECT is performed on the output to yield only the rows for which Acode =
Agent_code
and these common columns Acode, Cus_code are called as join columns.
3. a PROJECT is performed on the result to include only one join column.
Table name: CUSTOMER Table name: AGENT
STEP1: product of the above 2 tables yields
Cus_code Name Agent_code Acode Name
C01 ANU A01 A01 RAJ
C01 ANU A01 A02 TAJ
Page 18
Pcode Pdesc Price
1 Flash Light 5
2 Lamp 25
3 Battery 7
4 100W Bulb 15
Pcode Pdesc Price
1 Flash Light 5
3 Battery 7
Price
5
25
7
15
Pcode Pdesc Price
1 Flash Light 5
2 Lamp 25
3 Battery 7
4 100W Bulb 15
Cus_code Name Agent_code
C01 ANU A01
C02 RANI A02
Acode Name
A01 RAJ
A02 TAJ
C02 RANI A02 A01 RAJ
C02 RANI A02 A02 TAJ
STEP 2: SELECT rows for which Acode = Agent_code
Cus_code Name Agent_code Acode Name
C01 ANU A01 A01 RAJ
C02 RANI A02 A02 TAJ
STEP 3: PROJECT to remove Acode field from the result.
Cus_code Name Agent_code Name
C01 ANU A01 RAJ
C02 RANI A02 TAJ
The column on which the join occurs only once in new table.
Equi Join:
1. Links tables on the basis of equality condition.
2. Does not eliminate duplicate columns
Theta join : if any other comparison operator other than equality is used, the join is called
theta join.
Left outer join: yields all of the rows in CUSTOMER table, including those that do not have
a matching value in AGENT table.
Right outer join: yields all of the rows in AGENT table, including those that do not have a
matching value in CUSTOMER table.
DIVIDE: This operation uses single column table as the divisor and 2-column table as the
dividend. The tables must have a common column.
DIVIDE YIELDS
Page 19
CODE LOC
A 5
A 9
B 5
B 3
C 6
CODE
A
B
LOC
5
STUDENT
STUDENT
RollNo
STU_LNAME STU_FNAME
STU_PHONE
UNIT-II Chapter –I Entity Relationship modeling
Q: What are E-R Model Components or modules?
Ans:Three components: Entities, Attributes, and Relationships.
Entity: Entity is anything about which data are to be collected and
stored.
An entity may be concrete (a person or a book, for example) or abstract (like a holiday or a
concept).
An entity is represented by a rectangle containing entity’s name.
The entity name , a noun, is usually written in all capital letters.
Attribute:
Attributes are characteristics of entities.
For ex: STUDENT entity has the attributes
STU_FNAME,STU_PHONE etc.
Attributes are represented by ovals and are connected to the
entity rectangle with a line.
Each oval contains the name of the attribute it represents.
Attributes may share a domain.
Primary keys are underlined. (here RollNo is the primary key.)
Relationship
A relationship is an association between entities.
Relationships are described as verbs.
Relationships are represented by diamond-shaped symbols
Q: What are the different Types Of Attributes:
Ans:
1. Required and Optional Attributes:
Required attribute is an attribute that must have a value, it cannot be left empty.
Ex: STU_FNAME, STU_FNAME
Optional attribute is an attribute that does not require a value, it can be left empty.
Ex: STU_PHONE…all students may or may not have a phone at home.
2.Composite and Simple attributes:
A simple attribute cannot be subdivided.
Examples: Age, Sex, and Marital status
A composite attribute can be further subdivided to yield additional attributes.
Examples:ADDRESS into Street, City, State, Zip
PHONE NUMBER into Area code, Exchange number
3. Single-Valued and Multivalued Attributes:
A single-valued attribute can have only a single value.
Examples: A manufactured part can have only one serial number.
A multivalued attribute can have many values.
Multivalued attributes are shown by a double line connecting to the entity
Examples: i) A person may have several college degrees.
ii)A household may have several phones with different numbers
4. Derived Attribute and Stored Attribute
A derived attribute is not physically stored within the database; its value is computed from
other attributes.
It is indicated using a dotted line connecting the attribute with the entity.
Example: AGE can be derived from DOB and current date.
Page 20
What is Cardinality ?
Ans: Cardinality expresses the minimum and maximum number of entity occurrences
associated with one occurrence of the related entity.
In the ERD, cardinality is indicated by placing appropriate numbers beside the entities, using
the format (x,y).
The 1st
value represents the minimum number of associated entities,
while the 2nd
value represents the maximum number of associated entities.
These implemented by the application software or by triggers.
Q:When can you say an entity is Existence dependent/ independent?
Ans: An entity is said to be existence dependent if it can exist in the database only when it is
associated with another related entity occurrence.
Existence independence: if an entity can exist independently, then it is said to be existence
dependent.
Q:What is relationship strength? Explain about strong and weak relationships.
Ans: Relationship Strength is based on how the primary key of a related entity is defined.
They are of 2 types.
Weak (Non-identifying) relationship: a weak relationship also known as non-identifying
relationship, exists if the entity has a primary key that is not partially or totally derived from
the parent entity in the relationship
Strong relationship also known as identifying relationship, exists if the entity has a primary
key that is partially or totally derived from the parent entity in the relationship
What is Weak entity?
Ans: A weak entity is one that meets two conditions
1. The entity is existence- dependent.
2. The entity has a primary key that is partially or totally derived from the parent entity
in the relationship i.e, Strong relationship.
A weak entity id identified by using a double-walled entity rectangle.
Ex: DEPENDENT is the weak entity in the relationship EMPLOYEE has DEPENDENT.
What is meant by relationship participation?
Ans: Participation in an entity relationship is either optional or mandatory.
Optional participation means that one entity occurrence does not require a corresponding
entity occurrence in a particular relationship.
For Ex: in the “COURSE generates CLASS” relationship, there are some courses that do not
generate a class. Therefore, the CLASS entity is considered to be optional to the COURSE
entity.
Mandatory participation means that one entity occurrence require a corresponding entity
occurrence in a particular relationship
If every COURSE must generate a CLASS then the CLASS entity is considered to be
mandatory to the COURSE entity.
Types of Relationships
A relationship’s degree indicates the number of entities that participate in the relationship.
Different types of relationship degrees are :
1. Unary relationship : If an relationship is maintained within a single entity then such
relationship is called unary relationship.
Example: an employee within the EMPLOYEE entity is the manager for one or more
employees within that entity.
Page 21
when an entity has a relationship with itself then such relationship is called as recursive
relationship.
2. Binary Relationship: Binary Relationship exists when two entities are associated in a
relationship. Ex: the relationship “a PROFESSOR teaches one or more CLASSes”
What is a recursive relationship?
Ans: when an entity has a relationship with itself then such relationship is called as recursive
relationship.
What is an associative or composite or bridge entity?
Ans: When there is M:N relationship between two entities then we create a new entity called
bridge/composite entity that contains the primary keys of both the entities participating in
M:N relationship
Ex:
Explain database design challenges?
Ans:
1. Design Standards: Standards guide one in developing logical structures that reduce
data redundancies. Without design standards, it is not possible to design a proper
design or evaluate an existing design.
2. Processing Speed: high processing speed are top priority in database design as high
processing speed are necessary for many organizations
for example: a perfect design must use a 1:1 relationship to avoid nulls, while a higher
transaction –speed design might combine the two tables to avoid the use of an
additional relationship, using dummy entries to avoid nulls.
If the focus is on data-retrieval speed, one must include derived attributes in design.
3. Information requirements: a design that meets all logical requirements is an
important goal. The designer should consider end-user requirements such as
performance, security, shared access. He must also verify that all update, retrieval and
deletion options are available and also all query and reporting requirements.
Page 22
UNIT-III
Chapter-I Introduction to SQL
Q:What is SQL and What does SQL do?
SQL stands for structured query language.
SQL is non-procedural language, therefore you specify what is to be done rather than how is
it done.
American National Standards Institute (ANSI) prescribed a standard SQL.
SQL functions fits into two broad categories:
• It is a data definition language:(DDL):-SQL can create databse objects such as
tables,indexes and views.SQL can also define access rights to these database objects.
• It is a data manipulation language(DML):-SQL can be used to insert,update,delete
and retrieve data from the database
SQL is easy to learn
SQL can retrieve data from database
SQL can execute queries
SQL queries are used to answer question and also to perform actions such as adding,deleting
table rows.
Q:Explain various datatypes available in SQL?
AnsThe following table shows some common SQL datatypes
Datatype Format Comments
Numeric NUMBER(L,D) Ex: NUMBER(7,2) indicates number will be stored with two
decimal places and may be upto 7 digits long,including the
sign and decimal places.
INTEGER (OR)
INT Cannot be used if you want to store numbers that require
decimal places.
SMALLINT Limited to integer values upto six digits
DECIMAL(L,D) Greater lengths are acceptable, but smaller ones are not.
Character CHAR(L) Fixed length character data for upto 255 characters. If you
store strings that are not as long as the CHAR parameter
value,the remaining spaces are left unused
VARCHAR(L)
OR
VARCHAR2(L) Variable length character data will not leave unused spaces.
Date DATE
Stores dates in the julian date format.
Q: Explain how to create table using SQL?
Ans: The CREATE table is used to create a new table in the user database schema.
Syntax:
CREATE TABLE tablename (
Column1 datatype(column width) [constraints],
Column2 datatype(column width) [constraints],
……………
);
Page 23
Example:
CREATE TABLE VENDOR(
vno number(3) PRIMARY KEY,
vname varchar2(35) NOT NULL,
vcity varchar2(15));
If the above command is executed successfully, the message “table created “ is displayed.
The following are the rules for naming a table.
1. Table names should start with an alphabet
2. Underscores,numbers and letters are allowed but not blank spaces.
3. Maximun length of table name is 30 characters.
4. Reserved words of ORACLE cannot be used as table name.
5. Two different table should not have the same name.
6. Unique column names should be specified.
7. Proper data types and size should be specified.
Q: What are SQL constraints? Explain?
Ans: Entity integrity is enforced automatically when the primary key is specified in CREATE
TABLE command.
For Ex:
CREATE TABLE PRODUCT(
pno char(3),
pdesc varchar2(35) NOT NULL UNIQUE,
p_indate date,
qoh number(5),
price number(5),
vno number(3),
PRIMARY KEY(pno),
FOREIGN KEY(vno) REFERENCES VENDOR ON UPDATE CASCADE);
The primary key attribute contains both a NOT NULL and a UNIQUE specification.
The foreign key constraint definition ensures that
• You cannot delete a vendor from VENDOR table if atleast one PRODUCT row
references that VENDOR.
• ON UPDATE CASCADE (not supported by ORACLE) ensures that when a change is
made in VENDOR table, that change will be reflected automatically in PRODUCT
table.
Besides the primary key and foreign key constraints, the ANSI SQL standard defines the
following constraints.
• NOT NULL ensures that a column will not have null values.
• UNIQUE ensures that a column will not have duplicate values.
• DEFAULT defines a default value for a column(when no value is given).
• CHECK validates data in an attribute and sees that a specified condition exists.
Ex1: The minimum order must be atleast 10
Ex2:The date must be after APRIL 15, 2011
The CREATE TABLE command lets you define constraints in two different places.
• When you create the column definition (known as column constraint)
• When you use CONSTRAINT keyword (known as table constraint)
A column constraint applies to just one column.
A table constraint may apply to many columns.
Page 24
Q:Explain important data manipulation commands (DML) of SQL?
Ans:
INSERT: Used to enter data into a table.
Syntax:
INSERT INTO tablename VALUES (value1,value2,…..valuen)
Example:
INSERT INTO VENDOR VALUES (100,’RADHA’,’VJA’);
Observe that:
Character and date values must be entered between apostrophes(‘).
Numerical entries are not enclosed in apostrophes(‘).
Attribute entries are separated by commas.
Inserting Rows with NULL attribute
INSERT INTO product VALUES (‘P02’,’PENCIL’,’02-AUG-2011’, 25, 3, NULL);
Note that the NULL entry is accepted only because the vno attribute is optional in
PRODUCT table.
The NOT NULL declaration is not used in the CREATE TAVLE statement for these
attributes.
Inserting Rows with OPTIONAL attributes:
If the data is not available for all columns, then column list must be included following table
name.
INSERT INTO product(pno,pdesc) VALUES(‘P03’,’MOUSE’)
COPYING PARTS OF A TABLE
To create a new table based on selected column and rows of an existing table. In this
case, the new table will copy the attribute names,data characteristics and rows of
original table.
CREATE TABLE part AS
SELECT pno,pdesc,vno FROM product;
Note that no entity integrity(primary key) or referential integrity (foreign key) rules are
automatically applied to the new table.
Saving the table changes or COMMIT:
The COMMIT command permanently saves all changes such as rows added, attributes
modified and rows deleted made to any table in the database.
Syntax:
COMMIT;
Any changes made to table contents are not saved on disk until you close the database, close
the program you are using, or use the COMMIT command.
UPDATE Command:
The UPDATE command modifies an attribute value in one or more table rows.
Allows you to make data entries in an existing row’s columns.
Syntax:
UPDATE tablename
SET columnname = expression [,columnname = expression]
WHERE conditionlist;
Ex:To change the p_indate of product with pno P01 to 02-AUG-2011.
UPDATE PRODUCT
SET p_indate =’02-AUG-2011’
Page 25
WHERE pno=’P01’;
Restoring table contents or ROLLBACK:
ROLLBACK-undoes any changes since the last COMMIT command and brings the data
back to the values that existed before the changes were made.
Syntax: ROLLBACK;
Ex:
1. Create table called sales.
2. Insert 10 rows in sales table.
3. Execute the ROLLBACK command
ROLLBACK will undo only the result of INSERT and UPDATE commands.
All data definition commands(CREATE TABLE) are automatically committed to data
dictionary and cannot be rolled back.
DELETE Command
DELETE -deletes one or more rows from a table
If you do not specify a WHERE condition , all rows from table will be deleted.
REMOVAL OF SPECIFIED ROW(S):
Syntax: DELETE FROM tablename [WHERE conditionlist];
REMOVAL OF ALL ROWS:
Syntax: DELETE FROM tablename;
Viewing data in tables or SELECT
SELECT-lists the contents of a table.
Syntax:
SELECT columnlist
FROM tablename
[WHERE conditionlist];
The columnlist represents one or more attributes separated by commas.
You can use the * wildcard character to list all attributes.
Ex1: SELECT * FROM PRODUCT;
Ex2: SELECT pdesc,p_indate FROM product WHERE pno=’P01’;
Ex3:SELECT * FROM product WHERE p_indate>’01-AUG-2011’;
The SELECT statement retrieves all rows that match the specified condition.
WHERE clause adds conditional restrictions to SELECT statement.
The condition list is represented by one or more conditional expressions separated by logical
operators.
Comparison operators can be used to restrict output.
Comparison operators:
Symbol Meaning Example
= Equal to SELECT * FROM product WHERE pno=’P01’;
< Less than SELECT * FROM product WHERE price<10;
<= Less than or equal to
> Greater than SELECT * FROM product WHERE price>10;
>= Greater than or equal
to
<> or !
=
Not equal to SELECT * FROM product WHERE vno <> 100;
Using Computed Columns
Page 26
Oracle uses actual formula text as the label for the computed
column.
Ex: SELECT pno,qoh*price FROM PRODUCT;
Result:
Using Column aliases
An alias is an alternative name given to a column or table in any SQL
statement.
Ex2: SELECT pno,qoh*price AS total FROM PRODUCT;
Using Date arithmetic
SYSDATE is a special function that returns today’s date.
Ex:1 SELECT pno,p_indate,p_indate+90 AS ExpiryDate FROM product;
Ex:2 SELECT pno,p_indate,SYSDATE-90 AS CutDate FROM product
WHERE p_indate<=SYSDATE-90
The output would change based on today’s date
Arithmetic Operators:
Symbol Meaning Example
+ Addition
- Subtraction
* Multiply SELECT qoh, price*qoh FROM
product;
/ Division
^ Raised to power(some applications uses **
instead of ^)
Rules of Precedence: Perform operations within parentheses then perform ^ then *,/ then +,-
Logical Operators:
SQL allows you to have multiple conditions in a query through the use of logical operators.
Symbol Meaning Example
AND Both conditions must match SELECT * FROM product
WHERE price > 10 AND price < 100;
OR Either condition must match SELECT * FROM product
WHERE vno = 100 OR vno = 101
NOT Do not match a certain
condition
SELECT * FROM product
WHERE NOT(vno = 100)
Display the result when all the condition specified using the AND operator are satisfied
Display the result when Either of the condition specified using the OR operator are satisfied
NOT operator is used to find rows that do not match a certain condition. It negates the result
of conditional expression
Ex: SELECT * FROM product WHERE ( price < 50 AND p_indate > ’01-AUG-2011’) OR
vno = 100;
The rows of vno=100 are included regardless of p_indate and price of those rows.
Special Operators
BETWEEN operator:
Used to check whether an attribute value is within a range
Ex: To see list of products whose price is between $10 and $100, use the command:
Page 27
pno qoh*price
P01 PEN
P02 PENCIL
pno total
P01 PEN
P02 PENCIL
SELECT * FROM product WHERE price BETWEEN 10 AND 100;
IS NULL operator:
Used to check whether an attribute value is null.
Ex: To list all the products that do not have a vendor assigned, use the command:
SELECT * FROM product WHERE vno IS NULL;
LIKE operator:Used only with char and varchar2.
Matches a string pattern.
Used in conjuction with wildcards to find patterns within string attributes.
Ex1: To find all vendors whose name start with R
SELECT * FROM vendor WHERE vname LIKE ‘R%’;
To find all vendors whose name has ‘a’ as second letter.
Ex2: SELECT * FROM vendor WHERE vname LIKE ‘_a%’;
SQL allows you to use the percent sign (%) and underscore( _ ) wild card characters to make
matches when the entire string is not known.
Wildcard Meaning
% Matches any characters
_ Matches one characters
Matches can be made when the query entry is written exactly like table entry.
IN operator: matches any value within a VALUE list.
uses an equality operator i.e, it selects only those rows that match(are equal to) atleast
one of the values in the list
Ex:
SELECT * FROM product
WHERE vno IN(100 , 101);
All of the values in the list must be of same data type.
Each of the values in the value list is compared to the attribute.
IN operator is valuable when it is used in subqueries.
SELECT * FROM vendor
WHERE vno IN(SELECT vno FROM product );
Subquery (SELECT vno FROM product) will list all vendors who supply products.
IN operator will compare the values generated by subquery to vno values in VENDOR table.
EXISTS operator:checks whether subquery returns any row.
If subquery returns any row, run the main query otherwise don’t.
Ex:
SELECT * FROM vendor
WHERE (SELECT * FROM product WHERE qoh<=10);
Modifying structure of table:
ALTER Command: All changes to table structure are made using the ALTER command.
Syntax:
ALTER TABLE tablename
{ADD|MODIFY} (columnname datatype [{ADD|MODIFY} columnname datatype]);
To Change column’s datatype
Page 28
To change the vname datatype from varchar2 to char
ALTER TABLE vendor MODIFY (vname char(35));
To Change column’s data characteristics
To increase the width of vname column to 55 characters
ALTER TABLE vendor MODIFY (vname char(35));
To add a column
ALTER TABLE product ADD (pmin number(5));
If the table already has some data , we cannot add new column with NOT NULL as existing
rows will default to NULL for the new column.
TO ADD TABLE CONSTRAINTS:
Syntax: ALTER TABLE tablename ADD constraint [ADD constraint];
To add primary key:
ALTER TABLE part ADD PRIMARY KEY(part_no);
To add foreign key:
ALTER TABLE part ADD FOREIGN KEY(vno) REFERENCES vendor;
(OR)
ALTER TABLE part ADD PRIMARY KEY(part_no)
ADD FOREIGN KEY(vno) REFERENCES vendor;
To add primary and foreign key using the keyword CONSTRAINT:
ALTER TABLE part ADD CONSTRAINT pk_partno PRIMARY KEY(part_no)
ADD CONSTRAINT fk_vno FOREIGN KEY(vno) REFERENCES vendor;
TO REMOVE A COLUMN OR TABLE CONSTRAINT
Synax: ALTER TABLE tablename
DROP{ PRIMARY KEY | COLUMN columnname | CONSTRAINT constraintname};
Dropping a column: deleting a column
ALTER TABLE product DROP COLUMN pmin;
DELETING A TABLE FROM DATABASE:
A table can be deleted from the database using the DROP TABLE command.
Syntax:
DROP TABLE part;
Advanced select queries
ORDER BY clause: Orders the selected rows based on one or more attributes
• Used in the last portion of select statement
• By using this, rows can be sorted
• By default it takes ascending order
• DESC is used for sorting in descending order
• Sorting by column which is not in select list is possible.
• Sorting by column aliases
Example: To produce a list of products sorted in descending order of their prices.
SELECT pno,pdesc,p_indate,price
FROM product
Page 29
ORDER BY price DESC;
A multilevel ordered sequence is known as cascading order sequence and it can be created
easily by listing several attributes, separated by commas, after the ORDER BY clause.
SELECT * FROM employee ORDER BY e_lname,e_fname,e_initial;
DISTINCT clause: Used to eliminate duplicate rows.
Ex:How many different vendors are currently represented in the PRODUCT table?
SELECT DISTINCT vno FROM product;
Explain Aggregate functions?
Ans: Some of the aggregate functions are COUNT,MIN,MAX,AVG.
COUNT: Uses one parameter within parantheses.
COUNT(columnname)-Used to count the number of non-null values of an attribute
COUNT(*) aggregate function is used to count number of rows returned by query, including
the rows that contain nulls.
Ex1: How many rows in PRODUCT table have a price value less than or equal to $500?
SELECT COUNT(*) FROM product WHERE price<=500;
Ex:2:How many vendors referenced in the PRODUCT table have supplied products with
prices that are <+1?
SELECT COUNT(DISTINCT vno) FROM product WHERE price<=10;
MAX and MIN
Ex1: Which product has highest price?
SELECT * FROM product WHERE price = (SELECT MAX(price) FROM product);
(Here we cannot use SELECT * FROM product WHERE price = MAX(price); because The
aggregate functions can be used only in column list of a SELECT statement)
Ex2:Highest price in PRODUCT table?
SELECT MAX(price) FROM product;
Ex:3Lowest price in PRODUCT table?
SELECT MIN(price) FROM product;
Ex4: To find out the product that has the oldest date?
SELECT * FROM product WHERE price = (SELECT MIN(p_indate) FROM product);
Ex5: To find out the most recent product.
SELECT * FROM product WHERE price = (SELECT MAX(p_indate) FROM product);
SUM: Computes total sum of any specified attribute.
Ex:To find the total value of all items
SELECT SUM(qoh*price) AS TOTALVALUE
FROM product;
AVG
Ex1: To find the products whose prices exceed the average product price.
SELECT * FROM product
WHERE price > (SELECT AVG(price) FROM product)
ORDER BY price desc;
Explain about GROUP BY clause?
• Used to group rows on basis of certain common attribute value such as employees of
a department, products of a vendor.
Page 30
• WHERE clause can be used ,if needed.
• The only attributes that can be put in select clause are the aggregated functions and
the attributes that have been used for grouping the information.
Ex1:How many products are supplied by each vendor?
SELECT vno, COUNT(pno)
FROM product
GROUP BY vno;
Having clause:
Extension of the GROUP BY feature is the HAVING clause.
HAVING clause is applied to the output of GROUP BY operation.
Ex: how many products supplied by each vendor.List only the products whose average is
below $10
SELECT vno, COUNT(pno), AVG(price)
FROM product
GROUP BY vno
HAVING AVG(price) < 10;
Q: Explain about index in SQL
Ans:
Indexes are used to quickly access the data.
Syntax: CREATE INDEX <index name> ON <tablename>(column name);
An index can be created on one or more columns.
Based on the number of columns included in index, an index can be of 2 types.
1. Simple index 2.Composite Index.
To create Simple index:
An index created on a single column is called simple index.
Ex: CREATE INDEX p_in ON product(p_indate)
To create composite index:
An index created on a more than one column is called composite index.
Dropping indexes or deleting an index: Use the DROP INDEX command.
Ex: DROP INDEX p_in;
Q:What is database schema?
Ans:A schema is a group of database objects such as tables and indexes, that are related to
each other. Syntax: CREATE SCHEMAAUTHORIZATION {creator}
When a user is created, the DBMS automatically assigns schema to that user.
Schemas are useful to group tables by owner and enforce a first level of security by allowing
each user to see only the tables that belong to that user.
Labwork:
CREATE TABLE VENDOR(
Page 31
VENDOR
vn
o
vname vcity
10
0
RADHA VJA
10
1
ALIYA NULL
10
2
SIRI VJA
10
3
LAK GNT
vno number(3) PRIMARY KEY,
vname varchar2(35) NOT NULL,
vcity varchar2(15));
CREATE TABLE PRODUCT(
pno char(3),
pdesc varchar2(35) NOT NULL UNIQUE,
qoh number(5),
price number(5),
vno number(3),
PRIMARY KEY(pno),
FOREIGN KEY(vno) REFERENCES VENDOR);
CREATE TABLE CUSTOMER(
cno number(3) PRIMARY KEY,
cname varchar2(35) ,
city varchar2(5),
baldue number(5));
CREATE TABLE INVOICE(
invno number(3),
cno number(3),
invdate date,
PRIMARY KEY(invno),
FOREIGN KEY(cno) REFERENCES CUSTOMER);
Page 32
pno pdesc qoh price vno
P01 PEN 2 10 100
P02 CD 20 12 101
P03 PENCIL 200 3 NULL
P04 DVD 200
0
350 101
CUSTOMER
cno cname city baldue
20
1
ANU VJA 100
20
2
ASHA GNT 500
20
3
RAJ VJA
INVOICE
invno cn
o
invdate
301 20
1
20-AUG-2011
302 20
2
20-AUG-2011
303 20
3
21-AUG-2011
304 20
1
21-AUG-2011
All products sold are stored in LINE table
CREATE TABLE LINE(
invno number(3),
lineno char(3),
pno char(3),
line_units number(5),
line_price number(5),
PRIMARY KEY(invno,lineno),
FOREIGN KEY(pno) REFERENCES PRODUCT
FOREIGN KEY(invno) REFERENCES INVOICE);
CREATE TABLE EMP(
e_lname varchar2(20),
e_fname varchar2(20),
e_initial varchar2(2),
dob date,
sal number(8,2));
Page 33
LINE
invno lineno pno line_units line_price
301 L01 P01 10 10
301 L02 P02 10 12
301 L03 P03 20 3
302 L01 P01 30 10
302 L02 P02 20 12
303 L01 P01 35 10
303 L02 P02 15 12
EMP
e_lname e_fname e_initial dob Sal
REDDY SAM A 14-NOV-
1994
15000.25
NAIDU ANU S 14-OCT-1992 16234.50
JAIN NEHA K 28-NOV-
1993
15623.48
REDDY RAM T 14-SEP-1994 1623.89
Unit –III Chapter –II ADVANCED SQL
SQL data manipulation commands operate over entire table (ex: SELECT command lists all
rows from the table you specified in FROM clause) and are said to be set oriented
commands.
UNION statement:combines rows from two or more queries without including duplicate
rows.
Syntax: query UNION query
Query: SELECT cname,city FROM customer UNION SELECT cname,city FROM
customer3
Combines ouput of two or more SELECT queries. (The select statements must be union –
compatible.that is they must return the same attribute names and similar data types) without
including duplicate rows.
UNION ALL Combines ouput of two or more SELECT queries. (The select
statements must be union – compatible.that is they must return the same
attribute names and similar data types) and retains duplicate rows
SELECT cname,city FROM customer UNION ALL SELECT cname,city FROM customer3
INTERSECT statement: used to combine rows from two queries ,
returning only the rows that appear in both sets.
SELECT cname,city FROM customer INTERSECT SELECT cname,city FROM customer3
Page 34
CUSTOMER3
cno cname city baldue
40
1
JAY GNT 200
40
2
RAJ VJA 300
CUSTOMER
cname city
ANU VJA
ASHA GNT
RAJ VJA
JAY GNT
CUSTOMER
cno cname city baldue
20
1
ANU VJA 100
20
2
ASHA GNT 500
20
3
RAJ VJA
CUSTOMER
cname city
ANU VJA
ASHA GNT
RAJ VJA
JAY GNT
RAJ VJA
CUSTOMER
cname city
RAJ VJA
MINUS statement: combines rows from two queries and returns only
rows that appear in first set but not in the second.
SELECT cname,city FROM customer MINUS SELECT cname,city FROM customer3
SQL JOIN OPERATORS:
A join is used to combine rows from multiple tables and returns the rows with one of the
following conditions:
Join operations can be classified as inner joins and outer joins.
The inner join is traditional join in which only rows that meet a given criteria are
selected.
The join criteria can be an equality condition (also called a natural join or an equijoin) or
inequality condition( also called theta join)
Generally a join condition will be equality comparison of the P.K of one table and F.K of
related table
An outer join returns not only matching rows but also unmatched attribute values from
one table or both tables to be joined.
Join
specification
Join Type SQL
Syntax Example
Description
CROSS CROSS
JOIN
SELECT * FROM
T1,T2
SELECT *
FROM T1 CROSS
JOIN T2;
Returns the Cartesian product of T1 and
T2(old style)
Returns the Cartesian product of T1 and
T2(old style)
INNER Old-Style
JOIN
SELECT * FROM
T1,T2
WHERE
T1.C1=T2.C1;
Returns only the rows that meet the join
condition in the WHERE clause.
NATURAL
JOIN
SELECT *
FROM T1
NATURAL JOIN T2;
Returns only the rows with matching
values in the matching columns.The
matching columns must have the same
names and similar datatypes.
JOIN
USING
SELECT *
FROM T1 JOIN T2
USING(C1)
Returns only the rows with matching
values in the columns indicated in the
USING clause
JOIN ON
SELECT *
FROM T1 JOIN T2
ON T1.C1=T2.C1;
Returns only the rows that meet the join
condition in the ON clause
OUTER LEFT JOIN SELECT *
FROM T1 LEFT
OUTER JOIN T2
ON T1.C1=T2.C1;
Returns rows with matching values and
include all rows from left table(T1) with
unmatched values
RIGHT
JOIN
SELECT *
FROM T1 RIGHT
OUTER JOIN T2
ON T1.C1=T2.C1;
Returns rows with matching values and
include all rows from right table(T2) with
unmatched values
Page 35
CUSTOMER
cno cname city
20
1
ANU VJA
20
2
ASHA GNT
FULL
JOIN
SELECT *
FROM T1 FULL
OUTER JOIN T2
ON T1.C1=T2.C1;
Returns rows with matching values and
include all rows from both table(T1 and
T2) with unmatched values
RECURSIVE JOIN (OR) SELF JOIN:
An alias is an alternative name given to a column or table in any SQL statement.
An alias is especially useful when a table must be joined to itself in a recursive query
Ex:
SELECT E.Eno,E.Ename,M.Ename
FROM EMP E,EMP M
WHERE E.Mgr=E.Eno;
Cross Join:(also known as cartesian product) Examples:
SELECT * FROM invoice CROSS JOIN line;
The above query generates 4*7=28rows ( 4 rows in invoice table and 7 rows in line table)
Natural Join:
SELECT cno,cname,invno,invdate FROM customer NATURAL JOIN invoice;
You are not limited to two tables when performing a natural join.
It doesnot require a table qualifier for the common attribute.
SELECT invno,pno,pdesc,line_units,line_price
FROM invoice NATURAL JOIN line NATURAL JOIN product;
JOIN USING clause
It doesnot require a table qualifier for the common attribute.
SELECT invno,pno,pdesc,line_units,line_price
FROM invoice JOIN line USING(invno) JOIN product USING(pno);
JOIN ON clause
Do not require common attribute names in the joining tables.
Requires a table qualifier for the common attribute.
Lets you perform a join even when the tables do not share a common attribute name.
SELECT invoice.invno,pno,pdesc,line_units,line_price
FROM invoice JOIN line ON invoice.invno=line.invno
JOIN product ON line.pno=product.pno;
OUTER JOINS
SELECT pno,vendor.vno,vname FROM vendor LEFT JOIN product ON
vendor.vno=product.pno;
SELECT pno,vendor.vno,vname FROM vendor RIGHT JOIN product ON
vendor.vno=product.pno;
SELECT pno,vendor.vno,vname FROM vendor FULL JOIN product ON
vendor.vno=product.pno;
SUBQUERIES: used when it is required to process data based on other processed data
Characteristics of sub queries:
A subquery or nested query or inner query is a query inside another query.
A subquery is normally expressed inside parentheses
The output of inner query is used as input for the outer(high-level) query.
So inner query is executed first and then the outer query.
Subquery is based on the use of the SELECT statement to return one or more values to
another query. If the table into which you are inserting rows has one date attribute and one
Page 36
number attribute, the SELECT subquery should return rows in which 1st
column has date
values and 2nd
column has number values.
Inserting table rows with a select subquery or Copying parts of tables:
It add multiple rows to a table, using another table as source of the data.
CREATE TABLE PART(
part_no char(3) PRIMARY KEY,
part_desc varchar2(35),
vno number(3));
Syntax:
INSERT INTO target_tablename SELECT source_columnlist FROM source_tablename;
Example: INSERT INTO part SELECT * FROM product;
Both the tables(PART and PRODUCT) must have same attributes.The above query returns all
rows from table PRODUCT.
SELECT subquery Examples Explanation
UPDATE product
SET price=(SELECT AVG(price) FROM product )
WHERE vno=’100’………………Ex(2)
Updates the product price to
average product price for the
products provided by vendor 100.
DELETE FROM product WHERE vno IN(SELECT
vno FROM vendor WHERE vcity=’VJA’)
……………..Ex(3)
Delete the PRODUCT table rows
that are provided by vendors with
vcity=’VJA’
A subquery can return
i. One value as in Ex(2) ( the select subquery returns avg(price) which is one value).
ii. A list of values as in Ex(3) (the select subquery returns a list of vendors from ‘VJA’)
iii. A virtual table
iv. No value at all i.e, NULL . the output of the outer query might result in an error or a
null empty set.
WHERE subqueries
Ans:Ex: Find all products with a price greater than or equal to the average product price, you
write the following query.
SELECT pno,price FROM product
WHERE price>=(SELECT AVG(price) FROM product);
Note that this type of query,when used in a >,<,==,>= or <= conditional expression, requires
a subquery that returns only one single value.If the query returns more than a single value, the
DBMS will generate an error.
IN subqueries: Ans:Ex(2)
HAVING subqueries
Example:To list all products with the total quantity sold greater than the average quantity sold
SELECT pno,SUM(line_units) FROM line
GROUP BY pno HAVING SUM(line_units)>(SELECT AVG(line_units) FROM line);
MULTIROW subquery operators: ANY and ALL
1. ALL:used to do an inequality comparison(> or <) of one value to a list of values.
Example: What products have a product cost that is greater than all individual product costs
for products provided by vendor with vno 101
SELECT pno, qoh*price FROM product
WHERE qoh*price> ALL(SELECT qoh*price FROM product WHERE vno = 101);
Page 37
In the above query the ALL operator allows you to compare a single value(qoh*price) with a
list of values returned by the subquery.
2. ANY: ANY operator allows you to compare a single value with a list of values,
selecting only the
rows whose qoh*price is greater than any value of the list.
FROM subqueries
FROM clause specifies the table from which data will be drawn.
Example:To find all customers who purchased both products ‘PEN’ and ‘PENCIL’
SELECT DISTINCT cno, cname FROM customer,
( SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PEN’) cp1,
(SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PENCIL’)
cp2
WHERE customer.cno = cp1.cno AND cp1.cno=cp2.cno;
(OR)
CREATE VIEW cp1 AS
SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PEN’;
CREATE VIEW cp2 AS
SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PENCIL’;
SELECT DISTINCT cno, cname FROM customer NATURAL JOIN cp1 NATURAL JOIN
cp2;
Attribute List Subqueries or inline subquery.
The attribute list can also include a subquery expression also known as inline subquery.
The inline subquery must return one single value otherwise an error code is raised.
SELECT pno, price ,(SELECT AVG(price) FROM product) AS AVGPRICE ,
price - (SELECT AVG(price) FROM product) AS DIFF
FROM product;
The query used the full expression instead of column aliases when computing DIFF.
The column aliases cannot be used in computations in the attribute list when the alias is
defined in the same attribute list.
We can use Attribute List Subqueries to include data from other tables that are not
directly related to main table or tables in the query.
SELECT pno,SUM(line_units*line_price) AS sales,
(SELECT COUNT(*) FROM employee) AS ecount,
SUM(line_units*line_price)/ (SELECT COUNT(*) FROM employee) AS contib
FROM line
GROUP BY pno;
CORRELATED SUBQUERIES
To produce correlated subquery the DBMS does
i. It iniates the outer query
ii. For each row of the outer query result set, it executes the inner query by passing
the outer row to the inner query.( inner query references a column of the outer
subquery)
Example:To find all product sales who units sold > average units sold for that product.
SELECT invno, pno, line_units FROM line LS
WHERE LS.line_units>(SELECT AVG(line_units) FROM line LA WHERE LA.pno =
LS.pno);
Page 38
The inner query runs once using the first pno found in the outer line table and returns average
sale for that product.
CORRELATED subqueries can also be used with EXISTS special operator
Example: To know the vendor code and name for the products having qoh<10
SELECT vno, vname FROM vendor
WHERE EXISTS(SELECT * FROM product WHERE qoh<10 AND
vendor.vno=product.vno);
SQL functions: Used to generate information from data.
DUAL: is Oracle pseudo table used only for cases when a table is not really needed.
DATE/TIME FUNCTIONS
Function Examples
LAST_DAY:
returns the last day of the month based
on a date value.
Syntax:
last_day( date_value )
SELECT last_day(to_date('2003/03/15',
'yyyy/mm/dd')) FROM DUAL; would return Mar 31,
2003
SELECT last_day(to_date('2003/02/03',
'yyyy/mm/dd')) FROM DUAL; would return Feb 28,
2003
List employees born in the last seven days of a month
SELECT * FROM emp WHERE dob >=
LAST_DAY(dob)-7;
TO_CHAR function :
converts a number or date to a string.
Syntax: TO_CHAR(date_value,fmt)
fmt = format used can be
MONTH Name of month
MON:three-letter month name
MM-two digit month name
D Day of week (1-7).
DAY Name of day.
DD Day of month (1-31).
YYYY 4-digit year
YY: two digit year value
SELECT to_char(sysdate, 'yyyy/mm/dd') FROM DUAL;
would return '2003/07/09'
List all employees born in 1994.
SELECT * FROM emp WHERE
TO_CHAR(dob,’YYYY’)=’1994’;
List all employees born in the month of NOVEMBER
SELECT * FROM emp WHERE
TO_CHAR(dob,’MM’)=’11’;
List all employees born on 14th
of a month
SELECT * FROM emp WHERE
TO_CHAR(dob,’DD’)=’14’;
TO_DATE function:converts a string
to a date. Also used to translate a date
between formats.
Syntax: TO_DATE(char_value,fmt)
fmt = format used can be as above
SELECT to_date('2003/07/09', 'yyyy/mm/dd') FROM
DUAL;
would return a date value of July 9, 2003.
Find the age of employess as on 12-31-2012
SELECT
e_lname,TO_DATE(’12/31/2012’,’MM/DD/YYYY’)-
dob/365 AS YEARS FROM emp;
NOTE: ‘12/31/2012’ is a text string, not a date,
TO_DATE translates the text string to a valid oracle
date used in date arithmetic.
How many days are between 6/25/2011 and
10/27/2011
SELECT
TO_DATE(’2011/06/25’,’YYYY/MM/DD’)-
TO_DATE(‘OCTOBER 27,2011’,’MONTH
Page 39
,DD,YYYY’) FROM DUAL;
SYSDATE : returns todays date SELECT TO_DATE(’25-DEC-2011’,’DD-MON-
YYYY’)-SYSDATE FROM dual;
ADD_MONTHS: adds months to a
date.
Syntax:
add_months( date_value, n )
date_value is the starting date (before
the n months have been added).
n is the number of months to add to
date1.
SELECT add_months('01-Aug-03', 3) FROM DUAL;
would return '01-Nov-03'
SELECT pno,p_indate,ADD_MONTHS(p_indate,24)
FROM product
NUMERIC FUNCTIONS
Aggregate functions operate over a set of values(multiple rows) while numeric functions operate
over a single row.
Function Example
ABS
Returns absolute value of a number.
Syntax: ABS(numeric_value)
SELECT ABS(1.95),ABS(-1.93) FROM
DUAL;
Would return 1.95 1.93
ROUND function returns a number rounded
to a certain number of decimal places.
Syntax:ROUND(numeric_value,p)
p=precision
SELECT round(125.315) FROM DUAL;
would return 125
SELECT ROUND(sal) as sal1, ROUND(sal) as
sal2 FROM emp;
CEIL function returns the smallest integer
value that is greater than or equal to a
number.
Syntax: ceil( number )
SELECT ceil(-32.65) FROM DUAL;
would return -32.
SELECT ceil(32.65) FROM DUAL; would
return 33.
SELECT CEIL(sal) ,FLOOR(sal) FROM emp;
FLOOR function returns the largest integer
value that is equal to or less than a number.
Syntax: floor( number )
SELECT floor(5.9) FROM DUAL; would
return 5
SELECT floor(-5.9) FROM DUAL; would
return -6
The sqrt function returns the square root of
n.
Synatx: sqrt( n )
n is a positive number.
sqrt(9) would return 3
mod function returns the remainder of m
divided by n
mod(15, 4) would return 3
power function returns m raised to the nth
power.
Syntax : power( m, n )
m is the base. n is the exponent.
If m is negative, then n must be an integer.
power(3, 2) would return 9
Page 40
exp function returns e raised to the nth
power, where e = 2.71828183.
exp(3) would return 20.0855369231877
trunc function returns a number truncated to
a certain number of decimal places.
trunc(125.815, 0) would return 125
trunc(125.815, 1) would return 125.8
ln function returns the natural logarithm of a
number.
ln(20) would return 2.99573227355399
log function returns the logarithm of n base
m.
Syntax: log( m, n )
m must be a positive number, except 0 or 1.
n must be a positive number.
log(100, 1) would return 0
String Functions: are useful to concatenate strings of characters, printing names in upper case or
knowing the length of a given attribute.
Function Example
UPPER function converts all
letters in the specified string to
uppercase.
Syntax: UPPER(string)
upper('Tech on'); would return 'TECH ON
List all employee names in upper case.
SELECT UPPER (e_initial) || ‘.’|| UPPER (e_fname) ||
UPPER(e_lname) FROM EMP;
LOWER function converts all
letters in the specified string to
lowercase.
Syntax: LOWER(string)
List all employee names in lower case.
SELECT LOWER (e_initial) || ‘.’|| LOWER (e_fname) ||
LOWER(e_lname) FROM EMP;
SUBSTR function allows you to
extract a substring from a string.
Syntax:substr( string, p, l )
string is the source string.
p is the position for extraction.
l is optional. It is the number of
characters to extract.
substr('This is a test', 6, 2) would return 'is'
substr('This is a test', 6) would return 'is a test'
substr('This is a test', -3, 3) would return 'Net'
List first 3 characters of all employee last names..
Ex:SELECT SUBSTR(e_lname,1,3) AS prefix FROM EMP;
LENGTH function returns the
number of characters in the
specified string.
Syntax:
length( string)
length(NULL) would return NULL.
length('') would return NULL. 
length('Tech on the Net') would return 15.
List all employees last names and length of their last names.
SELECT e_lname, LENGTH(e_lname) FROM EMP;
Concatenation
The || operator allows you to
concatenate data from two
different character columns and
returns a single column.
Syntax: string1 || string2
'a' || 'b' || 'c' || 'd' would return 'abcd'.
List all employee names (concatenated)
SELECT e_initial || ‘.’|| e_fname || e_lname AS NAME
FROM EMP;
CONVERSION FUNCTIONS:allows you take a value of given data type and convert it to the
equivalent value in another data type.
Functions Example
TO_CHAR : returns a
character string from a
numeric value.
Syntax:
SELECT eno, TO_CHAR(sal, ‘9,999.99’) AS PRICE FROM
EMP;
Page 41
TO_CHAR(numeric_value,
fmt)
TO_NUMBER : returns a
formatted number from a
character string.
Syntxa:TO_NUMBER
(char_value, fmt)
fmt= format used can be:
9 - displays a digit
0 – displays a leading zero
, - displays the comma
. – displays the decimal point
$ - displays the dollar sign
B – leading blank
S – leading sign
MI – trailing minus sign
SELECT TO_NUMBER(‘-123.99’,’S9999.99’),
TO_NUMBER(’99.78-’,’B999.99MI’), FROM DUAL;
DECODE: compares an
attribute or expression with a
series of values and returns
an associated value or a
default value if no match is
found
Syntax: DECODE(e,x,y,d)
e – attribute or expression
x – value with which to
compare e
y – value to return in e = x
d – default value to return if e
is not equal to x
The following example returns the sales tax for specified
cities.
Compares vcity to ‘VJA’ ;if the value matches it returns .08
Compares vcity to ‘GNT’ ;if the value matches it returns .05
If there is no match it returns 0.00( the default value)
SELECT vno, vcity,
DECODE(vcity,’VJA’,.08,’GNT’,.05,0.00) AS TAX FROM
VENDOR;
Page 42
Oracle sequences: generates a numeric value that can be assigned to any column in any
table.
Use of sequences is optional, you can enter the values manually.
Oracle sequences have a name and can be used any where a value is expected.
Sequences can be created and deleted anytime.
The table attribute to which you assigned a value based on a sequence can be edited and
modified.
Oracle sequences are
• Independent objects in the database.
• Not a data type
• Not tied to a table or column
Syntax:
CREATE SEQUENCE name [START WITH n] [INCREMENT BY n] [CACHE |
NOCACHE]
where name is the name of the sequence
n is an integer that can be positive or negative.
START WITH specifies initial sequence value( the default value is 1)
INCREMENT BY determines the value by which the sequence is incremented.
The CACHE or NOCACHE indicates whether oracle will preallocate sequence numbers in
memory. (Oracle preallocates 20 values by default)
Example: CREATE SEQUENCE CSEQ1 START WITH 204 INCREMENT BY 1
NOCACHE;
To check all the sequences you have created.
SELECT * FROM USER_SEQUENCES;
To use sequences during data entry
you must use two special pseudo columns NEXTVAL and CURRVAL.
NEXTVAL retrieves the next available value from a sequence. Each time you use
NEXTVAL , the sequence is incremented.
CURRVAL retrieves the current value of sequence.
Example
INSERT INTO CUSTOMER VALUES (CSEQ1.NEXTVAL,’RAVI’,’NELLORE’, 500);
INSERT INTO INVOICE VALUES (‘I05’ , CSEQ1.CURRVAL,’22-AUG-2011’);
You cannot use CURRVAL unless a NEXTVAL was issued previously in the same session.
NEXTVAL retrieves the next available sequence number( here 204) and signs to cno in
CUSTOMER table.
CSEQ1.CURRVAL refers to last used CSEQ1.NEXTVAL sequence number(204).
In this way the relationship between INVOICE and CUSTOMER is established.
COMMIT; statement must be issued to make the changes permanent.
You can also issue a ROLLBACk statement , in which case the rows you inserted in
INVOICE and CUSTOMER will be rolled back.( but sequence number would not) That is, if
you use sequence number again you must get 204 but you will get 205 eventhough the row
204 is deleted.
DROPPING a SEQUENCE doesnot delete the values you assigned to table attributes.
Syntax: DROP SEQUENCE CSEQ1;
Page 43
VIEWS
A view is a virtual table based on SELECT query.
The tables on which view is based are called base tables.
Syntax:
CREATE VIEW viewname AS SELECT query
Characteristics:
A relational view has several special characteristics
• We can use the view instead of table in a SQL statement.
• Views are dynamically updated when the base table is updated.
• Views provide a level of security in the database. The view can restrict users to only
specified columns and specified rows in a table.
• View may also be used as the basis for reports
Example: CREATE VIEW PROD_STATS AS SELECT vno, SUM(qoh * price) AS TotalCost
FROM PRODUCT GROUP BY vno;
To drop a view
Syntax: DROP VIEW <view name>
Example:
DROP VIEW PROD_STATS
UPDATABLE VIEWS:To use batch update routines to update master table attribute with
transaction data.
To demonstrate a batch update routine, consider two tables
NOTE:There is 1:1 relationship between two tables
To Update qoh attribute (qoh – qty as that much quantity has been sold)
1. We have to join two tables
2. update qoh for each row of ProdMaster table with matching pno values in ProdSales
table.
We use a updatable view to do that.
Updatable view is a view that can be used to update attributes in the base tables that are used
in the view.
Not all views are updatable.
The most common updatable view restrictions are as follows:
1. GROUP BY and aggregate functions cannot be used.
2. Cannot use SET operators.
3. The P.K columns of base table you want to update must have unique values in the
view. That is, the two tables must have 1:1 relationship then only the view can be
used to update a base table.
Example: CREATE VIEW QUP AS ( SELECT ProdMaster.pno, qoh, qty FROM ProdMaster,
ProdSales
WHERE ProdMaster.pno=ProdSales.pno);
Page 44
ProdMaster
pno pdesc qoh
P01 SCREWS 60
P02 NUTS 37
P03 BOLTS 50
ProdSales
pno qty
P01 7
P02 3
UPDATE QUP SET qoh=qoh-qty;
Page 45
Q: What is PSM (Persistent Stored Module)?
Ans: A Persistent Stored Module is a block of code containing standard SQL statements and
procedural extensions that is stored and executed at the DBMS server. The PSM represents
business logic that can be encapsulated, stored and shared among multiple database users. A
PSM lets an administrator assign specific access rights to a stored module to ensure that only
authorized users can use it. Oracle implements PSMs through its procedural SQL language.
(PL/SQL)
Q: What is PL/SQL? Explain?
Ans: PL/SQL is a language that makes it possible to use and store procedural code and SQL
statements within the database.
It is also used to merge SQL and traditional programming constructs, such as
• Variables,
• conditional processing (IF-THEN-ELSE),
• basic loops (FOR and WHILE loops) and
• Error trapping.
The procedural code is executed as a unit by the DBMS when it is invoked by the end user.
End users can use PL/SQL to create
• Anonymous PL/SQL blocks.
• Triggers
• Stored Procedures
• PL/SQL functions
You can write PL/SQL code block by enclosing the commands inside BEGIN and END
clause.
Ex:
BEGIN
INSERT INTO vendor VALUES (105, ‘SITA’, ‘TNL’);
END;
/
This is an example of anonymous PL/SQL block because it has not given a specific name.
The above PL/SQL block executes as soon as you press ENTER key after typing /
You will see the message “PL/SQL procedure successfully completed”
If you want a more specific message such as “new vendor added”. You must type as follows:
SQL> SET SERVEROUTPUT ON
This SQL * plus command enables the client console (SQL * plus) to receive messages from
the server side(ORACLE DBMS).To send messages from the PL/SQL block to SQL * plus
console, use the DBMS_OUTPUT.PUT_LINE function.
The standard SQL , the PL/SQL code are executed at server side, not at client side.To stop
receiving messages from sever , enter SET SERVEROUTPUT OFF.
In oracle , you can use the SQL * plus command SHOW ERRORS to help you diagnose
errors found in PL/SQL blocks.
Q: Write anonymous PL/SQL program to insert rows into VENDOR table and display
the message “New vendor added”.
Ans:
BEGIN
INSERT INTO vendor VALUES (106,’GITA’,’VJA’);
DBMS_OUTPUT.PUT_LINE(‘New vendor added’);
END;
/
Page 46
PL/SQL Basic data types
Data Type Description
CHAR character values of a fixed length
VARCHAR2 variable length character values
NUMBER numeric values
DATE Date values
%TYPE inherits the datatype from a variable that you declared previously or from an
attribute of a database table. Ex: price1 PRODUCT.price %TYPE ;
assigns price1 the same datatype as the price column in the PRODUCT table.
Q: Write anonymous PL/SQL program to display the number of products in price range
0 and 10, 11 and 60 ,61 and 110 etc..
Ans:
DECLARE
P1 NUMBER(3) := 0;
P2 NUMBER(3) := 10;
NUM NUMBER(2) := 0;
BEGIN
WHILE P2<5000 LOOP
SELECT COUNT(pno) INTO NUM FROM product WHERE price BETWEEN P1 AND P2;
DBMS_OUTPUT.PUT_LINE(‘There are ‘|| NUM|| ‘ products with price between ‘||P1|| ‘ and
‘||P2);
P1 := P2+1;
P2 := P2+50;
END LOOP;
END;
/
The PL/SQL block shown above has following characteristics.
1. Each statement inside the PL/SQL code must end with a semicolon
2. The PL/SQL block starts with the DECLARE section in which you declare the
variable names, the data types and an initial value(optional).
3. A WHILE loop is used.
4. Uses the string concatenation symbol.
5. SELECT statement uses the INTO keyword to assign output of the query to a PL/SQL
variable
The most useful feature of PL/SQL block is that they let you create code that can be named,
stored and executed either implicitly or explicitly by the DBMS.
What is Trigger ? Explain.
Ans: A trigger is a procedural sql code which is fired when a DML statements like Insert,
Delete, Update is executed on a database table.
The syntax to create a trigger in oracle is:
CREATE OR REPLACE TRIGGER trigger_name
[BEFORE / AFTER] [DELETE /INSERT/UPDATE OF column_name ] ON table_name
[FOR EACH ROW]
[DECLARE]
[variable_name data-type [:= initial_value]]
BEGIN
PL/SQL instructions;
……
Page 47
END;
A trigger definition contains the following parts:
1.The triggering timing: BEFORE or AFTER. This timing indicates at what time the
trigger should get fired. (before or after the triggering statement is completed.)
2.The triggering statement/event: The statement that causes the trigger to execute
(INSERT, UPDATE or DELETE)
The triggering level: There are two types of triggers: statement – level triggers and row –
level triggers
• Statement – level triggers: This type of trigger is executed once, before or after the
triggering statement is completed.
• Row – level triggers: requires the use of the FOR EACH ROW keywords. This type
of trigger is executed once for each row affected. ( if you update 10 rows, the trigger
executes 10 times.
2. Triggering Action: The PL/SQL code enclosed between BEGIN and END keywords.
You can use a trigger to update an attribute in a table other than the one being
modified.
CREATE OR REPLACE TRIGGER TLP
AFTER INSERT ON line
FOR EACH ROW
BEGIN
UPDATE product
SET qoh = qoh - :NEW.LINE_UNITS
WHERE product.pno = :NEW.pno;
END;
/
TLP is a row level trigger that executes after inserting a new LINE row and reduces quantity
on hand (in PRODUCT table) of recently sold product by the number of units sold.
CREATE OR REPLACE TRIGGER trigger_name =>creates a trigger with the given name
or overwrites an existing trigger with the same name.
OF column_name =>This clause is used with update triggers. This clause is used when you
want to trigger an event only when a specific column is updated
ON table_name=> the name of the table or view to which the trigger is associated.
Example of a statement level trigger that is executed after an update of the qoh, pmin
attribute for an existing row or after an insert of a new row in the product table.
CREATE or REPLACE TRIGGER TPR
AFTER INSERT OR UPDATE OF QOH,PMIN ON PRODUCT
BEGIN
UPDATE PRODUCT
SET REORDER =1
WHERE QOH <= PMIN;
END;
/
Q: When does a trigger fire?
Ans: A trigger is triggered automatically when an associated DML statement is executed.
• A trigger is invoked before or after a data row is inserted, updated or deleted.
• A trigger is associated with a database table.
• Each database table may have one or more triggers.
• A trigger is executed as part of the transaction that triggered it.
Page 48
Q: How to delete a trigger?
Ans: When you delete a table, all its trigger objects are deleted with it.
If you want to delete a trigger without deleting the table, give the following command
DROP TRIGGER triggername.
Q: Write a program to update the customer balance in the CUSTOMER table after
inserting every new LINE row.
CREATE OR REPLACE TRIGGER TLC
AFTER INSERT ON line
FOR EACH ROW
DECLARE
cus CHAR(5);
tot NUMBER := 0; --to compute total cost
BEGIN
SELECT cno INTO cus FROM invoice --1) get the customer code
WHERE invoice.invno = :NEW.line_units;
tot := :NEW.line_price * :NEW.invno; --2)compute the total of the current line
UPDATE customer SET baldue = baldue + tot WHERE cno = cus;
DBMS_OUTPUT.PUT_LINE(‘ *** Balance updated for customer : ‘ || cus);
END;
/
The trigger is a row level trigger that executes for each new LINE row inserted.
The SELECT statement returns only one attribute (cno) from INVOICE table and that
attribute returns only one value.
You use the INTO clause to assign a value from a SELECT statement to a variable (cus) used
within a trigger.
Double dashes “--“ are used to indicate comments within the PL/SQL block.
Trigger action based on conditional DML predicates
You can create a trigger that executes after an insert, an update or a delete on the PRODUCT
table and to know which one of the three statements caused the trigger to execute use the
following syntax:
IF INSERTING THEN ……END IF;
IF UPDATING THEN ……END IF;
IF DELETING THEN……END IF;
Triggers can be used to
• To enforce constraints that cannot be enforced at the DBMS design and
implementation levels.
• To facilitate enforcement of referential integrity.
• Update table values, insert records in tables and call other stored procedures.
• Triggers add functionality by automating critical actions and providing appropriate
warnings and suggestions.
• Triggers add processing power to RDBMS and to database system as a whole.
Oracle recommends triggers for
• Auditing purposes (creating audit logs)
• Automating generation of derived column values.
• Enforcement of business or security constraints.
Page 49
• Creation of replica tables for back up purposes.
Q:What are the various type of triggers?
Statement – level triggers: This type of trigger is executed once, before or after the
triggering statement is completed.
Example of a statement level trigger that is executed after an update of the qoh, pmin
attribute for an existing row or after an insert of a new row in the product table.
CREATE or REPLACE TRIGGER TPR
AFTER INSERT OR UPDATE OF QOH,PMIN ON PRODUCT
BEGIN
UPDATE PRODUCT
SET REORDER =1
WHERE QOH <= PMIN;
END;
/
Row – level triggers: requires the use of the FOR EACH ROW keywords. This type of
trigger is executed once for each row affected. ( if you update 10 rows, the trigger executes
10 times.
Example: CREATE or REPLACE TRIGGER TPR
BEFORE INSERT OR UPDATE OF QOH, PMIN ON PRODUCT
FOR EACH ROW
BEGIN
IF :NEW.QOH <= :NEW.PMIN THEN
:NEW.REORDER :=1;
ELSE
:NEW.REORDER :=0;
END IF;
END;
/
What are Stored Procedures? Explain?
A stored procedure is a named group of SQL statements that have been previously created
and stored in the server database.
Advantages:
• Stored procedures accept input parameters so that a single procedure can be
used over the network by several clients using different input data.
• Stored procedures reduce network traffic and improve performance.
• Stored procedures can be used to help ensure the integrity of the database.
• Stored procedures help reduce code duplication by means of code isolation and code
sharing, there by minimizing the chance of errors and the cost of application
development and maintenance.
• Stored procedures are useful to encapsulate shared code to represent business
transactions i.e, you need not know the name of newly added attribute and would need
to add new parameter to the procedure call.
Syntax to create procedure:
CREATE OR REPLACE PROCEDURE procedure_name [(argument [in/out] data-type,….)]
[IS / AS] [variable_name data-type [:= initial_value]]
BEGIN
PL/SQL or SQL statements;
…
END;
Page 50
Syntax to execute a stored procedure
EXEC procedure_name[(parameter_list)];
Ex: Write a stored procedure to assign an additional 5 % discount for all products when the
QOH = 2PMIN
CREATE OR REPLACE PROCEDURE prod_discount AS
BEGIN
UPDATE product
SET discount = discount + .05
WHERE qoh >= pmin * 2;
DBMS_OUTPUT.PUT_LINE(‘*** Update Finished ***’);
END;
/
1. argument specifies the parameters that are passed to the stored procedures. A stored
procedure could have zero or more arguments.
2. IN/OUT indicates whether the parameter is for input, output or both.
3. Variables can be declared between the keywords IS and BEGIN.
To make percentage increase an input variable in the above procedure---
CREATE OR REPLACE PROCEDURE prod_discount ( pd IN NUMBER)
AS BEGIN
IF ((pd <= 0) OR (pd >= 1)) THEN
DBMS_OUTPUT.PUT_LINE(‘Error value must be greater than 0 and less than 1’);
ELSE
UPDATE product
SET discount = discount + .05
WHERE qoh >= pmin * 2;
DBMS_OUTPUT.PUT_LINE(‘*** Update Finished ***’);
END IF;
END;
/
To execute the above procedure---
EXEC prod_discount(.05);
Q: write a stored procedure to add new customer.
CREATE OR REPLACE PROCEDURE cadd (w_cname IN VARCHAR2, w_city IN
VARCHAR2)
AS
BEGIN
INSERT INTO customer (cno, cname, city) values(CSEQ1.NEXTVAL, w_cname,
w_city);
DBMS_OUTPUT.PUT_LINE(‘Customer added ’);
END;
/
The procedure uses
• several parameters one for each required attribute in the CUSTOMER table.
• CSEQ1 sequence to generate a new customer code.
The parameters can be null only when the table specifications permit null for that parameter.
To execute:
EXEC cadd(‘KALA’, ‘VJA’,NULL);
Page 51
Q: Write procedures to add new invoice and line row.
Ans:
CREATE OR REPLACE PROCEDURE invadd(w_cno IN NUMBER, w_date IN DATE)
AS BEGIN
INSERT INTO invoice
VALUES(ISEQ.NEXTVAL, w_cno, w_date);
DBMS_OUTPUT.PUT_LINE(‘Invoice Added’);
END;
/
CREATE OR REPLACE PROCEDURE lineadd (ln IN CHAR, pn IN CHAR, lu IN
NUMBER)
AS
lp NUMBER := 0;
BEGIN
SELECT price INTO lp
FROM product
WHERE pno = pn ;
INSERT INTO line VALUES(ISEQ. CURRVAL, ln, pn, lu, lp);
DBMS_OUTPUT.PUT_LINE(‘Invoice Line Added’);
END;
/
Q: What is a cursor? How many types of cursors are there? How to handle cursors?
Ans:Cursor is reserved area in memory in which output of the query is stored,
like an array holding rows and columns.
There are two types of cursors: implicit and explicit.
An implicit cursor is automatically created in PL/SQL when the SQL statement returns only
one value.
An explicit cursor is created to hold the output of an SQL statement that may return two or
more rows.(but could return 0 or only one row)
To create an explicit cursor, use the following syntax inside PL/SQL DECLARE section.
CURSOR cursor_name IS select-query;
The cursor declaration section only reserves a named memory area for the cursor.
Once you declared a cursor, you can use cursor processing commands anywhere between the
BEGIN and END keywords of the PL/SQL block.
Cursor Processing Commands
Cursor Command Explanation
OPEN Executes the SQL command and populates the cursor with data
Before you can use a cursor, you need to open it Ex: OPEN
cursor_name.
FETCH To retrieve data from the cursor and copy it to the PL/SQL variables.
The syntax is : FETCH cursor_name INTO variable1 [,variable2,
…..]
CLOSE The CLOSE command closes the cursor for processing
Cursor style processing involves retrieving data from the cursor one row at a time.
The set of rows the cursor holds is called the active set.
The data set contains a current row pointer.
Therefore after opening a cursor, the current row is the first row of the cursor.
Page 52
When you fetch a row from the cursor, the data from the current row in the cursor is copied to
the pl/sql variables. After the fetch, the current row pointer moves to the next row in the set
and continues until it reaches the end of the cursor.
Cursor Attributes determine when you reached the end of the cursor data set, number of
rows in cursor etc…
Attribute Description
%ROWCOUNT Returns the number of rows fetched so far.
If the cursor is not OPEN, it returns an ERROR.
If no fetch has been done but the cursor is OPEN, it returns 0.
%FOUND Returns TRUE if the last FETCH returned a row and FALSE if not.
If the cursor is not OPEN, it returns an ERROR.
If no fetch has been done, it contains NULL.
%NOTFOUND Returns TRUE if the last FETCH did not return any row and FALSE if it
did.
If the cursor is not OPEN, it returns an ERROR.
If no fetch has been done, it contains NULL.
%ISOPEN Returns TRUE if the cursor is OPEN or FALSE if the cursor is CLOSED.
CREATE OR REPLACE PROCEDURE pce IS
p product.pno%TYPE;
desc product.pdesc%TYPE;
tot NUMBER(3);
CURSOR pc IS
SELECT pno, pdesc FROM product
WHERE qoh > (SELECT AVG(qoh) FROM product);
BEGIN
DBMS_OUTPUT.PUT_LINE(‘PRODUCTS WITH QOH > AVG(QOH)’);
OPEN pc;
LOOP
FETCH pc INTO p,desc;
EXIT WHEN pc%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(p||’ => ‘||desc);
END LOOP;
DBMS_OUTPUT.PUT_LINE(‘TOTAL PRODUCT PROCESSED ‘||pc%ROWCOUNT);
CLOSE pc;
END;
/
Page 53
Unit-III Chapter –III Database Design
Q: What is an information system?
Ans: A complete information system is composed of people, hardware, software, the
databases, application programs and procedures.
The process of creating an information system is known as system development.
Q: The system development life cycle(SDLC)
The SDLC is an iterative rather than a sequential process.
1. Planning:
The SDLC planning phase yields a general overview of the company and its objectives.
An initial assessment of the information flow-and-extent requirements must be made to
answer questions like
Should the existing system be continued?
Should the existing system be modified?
Should the existing system be replaced?
If it is decided that a new system is necessary, then it is checked whether the new system is
feasible or not. The feasibility study includes
1. Technical feasibility: Can the development of the new system be done with current
equipment, existing software technology, and available personnel? Does it require
new technology?
2. Economic feasibility: Can we afford it? Is it a million dollar solution for a thousand
dollar problem?
3. Operational feasibility: Does the company possess the human, technical and
financial resources to keep the system operational? Will there be resistance from
users?
2.Analysis:
A thorough audit of user requirements and
understanding of system’s functional areas, actual and potential problems and opportunities.
The logical design must specify the appropriate conceptual data model, inputs, processes and
expected output requirements using tools such as DFDs,ER diagrams etc..
All data transformations (processes) are described and documented using such system
analysis tools.
3.Detailed System design
The design includes all the necessary technical specifications for the screens, menus, reports
and other devices that might be used to help make the system more efficient information
generator.
4. Implementation
The hardware, DBMS software and application programs are installed and the
database design is implemented.
During the intial stages of implementation phase, the system enters into a cycle of coding,
testing and debugging until it is ready to be delivered.
The system will be in full operation by the end of this phase but will be continuously
evaluated and fine-tuned.
5. Maintenance
Maintenance includes all the activity after the installation of software that is performed to
keep the system operational.
Major forms of maintenance activities are
fixing of errors fall under corrective maintenance.
Adaptive maintenance due to changes in the business environment.
Perfective maintenance to enhance the system.
Page 54
Page 55
Page 56
Page 57
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms
Dbms

More Related Content

What's hot

Relational database management systems
Relational database management systemsRelational database management systems
Relational database management systems
Datasoft Consulting
 
Computer based information system
Computer based information systemComputer based information system
Computer based information system
shoaibzaheer1
 
Mis chapter 7 database systems
Mis chapter 7 database systemsMis chapter 7 database systems
Mis chapter 7 database systems
Filmon Habtemichael Tesfai
 
DATABASE MANAGEMENT
DATABASE MANAGEMENTDATABASE MANAGEMENT
DATABASE MANAGEMENT
MiXvideos
 
Clifford sugerman
Clifford sugermanClifford sugerman
Clifford sugerman
Michael Snowhite
 
Information systems and its components 1
Information systems and its components 1Information systems and its components 1
Information systems and its components 1
Ashish Desai
 
Cibm work shop 2chapter six
Cibm  work shop 2chapter sixCibm  work shop 2chapter six
Cibm work shop 2chapter sixShaheen Khan
 
Lesson02 database system architecture
Lesson02 database system architectureLesson02 database system architecture
Lesson02 database system architecture
Muhammad Sikandar Mustafa
 
Database management system
Database management systemDatabase management system
Database management system
Faizan Shabbir
 
Mis classification By Ram K Paliwal
Mis classification By Ram K PaliwalMis classification By Ram K Paliwal
Mis classification By Ram K Paliwal
Ram Paliwal
 
Traditional vs modern dbms
Traditional vs modern dbmsTraditional vs modern dbms
Traditional vs modern dbms
AYUGUPTA98
 
Computer based information system
Computer based information systemComputer based information system
Computer based information system
pooja singhal
 
SA Chapter 2
SA Chapter 2SA Chapter 2
SA Chapter 2
Nuth Otanasap
 
Assignment of database
Assignment of databaseAssignment of database
Assignment of databasera na
 
Data Processing
Data ProcessingData Processing
DBMS Basics
DBMS BasicsDBMS Basics
DBMS Basics
Jaipal Dhobale
 
Lesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementLesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementAngel G Diaz
 

What's hot (19)

Relational database management systems
Relational database management systemsRelational database management systems
Relational database management systems
 
Lec01
Lec01Lec01
Lec01
 
Computer based information system
Computer based information systemComputer based information system
Computer based information system
 
Mis chapter 7 database systems
Mis chapter 7 database systemsMis chapter 7 database systems
Mis chapter 7 database systems
 
DATABASE MANAGEMENT
DATABASE MANAGEMENTDATABASE MANAGEMENT
DATABASE MANAGEMENT
 
Clifford sugerman
Clifford sugermanClifford sugerman
Clifford sugerman
 
Information systems and its components 1
Information systems and its components 1Information systems and its components 1
Information systems and its components 1
 
Cibm work shop 2chapter six
Cibm  work shop 2chapter sixCibm  work shop 2chapter six
Cibm work shop 2chapter six
 
Lesson02 database system architecture
Lesson02 database system architectureLesson02 database system architecture
Lesson02 database system architecture
 
Database management system
Database management systemDatabase management system
Database management system
 
Mis classification By Ram K Paliwal
Mis classification By Ram K PaliwalMis classification By Ram K Paliwal
Mis classification By Ram K Paliwal
 
Traditional vs modern dbms
Traditional vs modern dbmsTraditional vs modern dbms
Traditional vs modern dbms
 
Computer based information system
Computer based information systemComputer based information system
Computer based information system
 
SA Chapter 2
SA Chapter 2SA Chapter 2
SA Chapter 2
 
Types dbms
Types dbmsTypes dbms
Types dbms
 
Assignment of database
Assignment of databaseAssignment of database
Assignment of database
 
Data Processing
Data ProcessingData Processing
Data Processing
 
DBMS Basics
DBMS BasicsDBMS Basics
DBMS Basics
 
Lesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementLesson - 02 Network Design and Management
Lesson - 02 Network Design and Management
 

Similar to Dbms

Chap1-Introduction to database systems.ppt
Chap1-Introduction to database systems.pptChap1-Introduction to database systems.ppt
Chap1-Introduction to database systems.ppt
LisaMalar
 
Unit3rd
Unit3rdUnit3rd
D I T211 Chapter 1
D I T211    Chapter 1D I T211    Chapter 1
D I T211 Chapter 1askme
 
D I T211 Chapter 1 1
D I T211    Chapter 1 1D I T211    Chapter 1 1
D I T211 Chapter 1 1askme
 
Unit1 dbms
Unit1 dbmsUnit1 dbms
Unit1 dbms
gowrivageesan87
 
Components and Advantages of DBMS
Components and Advantages of DBMSComponents and Advantages of DBMS
Components and Advantages of DBMS
Shubham Joon
 
DBMS Part1.pptx
DBMS Part1.pptxDBMS Part1.pptx
DBMS Part1.pptx
Prof. Dr. K. Adisesha
 
DBMS.pptx
DBMS.pptxDBMS.pptx
dbms Lesson for the Engineering students.pdf
dbms Lesson for the Engineering students.pdfdbms Lesson for the Engineering students.pdf
dbms Lesson for the Engineering students.pdf
hpndegreecollegesklm
 
Unit 1: Introduction to DBMS Unit 1 Complete
Unit 1: Introduction to DBMS Unit 1 CompleteUnit 1: Introduction to DBMS Unit 1 Complete
Unit 1: Introduction to DBMS Unit 1 Complete
Raj vardhan
 
data base management report
data base management report data base management report
data base management report
shivam tripathi
 
DBMS_UNIT_1.pdf
DBMS_UNIT_1.pdfDBMS_UNIT_1.pdf
DBMS_UNIT_1.pdf
Koteswari Kasireddy
 
Chapter 1. Itroduction To DBMS -.pptx
Chapter 1. Itroduction To DBMS -.pptxChapter 1. Itroduction To DBMS -.pptx
Chapter 1. Itroduction To DBMS -.pptx
santosh96234
 
Ch1
Ch1Ch1
Ch1CAG
 
1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMSkoolkampus
 
Database Management and it is definition
Database Management and it is definitionDatabase Management and it is definition
Database Management and it is definition
Rashed Barakzai
 

Similar to Dbms (20)

Chap1-Introduction to database systems.ppt
Chap1-Introduction to database systems.pptChap1-Introduction to database systems.ppt
Chap1-Introduction to database systems.ppt
 
Unit3rd
Unit3rdUnit3rd
Unit3rd
 
D I T211 Chapter 1
D I T211    Chapter 1D I T211    Chapter 1
D I T211 Chapter 1
 
D I T211 Chapter 1 1
D I T211    Chapter 1 1D I T211    Chapter 1 1
D I T211 Chapter 1 1
 
Dbms
DbmsDbms
Dbms
 
Unit1 dbms
Unit1 dbmsUnit1 dbms
Unit1 dbms
 
Components and Advantages of DBMS
Components and Advantages of DBMSComponents and Advantages of DBMS
Components and Advantages of DBMS
 
DBMS NOTES.pdf
DBMS  NOTES.pdfDBMS  NOTES.pdf
DBMS NOTES.pdf
 
DBMS Part1.pptx
DBMS Part1.pptxDBMS Part1.pptx
DBMS Part1.pptx
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
 
dbms Lesson for the Engineering students.pdf
dbms Lesson for the Engineering students.pdfdbms Lesson for the Engineering students.pdf
dbms Lesson for the Engineering students.pdf
 
Unit 1: Introduction to DBMS Unit 1 Complete
Unit 1: Introduction to DBMS Unit 1 CompleteUnit 1: Introduction to DBMS Unit 1 Complete
Unit 1: Introduction to DBMS Unit 1 Complete
 
data base management report
data base management report data base management report
data base management report
 
DBMS_UNIT_1.pdf
DBMS_UNIT_1.pdfDBMS_UNIT_1.pdf
DBMS_UNIT_1.pdf
 
Chapter 1. Itroduction To DBMS -.pptx
Chapter 1. Itroduction To DBMS -.pptxChapter 1. Itroduction To DBMS -.pptx
Chapter 1. Itroduction To DBMS -.pptx
 
Ch1
Ch1Ch1
Ch1
 
Ch1
Ch1Ch1
Ch1
 
Ch1 2
Ch1 2Ch1 2
Ch1 2
 
1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
 
Database Management and it is definition
Database Management and it is definitionDatabase Management and it is definition
Database Management and it is definition
 

More from Lakshmi Sarvani Videla

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
Lakshmi Sarvani Videla
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
Lakshmi Sarvani Videla
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
Lakshmi Sarvani Videla
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
Lakshmi Sarvani Videla
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
Lakshmi Sarvani Videla
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
Lakshmi Sarvani Videla
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
Lakshmi Sarvani Videla
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
Lakshmi Sarvani Videla
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Lakshmi Sarvani Videla
 
Functions
FunctionsFunctions
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
Lakshmi Sarvani Videla
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
Lakshmi Sarvani Videla
 
Graphs
GraphsGraphs
B trees
B treesB trees
Functions in python3
Functions in python3Functions in python3
Functions in python3
Lakshmi Sarvani Videla
 
Dictionary
DictionaryDictionary
Sets
SetsSets
Lists
ListsLists
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 

More from Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
Functions
FunctionsFunctions
Functions
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
 
Graphs
GraphsGraphs
Graphs
 
B trees
B treesB trees
B trees
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
 
Dictionary
DictionaryDictionary
Dictionary
 
Sets
SetsSets
Sets
 
Lists
ListsLists
Lists
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 

Recently uploaded

De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 

Recently uploaded (20)

De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 

Dbms

  • 1. UNIT-I Chapter-I : DATABASE SYSTEMS Data: Data consists of raw facts, which the computer stores and reads. Data can consist of letters, numbers, sounds or images etc. that have some meaning in the user environment. Data are the raw material from which information is generated. Information: When data has been processed to give it more meaning, it is called as information. Database: An organized collection of logically related data usually designed to meet the information needs of multiple users in an organization. Database Management System: (DBMS) is a software tool used to define, create, maintain and provide controlled access to the database. DBMS software stores data structures, relationship between those structures and the access paths to those structures in a central location. Q) How the data is organized within a database? Ans: To help you visualize how a database stores data, think about a typical address book. Fields: Each field contains a specific type of information such as first name, last name, phone number, email etc… Records: Record is a collection of related fields. Ex: All information about one person in an address book. Tables: A complete collection of records makes a table Ex: Contacts table FirstName LastName Company Address City State Pincode Record1 Record2 Q) Why the database is important? Ans: If you keep list of all your business customers in a database, you can • You can sort the customers by pincode. • Create a simple onscreen entry form that even your technically unskilled employee can use successfully You can manipulate data in almost anyway you want. Files, File Systems & Problems With File System Data Management. Manual filing system works well when the number of items stored is quite small and they are only needed to be stored and retrieved. A manual filing system crashes when cross referencing and processing of information in the files is carried out. Limitations or disadvantages of File Processing Systems. Program data dependence: File descriptions are stored within each program that access a given file. In the invoicing system program access both the inventory pricing file and the customer master file. Page 1
  • 2. Therefore this program contains a detailed description for both these files.In the below figure both the customer master file is contained in both the order filing system and invoicing system. Suppose it is decided to change the customer address field length in the records in customer master file from 20 to 30 characters. For this, each related program have to be modified. Duplication of data or Redundancy of data: in the below figure, order filing system contains the inventory master file, the invoicing system contains inventory pricing file. Inventory master, inventory pricing file contains product descriptions and quantity. There is duplication of data which requires additional storage space. Orders Department Accounting Department Inconsistent data: The redundancy in storing the same data multiple times leads to data inconsistency when an update is applied to some of files but not to other. Limited data sharing: In the file processing system, users have little opportunity to shared data outside their applications. Lengthy development times: Developing an application by using the file systems is very skilled activity. The programmers has to write many programs for supporting file opening, file closing and iterative logic for representing operations, this is very lengthy process. Incompatible file formats: Since the structure of files is embedded in application, the structure is dependent on application programming languages. Ex: structure of file generated by COBOL is different from ‘C’ programming language. The application programmer has to develop software to convert the files to some common format for processing. This may be time consuming and expensive. Fixed Queries: Any query or report needed by organization has to be developed by the application programmer. Lack of security: All users could see all data and no security and authorization subsystem. No recovery and back up system:Data could be lost in case of hardware or software failure. All the data is stored in disk files and accessed according to access methods (sequential, direct etc..) provided by file system and chosen by application programmer. Page 2 Progra m A Progra m B Progra m C Order File System Customer Master File Inventory Master File Back Order File Progra m A Progra m B Invoicing System Inventory Pricing File Customer Master File
  • 3. Order Programs Accounting Programs Payroll Programs DBMS Database Customer master data Inventory master data Employee master data Back order data Q: What is a database system and What are the advantages of database systems? Database System: Database and DBMS software together is called a database system. Program data independence: DBMS allows certain types of changes to the structure of the database without affecting the stored data and the existing application. Improved data sharing: The DBMS helps create an environment in which end users have better access to more data and better managed data. Improved data security: The DBA uses security and authorization subsystem provided by DBMS to create accounts and to specify account restrictions. The DBMS will enforce these restrictions automatically. Better Data Integration: DBMS promotes and enforces integrity rules, thus minimizing data redundancy and maximizing data consistency. Minimized data inconsistency: Data inconsistency is also reduced in a properly designed database as such a database doesn’t allow different versions of same data in different places. Ex: company’s sales department stores salesman name as ‘Bill Brown’ and the same person name is stored as ‘William G Brown’ in company’s HR department. Improved Data access: A query is a question or specified request issued to DBMS for data manipulation. Example of query language is SQL An Adhoc query is a spur of the moment question. The DBMS sends back an answer ( called the query result set) to the application For Ex: How many of our cutomer have balances of Rs. 3000 or more? The DBMS gives quick answers to adhoc queries. Improved Decision Making: Better managed data and improved data access makes it possible to generate better quality information on which better decisions are based. Increased end- user productivity: The availability of data and tools that transform data into information allows end user to make quick decisions that can make the difference between success and failure in global economy. Page 3
  • 4. Database system environment: Database system environment is made up of five major parts. They are hardware, software, people, procedures and data. Hardware: Hardware refers to all of the system’s physical devices. For ex: computers (micro computers, servers etc..), storage devices, printers, networking devices (hubs, switches etc…) and other devices ( ATMS, ID readers etc..) Software: Three types softwares are needed to make the database system function fully. 1. Operating system software: manages all hardware components and makes it possible for all other software to run on the computers. Ex: UNIX, Microsoft windows. 2. DBMS Software: manages the data within the database system. Ex: SQL server, Oracle, DB2, My SQL 3. Application programs and utility software: are most commonly used to access data found within the database to generate reports, tabulations and other information for decision making. For Ex: All DBMS vendors provide GUI’s to create database structures, control database access and monitor database operations. 4. People includes all users of database system. On the basis of their job functions, five types of users can be identified. 1. System administrators: looks after database system general operations. 2. Database administrators (DBA) manages the DBMS and ensures that the database is functioning properly. 3. Database Designers or Database architects design database structure. The determination of what data are to be entered into the database and how the data are to be organized is an important part of database designer’s job. 4. System analysts and programmers design and implement the application programs. They design and create the data entry screens, reports and procedures through which end users access and manipulate the database’s data. 5. End users are the people who use the application programs to run the organisation’s daily operations. For Ex: clerks, managers, supervisors and directors. High level end users uses the information obtained from the database to make decisions. 5. Procedures: Procedures play an important role in a company. They enforce the standards by which the business is conducted within the organization and with customers. Procedures are also used to ensure that there is an organized way to monitor and audit both the data that enter the database and the information generated through the use of that data. 6. Data: are the raw materials from which information is generated. Data covers the collection of facts stored in the database. DBMS Functions: DBMS performs several functions that guarantee the integrity and consistency of the data in database. They are 1. Data dictionary management: The DBMS stores definitions of the data elements and their relationships in a data dictionary. The DBMS provide data abstractions and it removes structural and data dependency from the system. 2. Data Storage management: The DBMS creates and manages the complex structures required for data storage, thus you need not define and program the physical data characteristics. It also provide storage for on-screen definitions, report definitions, data validation etc.. 3. Data transformation and presentation: The DBMS must manage the data in proper format for each country while entering dates, names etc... must not allow different Page 4
  • 5. versions of same data in different places. Ex: company’s sales department stores salesman name as ‘Bill Brown’ and the same person name is stored as ‘William G Brown’ in company’s HR department 4. Security Management: DBMS creates a security system that enforces user security and data privacy. Security rules determine which users can access the database, which data items each user can access? , which data operations (read, add, delete or modify) the user can perform. This is important in multi user database system. 5. Multi user access control: The DBMS uses sophisticated algorithms to ensure that multiple users can access the database at the same time. 6. Back up and recovery management: DBMS provide special utilities that allow DBA to perform back up and restore procedures. Recovery management deals with the recovery of database after a failure, such as bad sector in disk or power failure. 7. Data integrity management: DBMS promotes and enforces integrity rules, thus minimizing data redundancy and maximizing data consistency. 8. Data access languages and application programming interfaces: The DBMS provide access through a query language. A query language is a non procedural language that lets the user specify what must be done without having to specify how it is to be done. Example of query language is SQL 9. Database communication interface: DBMS accepts end user requests from multiple, different network environments. Disadvantages of DBMS 1. Increased Costs: Database system requires hardware, software and highly skilled people. The cost of maintaining these. 2. Management Complexities: The database system hold important data that are accessed from multiple sources, security issues may occur. 3. Frequent Updates: must perform frequent updates an d apply latest patches and security measures to all components. These increases personnel training costs. 4. Vendor Dependence: due to heavy investment in technology and personnel training, companies do not change database vendors. As a result, vendors donot offer pricing point advantages to existing customers. 5. Frequent Updates / Replacement Cycles: DBMS vendors frequently upgrade their products by adding new functionality i.e, upgrade versions of software. Some of these versions require hardware upgrades and training to users costs money. Page 5
  • 6. UNIT-I Chapter-II Data modeling and data models Q: What is a data model? Ans: Data model is blue print containing all the instructions to build a database that will meet all the end –user requirements. This blue print contains both text descriptions in plain, unambiguous language and clear useful diagrams depicting the main data elements. Q: Explain The importance of data models? Ans:Data models are communication tool that enables interaction among the designer, the application programmer and end user. Data models are used to represent real world data and how the different degrees of data abstraction enables data modeling. Ex: a house blue print is an abstraction; you cannot live in a blue print, Similarly the data model is an abstraction, you cannot draw the required data out of the data model. As you cannot build a perfect house without blue print, you cannot create a good database without creating an appropriate data model. Q:What are Business Rules? Ans: Business rule is a description of policy, procedure within a specific organization. Properly written business rules are used to define entities, attributes, relationships and constraints. Example1: Consider 2 business rules • A customer may generate many invoices. • An invoice is generated by only one customer. These business rules establish 2 entities (CUSTOMER and INVOICE) and a 1:M relationship. Example 2: A business rule is as follows • A training session cannot be scheduled for <10 employees or for >30 employees This rule establishes a constraint (not <10 employees or for >30 employees) , two entities (EMPLOYEE and TRAINING) and a relationship between these entities. Q: How to Discover Business Rules Ans: The main sources of business rules are • company managers, • policy makers, • department managers and • written documentation such as a company’s procedures, standards or operation manuals, • direct interviews with end users. The process of identifying and documenting business rules is essential to database design for several reasons. • They help standardize the company’s view of data • They allow designer to develop relationship participation rules and constraints and to create an accurate data model. Q: Why not all business rules can be modeled? Ans: For ex: No pilot can fly more than 10 hours within 24- hour period . Such a business rule can be enforced by application software and not by database design. Page 6
  • 7. Q: Explain about hierarchical model? Ans: Its structure is represented by an upside – down tree. The hierarchical structure contains levels or segments. Within the hierarchy, the top layer (also called root) is the parent of the segment directly beneath it. Advantages: 1. It promotes data sharing. 2. Parent child relationship promotes conceptual simplicity and data integrity. 3. Database security is provided and enforced by DBMS. 4. It is efficient with 1:M relationships. Disadvantages: • Complex to implement and difficult to manage as it requires knowledge of physical data storage characteristics. • Can implement only 1:M relationships. So it has implementation limitations. • No standards. • No DDL and DML language in the DBMS. • Lacks structural independence. Changes in structure require changes in all application programs. • No adhoc queries • Access paths predefined This technology is best applied when conceptual model also resembles a tree and most data accesses begin with the same root file. Q: Explain about network model? Ans: Network model allows a record to have more than one parent. Advantages: • It can handle M:N and multi parent relationship types. • Data access is more flexible • There are standards defined to implement this model. • It includes DDL and DML commands in DBMS Disadvantages: • Little data independence. • Lacks structural independence. Changes in structure require changes in all application programs. • No adhoc queries • Access paths predefined Q: What is CODASYL and DBTG? Ans: To help establish database standards, the conference on data systems languages (CODASYL) created Database Task Group (DBTG) in late 1960s. The final DBTG report contained specifications for 3 crucial database components. The schema is the conceptual organization of the entire database as viewed by DBA The subschema defines the portion of the database as seen by the application programs. The application programs invoke the subschema required to access the appropriate database file. A data management language that defines the environment in which data can be managed. Page 7
  • 8. Q: Explain about The Relational Model ? Ans: Here tables are called as “Relations” Rows are called “Tuples” and column names as “attributes”. Every attribute has a domain. A domain is set of permissible values that can be given to an attribute. A common attribute existing in any two tables creates a relationship between the tables. It supports relationship types (1:1, 1: M or M: N) The RDBMS manages all the physical details, while the user sees the relational database as collection of tables. (it enables you to view data logically rather than physically.) The RDBMS uses SQL to translate user queries into instructions for retrieving the required data. The SQL engine executes all queries. Advantages • Promotes data and structural independence. • Tabular view improves conceptual simplicity. • Adhoc query capability is based on SQL • RDBMS isolates end user from physical level details. Disadvantages: • RDBMS requires substantial hardware and software overhead. • Conceptual simplicity gives untrained people the tools to use good system poorly. • It may produce islands of information problems as individuals and departments can easily develop their own applications. Q: Explain about The Entity Relationship Model? Ans: ER models are normally represented in an entity relationship diagram (ERD) The ER model is based on the following components: Entity: Entity is anything about which data are to be collected and stored Attribute: Attributes are characteristics of entities. Relationship:A relationship is an association between entities. Advantages: • Visual modeling yields conceptual simplicity. • Visual representation makes it an effective communication tool. • It can be integrated with dominant relational model. Disadvantages • There is limited constraint representation. • There is limited relationship representation. • There is no data manipulation language. Q: Explain the various notations used with ERDs ? Ans: The various notations used with ERDs are • The chen notation favors conceptual simplicity. • The crow’s foot notation favors implementation – oriented approach. • The UML notation can be used for both conceptual and implementation modeling. Q: Explain about Object Oriented model? Ans: In this model both the data and their relationships are contained in a single structure known as an Object. Page 8
  • 9. Object includes information about relationships between facts within the object and relationships with other objects. The OODM is the basis of OODBMS The OODM is said to be semantic data model because semantic indicated meaning. The object oriented data model is based on the following components • An object is an abstraction of a real-world entity. • Attributes describe the properties of an object. • Objects that share similar characteristics are grouped in classes. • A class is a collection of similar objects with shared structure (attributes) and behaviour (methods) (where as entities do not have methods) • Classes are organized in class hierarchy (which represents an upside – down tree in which each class has only one parent) • Inheritance is the ability of an object within class hierarchy to inherit the attributes and methods of the classes above it. Object oriented data models are depicted using UML diagrams. Advantages: • Semantic content is added • Visual representation includes semantic content. • Inheritance promotes data integrity. Disadvantages: • No widely accepted standard. • It is a complex navigational system. • There is a steep learning curve. • High system overhead slows transaction. Q) Distinguish between Logical and Physical data independence. Logical Data Independence: Logical data independence is the ability to modify the conceptual schema without having alteration in external schemas or application programs. Alterations in the conceptual schema may include addition or deletion of fresh entities, attributes or relationships and should be possible without having alteration to existing external schemas or having to rewrite application programs. Physical Data Independence: Physical data independence is the ability to modify the internal schema without having alteration to the conceptual schemas or application programs. Alteration in the internal schema might include. * Using new storage devices. * Using different data structures. * Switching from one access method to another. * Using different file organizations or storage structures. * Modifying indexes. Page 9
  • 10. Explain about the Conceptual, Internal and external and Physical Model (Or) Explain about different levels of data abstraction (Or) Explain about three schema architecture. Ans: The Conceptual Model 1. The conceptual model represents a global view of the organization’s data as viewed by all end-users. 2. It describes all entities and their attributes, the relationships among these entities and the constraints on these relationships. 3. The conceptual model forms the basis for the conceptual schema - a description of the database structure. 4. The conceptual model is independent of both software (DBMS and OS) and hardware. 5. The E-R model is the most widely used to represent conceptual model The Internal Model Page 10
  • 11. 1. The internal model adapts the conceptual model to a specific DBMS (e.g., hierarchical, network, and relational). 2. The internal model is software-dependent but hardware-independent. 3. Development of the internal model is especially important to hierarchical and network database models. The External Model 1. The external model is the end user’s/ applications programmer’s view (local view) of the database . 2. It is concerned about a specific business operation. 3. It is implemented through the CREATE VIEW command in SQL. Benefits of the external model • Application program development is simplified because the programmer does not have to be concerned about data not relevant to his/her application. • Communication with the end-user is simplified. • Identification of data required to support each business operation is simplified. • Access control and security can be easily implemented. Page 11
  • 12. The Physical Model • The physical model operates at the lowest level of abstraction, describing the way data is stored on storage media such as disks or tapes. • It requires the definition of physical storage devices and the access methods required to reach the data. • The physical model is both software and hardware-dependent. Page 12
  • 13. UNIT-I Chapter-III The Relational Database model Explain characteristics of relational table? 1. A table is perceived as a two-dimensional structure composed of rows and columns. 2. Each table row (tuple) represents a single entity occurrence within the entity set. 3. Each table column represents an attribute, and each column has a distinct name. 4. Each row/column intersection represent a single data value. 5. All values in a column must conform to the same data format. 6. Each column has a specific range of values known as the domain of that attribute. Example: The domain for the gender attribute consists of only two possibilities: M or F. The domain for a company’s date of hire attribute consists of all dates (from start up date to current date) Attribute may share a domain. For ex: a student address and a professor address share the same domain of all possible addresses. 7. The order of rows and columns is immaterial to the DBMS 8. Each table must have an attribute or a combination of attributes that uniquely identifies each row. Ex: Roll_No in the STUDENT table What are data types support by most DBMS? Ans: The different data types are 1. Numeric: Numeric data are data on which you can perform arithmetic operations. 2. Character: Character data or text data or string data can contain any character, symbol or digit not intended for mathematical manipulations. 3. Date: Date attributes contain calendar dates stored in special format known as the julian date format. Logical: Logical data can have only a true or false (yes or no) condition. What is data dictionary? Ans: The data dictionary provides detailed descriptions of all tables and so contains all of attributes names,characteristics and structure of each table in the system. What is system catalog? Ans: it is a detailed system data dictionary that describes all objects within the database, including data about table names, table’s creator etc.. The system catalog is a system – created database whose tables store the user created database characteristics and content. These tables can be queried just like user-defined table. Explain about indexes in relational database? Ans: An index is composed of an index key and a set of pointers. An index can be used to retrieve data more efficiently. When you define a table’s primary key, the DBMS automatically creates a unique index on the primary key columns. Page 13
  • 14. What is meant by functional dependence? Ans: The attribute B is functional dependent on A if each value in column A determines one and only one value in column B. Ex: What is composite key? Ans: A key may be composed of more than one attribute. Such a multi- attribute key is known as a composite key. What is meant by fully functional dependency? Ans: If attribute B is functionally dependent on a composite key A but not on any subset of that composite key, the attribute B is fully functionally dependent on A. Explain about various keys used in relational database model? Key Type Definition Example Super key An attribute (or combination of attributes) that uniquely identifies each row in a table. In STUDENT table, the super key could be any of the following: STU_NUM STU_NUM, STU_LNAME Candidate key A minimal (irreducible) super key is a candidate key. A super key that does not contain a subset of attributes that is itself a super key. STU_NUM,STU_LNAME is a super key, but it is not a candidate key because STU_NUM by itself can uniquely identifies each row in the STUDENT table. Primary key A candidate key is selected as a primary key. It cannot contain NULL values If employee’s PAN number has been included as one of the attribute in the EMPLOYEE table. EMP_NUM and EMP_PAN are both candidate keys because both uniquely identifies each employee. Selection of EMP_NUM as primary key would be designer’s choice. Secondary key An attribute or combination of attributes used strictly for data retrieval purposes Most of the time if I need city wise customers list from CUSTOMER table, I can place a secondary key on CUS_CITY column to get a speed reply. Foreign key An attribute in one table whose values must either match the primary key in another table or be null. Q: What is a constraint? Write short notes on integrity constraints/ rules with example? Ans: A constraint is a restriction placed upon the data values that can be stored in a column or columns of a table. Integrity Constraint are of 2 types 1. Entity integrity constraint 2. Referential integrity constraint Entity integrity : All primary key entries are unique and no part of a primary key may be null. Referential integrity: A foreign key may have either a null entry, as long as it is not part of its tables primary key or an entry that matches the primary key value in a table to which it is related. (Every non- null foreign key value must reference an existing primary key value.) Page 14
  • 15. Example: Table name: AGENT Primary key: AGENT_CODE Foreign Key: none AGENT_CODE AGENT_FNAME AGENT_PHONE A01 ANU 2475258 A02 RAM 2465258 Table Name: CUSTOMER Primary Key: CUS_CODE and Foreign Key: AGENT_CODE CUS_CODE CUS_FNAME AGENT_CODE C01 SWATHI NULL C02 DOLLY A01 C03 RAMA A01 Here the customer swathi is not assigned a agent yet, hence the agent code is NULL. No entry in agent code column in customer table has invalid entry as they reference a valid entry A01 which is anu’s agent code. Also primary keys of both tables contain null values and has unique values. Relational set operators or relational algebra Relational algebra is set of basic operations used to manipulate the data in relational model. These operations can be classified into two categories: 1. Basic set operations: These are When two or more tables share • the same number of columns and • the columns have the same names and • the columns share the same (or compatible) domains the Two tables are said to be union-compatible. UNION: union combines all rows from two tables, excluding duplicate rows. The two tables must be union- compatible. Example: R3=R1U R2 R1 R2 Page 15 Binary operations UNION INTERSECTION SET DIFFERENCE CARTESIAN PRODUCT Relational operations SELECT PROJECT JOIN DIVISION Fname A1 A2 A3 A4 A7
  • 16. yields UNION Intersect: Intersect yields only the rows that appear in both tables. The tables must be union –compatible to yield valid results. Page 16 Fname A1 A2 A3 A4 Fname A1 A7 A2 A4
  • 17. Example: R1 R2 R3=R1 n R2 yields INTERSECT Difference: Difference yields all rows in one table that are not found in the other table. The tables must be union-compatible. Example: R1 R2 R1-R2 =R3 yields DIFFERENCE Cartesian Product: yields all possible pairs of rows from two tables. R3=R1X R2 R1 R2 yields Page 17 Fname A1 A2 A4 Fname A1 A2 A3 A4 Fname A1 A7 A2 A4 Fname A3 Fname A1 A2 A3 A4 Fname A1 A7 A2 A4 Course Fname C1 A1 C1 A2 C1 A3 C2 A1 C2 A2 C2 A3 Course C1 C2 Fname A1 A2 A3
  • 18. Select: Also known as RESTRICT Yields values for all rows found in a table that satisfy a given condition. PRODUCT SELECT only price < $10 yields Project: yields all values for selected attributes. Project yield a vertical subset of a table. PRODUCT PROJECT Price yields Join: A join is used to combine rows from multiple tables. Natural Join links tables by selecting only the rows with common values in common columns. A natural join is a result of a three-stage process. 1.a PRODUCT of the tables is created. 2. a SELECT is performed on the output to yield only the rows for which Acode = Agent_code and these common columns Acode, Cus_code are called as join columns. 3. a PROJECT is performed on the result to include only one join column. Table name: CUSTOMER Table name: AGENT STEP1: product of the above 2 tables yields Cus_code Name Agent_code Acode Name C01 ANU A01 A01 RAJ C01 ANU A01 A02 TAJ Page 18 Pcode Pdesc Price 1 Flash Light 5 2 Lamp 25 3 Battery 7 4 100W Bulb 15 Pcode Pdesc Price 1 Flash Light 5 3 Battery 7 Price 5 25 7 15 Pcode Pdesc Price 1 Flash Light 5 2 Lamp 25 3 Battery 7 4 100W Bulb 15 Cus_code Name Agent_code C01 ANU A01 C02 RANI A02 Acode Name A01 RAJ A02 TAJ
  • 19. C02 RANI A02 A01 RAJ C02 RANI A02 A02 TAJ STEP 2: SELECT rows for which Acode = Agent_code Cus_code Name Agent_code Acode Name C01 ANU A01 A01 RAJ C02 RANI A02 A02 TAJ STEP 3: PROJECT to remove Acode field from the result. Cus_code Name Agent_code Name C01 ANU A01 RAJ C02 RANI A02 TAJ The column on which the join occurs only once in new table. Equi Join: 1. Links tables on the basis of equality condition. 2. Does not eliminate duplicate columns Theta join : if any other comparison operator other than equality is used, the join is called theta join. Left outer join: yields all of the rows in CUSTOMER table, including those that do not have a matching value in AGENT table. Right outer join: yields all of the rows in AGENT table, including those that do not have a matching value in CUSTOMER table. DIVIDE: This operation uses single column table as the divisor and 2-column table as the dividend. The tables must have a common column. DIVIDE YIELDS Page 19 CODE LOC A 5 A 9 B 5 B 3 C 6 CODE A B LOC 5
  • 20. STUDENT STUDENT RollNo STU_LNAME STU_FNAME STU_PHONE UNIT-II Chapter –I Entity Relationship modeling Q: What are E-R Model Components or modules? Ans:Three components: Entities, Attributes, and Relationships. Entity: Entity is anything about which data are to be collected and stored. An entity may be concrete (a person or a book, for example) or abstract (like a holiday or a concept). An entity is represented by a rectangle containing entity’s name. The entity name , a noun, is usually written in all capital letters. Attribute: Attributes are characteristics of entities. For ex: STUDENT entity has the attributes STU_FNAME,STU_PHONE etc. Attributes are represented by ovals and are connected to the entity rectangle with a line. Each oval contains the name of the attribute it represents. Attributes may share a domain. Primary keys are underlined. (here RollNo is the primary key.) Relationship A relationship is an association between entities. Relationships are described as verbs. Relationships are represented by diamond-shaped symbols Q: What are the different Types Of Attributes: Ans: 1. Required and Optional Attributes: Required attribute is an attribute that must have a value, it cannot be left empty. Ex: STU_FNAME, STU_FNAME Optional attribute is an attribute that does not require a value, it can be left empty. Ex: STU_PHONE…all students may or may not have a phone at home. 2.Composite and Simple attributes: A simple attribute cannot be subdivided. Examples: Age, Sex, and Marital status A composite attribute can be further subdivided to yield additional attributes. Examples:ADDRESS into Street, City, State, Zip PHONE NUMBER into Area code, Exchange number 3. Single-Valued and Multivalued Attributes: A single-valued attribute can have only a single value. Examples: A manufactured part can have only one serial number. A multivalued attribute can have many values. Multivalued attributes are shown by a double line connecting to the entity Examples: i) A person may have several college degrees. ii)A household may have several phones with different numbers 4. Derived Attribute and Stored Attribute A derived attribute is not physically stored within the database; its value is computed from other attributes. It is indicated using a dotted line connecting the attribute with the entity. Example: AGE can be derived from DOB and current date. Page 20
  • 21. What is Cardinality ? Ans: Cardinality expresses the minimum and maximum number of entity occurrences associated with one occurrence of the related entity. In the ERD, cardinality is indicated by placing appropriate numbers beside the entities, using the format (x,y). The 1st value represents the minimum number of associated entities, while the 2nd value represents the maximum number of associated entities. These implemented by the application software or by triggers. Q:When can you say an entity is Existence dependent/ independent? Ans: An entity is said to be existence dependent if it can exist in the database only when it is associated with another related entity occurrence. Existence independence: if an entity can exist independently, then it is said to be existence dependent. Q:What is relationship strength? Explain about strong and weak relationships. Ans: Relationship Strength is based on how the primary key of a related entity is defined. They are of 2 types. Weak (Non-identifying) relationship: a weak relationship also known as non-identifying relationship, exists if the entity has a primary key that is not partially or totally derived from the parent entity in the relationship Strong relationship also known as identifying relationship, exists if the entity has a primary key that is partially or totally derived from the parent entity in the relationship What is Weak entity? Ans: A weak entity is one that meets two conditions 1. The entity is existence- dependent. 2. The entity has a primary key that is partially or totally derived from the parent entity in the relationship i.e, Strong relationship. A weak entity id identified by using a double-walled entity rectangle. Ex: DEPENDENT is the weak entity in the relationship EMPLOYEE has DEPENDENT. What is meant by relationship participation? Ans: Participation in an entity relationship is either optional or mandatory. Optional participation means that one entity occurrence does not require a corresponding entity occurrence in a particular relationship. For Ex: in the “COURSE generates CLASS” relationship, there are some courses that do not generate a class. Therefore, the CLASS entity is considered to be optional to the COURSE entity. Mandatory participation means that one entity occurrence require a corresponding entity occurrence in a particular relationship If every COURSE must generate a CLASS then the CLASS entity is considered to be mandatory to the COURSE entity. Types of Relationships A relationship’s degree indicates the number of entities that participate in the relationship. Different types of relationship degrees are : 1. Unary relationship : If an relationship is maintained within a single entity then such relationship is called unary relationship. Example: an employee within the EMPLOYEE entity is the manager for one or more employees within that entity. Page 21
  • 22. when an entity has a relationship with itself then such relationship is called as recursive relationship. 2. Binary Relationship: Binary Relationship exists when two entities are associated in a relationship. Ex: the relationship “a PROFESSOR teaches one or more CLASSes” What is a recursive relationship? Ans: when an entity has a relationship with itself then such relationship is called as recursive relationship. What is an associative or composite or bridge entity? Ans: When there is M:N relationship between two entities then we create a new entity called bridge/composite entity that contains the primary keys of both the entities participating in M:N relationship Ex: Explain database design challenges? Ans: 1. Design Standards: Standards guide one in developing logical structures that reduce data redundancies. Without design standards, it is not possible to design a proper design or evaluate an existing design. 2. Processing Speed: high processing speed are top priority in database design as high processing speed are necessary for many organizations for example: a perfect design must use a 1:1 relationship to avoid nulls, while a higher transaction –speed design might combine the two tables to avoid the use of an additional relationship, using dummy entries to avoid nulls. If the focus is on data-retrieval speed, one must include derived attributes in design. 3. Information requirements: a design that meets all logical requirements is an important goal. The designer should consider end-user requirements such as performance, security, shared access. He must also verify that all update, retrieval and deletion options are available and also all query and reporting requirements. Page 22
  • 23. UNIT-III Chapter-I Introduction to SQL Q:What is SQL and What does SQL do? SQL stands for structured query language. SQL is non-procedural language, therefore you specify what is to be done rather than how is it done. American National Standards Institute (ANSI) prescribed a standard SQL. SQL functions fits into two broad categories: • It is a data definition language:(DDL):-SQL can create databse objects such as tables,indexes and views.SQL can also define access rights to these database objects. • It is a data manipulation language(DML):-SQL can be used to insert,update,delete and retrieve data from the database SQL is easy to learn SQL can retrieve data from database SQL can execute queries SQL queries are used to answer question and also to perform actions such as adding,deleting table rows. Q:Explain various datatypes available in SQL? AnsThe following table shows some common SQL datatypes Datatype Format Comments Numeric NUMBER(L,D) Ex: NUMBER(7,2) indicates number will be stored with two decimal places and may be upto 7 digits long,including the sign and decimal places. INTEGER (OR) INT Cannot be used if you want to store numbers that require decimal places. SMALLINT Limited to integer values upto six digits DECIMAL(L,D) Greater lengths are acceptable, but smaller ones are not. Character CHAR(L) Fixed length character data for upto 255 characters. If you store strings that are not as long as the CHAR parameter value,the remaining spaces are left unused VARCHAR(L) OR VARCHAR2(L) Variable length character data will not leave unused spaces. Date DATE Stores dates in the julian date format. Q: Explain how to create table using SQL? Ans: The CREATE table is used to create a new table in the user database schema. Syntax: CREATE TABLE tablename ( Column1 datatype(column width) [constraints], Column2 datatype(column width) [constraints], …………… ); Page 23
  • 24. Example: CREATE TABLE VENDOR( vno number(3) PRIMARY KEY, vname varchar2(35) NOT NULL, vcity varchar2(15)); If the above command is executed successfully, the message “table created “ is displayed. The following are the rules for naming a table. 1. Table names should start with an alphabet 2. Underscores,numbers and letters are allowed but not blank spaces. 3. Maximun length of table name is 30 characters. 4. Reserved words of ORACLE cannot be used as table name. 5. Two different table should not have the same name. 6. Unique column names should be specified. 7. Proper data types and size should be specified. Q: What are SQL constraints? Explain? Ans: Entity integrity is enforced automatically when the primary key is specified in CREATE TABLE command. For Ex: CREATE TABLE PRODUCT( pno char(3), pdesc varchar2(35) NOT NULL UNIQUE, p_indate date, qoh number(5), price number(5), vno number(3), PRIMARY KEY(pno), FOREIGN KEY(vno) REFERENCES VENDOR ON UPDATE CASCADE); The primary key attribute contains both a NOT NULL and a UNIQUE specification. The foreign key constraint definition ensures that • You cannot delete a vendor from VENDOR table if atleast one PRODUCT row references that VENDOR. • ON UPDATE CASCADE (not supported by ORACLE) ensures that when a change is made in VENDOR table, that change will be reflected automatically in PRODUCT table. Besides the primary key and foreign key constraints, the ANSI SQL standard defines the following constraints. • NOT NULL ensures that a column will not have null values. • UNIQUE ensures that a column will not have duplicate values. • DEFAULT defines a default value for a column(when no value is given). • CHECK validates data in an attribute and sees that a specified condition exists. Ex1: The minimum order must be atleast 10 Ex2:The date must be after APRIL 15, 2011 The CREATE TABLE command lets you define constraints in two different places. • When you create the column definition (known as column constraint) • When you use CONSTRAINT keyword (known as table constraint) A column constraint applies to just one column. A table constraint may apply to many columns. Page 24
  • 25. Q:Explain important data manipulation commands (DML) of SQL? Ans: INSERT: Used to enter data into a table. Syntax: INSERT INTO tablename VALUES (value1,value2,…..valuen) Example: INSERT INTO VENDOR VALUES (100,’RADHA’,’VJA’); Observe that: Character and date values must be entered between apostrophes(‘). Numerical entries are not enclosed in apostrophes(‘). Attribute entries are separated by commas. Inserting Rows with NULL attribute INSERT INTO product VALUES (‘P02’,’PENCIL’,’02-AUG-2011’, 25, 3, NULL); Note that the NULL entry is accepted only because the vno attribute is optional in PRODUCT table. The NOT NULL declaration is not used in the CREATE TAVLE statement for these attributes. Inserting Rows with OPTIONAL attributes: If the data is not available for all columns, then column list must be included following table name. INSERT INTO product(pno,pdesc) VALUES(‘P03’,’MOUSE’) COPYING PARTS OF A TABLE To create a new table based on selected column and rows of an existing table. In this case, the new table will copy the attribute names,data characteristics and rows of original table. CREATE TABLE part AS SELECT pno,pdesc,vno FROM product; Note that no entity integrity(primary key) or referential integrity (foreign key) rules are automatically applied to the new table. Saving the table changes or COMMIT: The COMMIT command permanently saves all changes such as rows added, attributes modified and rows deleted made to any table in the database. Syntax: COMMIT; Any changes made to table contents are not saved on disk until you close the database, close the program you are using, or use the COMMIT command. UPDATE Command: The UPDATE command modifies an attribute value in one or more table rows. Allows you to make data entries in an existing row’s columns. Syntax: UPDATE tablename SET columnname = expression [,columnname = expression] WHERE conditionlist; Ex:To change the p_indate of product with pno P01 to 02-AUG-2011. UPDATE PRODUCT SET p_indate =’02-AUG-2011’ Page 25
  • 26. WHERE pno=’P01’; Restoring table contents or ROLLBACK: ROLLBACK-undoes any changes since the last COMMIT command and brings the data back to the values that existed before the changes were made. Syntax: ROLLBACK; Ex: 1. Create table called sales. 2. Insert 10 rows in sales table. 3. Execute the ROLLBACK command ROLLBACK will undo only the result of INSERT and UPDATE commands. All data definition commands(CREATE TABLE) are automatically committed to data dictionary and cannot be rolled back. DELETE Command DELETE -deletes one or more rows from a table If you do not specify a WHERE condition , all rows from table will be deleted. REMOVAL OF SPECIFIED ROW(S): Syntax: DELETE FROM tablename [WHERE conditionlist]; REMOVAL OF ALL ROWS: Syntax: DELETE FROM tablename; Viewing data in tables or SELECT SELECT-lists the contents of a table. Syntax: SELECT columnlist FROM tablename [WHERE conditionlist]; The columnlist represents one or more attributes separated by commas. You can use the * wildcard character to list all attributes. Ex1: SELECT * FROM PRODUCT; Ex2: SELECT pdesc,p_indate FROM product WHERE pno=’P01’; Ex3:SELECT * FROM product WHERE p_indate>’01-AUG-2011’; The SELECT statement retrieves all rows that match the specified condition. WHERE clause adds conditional restrictions to SELECT statement. The condition list is represented by one or more conditional expressions separated by logical operators. Comparison operators can be used to restrict output. Comparison operators: Symbol Meaning Example = Equal to SELECT * FROM product WHERE pno=’P01’; < Less than SELECT * FROM product WHERE price<10; <= Less than or equal to > Greater than SELECT * FROM product WHERE price>10; >= Greater than or equal to <> or ! = Not equal to SELECT * FROM product WHERE vno <> 100; Using Computed Columns Page 26
  • 27. Oracle uses actual formula text as the label for the computed column. Ex: SELECT pno,qoh*price FROM PRODUCT; Result: Using Column aliases An alias is an alternative name given to a column or table in any SQL statement. Ex2: SELECT pno,qoh*price AS total FROM PRODUCT; Using Date arithmetic SYSDATE is a special function that returns today’s date. Ex:1 SELECT pno,p_indate,p_indate+90 AS ExpiryDate FROM product; Ex:2 SELECT pno,p_indate,SYSDATE-90 AS CutDate FROM product WHERE p_indate<=SYSDATE-90 The output would change based on today’s date Arithmetic Operators: Symbol Meaning Example + Addition - Subtraction * Multiply SELECT qoh, price*qoh FROM product; / Division ^ Raised to power(some applications uses ** instead of ^) Rules of Precedence: Perform operations within parentheses then perform ^ then *,/ then +,- Logical Operators: SQL allows you to have multiple conditions in a query through the use of logical operators. Symbol Meaning Example AND Both conditions must match SELECT * FROM product WHERE price > 10 AND price < 100; OR Either condition must match SELECT * FROM product WHERE vno = 100 OR vno = 101 NOT Do not match a certain condition SELECT * FROM product WHERE NOT(vno = 100) Display the result when all the condition specified using the AND operator are satisfied Display the result when Either of the condition specified using the OR operator are satisfied NOT operator is used to find rows that do not match a certain condition. It negates the result of conditional expression Ex: SELECT * FROM product WHERE ( price < 50 AND p_indate > ’01-AUG-2011’) OR vno = 100; The rows of vno=100 are included regardless of p_indate and price of those rows. Special Operators BETWEEN operator: Used to check whether an attribute value is within a range Ex: To see list of products whose price is between $10 and $100, use the command: Page 27 pno qoh*price P01 PEN P02 PENCIL pno total P01 PEN P02 PENCIL
  • 28. SELECT * FROM product WHERE price BETWEEN 10 AND 100; IS NULL operator: Used to check whether an attribute value is null. Ex: To list all the products that do not have a vendor assigned, use the command: SELECT * FROM product WHERE vno IS NULL; LIKE operator:Used only with char and varchar2. Matches a string pattern. Used in conjuction with wildcards to find patterns within string attributes. Ex1: To find all vendors whose name start with R SELECT * FROM vendor WHERE vname LIKE ‘R%’; To find all vendors whose name has ‘a’ as second letter. Ex2: SELECT * FROM vendor WHERE vname LIKE ‘_a%’; SQL allows you to use the percent sign (%) and underscore( _ ) wild card characters to make matches when the entire string is not known. Wildcard Meaning % Matches any characters _ Matches one characters Matches can be made when the query entry is written exactly like table entry. IN operator: matches any value within a VALUE list. uses an equality operator i.e, it selects only those rows that match(are equal to) atleast one of the values in the list Ex: SELECT * FROM product WHERE vno IN(100 , 101); All of the values in the list must be of same data type. Each of the values in the value list is compared to the attribute. IN operator is valuable when it is used in subqueries. SELECT * FROM vendor WHERE vno IN(SELECT vno FROM product ); Subquery (SELECT vno FROM product) will list all vendors who supply products. IN operator will compare the values generated by subquery to vno values in VENDOR table. EXISTS operator:checks whether subquery returns any row. If subquery returns any row, run the main query otherwise don’t. Ex: SELECT * FROM vendor WHERE (SELECT * FROM product WHERE qoh<=10); Modifying structure of table: ALTER Command: All changes to table structure are made using the ALTER command. Syntax: ALTER TABLE tablename {ADD|MODIFY} (columnname datatype [{ADD|MODIFY} columnname datatype]); To Change column’s datatype Page 28
  • 29. To change the vname datatype from varchar2 to char ALTER TABLE vendor MODIFY (vname char(35)); To Change column’s data characteristics To increase the width of vname column to 55 characters ALTER TABLE vendor MODIFY (vname char(35)); To add a column ALTER TABLE product ADD (pmin number(5)); If the table already has some data , we cannot add new column with NOT NULL as existing rows will default to NULL for the new column. TO ADD TABLE CONSTRAINTS: Syntax: ALTER TABLE tablename ADD constraint [ADD constraint]; To add primary key: ALTER TABLE part ADD PRIMARY KEY(part_no); To add foreign key: ALTER TABLE part ADD FOREIGN KEY(vno) REFERENCES vendor; (OR) ALTER TABLE part ADD PRIMARY KEY(part_no) ADD FOREIGN KEY(vno) REFERENCES vendor; To add primary and foreign key using the keyword CONSTRAINT: ALTER TABLE part ADD CONSTRAINT pk_partno PRIMARY KEY(part_no) ADD CONSTRAINT fk_vno FOREIGN KEY(vno) REFERENCES vendor; TO REMOVE A COLUMN OR TABLE CONSTRAINT Synax: ALTER TABLE tablename DROP{ PRIMARY KEY | COLUMN columnname | CONSTRAINT constraintname}; Dropping a column: deleting a column ALTER TABLE product DROP COLUMN pmin; DELETING A TABLE FROM DATABASE: A table can be deleted from the database using the DROP TABLE command. Syntax: DROP TABLE part; Advanced select queries ORDER BY clause: Orders the selected rows based on one or more attributes • Used in the last portion of select statement • By using this, rows can be sorted • By default it takes ascending order • DESC is used for sorting in descending order • Sorting by column which is not in select list is possible. • Sorting by column aliases Example: To produce a list of products sorted in descending order of their prices. SELECT pno,pdesc,p_indate,price FROM product Page 29
  • 30. ORDER BY price DESC; A multilevel ordered sequence is known as cascading order sequence and it can be created easily by listing several attributes, separated by commas, after the ORDER BY clause. SELECT * FROM employee ORDER BY e_lname,e_fname,e_initial; DISTINCT clause: Used to eliminate duplicate rows. Ex:How many different vendors are currently represented in the PRODUCT table? SELECT DISTINCT vno FROM product; Explain Aggregate functions? Ans: Some of the aggregate functions are COUNT,MIN,MAX,AVG. COUNT: Uses one parameter within parantheses. COUNT(columnname)-Used to count the number of non-null values of an attribute COUNT(*) aggregate function is used to count number of rows returned by query, including the rows that contain nulls. Ex1: How many rows in PRODUCT table have a price value less than or equal to $500? SELECT COUNT(*) FROM product WHERE price<=500; Ex:2:How many vendors referenced in the PRODUCT table have supplied products with prices that are <+1? SELECT COUNT(DISTINCT vno) FROM product WHERE price<=10; MAX and MIN Ex1: Which product has highest price? SELECT * FROM product WHERE price = (SELECT MAX(price) FROM product); (Here we cannot use SELECT * FROM product WHERE price = MAX(price); because The aggregate functions can be used only in column list of a SELECT statement) Ex2:Highest price in PRODUCT table? SELECT MAX(price) FROM product; Ex:3Lowest price in PRODUCT table? SELECT MIN(price) FROM product; Ex4: To find out the product that has the oldest date? SELECT * FROM product WHERE price = (SELECT MIN(p_indate) FROM product); Ex5: To find out the most recent product. SELECT * FROM product WHERE price = (SELECT MAX(p_indate) FROM product); SUM: Computes total sum of any specified attribute. Ex:To find the total value of all items SELECT SUM(qoh*price) AS TOTALVALUE FROM product; AVG Ex1: To find the products whose prices exceed the average product price. SELECT * FROM product WHERE price > (SELECT AVG(price) FROM product) ORDER BY price desc; Explain about GROUP BY clause? • Used to group rows on basis of certain common attribute value such as employees of a department, products of a vendor. Page 30
  • 31. • WHERE clause can be used ,if needed. • The only attributes that can be put in select clause are the aggregated functions and the attributes that have been used for grouping the information. Ex1:How many products are supplied by each vendor? SELECT vno, COUNT(pno) FROM product GROUP BY vno; Having clause: Extension of the GROUP BY feature is the HAVING clause. HAVING clause is applied to the output of GROUP BY operation. Ex: how many products supplied by each vendor.List only the products whose average is below $10 SELECT vno, COUNT(pno), AVG(price) FROM product GROUP BY vno HAVING AVG(price) < 10; Q: Explain about index in SQL Ans: Indexes are used to quickly access the data. Syntax: CREATE INDEX <index name> ON <tablename>(column name); An index can be created on one or more columns. Based on the number of columns included in index, an index can be of 2 types. 1. Simple index 2.Composite Index. To create Simple index: An index created on a single column is called simple index. Ex: CREATE INDEX p_in ON product(p_indate) To create composite index: An index created on a more than one column is called composite index. Dropping indexes or deleting an index: Use the DROP INDEX command. Ex: DROP INDEX p_in; Q:What is database schema? Ans:A schema is a group of database objects such as tables and indexes, that are related to each other. Syntax: CREATE SCHEMAAUTHORIZATION {creator} When a user is created, the DBMS automatically assigns schema to that user. Schemas are useful to group tables by owner and enforce a first level of security by allowing each user to see only the tables that belong to that user. Labwork: CREATE TABLE VENDOR( Page 31 VENDOR vn o vname vcity 10 0 RADHA VJA 10 1 ALIYA NULL 10 2 SIRI VJA 10 3 LAK GNT
  • 32. vno number(3) PRIMARY KEY, vname varchar2(35) NOT NULL, vcity varchar2(15)); CREATE TABLE PRODUCT( pno char(3), pdesc varchar2(35) NOT NULL UNIQUE, qoh number(5), price number(5), vno number(3), PRIMARY KEY(pno), FOREIGN KEY(vno) REFERENCES VENDOR); CREATE TABLE CUSTOMER( cno number(3) PRIMARY KEY, cname varchar2(35) , city varchar2(5), baldue number(5)); CREATE TABLE INVOICE( invno number(3), cno number(3), invdate date, PRIMARY KEY(invno), FOREIGN KEY(cno) REFERENCES CUSTOMER); Page 32 pno pdesc qoh price vno P01 PEN 2 10 100 P02 CD 20 12 101 P03 PENCIL 200 3 NULL P04 DVD 200 0 350 101 CUSTOMER cno cname city baldue 20 1 ANU VJA 100 20 2 ASHA GNT 500 20 3 RAJ VJA INVOICE invno cn o invdate 301 20 1 20-AUG-2011 302 20 2 20-AUG-2011 303 20 3 21-AUG-2011 304 20 1 21-AUG-2011
  • 33. All products sold are stored in LINE table CREATE TABLE LINE( invno number(3), lineno char(3), pno char(3), line_units number(5), line_price number(5), PRIMARY KEY(invno,lineno), FOREIGN KEY(pno) REFERENCES PRODUCT FOREIGN KEY(invno) REFERENCES INVOICE); CREATE TABLE EMP( e_lname varchar2(20), e_fname varchar2(20), e_initial varchar2(2), dob date, sal number(8,2)); Page 33 LINE invno lineno pno line_units line_price 301 L01 P01 10 10 301 L02 P02 10 12 301 L03 P03 20 3 302 L01 P01 30 10 302 L02 P02 20 12 303 L01 P01 35 10 303 L02 P02 15 12 EMP e_lname e_fname e_initial dob Sal REDDY SAM A 14-NOV- 1994 15000.25 NAIDU ANU S 14-OCT-1992 16234.50 JAIN NEHA K 28-NOV- 1993 15623.48 REDDY RAM T 14-SEP-1994 1623.89
  • 34. Unit –III Chapter –II ADVANCED SQL SQL data manipulation commands operate over entire table (ex: SELECT command lists all rows from the table you specified in FROM clause) and are said to be set oriented commands. UNION statement:combines rows from two or more queries without including duplicate rows. Syntax: query UNION query Query: SELECT cname,city FROM customer UNION SELECT cname,city FROM customer3 Combines ouput of two or more SELECT queries. (The select statements must be union – compatible.that is they must return the same attribute names and similar data types) without including duplicate rows. UNION ALL Combines ouput of two or more SELECT queries. (The select statements must be union – compatible.that is they must return the same attribute names and similar data types) and retains duplicate rows SELECT cname,city FROM customer UNION ALL SELECT cname,city FROM customer3 INTERSECT statement: used to combine rows from two queries , returning only the rows that appear in both sets. SELECT cname,city FROM customer INTERSECT SELECT cname,city FROM customer3 Page 34 CUSTOMER3 cno cname city baldue 40 1 JAY GNT 200 40 2 RAJ VJA 300 CUSTOMER cname city ANU VJA ASHA GNT RAJ VJA JAY GNT CUSTOMER cno cname city baldue 20 1 ANU VJA 100 20 2 ASHA GNT 500 20 3 RAJ VJA CUSTOMER cname city ANU VJA ASHA GNT RAJ VJA JAY GNT RAJ VJA CUSTOMER cname city RAJ VJA
  • 35. MINUS statement: combines rows from two queries and returns only rows that appear in first set but not in the second. SELECT cname,city FROM customer MINUS SELECT cname,city FROM customer3 SQL JOIN OPERATORS: A join is used to combine rows from multiple tables and returns the rows with one of the following conditions: Join operations can be classified as inner joins and outer joins. The inner join is traditional join in which only rows that meet a given criteria are selected. The join criteria can be an equality condition (also called a natural join or an equijoin) or inequality condition( also called theta join) Generally a join condition will be equality comparison of the P.K of one table and F.K of related table An outer join returns not only matching rows but also unmatched attribute values from one table or both tables to be joined. Join specification Join Type SQL Syntax Example Description CROSS CROSS JOIN SELECT * FROM T1,T2 SELECT * FROM T1 CROSS JOIN T2; Returns the Cartesian product of T1 and T2(old style) Returns the Cartesian product of T1 and T2(old style) INNER Old-Style JOIN SELECT * FROM T1,T2 WHERE T1.C1=T2.C1; Returns only the rows that meet the join condition in the WHERE clause. NATURAL JOIN SELECT * FROM T1 NATURAL JOIN T2; Returns only the rows with matching values in the matching columns.The matching columns must have the same names and similar datatypes. JOIN USING SELECT * FROM T1 JOIN T2 USING(C1) Returns only the rows with matching values in the columns indicated in the USING clause JOIN ON SELECT * FROM T1 JOIN T2 ON T1.C1=T2.C1; Returns only the rows that meet the join condition in the ON clause OUTER LEFT JOIN SELECT * FROM T1 LEFT OUTER JOIN T2 ON T1.C1=T2.C1; Returns rows with matching values and include all rows from left table(T1) with unmatched values RIGHT JOIN SELECT * FROM T1 RIGHT OUTER JOIN T2 ON T1.C1=T2.C1; Returns rows with matching values and include all rows from right table(T2) with unmatched values Page 35 CUSTOMER cno cname city 20 1 ANU VJA 20 2 ASHA GNT
  • 36. FULL JOIN SELECT * FROM T1 FULL OUTER JOIN T2 ON T1.C1=T2.C1; Returns rows with matching values and include all rows from both table(T1 and T2) with unmatched values RECURSIVE JOIN (OR) SELF JOIN: An alias is an alternative name given to a column or table in any SQL statement. An alias is especially useful when a table must be joined to itself in a recursive query Ex: SELECT E.Eno,E.Ename,M.Ename FROM EMP E,EMP M WHERE E.Mgr=E.Eno; Cross Join:(also known as cartesian product) Examples: SELECT * FROM invoice CROSS JOIN line; The above query generates 4*7=28rows ( 4 rows in invoice table and 7 rows in line table) Natural Join: SELECT cno,cname,invno,invdate FROM customer NATURAL JOIN invoice; You are not limited to two tables when performing a natural join. It doesnot require a table qualifier for the common attribute. SELECT invno,pno,pdesc,line_units,line_price FROM invoice NATURAL JOIN line NATURAL JOIN product; JOIN USING clause It doesnot require a table qualifier for the common attribute. SELECT invno,pno,pdesc,line_units,line_price FROM invoice JOIN line USING(invno) JOIN product USING(pno); JOIN ON clause Do not require common attribute names in the joining tables. Requires a table qualifier for the common attribute. Lets you perform a join even when the tables do not share a common attribute name. SELECT invoice.invno,pno,pdesc,line_units,line_price FROM invoice JOIN line ON invoice.invno=line.invno JOIN product ON line.pno=product.pno; OUTER JOINS SELECT pno,vendor.vno,vname FROM vendor LEFT JOIN product ON vendor.vno=product.pno; SELECT pno,vendor.vno,vname FROM vendor RIGHT JOIN product ON vendor.vno=product.pno; SELECT pno,vendor.vno,vname FROM vendor FULL JOIN product ON vendor.vno=product.pno; SUBQUERIES: used when it is required to process data based on other processed data Characteristics of sub queries: A subquery or nested query or inner query is a query inside another query. A subquery is normally expressed inside parentheses The output of inner query is used as input for the outer(high-level) query. So inner query is executed first and then the outer query. Subquery is based on the use of the SELECT statement to return one or more values to another query. If the table into which you are inserting rows has one date attribute and one Page 36
  • 37. number attribute, the SELECT subquery should return rows in which 1st column has date values and 2nd column has number values. Inserting table rows with a select subquery or Copying parts of tables: It add multiple rows to a table, using another table as source of the data. CREATE TABLE PART( part_no char(3) PRIMARY KEY, part_desc varchar2(35), vno number(3)); Syntax: INSERT INTO target_tablename SELECT source_columnlist FROM source_tablename; Example: INSERT INTO part SELECT * FROM product; Both the tables(PART and PRODUCT) must have same attributes.The above query returns all rows from table PRODUCT. SELECT subquery Examples Explanation UPDATE product SET price=(SELECT AVG(price) FROM product ) WHERE vno=’100’………………Ex(2) Updates the product price to average product price for the products provided by vendor 100. DELETE FROM product WHERE vno IN(SELECT vno FROM vendor WHERE vcity=’VJA’) ……………..Ex(3) Delete the PRODUCT table rows that are provided by vendors with vcity=’VJA’ A subquery can return i. One value as in Ex(2) ( the select subquery returns avg(price) which is one value). ii. A list of values as in Ex(3) (the select subquery returns a list of vendors from ‘VJA’) iii. A virtual table iv. No value at all i.e, NULL . the output of the outer query might result in an error or a null empty set. WHERE subqueries Ans:Ex: Find all products with a price greater than or equal to the average product price, you write the following query. SELECT pno,price FROM product WHERE price>=(SELECT AVG(price) FROM product); Note that this type of query,when used in a >,<,==,>= or <= conditional expression, requires a subquery that returns only one single value.If the query returns more than a single value, the DBMS will generate an error. IN subqueries: Ans:Ex(2) HAVING subqueries Example:To list all products with the total quantity sold greater than the average quantity sold SELECT pno,SUM(line_units) FROM line GROUP BY pno HAVING SUM(line_units)>(SELECT AVG(line_units) FROM line); MULTIROW subquery operators: ANY and ALL 1. ALL:used to do an inequality comparison(> or <) of one value to a list of values. Example: What products have a product cost that is greater than all individual product costs for products provided by vendor with vno 101 SELECT pno, qoh*price FROM product WHERE qoh*price> ALL(SELECT qoh*price FROM product WHERE vno = 101); Page 37
  • 38. In the above query the ALL operator allows you to compare a single value(qoh*price) with a list of values returned by the subquery. 2. ANY: ANY operator allows you to compare a single value with a list of values, selecting only the rows whose qoh*price is greater than any value of the list. FROM subqueries FROM clause specifies the table from which data will be drawn. Example:To find all customers who purchased both products ‘PEN’ and ‘PENCIL’ SELECT DISTINCT cno, cname FROM customer, ( SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PEN’) cp1, (SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PENCIL’) cp2 WHERE customer.cno = cp1.cno AND cp1.cno=cp2.cno; (OR) CREATE VIEW cp1 AS SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PEN’; CREATE VIEW cp2 AS SELECT invoice.cno FROM invoice NATURAL JOIN line WHERE pdesc=’PENCIL’; SELECT DISTINCT cno, cname FROM customer NATURAL JOIN cp1 NATURAL JOIN cp2; Attribute List Subqueries or inline subquery. The attribute list can also include a subquery expression also known as inline subquery. The inline subquery must return one single value otherwise an error code is raised. SELECT pno, price ,(SELECT AVG(price) FROM product) AS AVGPRICE , price - (SELECT AVG(price) FROM product) AS DIFF FROM product; The query used the full expression instead of column aliases when computing DIFF. The column aliases cannot be used in computations in the attribute list when the alias is defined in the same attribute list. We can use Attribute List Subqueries to include data from other tables that are not directly related to main table or tables in the query. SELECT pno,SUM(line_units*line_price) AS sales, (SELECT COUNT(*) FROM employee) AS ecount, SUM(line_units*line_price)/ (SELECT COUNT(*) FROM employee) AS contib FROM line GROUP BY pno; CORRELATED SUBQUERIES To produce correlated subquery the DBMS does i. It iniates the outer query ii. For each row of the outer query result set, it executes the inner query by passing the outer row to the inner query.( inner query references a column of the outer subquery) Example:To find all product sales who units sold > average units sold for that product. SELECT invno, pno, line_units FROM line LS WHERE LS.line_units>(SELECT AVG(line_units) FROM line LA WHERE LA.pno = LS.pno); Page 38
  • 39. The inner query runs once using the first pno found in the outer line table and returns average sale for that product. CORRELATED subqueries can also be used with EXISTS special operator Example: To know the vendor code and name for the products having qoh<10 SELECT vno, vname FROM vendor WHERE EXISTS(SELECT * FROM product WHERE qoh<10 AND vendor.vno=product.vno); SQL functions: Used to generate information from data. DUAL: is Oracle pseudo table used only for cases when a table is not really needed. DATE/TIME FUNCTIONS Function Examples LAST_DAY: returns the last day of the month based on a date value. Syntax: last_day( date_value ) SELECT last_day(to_date('2003/03/15', 'yyyy/mm/dd')) FROM DUAL; would return Mar 31, 2003 SELECT last_day(to_date('2003/02/03', 'yyyy/mm/dd')) FROM DUAL; would return Feb 28, 2003 List employees born in the last seven days of a month SELECT * FROM emp WHERE dob >= LAST_DAY(dob)-7; TO_CHAR function : converts a number or date to a string. Syntax: TO_CHAR(date_value,fmt) fmt = format used can be MONTH Name of month MON:three-letter month name MM-two digit month name D Day of week (1-7). DAY Name of day. DD Day of month (1-31). YYYY 4-digit year YY: two digit year value SELECT to_char(sysdate, 'yyyy/mm/dd') FROM DUAL; would return '2003/07/09' List all employees born in 1994. SELECT * FROM emp WHERE TO_CHAR(dob,’YYYY’)=’1994’; List all employees born in the month of NOVEMBER SELECT * FROM emp WHERE TO_CHAR(dob,’MM’)=’11’; List all employees born on 14th of a month SELECT * FROM emp WHERE TO_CHAR(dob,’DD’)=’14’; TO_DATE function:converts a string to a date. Also used to translate a date between formats. Syntax: TO_DATE(char_value,fmt) fmt = format used can be as above SELECT to_date('2003/07/09', 'yyyy/mm/dd') FROM DUAL; would return a date value of July 9, 2003. Find the age of employess as on 12-31-2012 SELECT e_lname,TO_DATE(’12/31/2012’,’MM/DD/YYYY’)- dob/365 AS YEARS FROM emp; NOTE: ‘12/31/2012’ is a text string, not a date, TO_DATE translates the text string to a valid oracle date used in date arithmetic. How many days are between 6/25/2011 and 10/27/2011 SELECT TO_DATE(’2011/06/25’,’YYYY/MM/DD’)- TO_DATE(‘OCTOBER 27,2011’,’MONTH Page 39
  • 40. ,DD,YYYY’) FROM DUAL; SYSDATE : returns todays date SELECT TO_DATE(’25-DEC-2011’,’DD-MON- YYYY’)-SYSDATE FROM dual; ADD_MONTHS: adds months to a date. Syntax: add_months( date_value, n ) date_value is the starting date (before the n months have been added). n is the number of months to add to date1. SELECT add_months('01-Aug-03', 3) FROM DUAL; would return '01-Nov-03' SELECT pno,p_indate,ADD_MONTHS(p_indate,24) FROM product NUMERIC FUNCTIONS Aggregate functions operate over a set of values(multiple rows) while numeric functions operate over a single row. Function Example ABS Returns absolute value of a number. Syntax: ABS(numeric_value) SELECT ABS(1.95),ABS(-1.93) FROM DUAL; Would return 1.95 1.93 ROUND function returns a number rounded to a certain number of decimal places. Syntax:ROUND(numeric_value,p) p=precision SELECT round(125.315) FROM DUAL; would return 125 SELECT ROUND(sal) as sal1, ROUND(sal) as sal2 FROM emp; CEIL function returns the smallest integer value that is greater than or equal to a number. Syntax: ceil( number ) SELECT ceil(-32.65) FROM DUAL; would return -32. SELECT ceil(32.65) FROM DUAL; would return 33. SELECT CEIL(sal) ,FLOOR(sal) FROM emp; FLOOR function returns the largest integer value that is equal to or less than a number. Syntax: floor( number ) SELECT floor(5.9) FROM DUAL; would return 5 SELECT floor(-5.9) FROM DUAL; would return -6 The sqrt function returns the square root of n. Synatx: sqrt( n ) n is a positive number. sqrt(9) would return 3 mod function returns the remainder of m divided by n mod(15, 4) would return 3 power function returns m raised to the nth power. Syntax : power( m, n ) m is the base. n is the exponent. If m is negative, then n must be an integer. power(3, 2) would return 9 Page 40
  • 41. exp function returns e raised to the nth power, where e = 2.71828183. exp(3) would return 20.0855369231877 trunc function returns a number truncated to a certain number of decimal places. trunc(125.815, 0) would return 125 trunc(125.815, 1) would return 125.8 ln function returns the natural logarithm of a number. ln(20) would return 2.99573227355399 log function returns the logarithm of n base m. Syntax: log( m, n ) m must be a positive number, except 0 or 1. n must be a positive number. log(100, 1) would return 0 String Functions: are useful to concatenate strings of characters, printing names in upper case or knowing the length of a given attribute. Function Example UPPER function converts all letters in the specified string to uppercase. Syntax: UPPER(string) upper('Tech on'); would return 'TECH ON List all employee names in upper case. SELECT UPPER (e_initial) || ‘.’|| UPPER (e_fname) || UPPER(e_lname) FROM EMP; LOWER function converts all letters in the specified string to lowercase. Syntax: LOWER(string) List all employee names in lower case. SELECT LOWER (e_initial) || ‘.’|| LOWER (e_fname) || LOWER(e_lname) FROM EMP; SUBSTR function allows you to extract a substring from a string. Syntax:substr( string, p, l ) string is the source string. p is the position for extraction. l is optional. It is the number of characters to extract. substr('This is a test', 6, 2) would return 'is' substr('This is a test', 6) would return 'is a test' substr('This is a test', -3, 3) would return 'Net' List first 3 characters of all employee last names.. Ex:SELECT SUBSTR(e_lname,1,3) AS prefix FROM EMP; LENGTH function returns the number of characters in the specified string. Syntax: length( string) length(NULL) would return NULL. length('') would return NULL. length('Tech on the Net') would return 15. List all employees last names and length of their last names. SELECT e_lname, LENGTH(e_lname) FROM EMP; Concatenation The || operator allows you to concatenate data from two different character columns and returns a single column. Syntax: string1 || string2 'a' || 'b' || 'c' || 'd' would return 'abcd'. List all employee names (concatenated) SELECT e_initial || ‘.’|| e_fname || e_lname AS NAME FROM EMP; CONVERSION FUNCTIONS:allows you take a value of given data type and convert it to the equivalent value in another data type. Functions Example TO_CHAR : returns a character string from a numeric value. Syntax: SELECT eno, TO_CHAR(sal, ‘9,999.99’) AS PRICE FROM EMP; Page 41
  • 42. TO_CHAR(numeric_value, fmt) TO_NUMBER : returns a formatted number from a character string. Syntxa:TO_NUMBER (char_value, fmt) fmt= format used can be: 9 - displays a digit 0 – displays a leading zero , - displays the comma . – displays the decimal point $ - displays the dollar sign B – leading blank S – leading sign MI – trailing minus sign SELECT TO_NUMBER(‘-123.99’,’S9999.99’), TO_NUMBER(’99.78-’,’B999.99MI’), FROM DUAL; DECODE: compares an attribute or expression with a series of values and returns an associated value or a default value if no match is found Syntax: DECODE(e,x,y,d) e – attribute or expression x – value with which to compare e y – value to return in e = x d – default value to return if e is not equal to x The following example returns the sales tax for specified cities. Compares vcity to ‘VJA’ ;if the value matches it returns .08 Compares vcity to ‘GNT’ ;if the value matches it returns .05 If there is no match it returns 0.00( the default value) SELECT vno, vcity, DECODE(vcity,’VJA’,.08,’GNT’,.05,0.00) AS TAX FROM VENDOR; Page 42
  • 43. Oracle sequences: generates a numeric value that can be assigned to any column in any table. Use of sequences is optional, you can enter the values manually. Oracle sequences have a name and can be used any where a value is expected. Sequences can be created and deleted anytime. The table attribute to which you assigned a value based on a sequence can be edited and modified. Oracle sequences are • Independent objects in the database. • Not a data type • Not tied to a table or column Syntax: CREATE SEQUENCE name [START WITH n] [INCREMENT BY n] [CACHE | NOCACHE] where name is the name of the sequence n is an integer that can be positive or negative. START WITH specifies initial sequence value( the default value is 1) INCREMENT BY determines the value by which the sequence is incremented. The CACHE or NOCACHE indicates whether oracle will preallocate sequence numbers in memory. (Oracle preallocates 20 values by default) Example: CREATE SEQUENCE CSEQ1 START WITH 204 INCREMENT BY 1 NOCACHE; To check all the sequences you have created. SELECT * FROM USER_SEQUENCES; To use sequences during data entry you must use two special pseudo columns NEXTVAL and CURRVAL. NEXTVAL retrieves the next available value from a sequence. Each time you use NEXTVAL , the sequence is incremented. CURRVAL retrieves the current value of sequence. Example INSERT INTO CUSTOMER VALUES (CSEQ1.NEXTVAL,’RAVI’,’NELLORE’, 500); INSERT INTO INVOICE VALUES (‘I05’ , CSEQ1.CURRVAL,’22-AUG-2011’); You cannot use CURRVAL unless a NEXTVAL was issued previously in the same session. NEXTVAL retrieves the next available sequence number( here 204) and signs to cno in CUSTOMER table. CSEQ1.CURRVAL refers to last used CSEQ1.NEXTVAL sequence number(204). In this way the relationship between INVOICE and CUSTOMER is established. COMMIT; statement must be issued to make the changes permanent. You can also issue a ROLLBACk statement , in which case the rows you inserted in INVOICE and CUSTOMER will be rolled back.( but sequence number would not) That is, if you use sequence number again you must get 204 but you will get 205 eventhough the row 204 is deleted. DROPPING a SEQUENCE doesnot delete the values you assigned to table attributes. Syntax: DROP SEQUENCE CSEQ1; Page 43
  • 44. VIEWS A view is a virtual table based on SELECT query. The tables on which view is based are called base tables. Syntax: CREATE VIEW viewname AS SELECT query Characteristics: A relational view has several special characteristics • We can use the view instead of table in a SQL statement. • Views are dynamically updated when the base table is updated. • Views provide a level of security in the database. The view can restrict users to only specified columns and specified rows in a table. • View may also be used as the basis for reports Example: CREATE VIEW PROD_STATS AS SELECT vno, SUM(qoh * price) AS TotalCost FROM PRODUCT GROUP BY vno; To drop a view Syntax: DROP VIEW <view name> Example: DROP VIEW PROD_STATS UPDATABLE VIEWS:To use batch update routines to update master table attribute with transaction data. To demonstrate a batch update routine, consider two tables NOTE:There is 1:1 relationship between two tables To Update qoh attribute (qoh – qty as that much quantity has been sold) 1. We have to join two tables 2. update qoh for each row of ProdMaster table with matching pno values in ProdSales table. We use a updatable view to do that. Updatable view is a view that can be used to update attributes in the base tables that are used in the view. Not all views are updatable. The most common updatable view restrictions are as follows: 1. GROUP BY and aggregate functions cannot be used. 2. Cannot use SET operators. 3. The P.K columns of base table you want to update must have unique values in the view. That is, the two tables must have 1:1 relationship then only the view can be used to update a base table. Example: CREATE VIEW QUP AS ( SELECT ProdMaster.pno, qoh, qty FROM ProdMaster, ProdSales WHERE ProdMaster.pno=ProdSales.pno); Page 44 ProdMaster pno pdesc qoh P01 SCREWS 60 P02 NUTS 37 P03 BOLTS 50 ProdSales pno qty P01 7 P02 3
  • 45. UPDATE QUP SET qoh=qoh-qty; Page 45
  • 46. Q: What is PSM (Persistent Stored Module)? Ans: A Persistent Stored Module is a block of code containing standard SQL statements and procedural extensions that is stored and executed at the DBMS server. The PSM represents business logic that can be encapsulated, stored and shared among multiple database users. A PSM lets an administrator assign specific access rights to a stored module to ensure that only authorized users can use it. Oracle implements PSMs through its procedural SQL language. (PL/SQL) Q: What is PL/SQL? Explain? Ans: PL/SQL is a language that makes it possible to use and store procedural code and SQL statements within the database. It is also used to merge SQL and traditional programming constructs, such as • Variables, • conditional processing (IF-THEN-ELSE), • basic loops (FOR and WHILE loops) and • Error trapping. The procedural code is executed as a unit by the DBMS when it is invoked by the end user. End users can use PL/SQL to create • Anonymous PL/SQL blocks. • Triggers • Stored Procedures • PL/SQL functions You can write PL/SQL code block by enclosing the commands inside BEGIN and END clause. Ex: BEGIN INSERT INTO vendor VALUES (105, ‘SITA’, ‘TNL’); END; / This is an example of anonymous PL/SQL block because it has not given a specific name. The above PL/SQL block executes as soon as you press ENTER key after typing / You will see the message “PL/SQL procedure successfully completed” If you want a more specific message such as “new vendor added”. You must type as follows: SQL> SET SERVEROUTPUT ON This SQL * plus command enables the client console (SQL * plus) to receive messages from the server side(ORACLE DBMS).To send messages from the PL/SQL block to SQL * plus console, use the DBMS_OUTPUT.PUT_LINE function. The standard SQL , the PL/SQL code are executed at server side, not at client side.To stop receiving messages from sever , enter SET SERVEROUTPUT OFF. In oracle , you can use the SQL * plus command SHOW ERRORS to help you diagnose errors found in PL/SQL blocks. Q: Write anonymous PL/SQL program to insert rows into VENDOR table and display the message “New vendor added”. Ans: BEGIN INSERT INTO vendor VALUES (106,’GITA’,’VJA’); DBMS_OUTPUT.PUT_LINE(‘New vendor added’); END; / Page 46
  • 47. PL/SQL Basic data types Data Type Description CHAR character values of a fixed length VARCHAR2 variable length character values NUMBER numeric values DATE Date values %TYPE inherits the datatype from a variable that you declared previously or from an attribute of a database table. Ex: price1 PRODUCT.price %TYPE ; assigns price1 the same datatype as the price column in the PRODUCT table. Q: Write anonymous PL/SQL program to display the number of products in price range 0 and 10, 11 and 60 ,61 and 110 etc.. Ans: DECLARE P1 NUMBER(3) := 0; P2 NUMBER(3) := 10; NUM NUMBER(2) := 0; BEGIN WHILE P2<5000 LOOP SELECT COUNT(pno) INTO NUM FROM product WHERE price BETWEEN P1 AND P2; DBMS_OUTPUT.PUT_LINE(‘There are ‘|| NUM|| ‘ products with price between ‘||P1|| ‘ and ‘||P2); P1 := P2+1; P2 := P2+50; END LOOP; END; / The PL/SQL block shown above has following characteristics. 1. Each statement inside the PL/SQL code must end with a semicolon 2. The PL/SQL block starts with the DECLARE section in which you declare the variable names, the data types and an initial value(optional). 3. A WHILE loop is used. 4. Uses the string concatenation symbol. 5. SELECT statement uses the INTO keyword to assign output of the query to a PL/SQL variable The most useful feature of PL/SQL block is that they let you create code that can be named, stored and executed either implicitly or explicitly by the DBMS. What is Trigger ? Explain. Ans: A trigger is a procedural sql code which is fired when a DML statements like Insert, Delete, Update is executed on a database table. The syntax to create a trigger in oracle is: CREATE OR REPLACE TRIGGER trigger_name [BEFORE / AFTER] [DELETE /INSERT/UPDATE OF column_name ] ON table_name [FOR EACH ROW] [DECLARE] [variable_name data-type [:= initial_value]] BEGIN PL/SQL instructions; …… Page 47
  • 48. END; A trigger definition contains the following parts: 1.The triggering timing: BEFORE or AFTER. This timing indicates at what time the trigger should get fired. (before or after the triggering statement is completed.) 2.The triggering statement/event: The statement that causes the trigger to execute (INSERT, UPDATE or DELETE) The triggering level: There are two types of triggers: statement – level triggers and row – level triggers • Statement – level triggers: This type of trigger is executed once, before or after the triggering statement is completed. • Row – level triggers: requires the use of the FOR EACH ROW keywords. This type of trigger is executed once for each row affected. ( if you update 10 rows, the trigger executes 10 times. 2. Triggering Action: The PL/SQL code enclosed between BEGIN and END keywords. You can use a trigger to update an attribute in a table other than the one being modified. CREATE OR REPLACE TRIGGER TLP AFTER INSERT ON line FOR EACH ROW BEGIN UPDATE product SET qoh = qoh - :NEW.LINE_UNITS WHERE product.pno = :NEW.pno; END; / TLP is a row level trigger that executes after inserting a new LINE row and reduces quantity on hand (in PRODUCT table) of recently sold product by the number of units sold. CREATE OR REPLACE TRIGGER trigger_name =>creates a trigger with the given name or overwrites an existing trigger with the same name. OF column_name =>This clause is used with update triggers. This clause is used when you want to trigger an event only when a specific column is updated ON table_name=> the name of the table or view to which the trigger is associated. Example of a statement level trigger that is executed after an update of the qoh, pmin attribute for an existing row or after an insert of a new row in the product table. CREATE or REPLACE TRIGGER TPR AFTER INSERT OR UPDATE OF QOH,PMIN ON PRODUCT BEGIN UPDATE PRODUCT SET REORDER =1 WHERE QOH <= PMIN; END; / Q: When does a trigger fire? Ans: A trigger is triggered automatically when an associated DML statement is executed. • A trigger is invoked before or after a data row is inserted, updated or deleted. • A trigger is associated with a database table. • Each database table may have one or more triggers. • A trigger is executed as part of the transaction that triggered it. Page 48
  • 49. Q: How to delete a trigger? Ans: When you delete a table, all its trigger objects are deleted with it. If you want to delete a trigger without deleting the table, give the following command DROP TRIGGER triggername. Q: Write a program to update the customer balance in the CUSTOMER table after inserting every new LINE row. CREATE OR REPLACE TRIGGER TLC AFTER INSERT ON line FOR EACH ROW DECLARE cus CHAR(5); tot NUMBER := 0; --to compute total cost BEGIN SELECT cno INTO cus FROM invoice --1) get the customer code WHERE invoice.invno = :NEW.line_units; tot := :NEW.line_price * :NEW.invno; --2)compute the total of the current line UPDATE customer SET baldue = baldue + tot WHERE cno = cus; DBMS_OUTPUT.PUT_LINE(‘ *** Balance updated for customer : ‘ || cus); END; / The trigger is a row level trigger that executes for each new LINE row inserted. The SELECT statement returns only one attribute (cno) from INVOICE table and that attribute returns only one value. You use the INTO clause to assign a value from a SELECT statement to a variable (cus) used within a trigger. Double dashes “--“ are used to indicate comments within the PL/SQL block. Trigger action based on conditional DML predicates You can create a trigger that executes after an insert, an update or a delete on the PRODUCT table and to know which one of the three statements caused the trigger to execute use the following syntax: IF INSERTING THEN ……END IF; IF UPDATING THEN ……END IF; IF DELETING THEN……END IF; Triggers can be used to • To enforce constraints that cannot be enforced at the DBMS design and implementation levels. • To facilitate enforcement of referential integrity. • Update table values, insert records in tables and call other stored procedures. • Triggers add functionality by automating critical actions and providing appropriate warnings and suggestions. • Triggers add processing power to RDBMS and to database system as a whole. Oracle recommends triggers for • Auditing purposes (creating audit logs) • Automating generation of derived column values. • Enforcement of business or security constraints. Page 49
  • 50. • Creation of replica tables for back up purposes. Q:What are the various type of triggers? Statement – level triggers: This type of trigger is executed once, before or after the triggering statement is completed. Example of a statement level trigger that is executed after an update of the qoh, pmin attribute for an existing row or after an insert of a new row in the product table. CREATE or REPLACE TRIGGER TPR AFTER INSERT OR UPDATE OF QOH,PMIN ON PRODUCT BEGIN UPDATE PRODUCT SET REORDER =1 WHERE QOH <= PMIN; END; / Row – level triggers: requires the use of the FOR EACH ROW keywords. This type of trigger is executed once for each row affected. ( if you update 10 rows, the trigger executes 10 times. Example: CREATE or REPLACE TRIGGER TPR BEFORE INSERT OR UPDATE OF QOH, PMIN ON PRODUCT FOR EACH ROW BEGIN IF :NEW.QOH <= :NEW.PMIN THEN :NEW.REORDER :=1; ELSE :NEW.REORDER :=0; END IF; END; / What are Stored Procedures? Explain? A stored procedure is a named group of SQL statements that have been previously created and stored in the server database. Advantages: • Stored procedures accept input parameters so that a single procedure can be used over the network by several clients using different input data. • Stored procedures reduce network traffic and improve performance. • Stored procedures can be used to help ensure the integrity of the database. • Stored procedures help reduce code duplication by means of code isolation and code sharing, there by minimizing the chance of errors and the cost of application development and maintenance. • Stored procedures are useful to encapsulate shared code to represent business transactions i.e, you need not know the name of newly added attribute and would need to add new parameter to the procedure call. Syntax to create procedure: CREATE OR REPLACE PROCEDURE procedure_name [(argument [in/out] data-type,….)] [IS / AS] [variable_name data-type [:= initial_value]] BEGIN PL/SQL or SQL statements; … END; Page 50
  • 51. Syntax to execute a stored procedure EXEC procedure_name[(parameter_list)]; Ex: Write a stored procedure to assign an additional 5 % discount for all products when the QOH = 2PMIN CREATE OR REPLACE PROCEDURE prod_discount AS BEGIN UPDATE product SET discount = discount + .05 WHERE qoh >= pmin * 2; DBMS_OUTPUT.PUT_LINE(‘*** Update Finished ***’); END; / 1. argument specifies the parameters that are passed to the stored procedures. A stored procedure could have zero or more arguments. 2. IN/OUT indicates whether the parameter is for input, output or both. 3. Variables can be declared between the keywords IS and BEGIN. To make percentage increase an input variable in the above procedure--- CREATE OR REPLACE PROCEDURE prod_discount ( pd IN NUMBER) AS BEGIN IF ((pd <= 0) OR (pd >= 1)) THEN DBMS_OUTPUT.PUT_LINE(‘Error value must be greater than 0 and less than 1’); ELSE UPDATE product SET discount = discount + .05 WHERE qoh >= pmin * 2; DBMS_OUTPUT.PUT_LINE(‘*** Update Finished ***’); END IF; END; / To execute the above procedure--- EXEC prod_discount(.05); Q: write a stored procedure to add new customer. CREATE OR REPLACE PROCEDURE cadd (w_cname IN VARCHAR2, w_city IN VARCHAR2) AS BEGIN INSERT INTO customer (cno, cname, city) values(CSEQ1.NEXTVAL, w_cname, w_city); DBMS_OUTPUT.PUT_LINE(‘Customer added ’); END; / The procedure uses • several parameters one for each required attribute in the CUSTOMER table. • CSEQ1 sequence to generate a new customer code. The parameters can be null only when the table specifications permit null for that parameter. To execute: EXEC cadd(‘KALA’, ‘VJA’,NULL); Page 51
  • 52. Q: Write procedures to add new invoice and line row. Ans: CREATE OR REPLACE PROCEDURE invadd(w_cno IN NUMBER, w_date IN DATE) AS BEGIN INSERT INTO invoice VALUES(ISEQ.NEXTVAL, w_cno, w_date); DBMS_OUTPUT.PUT_LINE(‘Invoice Added’); END; / CREATE OR REPLACE PROCEDURE lineadd (ln IN CHAR, pn IN CHAR, lu IN NUMBER) AS lp NUMBER := 0; BEGIN SELECT price INTO lp FROM product WHERE pno = pn ; INSERT INTO line VALUES(ISEQ. CURRVAL, ln, pn, lu, lp); DBMS_OUTPUT.PUT_LINE(‘Invoice Line Added’); END; / Q: What is a cursor? How many types of cursors are there? How to handle cursors? Ans:Cursor is reserved area in memory in which output of the query is stored, like an array holding rows and columns. There are two types of cursors: implicit and explicit. An implicit cursor is automatically created in PL/SQL when the SQL statement returns only one value. An explicit cursor is created to hold the output of an SQL statement that may return two or more rows.(but could return 0 or only one row) To create an explicit cursor, use the following syntax inside PL/SQL DECLARE section. CURSOR cursor_name IS select-query; The cursor declaration section only reserves a named memory area for the cursor. Once you declared a cursor, you can use cursor processing commands anywhere between the BEGIN and END keywords of the PL/SQL block. Cursor Processing Commands Cursor Command Explanation OPEN Executes the SQL command and populates the cursor with data Before you can use a cursor, you need to open it Ex: OPEN cursor_name. FETCH To retrieve data from the cursor and copy it to the PL/SQL variables. The syntax is : FETCH cursor_name INTO variable1 [,variable2, …..] CLOSE The CLOSE command closes the cursor for processing Cursor style processing involves retrieving data from the cursor one row at a time. The set of rows the cursor holds is called the active set. The data set contains a current row pointer. Therefore after opening a cursor, the current row is the first row of the cursor. Page 52
  • 53. When you fetch a row from the cursor, the data from the current row in the cursor is copied to the pl/sql variables. After the fetch, the current row pointer moves to the next row in the set and continues until it reaches the end of the cursor. Cursor Attributes determine when you reached the end of the cursor data set, number of rows in cursor etc… Attribute Description %ROWCOUNT Returns the number of rows fetched so far. If the cursor is not OPEN, it returns an ERROR. If no fetch has been done but the cursor is OPEN, it returns 0. %FOUND Returns TRUE if the last FETCH returned a row and FALSE if not. If the cursor is not OPEN, it returns an ERROR. If no fetch has been done, it contains NULL. %NOTFOUND Returns TRUE if the last FETCH did not return any row and FALSE if it did. If the cursor is not OPEN, it returns an ERROR. If no fetch has been done, it contains NULL. %ISOPEN Returns TRUE if the cursor is OPEN or FALSE if the cursor is CLOSED. CREATE OR REPLACE PROCEDURE pce IS p product.pno%TYPE; desc product.pdesc%TYPE; tot NUMBER(3); CURSOR pc IS SELECT pno, pdesc FROM product WHERE qoh > (SELECT AVG(qoh) FROM product); BEGIN DBMS_OUTPUT.PUT_LINE(‘PRODUCTS WITH QOH > AVG(QOH)’); OPEN pc; LOOP FETCH pc INTO p,desc; EXIT WHEN pc%NOTFOUND; DBMS_OUTPUT.PUT_LINE(p||’ => ‘||desc); END LOOP; DBMS_OUTPUT.PUT_LINE(‘TOTAL PRODUCT PROCESSED ‘||pc%ROWCOUNT); CLOSE pc; END; / Page 53
  • 54. Unit-III Chapter –III Database Design Q: What is an information system? Ans: A complete information system is composed of people, hardware, software, the databases, application programs and procedures. The process of creating an information system is known as system development. Q: The system development life cycle(SDLC) The SDLC is an iterative rather than a sequential process. 1. Planning: The SDLC planning phase yields a general overview of the company and its objectives. An initial assessment of the information flow-and-extent requirements must be made to answer questions like Should the existing system be continued? Should the existing system be modified? Should the existing system be replaced? If it is decided that a new system is necessary, then it is checked whether the new system is feasible or not. The feasibility study includes 1. Technical feasibility: Can the development of the new system be done with current equipment, existing software technology, and available personnel? Does it require new technology? 2. Economic feasibility: Can we afford it? Is it a million dollar solution for a thousand dollar problem? 3. Operational feasibility: Does the company possess the human, technical and financial resources to keep the system operational? Will there be resistance from users? 2.Analysis: A thorough audit of user requirements and understanding of system’s functional areas, actual and potential problems and opportunities. The logical design must specify the appropriate conceptual data model, inputs, processes and expected output requirements using tools such as DFDs,ER diagrams etc.. All data transformations (processes) are described and documented using such system analysis tools. 3.Detailed System design The design includes all the necessary technical specifications for the screens, menus, reports and other devices that might be used to help make the system more efficient information generator. 4. Implementation The hardware, DBMS software and application programs are installed and the database design is implemented. During the intial stages of implementation phase, the system enters into a cycle of coding, testing and debugging until it is ready to be delivered. The system will be in full operation by the end of this phase but will be continuously evaluated and fine-tuned. 5. Maintenance Maintenance includes all the activity after the installation of software that is performed to keep the system operational. Major forms of maintenance activities are fixing of errors fall under corrective maintenance. Adaptive maintenance due to changes in the business environment. Perfective maintenance to enhance the system. Page 54