Dbms Notes
Dbms Notes
Concept of DBMS
Logical/ Conceptual level - This is the next level of abstraction. It describes the
actual data stored in the database in the form of tables and relates them by means of
mapping. This level will not have any information on what a user views at external
level. This level will have all the data in the database. Any changes done in this level
will not affect the external or physical levels of data. That is any changes to the table
structure or the relation will not modify the data that the user is viewing at the
external view or the storage at the physical level. For example, suppose we have
added a new column skills which will not modify the external view data on which
the user was viewing Ages of the students. Similarly, it will have space allocated for
Skills in the physical memory, but it will not modify the space or address of Date of
Birth (using which Age will be derived) in the memory. Hence external and physical
independence is achieved.
Physical level - This is the lowest level in data abstraction. This level describes how
the data is actually stored in the physical memory like magnetic tapes, hard disks etc.
In this level the file organization methods like hashing, sequential, B+ tree comes into
picture. At this level, developer would know the requirement, size and accessing
frequency of the records clearly. So designing this level will not be much complex for
him.
4. Data Models
Depending on the levels of data we are modeling, we have divided data models into 3
categories Object Based, Physical and Record based Data models. Basically physical data
model represents the data at data layer or internal layer. Object and Record based data models
are modeled based on the data at the application and user level. They are basically
responsible for designing various objects of the database, and their mappings. They are
further divided into different categories as shown in below diagram.
It would be little difficult to design the relationship between the entities, since all the entities
are related in some way. It requires thorough practice and knowledge about the designing.
4.2.3 Relational Data Models
This model is based on the mathematical concepts of set theory. It considers the tables as a
two dimensional table with rows and columns. It is least bothered about the physical storage
of structure and data in the memory. It considers only the data and how it can be represented
in the form of rows and columns, and the way it can establish the relation between other
tables.
A relational data model revolves around 5 important rules.
1. Order of rows / records in the table is not important. For example, displaying the
records for Joseph is independent of displaying the records for Rose or Mathew in
Employee table. It does not change the meaning or level of them. Each record in
the table is independent of other. Similarly, order of columns in the table is not
important. That means, the value in each column for a record is independent of
other. For example, representing DEPT_ID at the end or at the beginning in the
employee table does not have any affect.
2. Each record in the table is unique. That is there is no duplicate record exists in the
table. This is achieved by the use of primary key or unique constraint.
3. Each column/attribute will have single value in a row. For example, in Department
table, DEPT_NAME column cannot have Accounting and Quality together in a
single cell. Both has to be in two different rows as shown above.
4. All attributes should be from same domain. That means each column should have
meaningful value. For example, Age column cannot have dates in it. It should
contain only valid numbers to represent individuals age. Similarly, name columns
should have valid names, Date columns should have proper dates.
5. Table names in the database should be unique. In the database, same schema cannot
contain two or more tables with same name. But two tables with different names
can have same column names. But same column name is not allowed in the same
table.
Examine below table structure for Employee, Department and Project and see if it satifies
relational data model rules.
4.3 Physical Data Model: Physical data model represent the model where it describes how
data are stored in computer memory, how they are scattered and ordered in the memory, and
how they would be retrieved from memory. Basically physical data model represents the data
at data layer or internal layer.
6. Data Independence
The three-schema architecture can be used to explain the concept of data independence,
which can be defined as the capacity to change the schema at one level of a database system
without having to change the schema at the next higher level. We can define two types of data
independence:
Logical data independence is the capacity to change the conceptual schema without
having to change external schemas or application programs. We may change the
conceptual schema to expand the database (by adding a record type or data item), or
to reduce the database (by removing a record type or data item). In the latter case,
external schemas that refer only to the remaining data should not be affected. Only the
view definition and the mappings need be changed in a DBMS that supports logical
data independence.
Physical data independence is the capacity to change the internal schema without
having to change the conceptual (or external) schemas. Changes to the internal
schema may be needed because some physical files had to be reorganizedfor
example, by creating additional access structuresto improve the performance of
retrieval or update. If the same data as before remains in the database, we should not
have to change the conceptual schema.
7.1 Data Description Language (DDL): As the name suggests, this language is used to
define the various types of data in the database and their relationship with each other. The
basic functions performed by DDL are: -
Create tables, files, databases and data dictionaries.
Specify the storage structure of each table on disk.
Integrity constraints on various tables.
Security and authorization information of each table.
Specify the structure of each table.
Overall design of the Database.
7.2 Data Manipulation Language (DML): A language that enables users to access or
manipulate data (retrieve, insert, update, delete) as organized by a certain Data Model is
called the Data Manipulation Language (DML). It can be of two types: -
Procedural DML - It describes what data is needed and how to get it. For example: -
Relational Algebra.
Non Procedural DML - It describes what data is needed without specifying how to
get it. For example: - Relational calculus.
8. Database Users: Database users are the one who really use and take the benefits of
database. There will be different types of users depending on their need and way of accessing
the database.
1. Application Programmers - They are the developers who interact with the
database by means of DML queries. These DML queries are written in the
application programs like C, C++, JAVA, Pascal etc. These queries are converted
into object code to communicate with the database. For example, writing a C
program to generate the report of employees who are working in particular
department will involve a query to fetch the data from database. It will include a
embedded SQL query in the C Program.
2. Sophisticated Users - They are database developers, who write SQL queries to
select/insert/delete/update data. They do not use any application or programs to
request the database. They directly interact with the database by means of query
language like SQL. These users will be scientists, engineers, analysts who
thoroughly study SQL and DBMS to apply the concepts in their requirement. In
short, we can say this category includes designers and developers of DBMS and
SQL.
3. Specialized Users - These are also sophisticated users, but they write special
database application programs. They are the developers who develop the complex
programs to the requirement.
4. Naive Users - these are the users who use the existing application to interact with
the database. For example, online library system, ticket booking systems, ATMs etc
which has existing application and users use them to interact with the database to
fulfill their requests.
9. Database Administrator
One of the main reasons for using DBMSs is to have central control of both the data and the
programs that access those data. A person who has such central control over the system is
called a database administrator (DBA). The functions of a DBA include:
Schema definition. The DBA creates the original database schema by executing a set
of data definition statements in the DDL. Storage structure and access-method
definition.
Schema and physical-organization modification. The DBA carries out changes to
the schema and physical organization to reflect the changing needs of the
organization, or to alter the physical organization to improve performance.
Granting of authorization for data access. By granting different types of
authorization, the database administrator can regulate which parts of the database
various users can access. The authorization information is kept in a special system
structure that the database system consults whenever someone attempts to access the
data in the system.
Routine maintenance. Examples of the database administrators routine maintenance
activities are: Periodically backing up the database, either onto tapes or onto remote
servers, to prevent loss of data in case of disasters such as flooding. Ensuring that
enough free disk space is available for normal operations, and upgrading disk space as
required. Monitoring jobs running on the database and ensuring that performance is
not degraded by very expensive tasks submitted by some users.
Entity Sets
An entity set is a collection of similar types of entities. An entity set may contain entities with
attribute sharing similar values. For example, a Students set may contain all the students of a
school; likewise a Teachers set may contain all the teachers of a school from all faculties.
Entity sets need not be disjoint.
Attributes
Entities are represented by means of their properties, called attributes. All attributes have
values. For example, a student entity may have name, class, and age as attributes.
There exists a domain or range of values that can be assigned to attributes. For example, a
student's name cannot be a numeric value. It has to be alphabetic. A student's age cannot be
negative, etc.
Types of Attributes
Simple attribute Simple attributes are atomic values, which cannot be divided
further. For example, a student's phone number is an atomic value of 10 digits.
Composite attribute Composite attributes are made of more than one simple
attribute. For example, a student's complete name may have first_name and
last_name.
Derived attribute Derived attributes are the attributes that do not exist in the
physical database, but their values are derived from other attributes present in the
database. For example, average_salary in a department should not be saved directly in
the database, instead it can be derived. For another example, age can be derived from
data_of_birth.
Single-value attribute Single-value attributes contain single value. For example
Social_Security_Number.
Multi-value attribute Multi-value attributes may contain more than one values.
For example, a person can have more than one phone number, email_address, etc.
These attribute types can come together in a way like
simple single-valued attributes
simple multi-valued attributes
composite single-valued attributes
composite multi-valued attributes
12. Relationship
The association among entities is called a relationship. For example, an employee works_at a
department, a student enrolls in a course. Here, Works_at and Enrolls are called relationships.
Relationship Set
A set of relationships of similar type is called a relationship set. Like entities, a relationship
too can have attributes. These attributes are called descriptive attributes.
Degree of Relationship
The number of participating entities in a relationship defines the degree of the relationship.
Binary = degree 2
Ternary = degree 3
n-ary = degree
13. Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with the
number of entities of other set via relationship set.
One-to-one One entity from entity set A can be associated with at most one entity
of entity set B and vice versa.
One-to-many One entity from entity set A can be associated with more than one
entities of entity set B however an entity from entity set B, can be associated with at
most one entity.
Many-to-one More than one entities from entity set A can be associated with at
most one entity of entity set B, however an entity from entity set B can be associated
with more than one entity from entity set A.
Many-to-many One entity from A can be associated with more than one entity
from B and vice versa.
Attributes
Attributes are the properties of entities. Attributes are represented by means of ellipses. Every
ellipse represents one attribute and is directly connected to its entity (rectangle).
If the attributes are composite, they are further divided in a tree like structure. Every node is
then connected to its attribute. That is, composite attributes are represented by ellipses that
are connected with an ellipse.
Multivalued attributes are depicted by double ellipse.
Many-to-many The following image reflects that more than one instance of an
entity on the left and more than one instance of an entity on the right can be
associated with the relationship. It depicts many-to-many relationship.
Participation Constraints
Total Participation Each entity is involved in the relationship. Total participation is
represented by double lines.
Partial participation Not all entities are involved in the relationship. Partial
participation is represented by single lines.
15. Super Key: Super Key is an attribute or a composite attribute which functionally
determines all of the entitys attributes. In other words, a superkey uniquely identifies each
entity in a table.
Considering the above STUDENT Table, any of the following can be identified as the
tables superkey:
1. STU_NUM As it determines all other attributes in the table.
2. STU_NUM, STU_LNAME As the combination also determines all remaining attributes.
3. STU_NUM, STU_LNAME, STU_FNAME As this combination also determines all
remaining attributes.
In fact, STU_NUM whenever consider either alone or in any possible combination
with other attributes, realizes a superkey. This is even true if the additional attributes are
having redundant values in the tables record.
Candidate Key: Candidate Key is a superkey whose values are not repeated in the table
records. In other words, when the values in a superkey are not repeated in the tables records,
then such a key is called a candidate key.
Considering the above STUDENT Table, following facts can be revealed The
superkey attribute STU_NUM can also be termed as a candidate key because the values for
STU_NUM are not redundant in the given example The composite superkey (STU_NUM,
STU_LNAME) cannot be considered as a candidate key because STU_NUM by itself is a
candidate key The combination (STU_LNAME, STU_FNAME, STU_INIT, STU_PHONE)
can also be considered as a candidate key provided that the values under the combination are
not be repeated in the tables records.
Primary Key: Primary Key is a candidate key which doesnt have repeated values nor does it
comes with a NULL value in the table. A primary key can uniquely identifies each row in any
table, thus a primary key is mainly utilized for record searching.
A primary key in any table is both a superkey as well as a candidate key.
It is possible to have more than one choice of candidate key in a particular table
example. In that case, the selection of the primary key would be driven by the
designers choice or by end user requirements.
17. Generalization
Going up in this structure is called generalization, where entities are clubbed together to
represent a more generalized view. For example, a particular student named Mira can be
generalized along with all the students. The entity shall be a student, and further, the student
is a person. The reverse is calledspecialization where a person is a student, and that student is
Mira.
As mentioned above, the process of generalizing entities, where the generalized entities
contain the properties of all the generalized entities, is called generalization. In
generalization, a number of entities are brought together into one generalized entity based on
their similar characteristics. For example, pigeon, house sparrow, crow and dove can all be
generalized as Birds.
Specialization
Specialization is the opposite of generalization. In specialization, a group of entities is
divided into sub-groups based on their characteristics. Take a group Person for example. A
person has name, date of birth, gender, etc. These properties are common in all persons,
human beings. But in a company, persons can be identified as employee, employer,
customer, or vendor, based on what role they play in the company.
Similarly, in a school database, persons can be specialized as teacher, student, or a staff,
based on what role they play in school as entities.
Aggregation
Aggregation is a process when relation between two entities is treated as a single entity.
Look at below ER diagram of STUDENT, COURSE and SUBJECTS. Student attends the
Course, and he has some subjects to study. At the same time, Course offers some subjects.
Here a relation is defined on a relation. But ER diagram does not entertain such a relation. It
supports mapping between entities, not between relations.
If we look at STUDENT and COURSE from SUBJECTs point of view, it does not
differentiate both of them. It offers its subject to both of them. So what can we do here is,
merge STUDENT and COURSE as one entity. This process of merging is called aggregation.
It is completely different from generalization. In generalization, we merge entities of same
domain into one entity. In this case we merge related entities into one entity.
Here we have merged STUDENT and COURSE into one entity STUDENT_COURSE. This
new entity forms the mapping with SUBJECTS. The new entity STUDENT_COURSE, in
turn has two entities STUDENT and COURSE with Attends relationship.
20. First normal form (1NF): 1NF is a property of a relation in a relational database. A
relation is in first normal form if and only if the domain of each attribute contains
only atomic (indivisible) values, and the value of each attribute contains only a single value
from that domain.
For example consider a table which is not in First normal form
Student Table :
Student Age Subject
Alex 14 Maths
Stuart 17 Maths
In First Normal Form, any row must not have a column in which more than one value is
saved, like separated with commas. Rather than that, we must separate such data into multiple
rows.
Student Table following 1NF will be :
Student Age Subject
Adam 15 Biology
Adam 15 Maths
Alex 14 Maths
Stuart 17 Maths
Using the First Normal Form, data redundancy increases, as there will be many columns with
same data in multiple rows but each row as a whole will be unique.
Adam 15
Alex 14
Stuart 17
In Student Table the candidate key will be Student column, because all other column
i.e Age is dependent on it.
New Subject Table introduced for 2NF will be :
Student Subject
Adam Biology
Adam Maths
Alex Maths
Stuart Maths
In Subject Table the candidate key will be {Student, Subject} column. Now, both the above
tables qualifies for Second Normal Form and will never suffer from Update Anomalies.
Although there are a few complex cases in which table in Second Normal Form suffers
Update Anomalies, and to handle those scenarios Third Normal Form is there.
Third normal form: Third normal form is a normal form that is used
in normalizing a database design to reduce the duplication of data and ensure referential
integrity by ensuring that (1) the entity is in second normal form, and (2) all the attributes in a
table are determined only by the candidate keys of that table and not by any non-prime
attributes.
Unit- II
Concept of SQL
SQL Structured Query Language is a special-purpose programming
language designed for managing data held in a relational database management
system (RDBMS), or for stream processing in a relational data stream management
system (RDSMS).
Originally based upon relational algebra and tuple relational calculus, SQL consists of
a data definition language, data manipulation language, and Data Control Language. The
scope of SQL includes data insert, query, update and delete, schema creation and
modification, and data access control. Although SQL is often described as, and to a great
extent is, a declarative language (4GL), it also includes procedural elements.
SQL was one of the first commercial languages for Edgar F. Codd's relational model,
as described in his influential 1970 paper, "A Relational Model of Data for Large Shared Data
Banks." Despite not entirely adhering to the relational model as described by Codd, it became
the most widely used database language.
SQL became a standard of the American National Standards Institute (ANSI) in 1986,
and of the International Organization for Standardization (ISO) in 1987 Since then, the
standard has been revised to include a larger set of features. Despite the existence of such
standards, most SQL code is not completely portable among different database systems
without adjustments.
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F.
Boyce in the early 1970s. This version, initially called SEQUEL (Structured English Query
Language), was designed to manipulate and retrieve data stored in IBM's original quasi-
relational database management system, System R, which a group at Laboratory had
developed during the 1970s. The acronym SEQUEL was later changed to SQL because
"SEQUEL" was a trademark of the UK-based Hawker Siddeley aircraft company.
In the late 1970s, Relational Software, Inc. (now Oracle Corporation) saw the potential of the
concepts described by Codd, Chamberlin, and Boyce, and developed their own SQL-
based RDBMS with aspirations of selling it to the U.S. Navy, Central Intelligence Agency,
and other U.S. government agencies. In June 1979, Relational Software, Inc. introduced the
first commercially available implementation of SQL, Oracle V2 (Version2)
for VAX computers.
After testing SQL at customer test sites to determine the usefulness and practicality of
the system, IBM began developing commercial products based on their System R prototype
including System/38, SQL/DS, and DB2, which were commercially available in 1979, 1981,
and 1983, respectively.
1. Benefits of SQL:
1. SQL is an English-like language. It uses words such as select, insert, delete as part of its
command set.
2. SQL is non-procedural language we specify what information we require without
specifying how to get it. All SQL statements use the query optimizer a part of RDBMS to
determine the fastest means of retrieving the specified data.
3. SQL processes sets of records rather than a single record at a time. The most common form
of a set of records is a table.
4. SQL can be used by a range of users including DBAs, application programmers,
management personnel and many other types of end users.
5. SQL provides commands for a variety of tasks including
- Querying Data
- Inserting, updating, deleting rows in a table
- Creating, modifying and deleting database objects
- Controlling access to the database and database objects
- Guaranteeing database consistency.
The important benefits of SQL are portability and the capability to support a wide range of
commands so as to perform a variety of tasks.
2. Embedded SQL
1. SQL provides a powerful declarative query language. However, access to a database
from a general-purpose programming language is required because,
o SQL is not as powerful as a general-purpose programming language. There are
queries that cannot be expressed in SQL, but can be programmed in C,
Fortran, Pascal, Cobol, etc.
o Nondeclarative actions -- such as printing a report, interacting with a user, or
sending the result to a GUI -- cannot be done from within SQL.
2. The SQL standard defines embedding of SQL as embedded SQL and the language in
which SQL queries are embedded is referred as host language.
3. The result of the query is made available to the program one tuple (record) at a time.
4. To identify embedded SQL requests to the preprocessor, we use EXEC SQL
statement:
EXEC SQL embedded SQL statement END-EXEC
6. Embedded SQL can execute any valid update, insert, or delete statements.
7. Dynamic SQL component allows programs to construct and submit SQL queries at
run time.
5. Literals
The terms literal and constant value are synonymous and refer to a fixed data value. For
example, 'JACK', 'BLUE ISLAND', and '101' are all character literals; 5001 is a numeric
literal. Note that character literals are enclosed in single quotation marks, which enable
Oracle to distinguish them from schema object names.
Text
Text specifies a text or character literal. We can specify character literals with the 'text'
notation, national character literals with the N'text' notation.
A text literal must be enclosed in single quotation marks.
A text literal can have a maximum length of 4000 bytes.
Here are some valid text literals:
'Hello', 'ORACLE.dbs', 'Jackie''s raincoat', '09-MAR-98', N'nchar literal'
Integer
We must use the integer notation to specify an integer whenever integer appears in
expressions, conditions, SQL functions, and SQL statements.
An integer can store a maximum of 38 digits of precision.
Here are some valid integers: 7, +255
Number
We must use the number notation to specify values whenever number appears in
expressions, conditions, SQL functions, and SQL statements.
A number can store a maximum of 38 digits of precision.
If we have established a decimal character other than a period (.) with the
initialization parameter NLS_NUMERIC_CHARACTERS, you must specify numeric
literals with 'text' notation. In such cases, Oracle automatically converts the text literal
to a numeric value.
For example, if the NLS_NUMERIC_CHARACTERS parameter specifies a decimal
character of comma, specify the number 5.123 as follows: '5,123'
Here are some valid representations of number: 25, +6.34, 0.5,
25e-03
6. Datatypes
Each literal or column value manipulated in ORACLE has a datatype.
A value's datatype associates a fixed set of properties with the value.
These datatypes define the domain of values that each column can contain or each
argument can have.
Character Datatypes
char(size)
The CHAR datatype specifies a fixed length character string.
When you create a table with a CHAR column, you can supply the column length in
bytes. ORACLE subsequently ensures that all values stored in that column have this
length.
If you insert a value that is shorter than the column length, ORACLE blank-pads the
value to column length.
If you try to insert a value that is too long for the column, ORACLE returns an error.
The default length for a CHAR column is 1 byte. The maximum length of CHAR data
is 255 bytes.
ORACLE compares CHAR values using blank-padded comparison semantics.
varchar2(size)
The VARCHAR2 datatype specifies a variable length character string.
When you create a VARCHAR2 column, you can supply the maximum number of
bytes of data that it can hold.
ORACLE subsequently stores each value in the column exactly as you specify it,
provided it does not exceed the column's maximum length.
If you try to insert a value that exceeds this length, ORACLE returns an error.
You must specify a maximum length for a VARCHAR2 column.
The maximum length of VARCHAR2 data is 2000 bytes.
ORACLE compares VARCHAR2 values using non-padded comparison semantics.
varchar(size)
The VARCHAR datatype is currently synonymous with the VARCHAR2 datatype.
Oracle Corporation recommends that you use VARCHAR2 rather than VARCHAR.
In a future version of ORACLE, VARCHAR might be a separate datatype used for
variable length character strings compared with different comparison semantics.
Number Datatypes
number(p,s)] where p is the precision, or the total number of digits and s is the scale, or the
number of digits to the right of the decimal point.
You can use number(p) which is a fixed point number with precision p and scale 0, or
number which is a floating point number with precision 38.
If the scale is negative, the actual data is rounded to the specified number of places to
the left of the decimal point.
For example, a specification of (10,-2) means to round to hundreds - 7456123.89
would be stored as 7456100.
float(b)] - where b specifies a floating point number with binary precision b.
The precision b can range from 1 to 126 with a default value of 126.
To convert from binary to decimal precision, multiply b by 0.30103.
To convert from decimal to binary precision, multiply the decimal precision by
3.32193.
The maximum of 126 digits of binary precision is roughly equivalent to 38 digits of
decimal precision.
Long Datatypes
LONG columns store variable length character strings containing up to 2 gigabytes, or
2**31-1 bytes.
LONG columns have many of the characteristics of VARCHAR2 columns.
You can use LONG columns to store long text strings.
ORACLE uses LONG columns in the data dictionary to store the text of view
definitions.
The length of LONG values may also be limited by the memory available on your
computer.
You can reference LONG columns in SQL statements in these places:
o SELECT lists
o SET clauses of UPDATE statements
o VALUES clauses of INSERT statements
The use of LONG values are subject to some restrictions:
o A table cannot contain more than one LONG column.
o LONG columns cannot appear in integrity constraints (except for NULL and NOT
NULL constraints).
o LONG columns cannot be indexed.
o A procedure or stored function cannot accept a LONG argument.
Also, LONG columns cannot appear in certain parts of SQL statements:
o WHERE, GROUP BY, ORDER BY, or CONNECT BY clauses or with the
DISTINCT operator in SELECT statements.
o SQL functions (such as SUBSTR or INSTR).
o expressions or conditions.
o select lists of queries containing GROUP BY clauses.
o select lists of subqueries or queries combined by set operators.
o select lists of CREATE TABLE AS SELECT statements.
Date Datatype
The DATE datatype is used to store date and time information. Although date and
time information can be represented in both CHAR and NUMBER datatypes, the
DATE datatype has special associated properties. For each DATE value the following
information is stored: century year month day hour minute second
You cannot specify a date literal. To specify a date value, you must convert a character
or numeric value to a date value with the TO_DATE function.
ORACLE automatically converts character values that are in the default date format
into date values when they are used in date expressions.
The default date format is specified by the initialization parameter
NLS_DATE_FORMAT and is a string such as 'DD-MON-YY'. This example date
format includes two-digit number for the day of the month, an abbreviation of the
month name, and the last two digits of this year.
If you specify a date value without a time component, the default time is 12:00:00a.m.
(midnight). If you specify a date value without a date, the default date is the first day
of the current month.
The date function SYSDATE returns the current date and time.
7. Nulls: If a column in a row has no value, then the column is said to be null, or to contain a
null. Nulls can appear in columns of any datatype that are not restricted by NOT NULL
or PRIMARY KEY integrity constraints. Use a null when the actual value is not known
or when a value would not be meaningful.
Do not use null to represent a value of zero, because they are not equivalent. (Oracle
currently treats a character value with a length of zero as null. However, this may not
continue to be true in future releases, and Oracle recommends that you do not treat empty
strings the same as NULLs.) Any arithmetic expression containing a null always evaluates to
null. For example, null added to 10 is null. In fact, all operators (except concatenation) return
null when given a null operand.
8. Pseudocolumns
A pseudocolumn behaves like a table column, but is not actually stored in the table. You can
select from pseudocolumns, but you cannot insert, update, or delete their values.
Rowid: For each row in the database, the ROWID pseudocolumn returns a row's address.
Rownum: For each row returned by a query, the ROWNUM pseudocolumn returns a number
indicating the order in which Oracle selects the row from a table or set of joined rows.
The first row selected has a ROWNUM of 1, the second has 2, and so on. You can use
ROWNUM to limit the number of rows returned by a query, as in this example:
SELECT * FROM emp WHERE ROWNUM < 10;
User: returns your current userid
Sysdate: returns current date and time.
Null: returns a null value
Sysdate: returns current date and time.
10. Operators
An operator is used to manipulate individual data items and return a result. These data items
are called operands or arguments. Some of the operators will be listed below
Arithmetic Operators
unary +-
arithmetic * /
binary +-
Logical Operators
not NOT
and AND
or OR
Character Operators
concatenate ||
Comparison Operators
equality =
inequality !=, <>
greater >, >=
less <, <=
equal to any IN, =ANY
not equal to NOT IN, !=ANY
any =ANY, !=ANY, >ANY, <="ANY,">=ANY
all =ALL, !=ALL, >ALL, <="ALL,">=ALL
between [NOT] BETWEEN x ANY y
exists EXISTS
like x [NOT] LIKE y [ESCAPE z]
null IS [NOT] NULL
Note: % matches any string of zero or more characters except null. The character ``_''
matches any single character.
Set Operators Set operators combines the results of two queries into a single result:
UNION - All distinct rows selected by either query.
UNION ALL - All rows selected by either query, including all duplicates.
INTERSECT - All distinct rows selected by both queries.
MINUS - All distinct rows selected by the first query but not the second.
11. Functions
A function is similar to an operator in that it manipulates data items and returns a result.
Functions differ from operators in the format in which they appear with their arguments. If
you call a function with an argument of a datatype other than the datatype expected by the
function, ORACLE implicitly converts the argument to the expected datatype before
performing the function. If you call a function with a null argument, the function
automatically returns null. The only functions that do not follow this rule are CONCAT,
REPLACE, DUMP and NVL.
There are two general types of functions:
single row (or scalar) functions
group functions (or aggregate) functions
These functions differ in the number of rows upon which they act. A single row function
returns a single result row for every row of a queried table or view, while a group function
returns a single result row for a group of queried rows.
Single row functions can appear in select lists (provided the SELECT statement does not
contain a GROUP BY clause), WHERE clauses, START WITH clauses, and CONNECT BY
clauses.
Group functions can appear in select lists and HAVING clauses. If you use the GROUP BY
clause in a SELECT statement, ORACLE divides the rows of a queried table or view into
groups. In a query containing a GROUP BY clause, all elements of the select list must be
either expressions from the GROUP BY clause, expressions containing group functions, or
constants. ORACLE applies the group functions in the select list to each group of rows and
returns a single result row for each group.
Single Row Functions
Numeric Functions
ABS(n) Returns the absolute value of n.
CEIL(n) Returns smallest integer greater than or equal to n.
COS(n) Returns the cosine of n (angle expressed in radians).
COSH(n) Returns the hyperbolic cosine of n.
EXP(n) Returns e raised to the nth power.
FLOOR(n) Returns largest integer equal to or less than n.
LN(n) Returns the natural logarithm of n (for n > 0).
LOG(m,n) Returns the logarithm, base m, of n. (m <> 0 or 1).
MOD(m,n) Returns remainder of m divided by n.
POWER(m,n) Returns m raised to the nth power (m**n).
ROUND(n[,m]) Returns n rounded to m places right of the decimal.
SIGN(n) Returns -1 if n<0; 0 if n=0; 1 if n>0.
SIN(n) Returns the sine of n.
SINH(n) Returns the hyperbolic sine of n.
SQRT(n) Returns square root of n.
TAN(n) Returns the tangent of n.
TANH(n) Returns the hyperbolic tangent of n.
TRUNC(n[,m]) Returns n truncated to m decimal places; else m=0.
Character Functions
CHR(n) Returns the character having binary equivalent to n.
CONCAT(c1, c2) Returns c1 concatenated with c2.
Returns char, with the first letter of each word in uppercase, all
INITCAP(char)
other letters in lowercase.
LOWER(char) Returns char, with all letters lowercase.
Returns c1, left-padded to length n with the sequence of
LPAD(c1,n[,c2])
characters in c2; c2 defaults to `\space', a single blank.
Removes characters from the left of c1, with initial characters
LTRIM(c1[,set]) removed up to the first character not in set; set defaults to
`\space', a single blank.
Returns the string, c1, with every occurrence of s1, search string,
replaced with replacement string, r1. If r1 replacement string is
REPLACE(c1, s1 [,r1])
omitted or null, all occurrences of s1 are removed. If s1 is null, c1
is returned.
Returns c1, right-padded to length n with c2, replicated as many
times as necessary; c2 defaults to `\space', a single blank. If c1 is
RPAD(c1,n[,c2])
longer than n, this function returns the portion of char1 that fits in
n.
Returns char, with final characters removed after the last
RTRIM(c1[,set])
character not in set; set defaults to `\space'.
Returns a character string containing the phonetic representation
SOUNDEX(char) of char. This function allows you to compare words that are
spelled differently, but sound alike in English.
Returns a portion of c1, beginning at character m, n characters
long. If m is positive, ORACLE counts from the beginning of
char to find the first character. If m is negative, ORACLE counts
SUBSTR(c1,m[,n])
backwards from the end of char. The value m cannot be 0. If n is
omitted, ORACLE returns all characters to the end of char. The
value n cannot be less than 1.
The same as SUBSTR, except that the arguments m and n are
SUBSTRB(c1,m[,n])
expressed in bytes, rather than in characters.
Returns c1 with all occurrences of each character
in from replaced by its corresponding character in to. Characters
in c1 that are not in from are not replaced. The argument from can
contain more characters than to. In this case, the extra characters
at the end of from have no corresponding characters in to. If these
TRANSLATE(c1,from,to)
extra characters appear in c1, they are removed from the return
value. You cannot use an empty string for to in order to remove
all characters in from from the return value. ORACLE interprets
the empty string as null, and if this function has a null argument,
it returns null.
UPPER(char) Returns char, with all letters uppercase.
Returns the decimal representation in the database set of the first
ASCII(char)
byte of char.
Searches c1 beginning with its nth character for the mth
occurrence of c2 and returns the position of the character in c1
that is the first character of this occurrence. If n is negative,
INSTR(c1,c2[,n[,m]]) ORACLE counts and searches backward from the end of c1. The
value of m must be positive. The default values of both n and m
are 1, meaning ORACLE begins searching at the first character of
c1 for the first occurrence of c2.
LENGTH(char) Returns the length of char in characters.
Date Functions
Returns the date d plus n months. The argument n can be any
integer. If d is the last day of the month or if the resulting
ADD_MONTHS(d,n) month has fewer days than the day component of d, then the
result is the last day of the resulting month. Otherwise, the
result has the same day component as d.
Returns the date of the last day of the month that contains d.
LAST_DAY(d) You might use this function to determine how many days are
left in the current month.
Returns number of months between dates d1 and d2. If d1 is
later than d2, result is positive; if earlier, negative. If d1 and
d2 are either the same days of the month or both last days of
MONTHS_BETWEEN(d1,d2) months, the result is always an integer; otherwise ORACLE
calculates the fractional portion of the result based on a 31-
day month and also considers the difference in time
components of d1 and d2.
NEW_TIME(d,z1,z2) Returns the date and time in time zone z2 when date and
time in time zone z1 are d. The arguments z1 and z2 can be
any of these text strings:
'AST' or 'ADT' Atlantic Standard or Daylight Time
'BST' or 'BDT' Bering Standard or Daylight Time
'CST' or 'CDT' Central Standard or Daylight Time
'EST' or 'EDT' Eastern Standard or Daylight Time
'GMT' Greenwich Mean Time
'HST' or 'HDT' Alaska-Hawaii Standard Time or
Daylight Time
'MST' or 'MDT' Mountain Standard Time or Daylight
Time
'NST' Newfoundland Standard Time
'PST' or 'PDT' Pacific Standard or Daylight Time
'YST' or 'YDT' Yukon Standard or Daylight Time
Returns the date of the first weekday named by char that is
later than the date d. The argument char must be a day of the
NEXT_DAY(d,char) week in your session's date language. The return value has
the same hours, minutes, and seconds component as the
argument d.
Returns d rounded to the unit specified by the format model
ROUND(d[,fmt])
fmt. If you omit fmt, d is rounded to the nearest day.
Returns the current date and time. Requires no arguments. In
distributed SQL statements, this function returns the date and
SYSDATE
time of your local database. You cannot use this function in
the condition of a CHECK constraint.
Returns d with the time portion of the day truncated to the
unit specified by the format model fmt. If you omit fmt, d is
truncated to the nearest day.
Format Model Rounding or Truncating Unit
Other Funtions
Returns the greatest of the list of exprs. All exprs after the first are
GREATEST(expr implicitly converted to the datatype of the first prior to the
[,expr] ...) comparison. ORACLE compares the exprs using non-padded
comparison semantics.
Returns the least of the list of exprs. All exprs after the first are
LEAST(expr implicitly converted to the datatype of the first prior to the
[,expr] ...) comparison. ORACLE compares the exprs using non-padded
comparison semantics.
If expr1 is null, returns expr2; if expr2 is not null, returns expr1. The
arguments expr1 and expr2 can have any datatype. If their datatypes
NVL(expr1,expr2)
are different, ORACLE converts expr2 to the datatype of expr1 before
comparing them.
UID Returns an integer that uniquely identifies the current user.
USER Returns the current ORACLE user with the datatype VARCHAR2.
Returns information of VARCHAR2 datatype about the current
session. This information can be useful for writing an application-
USERENV(option)
specific audit trail table or for determining the language-specific
characters currently used by your session.
Group Functions: Many group functions accept these options:
This option causes a group function to consider only distinct values of the
DISTINCT
argument expression.
ALL This option causes a group function to consider all values including all duplicates
All group functions except COUNT(*) ignore nulls. You can use the NVL in the argument to
a group function to substitute a value for a null. If a query with a group function returns no
rows or only rows with nulls for the argument to the group function, the group function
returns null.
Element Meaning
SCC or CC Century; ``S'' prefixes BC dates with ``-''.
YYYY or
4-digit year; ``S'' prefixes BC dates with ``-''.
SYYYY
IYYY 4-digit year based on the ISO standard.
YYY or YY or
Last 3, 2, or 1 digit(s) of year.
Y
IYY or IY or I Last 3, 2, or 1 digit(s) of ISO year.
Y, YYY Year with comma in this position.
SYEAR or Year, spelled out;``S'' prefixes BC dates with ``-''.
YEAR
RR Last 2 digits of year; for years in other centuries.
BC or AD BC/AD indicator.
B.C. or A.D. BC/AD indicator with periods.
Q Quarter of year (1,2,3,4; JAN-MAR = 1).
MM Month (01-12; JAN = 01).
RM Roman numeral month (I-XII; JAN = I).
MONTH Name of month, padded with blanks to length of 9 characters.
MON Abbreviated name of month.
Week of year (1-53) where week 1 starts on the first day of the year and
WW
continues to the seventh day of the year.
IW Week of year (1-52 or 1-53) based on the ISO standard.
Week of month (1-5) where week 1 starts on the first day of the month and
W
ends on the seventh.
DDD Day of year (1-366).
DD Day of month (1-31).
D Day of week (1-7).
DAY Name of day, padded with blanks to length of 9 characters.
DY Abbreviated name of day.
Julian day; the number of days since January 1, 4712 BC. Numbers specified
J
with 'J' must be integers.
AM or PM Meridian indicator.
A.M. or P.M. Meridian indicator with periods.
HH or HH12 Hour of day (1-12).
HH24 Hour of day (0-23).
MI Minute (0-59).
SS Second (0-59).
SSSSS Seconds past midnight 90-86399).
-/,.;:"text" Punctuation and quoted text is reproduced in the result.
The RR date format element is similar to the YY date format element, but it provides
additional flexibility for storing date values in other centuries. The RR date format element
allows you to store twenty-first century dates in the twentieth century by specifying only the
last two digits of the year. It will also allow you to store twentieth century dates in the
twenty-first century in the same way if necessary.
If you use the TO_DATE function with the YY date format element, the date value returned is
always in the current century. If you use the RR date format element instead, the century of
the return value varies according to the specified two-digit year and the last two digits of the
current year.
The following suffixes can be added to date format elements:
Example Example
Suffix Meaning
Element Value
TH Ordinal number DDTH 4TH
SP Spelled number DDSP FOUR
SPTH or THSP Spelled, ordinal number DDSPTH FOURTH
Capitalization in a spelled-out word, abbreviation, or Roman numeral follows capitalization
in the corresponding format element. For example, the date format model 'DAY' produces
capitalized words like 'MONDAY'; 'Day' produces 'Monday'; and 'day' produces 'monday'.
DELETE Operation:
The DELETE command can remove all the rows from the table or a set of rows from the
table.
eg: DELETE FROM student; It will DELETE all the rows from student table.
eg: DELETE FROM student WHERE reg_no='A101'; If condition will be satisfied then
it will delete a row from the table Register number A101 will be deleted from the table
UPDATE Operation:
The UPDATE command is used to change or modify data values in a table and UPDATE
command can Update all the rows from the table or a set of rows from the table.
eg : UPDATE Student SET course='MCA';
Course is a column name, suppose ant time you want to update something like that in the
student table course should be MCA for all students then you can use this type of query. It
will update all the rows in the table all rows will have MCA course.
Now, if you want update particular row then see below.
UPDATE Student SET course='MCA' where reg_no='A101'; it will update only one row
that will have Register no. A101.
you can use different-different types of condition in WHERE clause, eg salary updation, if
salary has increased someone's then simply multiply, addition you can do in salary column.
Select: Select command is used to view Data in the Table (Select Command). Once data has
been inserted into a table, the next most logical operation would be to view what has been
inserted. The SELECT SQL verb is used to achieve this.
All Rows and All Columns
Syntax: SELECT * FROM Table_name;
eg: Select * from Student; It will show all the table records.
SELECT First_name, DOB FROM STUDENT WHERE Reg_no = 'S101'; Cover it by
single inverted comma if its datatype is varchar or char.
This Command will show one row. because you have given condition for only one row and
particular records. If condition which has given in WHERE Clause is true then records will
be fetched otherwise it will show no records selected.
Eliminating Duplicates:
A table could hold duplicate rows. In such a case, you can eliminate duplicates.
Syntax: SELECT DISTINCT col, col, .., FROM table_name;
eg : SELECT DISTINCT * FROM Student;
or : SELECT DISTINCT first_name, city, pincode FROM Student;
It scans through entire rows, and eliminates rows that have exactly the same contents in each
column.
Sorting DATA:
The Rows retrieved from the table will be sorted in either Ascending or Descending order
depending on the condition specified in select statement, the Keyword has used ORDER BY.
SELECT * FROM Student
ORDER BY First_Name;
it will in show records as alphabetical order from A to Z ascending order. If you want
Descending order means Z to A then used DESC Keyword at last.
eg : SELECT first_name, city,pincode FROM Student
ORDER BY First_name DESC;
UNIT III
Schema Objects
4. Sequences:
Sequences are database objects from which multiple users can generate unique integers. The
sequence generator generates sequential numbers, which can help to generate unique primary
keys automatically, and to coordinate keys across multiple rows or tables.
Without sequences, sequential values can only be produced programmatically. A new
primary key value can be obtained by selecting the most recently produced value and
incrementing it. This method requires a lock during the transaction and causes multiple users
to wait for the next value of the primary key
Syntax:
CREATE SEQUENCE sequence_name
[START WITH start_num]
[INCREMENT BY increment_num]
[ { MAXVALUE maximum_num | NOMAXVALUE } ]
[ { MINVALUE minimum_num | NOMINVALUE } ]
[ { CYCLE | NOCYCLE } ]
[ { CACHE cache_num | NOCACHE } ]
[ { ORDER | NOORDER } ];
The default start_num is 1.
1. The default increment number is 1.
2. minimum_num must be less than or equal to start_num, and minimum_num must be
less than maximum_num.
3. NOMINVALUE specifies the maximum is 1 for an ascending sequence or -10^26 for
a descending sequence.
4. NOMINVALUE is the default.
5. maximum_num must be greater than or equal to start_num, and maximum_num must
be greater than minimum_num.
6. NOCYCLE specifies the sequence cannot generate any more integers after reaching
its maximum or minimum value.
7. NOCYCLE is the default.
8. CACHE cache_num specifies the number of integers to keep in memory.
9. The default number of integers to cache is 20.
10. The minimum number of integers that may be cached is 2.
11. NOCACHE specifies no integers are to be stored.
12. ORDER guarantees the integers are generated in the order of the request.
13. NOORDER is the default.
e.g:
CREATE SEQUENCE emp_sequence
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;