You are on page 1of 45

P

a
g
e
3
4
Relational Database
Management System
Material
-By
Rahat and Yugander
INDEX
P
a
g
e
3
4
l. Basics of database 03
2. SQL 05
3. Joins 09
4. Views l0
5. Embedded SQL l2
6. Transactions l3
7. Online analytical processing (OLAP) l7
8. Miscellaneous Questions l9
9. Sample Queries 30
B ASICS OF DATABASE
1. !at is a "atabase#
P
a
g
e
3
4
Organised collection of interrelated data and the data in the database is integrated, can be
shared and can be accessed concurrently.
$. !at is "atabase management system %DBMS&#
DBMS is a collection of interrelated files and a set of programs that allow user to access
and modify this data.
'. !at is t!e main aim o( )sing a DBMS#
The main aim of using DBMS is to provide a convenient and efficient way to store, retrieve
and modify information
*. De(ine master (ile an" Transa+tion (ile.
A master file stores relatively static data about an entity and changes rarely and where as
transaction file stores relatively transient data about a particular data processing task and
changes more frequently as transactions happen more frequently and in large numbers
,. Disa"-antages o( tra"itional (ile system to in(ormation .ro+essing or a"-antages
o( DBMS.
Data security, data redundancy, data isolation, program/ data dependency, lack of flexibility
and concurrent access anomalies
/. De(ine "atabase s+!ema.
The overall design of database is called database schema.
0. !at is t!ree le-el ar+!ite+t)re (or a DBMS#
The 3 levels of DBMS architecture are: External/ view level, conceptual/ logical level and
internal/ physical level
1. State "i((erent )sers o( "atabase#
End user, application programmer and database administrator.
2. !at are t!e "i((erent ()n+tions o( DBA#
l. Defining conceptual schema
2. Defining the internal schema
3. Liaising with users
4. Granting of authorization for data access
5. Defining integrity constraints
13. !at is "ata mo"el# Name t4o 4i"ely )se" "ata mo"els#
A data model is a conceptual tool to describe data, data relationships, data semantics and
consistency constraints. Two widely used data models are
P
a
g
e
3
4
l. Object based logical model (E-R model).
2. Record based logical model.
11. De(ine t!e terms entity5 attrib)te5 relation.
Entity is a common word anything real or abstract, about which we want to store data.
Attribute is a characteristic property of an entity. An entity would have multiple attributes.
Relationship is a natural association that exists between one or more entities.
1$. !at is +ar"inality o( a relations!i.# !at are t!e "i((erent ty.es o( +ar"inality
relations!i.#
Cardinality of a relationship defines the type of relationship between two participating
entities. There are 4 types of cardinality relationship. Those are
l. One to one relationship
2. One to many relationship
3. Many to one relationship
4. Many to many
relationship
1'. !at is normalisation# !at is t!e nee" o( it#
Data base based on the E-R model may have some amount of
inconsistency, ambiguity and redundancy. To resolve these issues
some amount of refinement is required. This refinement process is
called as normalisation.
This normalisation technique is based on a mathematical foundation.
We need to refine our design so that we make an efficient database in
terms of storage space and insert, update and delete operations
1*. !at are t!e ty.es o( normalisation#
The following are the types of normal forms:
First Normal Form %1NF&6 A relation R is said to be in the first normal
form if and only if all the attributes of the relation R are atomic in nature.
Se+on" Normal Form %$NF&6 A relation is said to be in Second Normal
form if and only if, it is in the first normal form and no partial
dependency exists between non-key attributes and key attributes.
T!ir" Normal Form %'NF&6 A relation R is said to be in the third normal
form if and only if, it is in 3NF and no transitive dependency exists
between non-key attributes and key attributes.
P
a
g
e
3
4
Boy+e Co"" Normal Form %BCNF&6 A relation is said to be in BCNF if
and only if the determinants are candidate keys. BCNF relation is a
strong 3NF, but not every 3NF relation is BCNF.
1,. !at are t!e merits an" "emerits o( normali7ation#
Merits: l. Normalization is based on mathematical foundation. 2.
Removes the redundancy to the greater extent. After 3NF, data
redundancy is minimised to the extent of foreign keys. 3. Removes the
anomalies present in lnserts, Updates and Deletes.
Demerit: l. Data retrieval (Select) operation performance will be
severely affected. 2. Normalization may not always represent real world
scenarios.
1/. !at is a "eterminant#
Attribute X can be defined as determinant if it uniquely defines the
attribute value Y in given relationship or entity.
10. !at is RDBMS#
Relational Database: Any database for which the logical organization is
based on relational data model.
RDBMS: A DBMS that manages the relational database.
An RDBMS is type of DBMS that stores data in the form of related
tables.
11. Name some .o.)lar RDBMS .a+8ages.
Oracle- By Oracle Corp.
Sybase- Sybase lnc.
lnformix- lnformix Software lnc.
DB2- lBM
lngres- Computer Associates lnternational lnc.
SQL Server- Microsoft
12. !ere "o 4e )se RDBMS#
Database are widely used in real life applications such as:
Airlines, Banking, Universities, Telecommunications and Sales.
S 9:
$3. !at is S9:#
P
a
g
e
3
4
SQL stands for Structured Query Language. lt is a query language
used to communicate with a database
$1. !at is a "atabase ;)ery#
The process of requesting data from the database and receiving back
the results is called a database query.
$$. !at are t!e "i((eren+e bet4een +!ar an" -ar+!ar$ "ata
ty.es#
Ex. A. Name varchar2 (20): B. Name char (20)
lf you store string of length of l0 characters the varchar2 uses l0
character spaces and remaining memory is given to the OS where as in
case of char the remaining spaces will be wasted.
$'. !at are t!e S9: statements#
Data definition language (create, alter, drop, truncate)
Data manipulation language (insert, delete, update)
Data control language (grant, revoke)
Data querying/ retrieving language (select)
Transaction control language (commit, rollback)
$*. !at is a +onstraint# !at are t!e "i((erent +onstraints
a-ailable#
Constraint is defined as imposing predefined rules on table columns
There mainly two types of constraints. Those are
l. Column constraint: lt is specified as part of a column definition and
applies only to that column
2. Table constraint: lt is declared independently from a column
definition and can apply to more than one column in a table
These constraints must be used when constraint is applied for more
than one column of a table
$,. !at is a 8ey#
Key is a pre defined constraint. There are different keys are available.
Those are
P
a
g
e
3
4
l. Unique
2. Not null
3. Primary key
4. Foreign key
5. Check
6. Default
$/. !at is Can"i"ate 8ey#
A candidate key is a set of one or more attributes that can uniquely
identify a row in a given table.
$0. !at is Sim.le Can"i"ate <ey#
A candidate key comprising of one attribute only.
$1. !at is Com.osite Can"i"ate 8ey#
A candidate key comprising of two or more attributes.
$2. !at is in-ali" +an"i"ate 8ey#
A candidate key should be comprised of a set of attributes that can
uniquely identify a row. A subset of the attributes should not posses the
unique identification property.
'3. !at is =rimary 8ey#
During the creation of the table, the database designer chooses one of
the candidate key from amongst the several available, to uniquely
identify rows in the table. The candidate key so chosen is called the
primary key. The primary key of a table is always not null and unique.
'1. !at is (oreign 8ey#
P
a
g
e
3
4
A foreign key is a set of attribute(s) the values of which are required to
match the values of a candidate key in the same or another table. The
foreign key attribute(s) can have duplicate or null values.
'$. !at is s).er 8ey#
Any superset of a candidate key is a super key.
''. !at is non-8ey attrib)tes#
The attributes other than the primary key attributes in a table/relation
are called non-key attributes.
'*. !at is an in"e>#
An index is a structure that provides rapid access to the rows of a table
based on the values of one or more columns
',. !at are t!e a"-antages o( in"e>#
l. lt speeds up the execution of SQL statements with search conditions
that refer to the indexed column(s).
2. lt is most appropriate when retrieval of data from tables is more
frequent than inserts and updates.
'/. !at are t!e "isa"-antages o( !a-ing an in"e>#
(a) lt consumes additional disk space. (b) The index table must be
updated every time a row is added to the table and every time the
indexed column is updated in existing row. This imposes additional
overhead on insert and update statements for the table.
'0. ?o4 "o 4e s)..ress %a-oi"& ").li+ate ro4s in sele+t
statement#
Distinct is a key word. lt is used to suppress duplicate rows in select
statement output.
'1. !y "o 4e )se @4!ereA +la)se#
"Where" is a clause, it used to specify a search condition that limits the
number of rows retrieved.
P
a
g
e
3
4
'2. !at are t!e "i((erent o.erators t!at +an be )se" 4it!in 4!ere
+la)se#
The operators used with where are
l. Comparison operators (=,
<>, <, <=, >, >=)
2. Logical operators (and, or
,not)
3. Between, in, like
4. ls null, is not null
*3. !at is a NB:: -al)e#
A null value is used to indicate the absence of a value. lt is not a zero
or blank character. Null cannot be compared to any other value.
*1. !y "o 4e )se is n)ll5 is not n)ll o.erators#
lf it is needed to find out the rows, then we have to use is null operator
so that we get the rows whose one of the column values is null and in
order to retrieve the rows which does not have null as any column
value.
*$. !at are aggregate ()n+tions C +ol)mn ()n+tions#
A SQL column function takes an entire column of data as its arguments
and produces a single data item that summarizes the column.
Sum(), avg(), min(), max(), count() and count()
*'. !at is t!e "i((eren+e bet4een +o)nt%& an" +o)nt%D&#
Count () counts the non-null values in a column and where as count()
counts rows of query results and does not depend on the presence or
absence of null values in a column. lf there are no rows, it returns a
value of zero
**. !at are t!e "i((erent +la)ses a-ailable in S9:#
There are 4 clauses. Those are
l. where
2. group by
3. having
4. order by
*,. ?o4 "o 4e )se Egro). byF +la)se#
The group by clause is used in select statement to collect data across
multiple records and group the results by one or more columns.
*/. ?o4 is E!a-ingF +la)se is )se"#
P
a
g
e
3
4
The having clause is used along with the group by clause. The having
clause can be used to select and reject row groups. The having clause
specifies condition for groups.
*0. !at is )nion#
The union operation combines the rows from two sets of query results.
By default, the union operation eliminates duplicate rows as part of its
processing.
*1. !at is t!e "i((eren+e bet4een )nion an" )nion all#
By using Union all we retrieve all the records from both tables (it may
have some duplicates) where as by using union all the records from
both tables and there will not be any duplicates in query result.
*2. ?o4 "oes interse+t %o.eration& 4or8#
The intersect operation selects the common row from two sets of query
results.
,3. !at is a s)b ;)ery#
A query within a query is called sub query. lnner query is written in the
where clause and it is evaluated first and based on the result of that
outer query is evaluated.
,1. ?o4 "oes +o-relate" s)b ;)ery 4or8#
ln co-related sub-queries, SQL performs a sub query, once for each
row of the main query. The column(s) from the table of the outer query
is always referred in the inner clause.
G OINS
,$. !at is a Hoin#
Joins are used in queries to explain how different tables are related.
Joins also let you select data from a table depending upon data from
another table.
Types of joins: lNNER JOlNs, OUTER JOlNs, CROSS JOlNs. OUTER
JOlNs are further classified as LEFT OUTER JOlNS, RlGHT OUTER
JOlNS and FULL OUTER JOlNS.
,'. !at is a Cartesian HoinC +ross Hoin#
Cross joins return all rows the first table. Each row from the first table is
combined with all rows from the second table. Cross joins are also
known as the Cartesian product of two tables
,*. !at is a sel( Hoin#
P
a
g
e
3
4
Joining a table itself is known as self join
,,. !at is an inner Hoin#
An inner join between two(more) tables is the Cartesian product that
satisfies the join condition in the where clause. lnner join uses
comparison operators to match rows from two tables based on the
values in common columns from each table.
,/. !at is an e;)i Hoin#
Equi join is also an inner join in which the joining condition is based on
equality condition between values in the common columns
,0. !at is an o)ter Hoin#
An outer join is used to retrieve the rows with an unmatched value in
the relevant column
,1. !at are t!e "i((erent o)ter Hoins a-ailable#
There are 3 outer joins. Those are
l. Left outer join
2. Right outer join
3. Full outer join
,2. ?o4 "oes a le(t Hoin 4or8#
Left outer join includes null extended copies of the unmatched rows
from the first (left) table but does not include unmatched rows from the
second (right) table.
/3. !at "oes a rig!t o)ter Hoin in+l)"e#
The right outer join includes null extended copies of the unmatched
rows all the rows from the second (right) table and only common rows
from first table
/1. !at is t!e )se o( 8ey4or" E>ists#
The exists checks whether a sub query produces any row(s) of results.
lf a query following exists returns at least one row, the exists returns
true and stops further execution of the inner select statement. The
outer query will be executed only if the exists returns true. lf the inner
query produces no rows, the exists returns false and the outer query
will not be executed. The exists cannot produce a null value.
I IES
/$. !at is a -ie4#
P
a
g
e
3
4
A view is virtual table in the database defined by a query. A view does
not exist in the database as a stored set of data values.
/'. !at is a !ori7ontal -ie4#
Horizontal view restricts a user's access to selected rows of a table.
/*. !at is -erti+al -ie4#
Vertical view restricts a user's access to select columns of a table.
/,. !at is a Hoine" -ie4#
A joined view draws its data from two or three different tables and
presents the query results as a single virtual table. Once the view is
defined, one can use a single table query against the view for the
requests that would otherwise each require a two or three table join.
//. !at is t!e )se o( E+!e+8 o.tion Ein +ase o( -ie4s#
lf a view is created on specific a deptno, and while inserting values into
view it should check whether the deptno is correct deptno for which the
present view working for. This will be taken care if the view is created
with check option.
/0. !at are t!e a"-antages o( -ie4s#
Security: security is provided to the data base to the user to a specific
no. of rows of a table.
Query simplicity: by using joined views data can be accessed from
different tables.
Data integrity: if data is accessed and entered through a view, the
DBMS can automatically check the data to ensure that it meets
specified integrity constraints.
/1. !at are t!e "isa"-antages o( -ie4s#
Performance: The DBMS the query against the view into queries
against the underlying source table. lf a table is defined by a multi table
query, then even a simple query against a view becomes a complicated
join, and it may take a long time to complete. This is reference to insert,
delete and update operations
Update restrictions: when a user tries to update rows of a view, the
DBMS must translate the request into an update into an update on
rows of the underlying source table. This is possible for simple views,
but more complicated views cannot be updated.
/2. !y "o 4e )se "ata +ontrol lang)age statements# !at are
t!ose#
P
a
g
e
3
4
DCL statements are used to control access to the data base and the
data. lt is used to enforce data security. The DCL statements are
Grant, revoke.
03. !at is t!e )se o( grant statement#
The grant statement is used to grant security privileges on data base
objects to specific users. By using grant you can give permissions like
insert, delete and update.
01. !at is a grant statement 4it! a grant o.tion#
A grant statement with grant option clause conveys along with the
specified privileges, the right to grant those privileges to the other user.
0$. ?o4 "o 4e +an+el t!e .ri-ileges on t!e table#
By using revoke statement we can revoke the privileges previously
granted with the grant statement.
E MBEDDED S9:
0'. !at is t!e .)r.ose o( embe""e" S9:#
To blend SQL statements directly into a program written in a host
programming language. Such as C, Pascal, Cobol, Fortran use
embedded SQL.
0*. !y "o 4e )se embe""e" S9:#
SQL has the following limitations
l. No provision to declare
variable
2. No unconditional branching
3. No if statement to test
conditions
4. No for loop, do or while
statements to construct loops
5. No block structure
0,. !at is EXEC S9:#
Every embedded SQL statement begins with an introducer that flags it
as a SQL statement. The lBM SQL products use the introducer EXEC
SQL for most host languages
0/. !at is S9:CA#
The SQLCA (the SQL communication area) is data structure that
contains error variables and status indicator. By examining SQLCA, the
application program can determine the success or failure of its
embedded SQL statements.
P
a
g
e
3
4
00. De(ine !ost -ariable#
A host variable is a program variable. lt is defined using the data types
of the programming language like C and manipulated by the
programming language. To identify the host variable, the variable is
prefixed by a colon (:) when it appears in embedded SQL statements.
01. !at is t!e )se o( !ost -ariables#
The uses of host language variables are
l. To store data into the database
2. To retrieve data from the database
02. !at are t!e in"i+ator -ariables#
To store null values in the database or retrieve null values from the
database, embedded SQL allows each variable to have a companion
host indicator variable.
13. !at are t!e "i((erent -al)es asso+iate" 4it! in"i+ator
-ariable#
ln the embedded SQL statement, the host variable and lndicator
variable together specify a single SQL-style value as follows
l. An indicator value of zero indicates that the host variable contains a
valid value
2. A negative indicator value indicates that the host variable should be
assumed to have a null value; the actual value of the host variable is
irrelevant and should be disregarded
3. A positive indicator value indicates that the host variable contains a
valid value which may have been rounded off or truncated
T RANSACTIONS
11. !at is a transa+tion# E>.lain t!e terms asso+iate" 4it! it#
A transaction is nothing but an interaction between different users, or
different systems or user and system.
Transaction is a logical unit of work which takes database from one
consistent state to another consistent state.
A transaction cannot be divided into smaller tasks
The successful completion of a transaction is called as the commit
state. After this changes are permanent and irreversible
P
a
g
e
3
4
lf one step fails, the complete transaction fails and the system is taken
back to the original state that existed before the start of the transaction.
This process of going back to the original state is called as rollback.
lf the transaction rolls back, then the transaction is reaches the abort
state.
1$. !at is a transa+tion .ro+essing system# !at are t!e
"i((erent transa+tion systems are t!ere#
The transaction processing (TP) systems which mimic the real life
system like salary processing, library, banking, air line,
defence missile systems are basically divided into three categories.
Those are
l. Batch transaction Systems
2. Online transaction processing systems
3. Real time transaction processing systems
1'. !at is a bat+! transa+tion .ro+essing systems#
ln the batch transaction processing system, a set of application
programs will be working on a set of input data to produce desired
output. ln this process there will be absolutely no human interaction.
Ex. Salary slip generation
1*. !at is online transa+tion .ro+essing system %O:T=&#
ln the OLTP systems, the user will be continuously interacting with
system through a computer or a terminal on a regular basis.
Ex. Air line reservation, banking ATM
1,. !at is Real time transa+tion .ro+essing system#
This system is the most complicated among all the transaction
systems. lt is capable of handling unexpected inputs to unexpected
output
Ex. Air traffic control systems or missile defence system
And even these are systems are capable of handling a sudden change
in the air pressure, the temperature, the wind direction, the target
speed and the direction and can change their output based on these.
1/. !at are t!e .ro.erties o( a transa+tion#
Every transaction must these properties. Those are
l. Atomicity 2. Consistency
P
a
g
e
3
4
3. lsolation 4. Durability
Most of the times we call these as AClD properties.
10. !at is ")rability in +ase o( a transa+tion#
Once a transaction completes (commits), the changes made to the
database permanent and are available to all the transactions that follow
it.
11. !at are t!e re;)irements (or an O:T= system#
ln addition to AClD properties, OLTP systems require integrity and
concurrency.
12. !at is integrity# !at are t!e "i((erent integrity +!e+8s#
All the data entered into the system must be validated for its
correctness and adherence to the organisation's business rules. This is
implemented in RDBMS using three types of integrity checks. Those
are
l. Domain integrity
2. Entity integrity
3. Referential integrity
23. !at is "omain integrity +!e+8#
lt involves the implementation of business domain specific rules.
21. ?o4 is an entity integrity +!e+8 im.lemente"#
Entity integrity check is implemented using the primary key constraint.
Basically entity integrity refers to the fact that a particular attribute
uniquely indentifies the physical entity.
2$. ?o4 is re(erential integrity im.lemente"#
lt is implemented using the relationships between primary keys and
foreign keys of tables within a database. This ensures data
consistency. Referential integrity requires that the value of every
foreign key in every table be matched by the value of a primary key in
another table. This relationship is called parent-child relationship.
2'. !at are t!e "i((erent restri+tions t!at 4e +an .)t on t!e
(oreign 8ey at t!e time o( +reation#
l. On delete restrict: do not allow delete the parent table data if it is
referred in the child table.
P
a
g
e
3
4
2. On delete set null: on delete the parent table data, set null value in
the child table wherever the deleted data is referred.
3. On delete set default: set the null values to child rows on deletion of
parent records.
4. On delete set cascade: delete all the child table record from the
parent table on the deletion of parent record in the parent table.
2*. !at is +on+)rren+y#
Concurrency means allowing different users transactions to execute
simultaneously.
2,. !at is lo+8ing#
Locking is a mechanism to have a controlled access to the resources
like database, Table space, table, rows and columns. While these
resources are put under lock by some transaction, other transactions
have very restricted or no access to these resources, depending on the
locking mode.
2/. !at is t!e .rimary aim o( im.lementing lo+8s on a table#
To achieve consistency while supporting concurrency of transactions.
20. !at is a s!are" lo+8#
When a particular table is locked in the shared mode by one
transaction, all the other transactions can perform the read operation
on the locked resource, but no updates or modifications are possible by
other transactions.
21. !at is a s!are" lo+8#
This is most restrictive lock. Once a transaction puts the x lock on a
particular resource, no other transaction can put any kind of lock on this
resource. This resource is exclusively reserved for first transaction.
This x lock allows the least concurrency.
Usually insert/ update/ delete operations put the x lock on resources
before writing /modifying /deleting operations.
22. !at is gran)larity o( lo+8ing#
Granularity of refers to the granular at which a resource can be locked.
ln RDBMS application is capable of locking a table explicitly, then the
granularity of locking at field level. lf it can lock only up to the row level,
the granularity of that is RDBMS product is row level.
133. !at is intent lo+8ing#
P
a
g
e
3
4
ln the intent locking only the intention of locking is expressed at the
ancestor node of the required resource and the resource at the lower
level is locked explicitly only when required.
The intent locking mechanism not only increases concurrency but also
stops the implicit locking of ancestral resources.
131. ?o4 many 4ays is intent lo+8ing +lassi(ie"#
lntent locking is classified as intent shared lock and intent exclusive
lock.
13$. !at is s!are" intent lo+8#
The combination of shared and intent exclusive lock is referred to as
shared intent exclusive lock or SlX lock.
A share and intent exclusive lock indicates an S lock at the current level
plus an intention to insert, delete, update data at lower level of
granularity.
13'. !at is a Dea" lo+8#
Dead lock is situation where one transaction is waiting for another
transaction to release the resource it needs, and vice versa. Each
transaction will be waiting for the other for the other to release the
resource.
13*. !at is time stam.ing#
This is one of the concurrency management techniques. Every
resource in database will be associated with last successful read and
last successful write timestamp (time of occurrence up to milliseconds)
13,. !at is t!e biggest a+!ie-ement o( time stam.ing#
The biggest advantage of time stamping is it leads to no dead lock
condition as no resources are locked.
13/. !y is time stam.ing not )se" in RDBMS al4ays#
Time stamping technique leads to large number of rollbacks. Due to
this reason time stamping technique is not implemented as the
concurrency control mechanism in most of the commercial RDBMS
application.
130. !i+! one is t!e 4i"ely )se" +on+)rren+y +ontrol met!o"#
Almost all the commercial RDMBS packages use a locking technique
as the concurrency controlling mechanism while maintaining the
consistency in the system.
131. ?o4 "o 4e im.lement se+)rity in RDBMS .a+8ages#
P
a
g
e
3
4
Security is implemented in RDBMS packages using:
l. User id and pass word to restrict the users from acquiring an
unauthorised access.
2. Grant and revoke statements to provide restrict access control to
resources like tables.
3. Database views to restrict access to sensitive data.
4. Encryption of data to avoid unauthorised access.
132. !en "oes a "ata base go into an in+onsistent state#
A data base may go in an inconsistent state by
l. An application error
2. Power failure
3. OS or database failure
113. !at is re+o-ery#
lf the database is in inconsistent state, it is necessary to restore it to a
consistent state. Recovery process can be achieved either using files
or backups of the database.
111. !at are t!e "i((erent ba+8). te+!ni;)es a-ailable#
l. Dumping
2. Cold backup
3. Hot backup
11$. !at is an instan+e (ail)re in RDBMS#
l. Un-planned power shut down
2. Sudden break down in OS or database
3. Memory failure
11'. !at is a transa+tion log#
Transactional log or the journal log or redo log is physical life. lnstance
failures can be taken handled by making use of transactional log or
redo files.
O N:INE ANA:JTICA: =ROCESSINK %O:A=&
11*. !at is an online analyti+al .ro+essing %O:A=&#
P
a
g
e
3
4
An organisation's success also depends on its ability to analyze data
and to make intelligent decisions that would potentially affect its future.
Systems that facilitate such analysis are called OLAP.
11,. !at is t!e "i((eren+e bet4een O:T= an" O:A=#
ln OLAP we take the historical data stored to enable trend analysis and
future predictions. We will de normalize databases to facilitate queries
and analysis. We won't have frequent updates. Joins will be simple as
tables are de normalized.
ln OLTP Old data is purged or archived. We use normalized databases
to facilitate insertion, deletion, and updation. Updates are more
common and the joins are more as the tables are normalized.
11/. !at is a "ata 4are !o)se#
A data ware house is repository which stores integrated information for
efficient querying and analysis. Data ware house has data collected
from multiple, disparate sources of an organisation.
110. !at is t!e nee" (or going "ata 4are !o)sing#
l. Analysis requires millions of records of data which are historical in
nature.
2. Data is collected from heterogeneous sources
3. Need to make quick and effective strategic decisions
111. !at are t!e +!ara+teristi+s o( "ata 4are !o)se#
According to Bill lnmon, known as the father of data ware housing, a
data ware house is a subject oriented, integrated, time variant, non
volatile collection of data in support of management decisions.
112. !at is ET: in "ata 4are !o)sing#
ETL stands for Extraction, transformation and loading. lt is very
important step in data ware housing. The definition of ETL: it is
described as the process of selecting, migrating, transforming, cleaning
and converting mapped data from the operational environment to data
warehousing environment.
1$3. !at is a (a+t table#
Each data ware house or data mart includes one or more fact tables. A
fact table is central to a star or snowflake schema, and captures the
data that measures the organisations business operations. Fact tables
generally contain large number of rows.
1$1. !at is a "imension table#
P
a
g
e
3
4
Dimension table contains attributes that describe fact records in the fact
table. Some of these attributes provide descriptive information.
1$$. !at is +)be# An" 4!at is t!e )se o( it#
The OLAP tools allows you to turn data stored in relational databases
into meaningful, easy to navigate business information by creating data
cube. The dimensions of data a cube represent distinct categories for
analyzing business data.
M ISCE::ANEOBS 9BESTIONS6
1. De(ine t!e LIntegrity r)lesL
There are two lntegrity rules.
Entity lntegrity: States that "Primary key cannot have NULL value"
Referential lntegrity: States that "Foreign Key can be either a NULL
value or should be Primary Key value of other relation.
$. !at is System R# !at are its t4o maHor s)bsystems#
System R was designed and developed over a period of l974-79 at
lBM San Jose Research Centre. lt is a prototype and its purpose was
to demonstrate that it is possible to build a Relational System that can
be used in a real life environment to solve real life problems, with
performance at least comparable to that of existing system.
lts two subsystems are
Research Storage and System Relational Data System.
'. !at is Data In"e.en"en+e#
Data independence means that "the application is independent of the
storage structure and access strategy of data". ln other words, the
ability to modify the schema definition in one level should not affect the
schema definition in the next higher level.
Two types of Data lndependence:
Physical Data lndependence: Modification in physical level should not
affect the logical level.
Logical Data lndependence: Modification in logical level should affect
the view level.
NOTE: Logical Data lndependence is more difficult to achieve
*. !at is a -ie4# ?o4 it is relate" to "ata in"e.en"en+e#
P
a
g
e
3
4
A view may be thought of as a virtual table, that is, a table that does not
really exist in its own right but is instead derived from one or more
underlying base table. ln other words, there is no stored file that direct
represents the view instead a definition of view is stored in data
dictionary.
Growth and restructuring of base tables is not reflected in views. Thus
the view can insulate users from the effects of restructuring and growth
in the database. Hence accounts for logical data independence.
,. !at is ea8 Entity set#
An entity set may not have sufficient attributes to form a primary key,
and its primary key compromises of its partial key and primary key of its
parent entity, then it is said to be Weak Entity set.
/. !at is an attrib)te#
lt is a particular property, which describes the entity.
0. !at are Relation S+!ema an" a Relation#
A relation Schema denoted by R (Al, A2, An) is made up of the relation
name R and the list of attributes Ai that it contains. A relation is defined
as a set of tuples. Let r be the relation which contains set tuples (tl, t2,
t3, tn). Each tuple is an ordered list of n-values t= (vl, v2, vn).
1. !at is 9)ery e-al)ation engine#
lt executes low-level instruction generated by compiler.
2. !at is F)n+tional De.en"en+y#
A Functional dependency is denoted by X Y between two sets of
attributes X and Y that are subsets of R specifies a constraint on the
possible tuple that can form a relation state r of R. The constraint is for
any two tuples tl and t2 in r if tl[X] = t2[X] then they have tl[Y] = t2[Y].
This means the value of X component of a tuple uniquely determines
the value of component Y.
13. !at is M)lti-al)e" "e.en"en+y#
Multivalued dependency denoted by X Y specified on relation schema
R, where X and Y are both subsets of R, specifies the following
constraint on any relation r of R: if two tuples tl and t2 exist in r such
that tl[X] = t2[X] then t3 and t4 should also exist in r with the following
properties
t3[x] = t4[X] = tl[X] = t2[X]
t3[Y] = tl[Y] and t4[Y] = t2[Y]
t3 [Z] = t2 [Z] and t4 [Z] = tl [Z]
P
a
g
e
3
4
where [Z = (R-(X U Y))]
11. !at is :ossless Hoin .ro.erty#
lt guarantees that the spurious tuple generation does not occur with
respect to relation schemas after decomposition.
1$. !at is F)lly F)n+tional "e.en"en+y#
lt is based on concept of full functional dependency. A functional
dependency X Y is fully functional dependency if removal of any
attribute A from X means that the dependency does not hold any more.
1'. !at is Domain-<ey Normal Form#
A relation is said to be in DKNF if all constraints and dependencies that
should hold on the constraint can be enforced by simply enforcing the
domain constraint and key constraint on the relation.
1*. !at are .artial5 alternate5 arti(i+ial5 +om.o)n" an" nat)ral
8ey#
Partial Key:
lt is a set of attributes that can uniquely identify weak entities and that
are related to same owner entity. lt is sometime called as Discriminator.
Alternate Key:
All Candidate Keys excluding the Primary Key are known as Alternate
Keys.
Artificial Key:
lf no obvious key either stands alone or compound is available, then
the last resort is to simply create a key, by assigning a unique number
to each record or occurrence. Then this is known as developing an
artificial key.
Compound Key:
lf no single data element uniquely identifies occurrences within a
construct, then combining multiple elements to create a unique
identifier for the construct is known as creating a compound key.
Natural Key:
When one of the data elements stored within a construct is utilized as
the primary key, then it is called the natural key.
1,. !at is in"e>ing an" 4!at are t!e "i((erent 8in"s o( in"e>ing#
P
a
g
e
3
4
lndexing is a technique for determining how quickly specific data can
be found.
Types:
Binary search style indexing
B-Tree indexing
lnverted list indexing
Memory resident table
Table indexing
1/. !at is meant by ;)ery o.timi7ation#
The phase that identifies an efficient execution plan for evaluating a
query that has the least estimated cost is referred to as query
optimization.
10. !at "o yo) mean by atomi+ity an" aggregation#
Atomicity:
Either all actions are carried out or none are. Users should not have to
worry about the effect of incomplete transactions. DBMS ensures this
by undoing the actions of incomplete transactions.
Aggregation:
A concept which is used to model a relationship between a collection of
entities and relationships. lt is used when we need to express a
relationship among relationships.
11. !at is a +!e+8.oint an" !en "oes it o++)r#
A Checkpoint is like a snapshot of the DBMS state. By taking
checkpoints, the DBMS can reduce the amount of work to be done
during restart in the event of subsequent crashes.
12. !at is Ltrans.arent DBMSL#
lt is one, which keeps its Physical Structure hidden from user.
$3. !at is RDBMS <ERNE:#
Two important pieces of RDBMS architecture are the kernel, which is
the software, and the data dictionary, which consists of the system-
level data structures used by the kernel to manage the database
P
a
g
e
3
4
You might think of an RDBMS as an operating system (or set of
subsystems), designed specifically for controlling data access; its
primary functions are storing, retrieving, and securing data. An RDBMS
maintains its own list of authorized users and their associated
privileges; manages memory caches and paging; controls locking for
concurrent resource usage; dispatches and schedules user requests;
and manages space usage within its table-space structures.
$1. ?o4 "o yo) +omm)ni+ate 4it! an RDBMS#
You communicate with an RDBMS using Structured Query Language
(SQL)
$$. De(ine S9: an" state t!e "i((eren+es bet4een S9: an" ot!er
+on-entional .rogramming :ang)ages
SQL is a nonprocedural language that is designed specifically for data
access operations on normalized relational database structures. The
primary difference between SQL and other conventional programming
languages is that SQL statements specify what data operations should
be performed rather than how to perform them.
$'. !at is an Ora+le Instan+e#
The Oracle system processes, also known as Oracle background
processes, provide functions for the user processes-functions that
would otherwise be done by the user processes themselves
Oracle database-wide system memory is known as the SGA, the
system global area or shared global area. The data and control
structures in the SGA are shareable, and all the Oracle background
processes and user processes can use them.
The combination of the SGA and the Oracle background processes is
known as an Oracle instance
$*. !at is ROID#
The ROWlD is a unique database-wide physical address for every row
on every table. Once assigned (when the row is first inserted into the
database), it never changes until the row is deleted or the table is
dropped.
The ROWlD consists of the following three components, the
combination of which uniquely identifies the physical storage location of
the row.
Oracle database file number, which contains the block with the rows
Oracle block address, which contains the row
The row within the block (because each block can hold many rows)
P
a
g
e
3
4
The ROWlD is used internally in indexes as a quick means of retrieving
rows with a particular key value. Application developers also use it in
SQL statements as a quick way to access a row once they know the
ROWlD
$,. !at is "atabase Trigger#
A database trigger is a PL/SQL block that can defined to automatically
execute for insert, update, and delete statements against a table. The
trigger can e defined to execute once for the entire statement or once
for every row that is inserted, updated, or deleted. For any one table,
there are twelve events for which you can define database triggers. A
database trigger can call database procedures that are also written in
PL/SQL.
$/. Name t4o )tilities t!at Ora+le .ro-i"es5 4!i+! are )se (or
ba+8). an" re+o-ery.
Along with the RDBMS software, Oracle provides two utilities that you
can use to back up and restore the database. These utilities are Export
and lmport.
The Export utility dumps the definitions and data for the specified part
of the database to an operating system binary file. The lmport utility
reads the file produced by an export, recreates the definitions of
objects, and inserts the data
lf Export and lmport are used as a means of backing up and recovering
the database, all the changes made to the database cannot be
recovered since the export was performed. The best you can do is
recover the database to the time when the export was last performed.
$0. !at are store"-.ro+e")res# An" 4!at are t!e a"-antages o(
)sing t!em.
Stored procedures are database objects that perform a user defined
operation. A stored procedure can have a set of compound SQL
statements. A stored procedure executes the SQL commands and
returns the result to the client. Stored procedures are used to reduce
network traffic.
$1. Does =:CS9: s)..ort Lo-erloa"ingL# E>.lain
The concept of overloading in PL/SQL relates to the idea that you can
define procedures and functions with the same name. PL/SQL does not
look only at the referenced name, however, to resolve a procedure or
function call. The count and data types of formal parameters are also
considered.
PL/SQL also attempts to resolve any procedure or function calls in
locally defined packages before looking at globally defined packages or
internal functions. To further ensure calling the proper procedure, you
P
a
g
e
3
4
can use the dot notation. Prefacing a procedure or function name with
the package name fully qualifies any procedure or function reference.
$2. !at are +)rsors gi-e "i((erent ty.es o( +)rsors#
PL/SQL uses cursors for all database information accesses statements.
The language supports the use two types of cursors
lmplicit, and Explicit.
'3. !at is "e-normali7ation an" 4!en 4o)l" yo) go (or it#
As the name indicates, de-normalization is the reverse process of
normalization. lt's the controlled introduction of redundancy in to
the database design. lt helps improve the query performance as the
number of joins could be reduced.
'1. !atMs t!e "i((eren+e bet4een a .rimary 8ey an" a )ni;)e
8ey#
Both primary key and unique enforce uniqueness of the column on
which
they are defined. But by default primary key creates a clustered index
on the column, where are unique creates a non-clustered index by
default. Another major difference is that, primary key doesn't allow
NULLs, but unique key allows one NULL only.
'$. !at are )ser "e(ine" "ata ty.es an" 4!en yo) s!o)l" go (or
t!em#
User defined data types let you extend the base SQL Server data types
by
providing a descriptive name, and format to the database. Take for
example, in your database, there is a column called Flight_Num which
appears in many tables. ln all these tables it should be varchar(8).
ln this case you could create a user defined data type called
Flight_num_type of varchar(8) and use it across all your tables.
''. !at are "e(a)lts# Is t!ere a +ol)mn to 4!i+! a "e(a)lt +anMt be
bo)n"#
A default is a value that will be used by a column, if no value is
supplied to that column while inserting data. lDENTlTY columns and
timestamp columns can't have defaults bound to them.
'*. !at is a transa+tion an" 4!at are ACID .ro.erties#
A transaction is a logical unit of work in which, all the steps must
be performed or none. AClD stands for Atomicity, Consistency,
lsolation, Durability. These are the properties of a transaction.
',. !atMs t!e "i((eren+e bet4een DE:ETE TAB:E an" TRBNCATE
TAB:E +omman"s#
P
a
g
e
3
4
DELETE TABLE is a logged operation, so the deletion of each row gets
logged in the transaction log, which makes it slow. TRUNCATE TABLE
also deletes all the rows in a table, but it won't log the deletion of
each row, instead it logs the deallocation of the data pages of the
table, which makes it faster. Of course, TRUNCATE TABLE can be
rolled
back.
'/. !at are +onstraints# Name "i((erent ty.es o( +onstraints.
Constraints enable the RDBMS enforce the integrity of the database
automatically, without needing you to create triggers, rule or defaults.
Types of constraints: NOT NULL, CHECK, UNlQUE, PRlMARY KEY,
FORElGN KEY
37. !at are t!e ste.s yo) 4ill ta8e to im.ro-e .er(orman+e o( a
.oor
.er(orming ;)ery#
There could be a lot of reasons
behind the poor performance of a query. But some general issues that
you could talk about would be: No indexes, table scans, missing or out
of date statistics, blocking, excess recompilations of stored
procedures, procedures and triggers without SET NOCOUNT ON,
poorly
written query with unnecessarily complicated joins, too much
normalization, excess usage of cursors and temporary tables.
Some of the tools/ways that help you troubleshooting performance
problems are: SET SHOWPLAN_ALL ON, SET SHOWPLAN_TEXT
ON, SET
STATlSTlCS lO ON, SQL Server Profiler, Windows NT /2000
Performance
monitor, Graphical execution plan in Query Analyzer.
'1. !at are +)rsors# E>.lain "i((erent ty.es o( +)rsors. !at are
t!e
"isa"-antages o( +)rsors# ?o4 +an yo) a-oi" +)rsors#
Cursors allow row-by-row processing of the result sets.
Types of cursors: Static, Dynamic, Forward-only, Keyset-driven.
Disadvantages of cursors: Each time you fetch a row from the cursor,
it results in a network roundtrip; where as a normal SELECT query
makes only one roundtrip, however large the result set is. Cursors are
also costly because they require more resources and temporary
storage
(results in more lO operations). Further, there are restrictions on
the SELECT statements that can be used with some types of cursors.
P
a
g
e
3
4
Most of the times, set based operations can be used instead of
cursors. Here is an example:
lf you have to give a flat hike to your employees using the following
criteria:
Salary between 30000 and 40000 -- 5000 hike
Salary between 40000 and 55000 -- 7000 hike
Salary between 55000 and 65000 -- 9000 hike
ln this situation many developers tend to use a cursor, determine each
employee's salary and update his salary according to the above
formula. But the same can be achieved by multiple update statements
or
can be combined in a single UPDATE statement as shown below:
UPDATE tbl_emp SET salary =
CASE WHEN salary BETWEEN 30000 AND 40000 THEN salary +
5000
WHEN salary BETWEEN 40000 AND 55000 THEN salary + 7000
WHEN salary BETWEEN 55000 AND 65000 THEN salary + l0000
END
Another situation in which developers tend to use cursors: You need to
call a stored procedure when a column in a particular row meets
certain condition. You don't have to use cursors for this. This can be
achieved using WHlLE loop, as long as there is a unique key to
identify each row. For examples of using WHlLE loop for row by row
processing, check out the 'My code library' section of my site or
search for WHlLE.
'2. !at are triggers# ?o4 many triggers yo) +an !a-e on a
table# ?o4 to
in-o8e a trigger on "eman"#
Triggers are special kind of stored procedures that get executed
automatically when an lNSERT, UPDATE or DELETE operation takes
place
on a table.
ln SQL Server 6.5 you could define only 3 triggers per table, one for
lNSERT, one for UPDATE and one for DELETE. From SQL Server 7.0
onwards, this restriction is gone, and you could create multiple
triggers per each action. But in 7.0 there's no way to control the
order in which the triggers fire. ln SQL Server 2000 you could specify
which trigger fires first or fires last using sp_settriggerorder
Triggers can't be invoked on demand. They get triggered only when an
associated action (lNSERT, UPDATE, DELETE) happens on the table
on
which they are defined.
P
a
g
e
3
4
Triggers are generally used to implement business rules, auditing.
Triggers can also be used to extend the referential integrity checks,
but wherever possible, use constraints for this purpose, instead of
triggers, as constraints are much faster.
Till SQL Server 7.0, triggers fire only after the data modification
operation happens. So in a way, they are called post triggers. But in
SQL Server 2000 you could create pre triggers also. Search SQL
Server
2000 books online for lNSTEAD OF triggers.
Also check out books online for 'inserted table', 'deleted table' and
COLUMNS_UPDATED ()
There is a trigger defined for lNSERT operations on a table, in an
OLTP system. The trigger is written to instantiate a COM object and
pass the newly inserted rows to it for some custom processing. What
do you think of this implementation? Can this be implemented better?
lnstantiating COM objects is a time consuming process and since you
are doing it from within a trigger, it slows down the data insertion
process. Same is the case with sending emails from triggers. This
scenario can be better implemented by logging all the necessary data
into a separate table, and have a job which periodically checks this
table and does the needful.
*3. !at is aggregation5 "e+om.osition5 generali7ation in
RDBMS#
Aggregation: Selecting the data in group of records is called
aggregation.
There are five aggregate system functions they are viz. Sum, Min, Max,
Avg, Count. They all have their own purpose.
Decomposition: Selecting all data without any grouping and aggregate
functions is called Decomposition. The data is selected, as it is present
in the table.
Generalization: while generalization seems to be simplification of data,
i.e. to bring the data from Un-normalized form to normalized form.
*1. !at is t!e "i((eren+e bet4een -ie4 an" Hoin#
View is a virtual table where data is not stored physically but gives the
convenient method to retrieve and manipulate the information as
needed. lt is done for SECURlTY REASONS.
Join is joining of two related tables.
*$. Is it .ossible to !a-e .rimary 8ey an" (oreign 8ey in one table
i( yes so 4!at is t!e )se o( (oreign 8ey#
P
a
g
e
3
4
Yes, we can have.
Foreign key is used to make relationship with another table. We can
think foreign key as a copy of primary key from another relational table.
*'. !at is t!e "i((eren+e bet4een =rimary <ey an" Aggregate
<ey
!at is t!e "i((eren+e bet4een -ar+!ar an" -ar+!ar$#
Primary Key is a much similar to unique key. Only difference is that
unique key can be null but primary key cannot be null. Primary key is
used to avoid duplication of data.
A primary key consists of more than one column. Also known as
a concatenated key or Aggregate Key. it is also called as composite
key.
Example for varchar & varchar2()
Emp_name varchar(l0) - if you enter value less than l0 then
remaining space cannot be deleted. it used total l0 spaces.
Emp_name varchar2(l0) - if you enter value less than l0 then
remaining space is automatically deleted.
BJTRE**. !at is "i((eren+e bet4een RDBMS an" DBMS#
DBMS includes the theoretical part that how data's are stored in a
table. lt does not relate tables with another. While RDBMS is the
procedural way that includes SQL syntaxes for relating tables with
another and handling data's stored in tables. DBMS doesn't show the
relation while RDBMS show the relation and moreover DBMS is for
small organisations where RDBMS for large amount of data ln DBMS
all the tables are treated as different entities. There is no relation
established among these entities. But the tables in RDBMS are
dependent and the user can establish various integrity constraints on
these tables so that the ultimate data used by the user remains correct.
ln DBMS there are entity sets in the form of tables but relationship
among them is not defined while in RDBMS in each entity is well
defined with a relationship set so as retrieve our data fast and easy.
*,. E>.lain Co""Ms r)le.
ln l985, Edgar Codd published a set of l3 rules which he defined as an
evaluation scheme for a product which claimed to be a Relational
DBMS. And they are:
Rule l : The information Rule.
"All information in a relational data base is represented explicitly at the
logical level and in exactly one way - by values in tables."
P
a
g
e
3
4
Everything within the database exists in tables and is accessed via
table access routines.
Rule 2 : Guaranteed access Rule.
"Each and every datum (atomic value) in a relational data base is
guaranteed to be logically accessible by resorting to a combination of
table name, primary key value and column name."
To access any data-item you specify which column within which table it
exists, there is no reading of characters l0 to 20 of a 255 byte string.
Rule 3 : Systematic treatment of null values.
"Null values (distinct from the empty character string or a string of blank
characters and distinct from zero or any other number) are supported in
fully relational DBMS for representing missing information and
inapplicable information in a systematic way, independent of data type."
lf data does not exist or does not apply then a value of NULL is applied,
this is understood by the RDBMS as meaning non-applicable data.
Rule 4 : Dynamic on-line catalog based on the relational model.
"The data base description is represented at the logical level in the
same way as-ordinary data, so that authorized users can apply the
same relational language to its interrogation as they apply to the
regular data."
The Data Dictionary is held within the RDBMS, thus there is no-need
for off-line volumes to tell you the structure of the database.
Rule 5 : Comprehensive data sub-language Rule.
"A relational system may support several languages and various modes
of terminal use (for example, the fill-in-the-blanks mode). However,
there must be at least one language whose statements are expressible,
per some well-defined syntax, as character strings and that is
comprehensive in supporting all the following items
Data Definition
View Definition
Data Manipulation (lnteractive and by program).
lntegrity Constraints
Authorization.
Every RDBMS should provide a language to allow the user to query the
contents of the RDBMS and also manipulate the contents of the
RDBMS.
P
a
g
e
3
4
Rule 6 : .View updating Rule
"All views that are theoretically updatable are also updatable by the
system."
Not only can the user modify data, but so can the RDBMS when the
user is not logged-in.
Rule 7 : High-level insert, update and delete.
"The capability of handling a base relation or a derived relation as a
single operand applies not only to the retrieval of data but also to the
insertion, update and deletion of data."
The user should be able to modify several tables by modifying the view
to which they act as base tables.
Rule 8 : Physical data independence.
"Application programs and terminal activities remain logically
unimpaired whenever any changes are made in either storage
representations or access methods."
The user should not be aware of where or upon which media data-files
are stored
Rule 9 : Logical data independence.
"Application programs and terminal activities remain logically
unimpaired when information-preserving changes of any kind that
theoretically permit un-impairment are made to the base tables."
User programs and the user should not be aware of any changes to the
structure of the tables (such as the addition of extra columns).
Rule l0 : lntegrity independence.
"lntegrity constraints specific to a particular relational data base must
be definable in the relational data sub-language and storable in the
catalog, not in the application programs."
lf a column only accepts certain values, then it is the RDBMS which
enforces these constraints and not the user program, this means that
an invalid value can never be entered into this column, whilst if the
constraints were enforced via programs there is always a chance that a
buggy program might allow incorrect values into the system.
Rule ll : Distribution independence.
"A relational DBMS has distribution independence."
P
a
g
e
3
4
The RDBMS may spread across more than one system and across
several networks, however to the end-user the tables should appear no
different to those that are local.
Rule l2 : Non-subversion Rule.
"lf a relational system has a low-level (single-record-at-a-time)
language, that low level cannot be used to subvert or bypass the
integrity Rules and constraints expressed in the higher level relational
language (multiple-records-at-a-time)."
S AM=:E 9BERIES6
1.DD: STATEMENTS.
1. Create6 it is used to create any database object.
Ex. Create table emp (empno number(4), ename varchar2(20), sal
number(8,2), dept number(2));
$. Dro.6 it is used to delete database objects permanently.
Ex. Drop table emp;
'. Alter6 it is used to modify the data base objects. Like increasing
column size, adding a new column and deleting a column.
Ex. Alter table emp modify empno number(5);
Alter table emp add loc varchar2(20);
Alter table emp drop commission;
$. DM: STATEMENTS6
1. Insert 6 it is used to insert the values (or rows )into database.
a. lnsert into emp values(l00l,'yugander',50000,l0);
b. insert into(empno, ename, sal)values(l002,rahat,50000);
(here in above case null will be inserted for dept column)
c. insert into emp values(&empno,'&ename',&sal,&deptno);
Above statement is used insert more values into table. lt asks values
from the keyboard and after entering entire row press '\' so that we can
insert another row. After entering values, it should be committed.
$. Delete6
lt is used to delete data(rows) from the database objects. Mainly deals
with data(rows)
P
a
g
e
3
4
Delete from emp;
Delete from emp where deptno=l0;
( delete rows from deptno l0)
'. B."ate6
lt is used to update the table data. We can update single column or any
number of columns using a single update statement.
Update emp set sal=sal+3000 where empno =l00l;
lf the where condition is not mentioned then whole table will be
updated.
Update emp set sal=sal+2000, deptno=l0 where deptno=ll;
lt updates sal, deptno whose deptno is l0
'. TC: STATEMENTS6
Tcl statements are used to save or cancel the database transactions
1. Commit6
lt is used to save the transactions permanently to the database.
Ex. Dml statements
Commit;
Above dml statements will be saved permanently.
$. Rollba+86
it is used to cancel the dml statements.
Ex. Dml statements
Rollback;
Above dml statements will be cancelled
*. 4!ere +la)se6
There are many operators used in where clause to specify the
condition.
Consider this SQL statement and corresponding where clause
explanation follows.
Select from emp where[]
l. comparator operators
P
a
g
e
3
4
Ex. where empno=l00l;(= is comparison operator)( we can have <, >,
<>,>=,<=)
2. Special operators.
These are used to retrieve data very fast. These are
l. in, not in 2. Between, not
between
3. like, not like 4. ls null, is not null
Ex. l. Where empno in (l00l,l005,llll)
Above statement retrieves data from all the three records if three record
found else retrieves from the records found from the specified
empno.s.' not in' is just opposite of this
We can use 'in' statement with numbers, dates and characters.
2. Where joining _date between l2-aug-07 and 3l-dec-07;sss
where sal between l0000 and 20000;
ln the above statements between is used with numbers and dates.
Between takes both the extremes. And not between is opposite to
between.
3.where ename like 's%';
Above statement retrieves all the names which start with 's'
Above statement retrieves patterns. Like has got Meta characters i.e. _
and %
_ represents single character
% represents 0 or more characters
and 'like' always deals with characters only.
4. where sal is null;
Above statement retrieves the records whose sal is a null value . ls
null operator is used to compare null values. We can't compare a null
value with another null value. Where as 'is not null' is 0pposite of 'is
null' .
,. Bsing .re-"e(ine" ()n+tions
1. Arit!meti+ ()n+tions 6
select ename ,abs(commission-sal) from emp;
P
a
g
e
3
4
select power (2,4), sin(l0), ln (l0),log (l0,l00),exp(2),sign(5),round
(l23.25.2),trunc(l23.25.2),
mod (5,2) from dual;
ln the above statement, many arithmetic functions have been used.
These are already pre defined or built ins.
$. C!ara+ter ()n+tions6
Select initcap(S), lower(S), upper(S), length(S), reverse(S) from dual;
ln the above statement S stands for a string. lnitcap function gives
initials as the caps and remaining letters as small letters
Ex. lf input is yugander jetty and the output will be Yugander Jetty
And length function gives length of string.
Select ascii(c)(i.e. c is a character), chr(n), concat(sl,s2) from dual;s
The above statement ascii function gives the equivalent ascii value, +!r
fuction takes input as a number and gives its equivalent as+ii character
and +on+at is fuction used to concat 2 strings
'. Date ()n+tions6
These are applicable to dates only.
Select sysdate from the dual;
Select add_months(d,+ or n), months_between(dl,d2) from dual;
ln the above statements sysdate is pseudo column it gives the today's
date. Add_months () is a function used to add n months to the given
date d. Months_between () gives no.of months between given dates
*. Keneral ()n+tions6
Select least(l00,l0,7,99), greatest(l00,l0,7,99) from dual;
ln above statement, least() gives least value in the given list and
greatest() gives biggest of the list values.
Select vsize(ename) from dual;
Gives the memory occupied by that coloumn.
Select ename, job, deptno, decode( deptno, l0,sal0.20,20, sal0.35,
30, sal0.40) bonus from emp;
De+o"e %& in the above statement, for dept=l0, 20, 30 calculates the
bonus and generates a report on each person with his name, job,
deptno and bonus.
P
a
g
e
3
4
lt is a very important function to check multiple conditions while
manipulating and retrieving it is equivalent to i( statement in C
/. Kro). by +la)se6
it is used to group the rows based on specified column.
Select deptno,sum(sal) from emp group by deptno;
Above statements groups the rows based on deptno and gives the total
sal dept. Wise
0. ?a-ing +la)se6
Select job, count(), sum (sal), avg(sal) from emp where deptno=30
group by job
having count() > l ;
This select statement gives the output as , first it takes deptno=30 and
then groups according to job and checks for count after the grouping is
done.
Having clause is used to check on the grouped results.
1. Or"er by6
Select from emp order by sal desc;
This statement gives sal in descending order. Default order is
ascending for order by clause.
2. Goins6
l. Select empno, ename, sal, emp.deptno, dname, loc from emp, dept
where emp.deptno=dept.deptno;
This is an inner join (equi join). Here deptno is present in both the
tables. lt is needed to mention table name in front of column name.
This retrieves the rows from the both table which ever satisfy the
condition mentioned.
2. select fname, cname from faculty, course;
Here faculty, course are the two table with a single column and fname,
cname are the columns. fname is in faculty and cname is in course.
Above statement is a join called Cartesian join. lt is used to retrieve
data from more than one table without any condition. lt retrieves all the
possible combinations of rows.
3. select empno,ename, sal, emp.deptno, dname ,loc from emp, dept
where emp.deptno=dept.deptno(+);
P
a
g
e
3
4
This is called left outer join all the rows from the dept table and
common rows from the emp table
ln the above case lf + is on the left side then it is called right outer join
then common rows from the dept table and all the rows from the emp
table will be in output.
4.left+ right outer join will give you full outer join here '+' will be union
operator.
5. select worker .ename "subordinate" , manager.ename "superior"
from emp worker,emp superior where worker.mgr=superior.mgr;
Here emp table is joined to itself . it is acting once as worker table and
once as superior
Set o.erators
Select job from emp where deptno=l0
lntersect
Select job from emp where deptno=20;
This query gives the output as common job froml0 and 20 dept.s from
emp table.
Different set operators are union, union all, minus and intersect.
13. DATA CONTRO: :ANKBAKE6
l. Grant insert, delete on emp to userl;
this statements gives the permissions to userl;
2. grant all on emp to userl;
all means insert, delete, update permissions.
3. Revoke all on emp from userl;
Above statement takes back the permissions from userl;
4. grant all on emp to userl with grant option;
Userl can also give this table to other users
Creating )sers
DBA creates users
Create user y)gan"er identified by !elloN
Here user name is yugander and hello is his pass word and after this
he should be given resource to connect . so it will be as follows
P
a
g
e
3
4
Grant connect, resource to yugander;
11. SOME S=ECIA: DD: COMMANDS
l. rename emp to employ;
This query changes the table name emp to employ. This change is
permanent.
2. alter table emp rename column mgr to mgrno;
This changes the column name permanetntly.
3. truncate table emp;
lt deletes entire table data and just structure remains.
1$. SBB9BERIS
l. Select from emp where empno in(select distinct empno from incr);
Here first inner select will be executed and then based on that result,
outer query is evaluated.
2. select from emp where sal > any (select sal from emp where
deptno=l0);
Or
Select from emp where sal>(select min(sal) from the emp);
lt lists all the employees whose sal is more than lowest sal of deptno.
ln above query replace 'any' with 'all' and in the next query min() with
max() we will get max sal
1'. CO-RE:ATED SBB 9BERIES
Select empno, ename , sal , job, deptno from emp where sal>(select
avg(sal) from emp where deptno=e.deptno);
This lists the employees whose sal is more than their average salary.
1*. IM=ORTANT 9BERIES
1. To. !ig!ly .ai" em.loyees
Select rownum, empno,ename, job,sal from (select rownum,
empno,ename, job,sal from emp order by sal desc where rownum<=5);
$. N t! ma>. Salary
Select rownum, empno,ename, job,sal from (select rownum,
empno,ename, job,sal from emp order by sal desc ) group by rownum,
empno,ename,job, sal having rownum=&n;
P
a
g
e
3
4
'. Retrie-e Alternate Ro4s
Select rownum, empno,ename, job,sal from emp group by rownum,
empno,ename,job, sal having mod( rownum,2)=0;/ for even numbered
rows/
Select rownum, empno,ename, job,sal from emp group by rownum,
empno,ename,job, sal having mod( rownum,2)=l;/ odd numbered
rows/
*. Remo-ing ").li+ate ro4s
Delete from emp where rowid not in (select min(rowid) from emp group
by empno);
1,. CONSTRAINTS
1. Im.osing all t!e 8ey +onstraints %Col)mn le-el& at t!e time o(
table +reation.
Create table emp(empno number(4) primary key, ename varchar2(20)
not null, sex char(l) check( sex in('m', 'n')), job varchar2(20), hire_date
default sysdate, e-mail unique, deptno references dept(deptno));
Here in above query the key constraints are are imposed at the time of
creation. lt is a good practice to give name to the imposed constraints.
So this example after naming the constraints.
$. rite a ;)ery to a+ti-ate "e(a)lt +onstraint
Here consider that dept is table, at the time of creation of table we have
provided loc as Hyderabad.
So the query as follows
lnsert into dept values(30, 'import', default);
'. rite a ;)ery to e>.lain re(erential integrity
On "elete +as+a"e +la)se6
Consider table emp the attributes are empno, ename, deptno and this
deptno references to deptno of dept table.
Take one more table called incr, the attributes are empno, amount .
empno of incr references to empno of emp table.
First if i want to delete emp table it won't be deleted because child rows
will be existing. The same thing happens if dept table is deleted.
ln order to avoid these things it is needed to include on "elete
+as+a"e clause with reference key at the time of creating a table.
P
a
g
e
3
4
Ex. Create table dept(deptno number(2), dname varchar(20));
Create table emp(empno number(4) primary key, ename varchar2(20),
deptno number(2) references dept(deptno) on delete cascade);
Create table incr(empno number(4) references emp( empno) on delete
cascade, amount number(8,2));
lf the table are defined as shown above, there won't be problem if you
delete a table which has got children. lt automatically removes all the
child rows whenever parent record is removed.
Cas+a"e +onstraints6
lf this constraint is applied with reference key constraint, it
automatically removes foreign key constraint when parent table is
dropped.
Child rows will not be removed
Table +onstraints
The constraints defined next to table definition are called table
constraints.
Create table customer_details(
Cust_id_number(5) constraint Nnull Not Null,
Cust_name varchar2(20) constraint Nnull2 Not Null,
Account_num number(5),
Account_type varchar2(l0) constraint Nnull3 Not Null,
Bank_branch varchar2(25) constraint Nnull4 Not Null,
Cust_email varchar2(30),
Constraint Pkeyl primary key(cust_id,account_num));
1/. Mis+ellenio)s 9)eries
1. Kenerate serial n)mbers (or a table ro4s.
Select rownum, empno,ename ,sal from emp;
Here this numbering is not permanent. And this numbering is done with
the help of rownum.it is pseudo column.
$. 4!at is ro4e" # 4rite a ;)ery on it#
P
a
g
e
3
4
Row id is a unique stored permanently in database. lt is automatically
generated for every row inserted into the database. lt comprises of l8
bit value holds object id, block id, file id, record id.
Select rowid, ename from emp;
'. ?o4 "o 4e +reate a -ie4#
Create view view_emp as select from emp where deptno=l0;
View is stored select statement. lt will not hold any data. lt is virtual
table.
*. ?o4 "o "elete a -ie4#
Drop view view_emp;
,. !at is a (or+e -ie4#
A view can be created without having a table. That view is called force
view. Example is
Create force view view_emp as select from emp;
/. !at is materiali7e" -ie4 # 4rite a ;)ery#
lt is a static view. lt holds data in it. No DML is allowed on it. DML on
table will not reflect in view.
lt is created DBA, the query as follows..
Create materialized view mvi as select from emp;
0. !at is a synonym# rite a ;)ery on it#
lt is used to hide the original name and owner of the database object. lt
provides security while sharing by hiding identity of the component.
DML on synonym are reflected in the table.
Synonyms are two types.
l. Private synonyms. 2. Public synonyms
Create synonym esyn for emp;/ private synonym acts as view only/
Create public synonym stu_info for student;
After this creation we can give permission to users for accessing. The
user will not know where it has come from because with the same
name it will be accessed any where.
1. !at is se;)en+e# rite a ;)ery on it#
P
a
g
e
3
4
lt is used to generate numbers automatically. Not related to any table. lt
uses two pseudo columns
l. next val 2. Curr val
Create sequence sl incremented by l;
Select sl.next val from dual;
Select sl.curr val from dual;
2. !at is an in"e># rite a ;)ery on it#
lt is a pointer it locates physical address of data. lt will improve
performance while retrieving or manipulating data from the table. lt is
automatically activated when indexed column is referred in where
clause.
Create index idxl on emp( job);
We can have composite index.
Create index idx2 on emp( deptno, job);
13. !at is EroleF# 4rite a ;)ery on it#
lt is created by DBA.
lt holds the collection of permissions on default database objects to
stored.
For example there are three tables.
Create role rahat_role;
Grant all on emp to rahat_role;
Grant select, insert on dept to rahat_role;
Grant all on student to rahat_role ;
Grant role rahat_role to ugy;
Here rahat_role is a role i.e collection of permissions on different
tables. Ugy is a user
Rahat_role is given to the user ugy by the user rahat.
To delete a role
Drop role rahat_role;
Revoke all on emp from rahat_role;
The query gets back the permissions from the role rahat_role
P
a
g
e
3
4

You might also like