Unit 2 SQL With Oracle9i Database: Structure
Unit 2 SQL With Oracle9i Database: Structure
2.1 Introduction
Before moving on to further details here below, let us glance at the
terminology that helps us through following points.
The data dictionary is maintained by the database and contains all the
information on how data is stored in the database, where it is, and how
the database can work with it.
DDL stands for data definition language. These are the SQL
statements that begins with create, revoke, grant and drop. These are
the statements that are used to create and remove the database
objects. They are also statements that are used to create permission to
access the database and database objects.
DML (data manipulation language) are SQL statements that begin
with select, insert, delete, or update. These are statements used to
manipulate the contents of a database.
Grants are privileges given out by owners of objects, allowing other
users to work with the owner's data. Grants fall into two categories:
object-level grants and system-level grants. Object-level grants give
users the ability to select, insert, update, or delete against a database
object like a table. Examples of system-level grants are giving a
database user the ability to connect to the database or permission to
create an object in the database.
database without using SQL directly, but these applications in turn must use
SQL when executing the user's request. This unit provides background
information on SQL as used by most database systems which covers the
following topics:
How to log into (access SQL *plus)
DDL and DML : Difference
Basic format of every SELECT statement
How to format retrieved data
The most common set commands
How to join tables
Command Description
alter procedure Recompiles a stored procedure
Adds a column, redefines a column or changes
alter table
storage allocation of the given table
alter table add constraint Adds a constraint to an existing table
create table Creates table
create index Creates an index
drop table Removes a table
drop index Removes an index
Grant Grants privileges or roles to a user or another role
Revoke Removes privileges from a user or database role
Table 2.1: Basic DDL clauses
Table
Table is the database object that gets loaded into an Oracle database must
be placed inside an Oracle table. In fact all the information needed by an
oracle database to manage itself is stored in a series of tables that are
commonly known as the data dictionary.
The following example illustrates how to create a table with pet_id, pet_kind,
pet_name as its column.
The table name in the above example should adhere strictly to the following
norms:
While naming a table the first letter should be an alphabet.
Oracle9i reserved words cannot be used to name a table.
Maximum length for a table name is 30 characters.
Two different tables should not have the same name.
Underscore, numerals and letters are allowed but not blank space and
single quotes.
4. PET_NAME VARCHAR2(20);
SQL > /
Table created
PETS has three columns associated with it. The first column is PET_ID,
which is of type integer. It has an integrity constraint of primary key
associated with it, which means that database will enforce the business rule
that each per ID must be unique with in this table. This means that no two
entries in the table PETS could share the same pet ID. If you attempted to
place a duplicate pet ID in the tables PETS, you would receive an error.
The last column is PET_NAME, which is also of type varchar2 with a width
of up to 20 characters. Unlike PET_KIND column, whenever a row of data is
entered into PETS, an associated data entry is optional for the PET_NAME
column.
Table created
SQL> create or replace type <type name> as object (column1 data
type, column2 data type, column3 datatype…) ;
In the syntax that is given above, the 'create or replace' command is given
so that the type could be created or replaced. The keyword type precedes
the name of the type itself. Since the type is a database object the keyword
object follows it. Following this, the column names and the data types are
given in the order required by the user. This command is processed as a
PL/SQL statement and hence there will not be a semi-colon following the
SQL statement. Instead, a back slash is given.
Example
SQL> create or replace type address as object(add1 number(5),
add2 varchar2(10), add3 varchar2(5), add4 number(7));
Type created
The object types that are created could be made public. This type could be
used in other database objects such as tables, procedures etc. A row could
be inserted using the select command. The standard dot notation is used to
represent the data type that has been defined by the user. It is possible to
nest the user defined types i.e., a type can be enclosed within another type.
Example
SQL> create or replace type emp as object (eno number(2),
ename varchar2(10), eadd address) ;
Example
SQL > alter table MITE modify (empno number(8));
The following example explains how to add a column of type long to the
customer table.
Example
SQL > alter table MITE add (remarks long);
Example
SQL> alter table MITE add partition p3 values less than ('r');
The example shows us how to alter a table to add a partition. When we add
a partition to a table we must remember that the partition can be added only
to the end of the table. In other words a partition can only be appended to
the table. If the last partition contained values that are less than the max
value then no other partition can be appended to the table. In such a case
the split partition option can be used to insert a partition in the desired
position.
A split partition gives us the option of splitting a partition in two. It gives the
new partitions, new physical attributes and the older attributes are
discarded. An example with the split partition option explains this concept
clearly.
Example
SQL> alter table MITE split partition p1 at (‘c’) into (partition p1,
partition p3)
The example shows us how a single partition can be split into two with the
help of the split partition keyword. This gives us two partitions i.e., p1 and
p2. p1 contains values that are less than c and the partition p3 contains
values that are less than h.
A partition name can be renamed using the rename option. The rename
option works as shown below:
Example
SQL> alter table MITE rename partition p2 to par2;
A partition can be altered to delete all rows from it. To delete all rows from a
partition we can use the truncate partition command along with the alter
table command.
Example
SQL> alter table MITE truncate partition p1;
2.5.4 Truncate:
The following command would delete all the records from the table.
Command to truncate a table is truncate table <table_name>;
Example
SQL > Truncate table MITE;
Implementation of this command would delete all the rows associated with
the table, only the structure of the table remains. The truncate command
deletes all rows from the specified table. An example gives a better
understanding of the concept.
Example
SQL > truncate table MITE reuse storage;
The above command deletes all rows from the table employee and reuses
any space that was occupied by the rows of the table employee.
Desc
If the user wants to view the structure of the table, the following Desc helps
to achieve the same. Command to view the table's structure is Desc <table
name>
Example
SQL > Desc MITE;
The above command will display the structure of the MITE table.
2.5.5 Constraints:
Constraints would be defined along with the columns in a table to avoid
invalid entries. For example, an organization maintaining details regarding
textbooks, purchase order and employee's name wants to secure these
informations, they can enforce data integrity on the related data objects.
Integrity Constraints
An Integrity Constraint is a mechanism used by Oracle9i to prevent invalid
data entry into the table. It is nothing but enforcing rule for the columns in a
table. The various types of integrity constraints are explained below:
Not Null
Null is a column that contains no data. In other words, it contains nothing.
Null values have a way of getting into your database where you lease
expect them for example, in numeric columns. This creates a big problem
since 1+null=null. Null is not 0.
It is advisable to create primary key columns with the not null clause. This
makes it mandatory, before entering a row of data to specify a valid entry for
all the table columns so tagged. Or the database will refuse the record.
Example
SQL > create table IT (empno number(6) constraint eno Not Null, empname
varchar2(30));
This command will ensure that the user enters a value for the empno
column on the IT table, failing which it returns an error message.
Check constraints
These are rules governed by logical expressions or Boolean expressions.
Check conditions cannot contain subqueries. The following example will
help us to understand it much better & creates a table IT_order with a check
constraint to restrict the values of ordid to be within 1000.
Example
SQL > create table IT_order (ordid number(4) constraint checkit check
(ordid > 1000), orderdate date, commplan character(l), custid number(6),
shipdate date, total number (8, 2));
Unique Constraints
Usage of the unique key constraint is to prevent the duplication of values
within the rows of a specified column or a set of columns in a table.
Columns defined with this constraint can also allow Null values. If unique
key constraint is defined in more than one column, it is said to be
composite unique key. In the above case combination of columns cannot
be duplicated. Maximum combination of columns that s composite unique
key can contain is 16.
Example
SQL > create table ITT (id number(6), stdprice number(8,2), minprice
number(8,2), startdate date, enddate date constraint unidate unique);
The same constraint can be enforced using table level syntax also.
Example
SQL > create table IT (id number(6), stdprice number (8,2), enddate date,
startdate date, constraint unitab unique (enddate));
Example
SQL > create table MITE(empid number(6),empname varchar2(30),
constraint compuni unique(empid, empname));
We relate the two tables together in the where clause. Once we understand
what primary key is and what is foreign key, joining them becomes simple.
The foreign key is a direct road map to the source table.
This constraint avoids duplication of rows and does not allow Null values,
when enforced in a column or set of columns. As a result it is used to
identify a row. A table can have only one primary key. If a primary key
constraint is assigned to more than one column (i.e.) or combination of
columns it is said to be a composite primary key, which can contain a
maximum of 16 columns. Primary key constraint cannot be defined in an
alter table command when the table contains rows having Null values.
Example
SQL > create table IT_customer (custid number(6) constraint prim_con
primary key, name varchar(45), address varchar2(30), city char(30), repid
number(4), creditlimit number(8,2));
Example
SQL> create table ITT_item (itemid number(6), ordid number(6), prodid
number(6), actualprice number(8, 2), qty number(8,2), constraint prim_id
primary key(itemid));
This constraint ensures that no two itemid's in the table will have the same
values and that they do not contain Null values.
Example
SQL > alter table ITT_item add primary key (IT_order, orderdate) disable;
The above command ensures that no two rows in the table have a same
value for both IT_order and orderdate. Since the constraint is disabled it can
only be defined in the Oracle9i database dictionary but not enforced.
Example
SQL > create table T_order (IT_order number(4), orderdate date, commplan
character(1), shipdate date, total number(8,2), empid number(6), constraint
fk_empid foreign key (empid) references MITE(empid));
Example
SQL > create table IT_order (order number(4), orderdate date, commplan
character(1), shipdate date, total number(8,2), empid number(6) constraint
fk_empid references IT_customer(empid));
The above command creates a table IT_order and will also enable a foreign
key on empid column, which would refer to the primary key on the empid
column of the IT_customer table. Before enabling this constraint, we ensure
that empid column of the customer table has been defined with either
unique key or primary key constraint. The foreign key constraint ensures
that all values in the column empid in IT_order table have a corresponding
empid value in the IT_customer table.
The referential integrity constraint does not use foreign key keyword to
identify the columns that make up the foreign key, because the constraint is
defined at column level on custid column, the foreign key is automatically
enforced on the custid column.
We can define a referential integrity constraint using table level syntax also.
The example illustrates the same concept for better understanding.
Example
SQL > create table IT_order (order number(4), orderdate date, commplan
character(1), ship_date date, total number(8,2), empid number(6), constraint
fk_empid references IT_customer(empid) on delete cascade);
Foreign key keyword is an optional one and can be used for a more
understandable syntax and missing it doesn't cause any error. Because of
the on-delete-cascade option we find that deleting a particular empid value
from the IT_customer table, results in the deletion of the corresponding rows
from the order table also.
An option that could be used to set all constraints to deferred or enable all
constraints is available and has the following syntax:
When we create a table or cluster, we must specify a datatype for each of its
columns. When we create a procedure or stored function, we must specify a
datatype for each of its arguments. These datatypes define the domain of
values that each column can contain or each argument can have. For
example, DATE columns cannot accept the value February 29 (except for a
leap year) or the values 2 or 'SHOE'. Each value subsequently placed in a
column assumes the column's datatype. For example, if we insert '01-JAN-
98' into a DATE column, then Oracle treats the '01-JAN-98' character string
as a DATE value after verifying that it translates to a valid date.
Each data type has a set of rules associated with it, and the Oracle9i
database engine is designed to support those rules.
Data type Description
char(size) Stores fixed-length character data
varchar2(size) Stores variable length character data
number(l, d) Stores numeric data, where l stands for length and d stands
for number of decimal digits
Blob A binary large object the BLOB data type stores up to 4GB
of binary data(such as text, graphic images, video clips and
sound waveforms) in the database. Oracle recommends that
you always use LOB(BLOB,CLOB, NCLOB and BFILE) data
types rather than LONG data types.
Raw(size) Raw binary data the raw data type is variable length data
type like varchar2 character data type. It is intended for
binary data or byte strings such as graphics, sound
documents or array of binary data.
Date Stores dates
Long Stores variable – length character data column defined as
LONG can store variable character data containing up to
2GB of information. A table can obtain multiple LOB
columns but on one LONG column.
Number datatype
This datatype can store positive numbers, negative numbers, zeroes, fixed
point numbers, floating point numbers of magnitude ranging between
1.0 x 10 –130 to 9.9.9 x 10125 with a precision of 38.
Column_name number {p = 38, S = 0}
Column_name number (p) {fixed point}
Column_name number (p, s) {floating point}
Where p is the precision, which refers to the total number of digits, it varies
between 1 - 38; s is the scale, which refers to number of digits to the right of
the decimal point, which varies between -84 to 127.
Date datatype
Date datatype is used to store date and time in a table. Oracle9i database
makes use of its own format to store date in a fixed length of 7 bytes each
for century, month, day, year, hour, minute and second. Default date
Sikkim Manipal University Page No.: 48
Oracle9i and Distributed Database Unit 2
datatype is "dd-mon-yy". To view system's date and time we can use the
SQL function called sysdate(). Valid date is from Jan 1, 4712 BC to Dec 31,
4712 AD.
Raw (size)
Raw datatype is used to store byte oriented data like binary data or byte
strings and the maximum size of this datatype is 255 bytes. Only storage
and retrieval of data are possible, manipulations of data cannot be done.
Raw datatype can be indexed.
Long raw
Long datatype is used to store binary data of variable length, which can
have a maximum size of 2 GB. This datatype cannot be indexed.
LOB
This is otherwise known as Large Object data types. This can store
unstructured information such as sound clips. Video files etc., up to
4 gigabytes in size. They allow efficient, random, piece-wise access to the
data. The LOB types store values, which are known as locators. These
locators store the location of the large objects. The location may be stored
out-of-line or in an external file. LOB values can be operated upon through
the use of the locators. To manipulate LOBs the DBMS_LOB package can
be used. LOBs can be either external or internal depending on their location
with regard to the database.
CLOB
A column with its data type as CLOB stores character objects with single
byte characters. It cannot contain character sets of varying widths. A table
can have multiple columns with its data type as CLOB.
BLOB
A column with its data type as BLOB can store large binary objects such
as graphics, video clips and sound files. A table can have multiple columns
with BLOB as its data type.
BFILE
A BFILE column stores file pointers to LOBs managed by file systems
external to the database.
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. Character literals are enclosed in single
quotation marks, which enable Oracle to distinguish them from schema
object names.
Many SQL statements and functions require you to specify character and
numeric literal values. You can also specify literals as part of expressions
and conditions. You can specify character literals with the 'text’ notation,
national character literals with the N 'text' notation, and numeric literals with
the integer or number notation, depending on the context of the literal.
Text Literals
Text specifies a text or character literal. You must use this notation to
specify values whenever 'text' or char appear in expressions, conditions,
SQL functions, and SQL statements in other parts of this reference.
The syntax of text is as follows:
text:: =
where
N specifies representation of the literal using the national character set.
Text entered using this notation is translated into the national character
set by Oracle when used.
' ' are two single quotation marks that begin and end text literals. To
represent one single quotation mark within a literal, enter two single
quotation marks.
Integer Literals
You must use the integer notation to specify an integer whenever integer
appears in expressions, conditions, SQL functions, and SQL statements
described in other parts of this reference.
integer::=
Number Literals
You must use the number notation to specify values whenever number
appears in expressions, conditions, SQL functions, and SQL statements in
other parts of this reference.
number::=
where
+ or - indicates a positive or negative value. If you omit the sign, then a
positive value is the default.
digit is one of 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9.
If you have established a decimal character other than a period (.) with the
initialization parameter NLS_NUMERIC_CHARACTERS, then you must
specify numeric literals with 'text' notation. In such cases, Oracle
automatically converts the text literal to a numeric value.
Interval Literals
An interval literal specifies a period of time. You can specify these
differences in terms of years and months, or in terms of days, hours,
minutes, and seconds. Oracle supports two types of interval literals, YEAR
TO MONTH and DAY TO SECOND. Each type contains a leading field and
may contain a trailing field. The leading field defines the basic unit of date or
time being measured. The trailing field defines the smallest increment of the
basic unit being considered. For example, a YEAR TO MONTH interval
considers an interval of years to the nearest month. A DAY TO MINUTE
interval considers an interval of days to the nearest minute.
If you have date data in numeric form, then you can use the
NUMTOYMINTERVAL or NUMTODSINTERVAL conversion function to
convert the numeric data into interval literals.
Each time the command is executed you receive the message “1 row
created”. When you load data into a table, you may also specify the column
to load it into. This ensures that there is no mistaking where you want the
data to be places. In the next example, the columns are specified after the
insert command.
The following example copies all the rows from order_info table to ord table,
provided a table named ord exists having the same structure as order_info.
Example
SQL > Insert into ord (select * from order_info);
But the structure of the table ord is the same as that of order_info.
SQL>
Note the semicolons which signify that you have completed the given SQL
statement. In SQL *Plus there are two ways to signify that you have reached
the end of the SQL statement by
the semicolon at the end of a line
the slash on a separate line
6 rows selected
Now let's take a look at the contents of the newly created MASS_NEWHIRE
table. Remember, this new table only contains entries from the state_cd =
'MA'.
Example
SQL > select distinct repid from IT_customer;
The only other decision you have to name is whether the named column is
retrieved in ascending or descending order. By default, you get ascending
order. This is done on column-by-column or number-by-number basis.
SQL> select state_id, lname, salary
2 from emptable
3* order by state_id desc, lname;
To restate the point, the order by clause determines how the results of the
query will be played back.
Alias
When a column which has a user defined data type is used we have to use
an alias name for the table from which the columns are being selected. After
the alias name we give the column name followed by the attribute from the
user-defined type. This retrieves a single row with two columns from the
database.
SQL > select eno, e.eadd.add1 from emp e;
Before binding all references must be resolved. This process is called name
resolution. All names are considered to be in the same namespace. One
declaration or definition in an inner scope can hide another in an outer
scope. Hence an alias name is required so that the exact data is retrieved
and displayed.
To retrieve specific rows from the table we have to use the 'where' clause as
mentioned before. In addition to this we have to use the dot notation if the
table contains user-defined types. We also should use an alias name. As
you see in the example will help in better understanding of the concept.
Example
SQL> select eno, e.eadd.add1 from emp e where e.eadd.add1 = 11;
In the above syntax, the 'where' clause and the 'set' clause can also include
queries. Update sets each field with the value that we supply, provided it
satisfies the 'where' condition. In the following example a query has been
included in the 'where' clause to select the rows which are to be updated.
Example
SQL>Update item set actualprice = 150.50 where prodid in (select prodid
from product);
With the update command one can update rows in the table. A single
column may be updated or more than one column could be updated.
Specific rows could be updated based on a specific condition. The following
example shows how to update tables that contain user defined data types.
Example
SQL > update employee set eadd = address (10, 'MITE', 576119 ) where
eno = 10;
Example
SQL> update employee e set e.eadd.add2 = 'bgl' where e.eno = 10;
The above example shows us how we can update a table, which contains a
column, which has a user-defined type as its data type. It also shows how a
single column of the user defined type can be accessed and its value
changed. An alias for the table has to be used so that internal references
are resolved.
To delete several rows from a table, select the rows with appropriate
conditions in a 'where' clause. The 'where' clause can also include a query.
The following example will delete a row from customer whose comm_type
is 'b'.
Example
SQL > delete from IT_customer where custid in (select custid from
order_info where commplan = 'b');
The delete command allows deletion of rows from a table. A single row or
many rows could be deleted. Alternatively, all rows of a table could also be
deleted while the structure is maintained. To delete specific rows from a
table the 'where' clause is used in combination with the delete command.
Example
SQL> delete from emp e where e.eadd.add1 = 10;
The above example shows how we can delete a specific row from a table
that has a user defined type.
Example
SQL> select prodid, stdprice, minprice, stdprice + minprice from price where
enddate = '4-jun-89';
We know that stdprice + minprice is not a column in the table price, yet
SQL *Plus would display it as a separate column. Arithmetic operations can
also be performed in a 'where clause’.
Example
SQL > select ordid, itemid, 100 * (actualprice + qty) from IT_item
where prodid = 120;
In the above example, only after adding actual_price with qty it is multiplied
by 100. If parenthesis is omitted then multiplication will be performed first
followed by addition. Thus we can control the order of evaluation by using
parenthesis. We now move on to study about comparison operators.
The last four operators mentioned above can also be used for checking the
NOT conditions like NOT BETWEEN, and so on. The following example
illustrates the comparison operator "greater than".
Sikkim Manipal University Page No.: 62
Oracle9i and Distributed Database Unit 2
Example
SQL > select * from EMP where sal > 5000;
The above example displays the rows for which the sal is greater than 5000.
An example discussed below is used to check for negations, (i.e. check for
NOT conditions).
Example
SQL > select * from order_info where not (custid = 2 or custid = 4);
The above command will display all columns where custid is neither 2 nor 4.
The IN operator can be used to select rows that match one of the values in
a list.
Example
SQL > select * from emp where sal in (5000, 7000);
The above example lists all the columns in a table for which salary lies in the
given list. When we search for character values using IN operator, the
column name must exactly match with the values present in the list. In the
case of LIKE operator, which is used to search a character pattern, we need
not know the exact character value. The LIKE operator recognizes special
characters like % and _. The former can match zero or more characters,
while the latter matches exactly one character. % cannot be used to match
a Null character.
Example
SQL > select name, address, city from IT_customer where name like 'S%';
The above command will display the columns name, address, city and
phone whose name begins with 'S'.
Example
SQL > select * from IT_customer where name like 'A_F';
The above command lists all the columns from customer table whose
names are three letters long, starting with 'A' and ending with 'F'.
AND operator:
Example
SQL > select * from emp where sal >4000 AND deptno < 2;
And operator will display all the columns from EMP table provided both the
conditions mentioned below are satisfied.
We have now gone through the various operators supported by SQL *Plus.
Now let us have a look at the order in which these operators are evaluated.
Operator precedence
The precedence of the operators discussed above are given below: -
Arithmetic operators ------------------- highest precedence
Comparison operators
NOT logic operator
AND logic operator
OR logic operator -----------------------lowest precedence
can also be included in a ‘where’ clause. The single row functions can be
broadly classified as:
Date Functions
Character functions
Numeric function
Conversion functions
Miscellaneous functions.
The examples that follow mostly use the system table "dual". It is a table,
which is automatically created by Oracle9i along with the data dictionary.
Dual table has one column defined to be of varchar2 type and contains only
one row with value 'x'.
Example
SQL > select * from dual;
Add_months
This date function returns a date after adding a specified date with the
specified number of months. The format is add_months(d,n), where d is the
date and n represents the number of months.
Example
SQL > select add_months(sysdate, 2) from dual;
The result will be '7-apr-99', if sysdate is '7-feb-99' .The sysdate variable will
display system date.
Last_day
The format is last_day(d), which returns the date corresponding to the last
day of the month.
Example
SQL > select sysdate, last_day(sysdate) from dual;
The above command will display the system date and last day of the current
month.
Months_between
To find out the number of months between two dates, we use the
months_between function.
Where d1, d2 are dates. The following example returns the number of
months between two dates. The output will be a number.
Example
SQL > select months_between(to_date('1-5-99','dd-mm-yy'),
to_date ('1-3-99', 'dd-mm-yy')) "date" from dual;
The result will be 2.
Next_day
The format for this function is next_day(d,day)
Where d represents date and day implies any weekday. This function can
be illustrated with the following example.
Example
SQL > select next_day (sysdate, 'Wednesday') from dual;
Round
This function returns the date, which is rounded to the unit specified by the
format model. Its format is Round(d,[ fmt])
Where d is the date and fmt is the format model. Fmt is only an option, by
default date will be rounded to the nearest day.
Example
SQL> select round (to_date('23-sept-98', 'dd-mon-yy'), 'year') from dual;
'1-jan-99' will be the result.
Example
SQL > select round (to_date('23-sept-98', 'dd-mon-yy'), 'month') from dual;
'01-oct-99' will be the result.
Example
SQL > select round (to_date('23-oct-98', 'dd-mon-yy'), 'day') from dual;
'20-oct-99' will be the result because it rounds '23-oct-99'.
Truncate
This function return the date with the time portion of the day truncated to the
unit specified by format model. The syntax is trunc(d,(fmt]). If fmt is
neglected, then date is converted to the nearest day.
Example
SQL > select trunc(to_date('23-oct-99', 'dd-mon-yy'), 'year') from dual;
'01-jan-99' will be the result.
Example
SQL > select trunc(to_date('23-oct-99', 'dd-mon-yy'), 'month') from dual;
‘01-oct-99' will be the result.
Example
SQL > select trunc(to_date('23-oct-99','dd-mon-yy'), 'day') from dual;
'20-oct-99' will be the result because it rounds '23-oct-99' to the nearest
Sunday.
Example
SQL > select trunc (to_date('23-oct-99', 'dd-mon-yy')) from dual;
The above statement does not include fmt, and therefore it is rounded to
nearest day i.e. '23- oct-99'.
Greatest
The function is greatest(d1, d2,), where d1 and d2 are dates. This function
returns the last date present in the argument. Consider the following
example, which will display the date in the list.
Example
SQL> select greatest(to_date('2-7-88', 'dd-mm-yy'), to_date('7-4-99',
'dd-mm-yy')) from dual;
Output will be '7-apr-99'
Arithmetic operations on date values
We can perform arithmetic operations on date values which comprises
date + number -------- adds number of days to date
date – number --------subtracts number of days from date
date – date --------- subtracts dates, producing number of days
Example
SQL> select ordid, shipdate + 15 from order_info where ordid =700;
2.9 Summary
This unit has given the broad details about structured Query Language for
Oracle9i. It has helped in preparing Queries using DML, DDL. It also lists
the various operators available for SQL using Oracle9i. And also given light
on functions of Oracle9i SQL.
2.10 Exercise
1. Write a note on SELECT, INSERT, DELETE and UPDATE with
example.
2. What are the operators available with SQL for Oracle9i ?
3. List the 10 Numeric functions with examples.