0% found this document useful (0 votes)
38 views

Oracle Datatypes: Character Strings CHAR (Size)

This document discusses Oracle datatypes. It describes character strings like CHAR, NCHAR, VARCHAR2, NVARCHAR2, and LONG. It also covers number, date/time datatypes like DATE, TIMESTAMP, and intervals. Binary datatypes like RAW, LONG RAW, BLOB, CLOB, and NCLOB are described. It also discusses rows using ROWID and DDL using commands to create, alter, and drop objects. Tables are created using the CREATE TABLE command and data is inserted using the INSERT command. The SELECT command is used to view data in tables.

Uploaded by

NishantRai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Oracle Datatypes: Character Strings CHAR (Size)

This document discusses Oracle datatypes. It describes character strings like CHAR, NCHAR, VARCHAR2, NVARCHAR2, and LONG. It also covers number, date/time datatypes like DATE, TIMESTAMP, and intervals. Binary datatypes like RAW, LONG RAW, BLOB, CLOB, and NCLOB are described. It also discusses rows using ROWID and DDL using commands to create, alter, and drop objects. Tables are created using the CREATE TABLE command and data is inserted using the INSERT command. The SELECT command is used to view data in tables.

Uploaded by

NishantRai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

ORACLE DATATYPES

These Oracle datatypes are as follows:


Character Strings
CHAR (size) –
•A fixed-sized field of characters.
•It can only hold 2000 characters.
•If you don’t specify the length of the CHAR datatype, the default size is a
single character (i.e. 1 byte).
•If the user enters the value shorter than the specified length then the
database blank pads it to the fixed length.
•If the user enters the value larger than the specified length then the
database would return an error.
NCHAR (size) – A fixed-sized field of characters, where the character set is
determined by its definition. So, the maximum size is 2000 bytes per row or
2000 characters. This handles multibyte character sets.
VARCHAR/VARCHAR2 (size) – A variable-sized field of
characters. The largest this datatype can become is 4000
characters.

NVARCHAR2 (size) – A variable-sized field of characters,


where the character set is determined by its definition. The
maximum size is 4000 bytes per row or 4000 characters. This
handles multibyte character sets.

Note: The VARCHAR2 datatype is the successor of


VARCHAR. So it is recommended that you use VARCHAR2 as
a variable-sized array of characters.
LONG – A variable-sized field of characters. The maximum size of this field
is 2GB.
Following restrictions needs to be fulfilled when a long data type
attribute is cast on a column in a table.
–Only one column in a table can have long data type.
–The column should not contain unique /primary key constraints.
–The column cannot be indexed.
–Procedures or stored procedures cannot accept long data type as arguments.

NUMBER (precision, scale) –


A variable-sized number, where the precision is between 1 and 38 and size is
between -84 and 127. A NUMBER datatype with only one parameter is
NUMBER (precision), where the parameter specifies the precision of the
number. A NUMBER datatype with no parameters is set to its maximum size.
Date and Time
DATE –
A DATE data type is used to store date & time. Oracle 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, & second. One thing to note is that the time is stored as
part of the date. The default format DD-MON-YY.( can be overridden by
NLS_DATE_FORMAT.)
TIMESTAMP (precision) –
A variable-sized value ranging from 7 to 11 bytes, that is used to represent a
date/time value. It includes both date and time. The precision parameter
determines how many numbers are in the fractional part of SECOND field. The
precision of the SECOND field within the TIMESTAMP value may have a value
ranging from 0 to 9 with a default precision of 6.

TIMESTAMP (precision) WITH TIME ZONE – A fixed-sized value of 13 bytes,


which represents a date/time value along with a time zone setting. There are
two ways one can set the time zone. The first is by using the UTC offset, say
‘+10:0’, or secondly by the region name, say ‘Australia/Sydney’.

TIMESTAMP (precision) WITH LOCAL TIME – A variable value ranging from 7


to 11 bytes. This particular datatype is similar to the TIMESTAMP WITH TIME
ZONE datatype. The difference is that the data is normalised to the database
time zone when stored. The entry is manipulated to concur with the client’s time
zone when retrieved.
Intervals

INTERVAL DAY (day_precision) TO SECOND (second_precision) – A fixed-


sized 11 byte value that represents a period of time. It includes days, hours,
minutes and seconds.
INTERVAL YEAR (year_precision) TO MONTH - A fixed-sized 5 byte value
that represents a period of time. It includes years and months.
Binaries
RAW (size) – A variable-sized field of raw binary data. The maximum size for
this datatype is 2000 bytes.

LONG RAW - A variable-sized field of raw binary data. The maximum size for
this datatype is 2 GB.

BLOB – The Binary Large Object is a field that holds unstructured binary data.
The maximum size for this datatype is 4 GB.

CLOB – The Character Large Object is a field that holds single byte character
data. The maximum size for this datatype is 4 GB.

NCLOB – The National Character Large Object is a field that holds either single
byte of multibyte character data dependent on the national character set. The
maximum size for this datatype is 4 GB.

BFILE – An external binary file. The maximum size for this file is 4 GB. The size
is also limited by the operating system.
Rows

ROWID – A datatype that contains binary data that is used to identify a row.
Each ROWID is:
6 bytes for normal indexes on non-partitioned tables, local indexes on partitioned
tables and row pointers for chained/migrated rows.
10 bytes for global indexes on partitioned tables.

ROWID – The Universal ROWID is the datatype used to store both logical and
physical ROWID values as well as foreign tables accessed through a gateway.
DDL (Data Definition Language)

Data Definition Language is used

1. to Create an object
2. to alter the structure of an object
3. to drop an object created
How To Create Table?

• Syntax :
create table tablename
(columnname data type(size),
columnname data type(size), …);

 We should specify unique column name


 We should specify proper data type along with its width.
 If the above command is executed successfully the message
“table created” is displayed.
Naming Conventions For Table Name

The table name should adhere strictly to the following norms

 First letter should be alphabet


 Reserve word cannot be used
 Maximum length for table name is 30 characters
 Two different tables cannot have same name
 Underscore, numerals & letters are allowed but not blank space and
Single quotes.

Note:
If the user uses double quotes then upper & lower case are not
equivalent.
e.g. “info”, “INFO”, “Info” are not same.
Example To Create Table

• Create table client_master

(client_no varchar2(6), name varchar2(15),


addr varchar2(25), bal_due number(10,2));
How to view table structure?

Command: desc
If user wants to view the table structure above
command will achieve the same.

Syntax:
Desc (tablename);

Example:
desc client_master;
This will display structure of table client_master.
How to Insert Data Into Table?
• Insert Command:
Once table is created it remains
skeletal structure unless it is populated with data. The
insert command is used to add one or more records to
a table.
• Syntax:
insert into tablename values(expression,expression,…);
OR
insert into tablename
(columnname, coumnname,…)
values(expression,expression,…);
Insert Statement :
When inserting a single row of data into a table, the insert
operation,
 creates a new row in the database table

 loads that row with the values passed into all the columns
specified

 In Insert statement columns & values have one to one


relationship. (first value into first column)

 If there are exactly same no. of values as per no. of columns &
values are in accordance with the way the column were created;
then no need to specify column name

 If there are less values than the columns in the table then it is
mandatory to indicate both the column name & its corresponding
value in the insert statement.
(….continued)

 While using ‘insert’ values are separated by commas and values


having data types char, varchar/varchar2, raw, long, date should be
enclosed in the single quotes.

 Values must be entered in the same order as they are defined in the
table.
• Use Of ‘&’ In Insert Statement

Syntax:
Insert into tablename values(‘&expr1’, ‘&expr2’,…..);

Oracle will prompt the user to enter values for specified


columns.

• Discussion of example.
Viewing Data In The Table

Select Command:

Request for information stored in a table can be done


through the select command.
(This is generally referred as ‘querying’ the table.)

We can use ‘Select’ to view


1. All rows and all columns
2. Filtered data
• All Rows And All Columns:
Syntax
In order to view global table data the syntax is,
select columnname1, columnname2, ……..n from tablename;
e.g.
select cl_no, name, addr, bal_due from client_master;
OR
select * from tablename;
e.g.
select * from client_master;

Note: Oracle allows the user to use the meta character


asterisk(*), which is expanded by the oracle to mean all columns.
• Filtering Table Data:
SQL gives us filtering out data that is not
required. The ways of filtering table data are,

1. selected columns and all rows

2. selected rows and all columns

3. selected columns and selected rows

Note: Oracle engine compiles the sentence, executes it, and


retrieves data for all columns/rows from the table.
• selected columns and all rows:
It is retrieval of specific columns of a table.

Syntax:
select columnname ,columnname
from tablename;
Example:
select client_no, name
from client_master;
• selected rows and all columns

• Oracle provides the option of using where clause in combination


with select statement to apply filter on rows.

• Syntax

Select *
from tablename
where search condition;

Example
Select *
from client_master
where bal_due>0;
• Where Clause

 When where clause added to SQL sentence, the oracle server


compares each record from the table with the condition specified in
where clause.

 Oracle displays only those records that satisfy the specified


condition.

 Where clause can appear only after from clause.

 In search condition all standard operators can be used.


(logical, arithmetic, comparison)
• Selected Rows And Selected Columns

It is used to view specific data set from a table .

Syntax:
select columnname, columnname
from tablename
where search condition;
Example:
Select client_no, client_name
from client_master
where bal_due>0;
• Elimination Of Duplicates:

 To prevent selection of duplicate rows, we include distinct clause in


the select statement.

Syntax:
select distinct columnname, columnname
from tablename;

The above syntax scans through the values of columns


specified and displays unique values from amongst them.

Example:
select distinct job from employee;
(….continued)

Syntax:
select distinct *
from tablename;
 The above syntax scans through entire rows, and eliminates rows
that have exactly the same contents in each column.

Example:
select distinct * from client_master;
This will select only unique row from client_master,
• Sorting Data In a Table

 Oracle allows data from a table to be viewed in a sorted order.

 The rows retrieved from the table will be sorted in either


ascending or descending order

 Ascending or descending order of the data is depends on condition


specified in the select clause.

Syntax
select * from tablename
order by columnname, columname [sort order];
• Ascending Order:

select * from client_master


order by client_no;
The above query will display all records from client_master
sorted in ascending order as per column client_no.

• Descending Order:
For viewing the data in descending order the word ‘desc’ must
mentioned after the column name and before the semi colon in
order by clause.
In case there is no mention of sort order in order by clause
Oracle engine sorts in ascending order by default.

select client_no, name, addr1, city


from client_master
order by client_no desc;
• Creating a Table From a Table:
syntax:
create table tablename
[(columnname, columnname)]
as select columnname, columnname from tablename;

 Source Table: Source table is that identified in the select clause.


Target Table: The target table is one specified In the create statement.

 The above SQL statement will populate target table with the data from
source table.
• (….continued)

• Example:

create table course(Coursename, Fees, Stud_admit)


as select Coursename, Fees, Stud_admit
from college_info;

If the source table college_info was populated with records these


will be uploaded in course table.
• How to create table structure from Source Table?

• To create a target table without records from source table


(only structure creation) , the select statement should
contain a ‘where clause’.

• The condition specified in where clause must not be


satisfied.
• Inserting Data Into a Table From Another Table:

Oracle allows to populate a table with data that already exist in another
table. (one row at a time into a table).

Insertion of records from one table to another:


Syntax:
insert into tablename
select columnname, columnname
from tablename;

Example:
insert into Client_master
select client_name, addr
from client_info;
Insertion of data set from one table to another:
Syntax:
insert into tablename
select columnname, columnname
from tablename
where column=expr;

Example:
insert into Client_master
select client_name, addr
from client_info
where client_name=‘Akash’;
• Delete Operations:

 The Delete command is used to remove rows from table.


 We can use delete command for two operations.
1. To delete all the rows from table
2. To delete a set of rows.

• Removal of All Rows:


Syntax:
delete from tablename;
Example:
delete from client_master;
• (…continued)
Removal Of Specified Rows:

Syntax:
delete from tablename
where search condition;

Example:
delete from client_master
where bal_due<1000;
• Updating Contents Of A Table:

 Update command is used to change data values in a table.

 We can use update command


 To update all rows from a table
 To update data set of rows from a table

Updating All Rows:


Syntax:
update tablename
set columnname=‘expr’;
Example:
update client_master
set bal_due=5000;
• Updating Records Conditionally:

Syntax:
update tablename
set columnname=‘expr’
where search condition;
Example:
update client_master
set bal_due=5000
where client_name=‘Thomus’;
• How to Modify Structure Of Table?

We can modify structure of table by,


1. Adding new column
2. Modifying existing column

To Add New Columns:


Syntax:
alter table tablename
add(newcolumnname datatype(size),…..n);

alter table client_master


add(client_tele numer(8));
• Modifying Existing Column:

Syntax:
alter table tablename
modify(columnname newdatatype(newsize),…..n);
Example:
alter table client_master
add(client_tele numer(10));
• Restrictions on the Alter Table:

Alter table cannot be performed


 to change name of the table
 to change name of the column
 to drop a column
 Decrease size of a column if table data exists
• To Rename Tables:

Syntax:
rename oldname to newname;

Example:
rename client_master to client_info;
• Destroying Table:

Syntax:
drop table tablename;
Example:
drop table client_master;
• Examining Objects Created By User:

select * from tab;

This command will display object name and type of


object.

You might also like