SQL – Standard Query Language
SQL has been adopted by most RDBMSs for the retrieval and management of data in relational
databases. The American National Standards Institute (ANSI) has been refining standards for the SQL
language. Oracle, like many other companies, has taken the standard and extended it to include other
functionalities.
SQL statements are like plain English but with specific syntax. SQL is a simple yet powerful language to
create, access and manipulate data and structure in the database. SQL statements can be categorized
as listed below.
SQL Category Description
Used to access, create, modify or delete data in existing
Data Manipulation Language (DML) structures of the database.
SELECT – query information
INSERT – add new rows
UPDATE – modify existing rows
DELETE – delete existing rows
MERGE – perform conditional update or insert
operation
EXPLAIN PLAN – see an execution plan of SQL
LOCK TABLE – lock a table to restrict access
Used to define, alter and drop database objects and their
Data Definition Language (DDL) privileges.
CREATE – add new database objects
ALTER ‐ modifies existing database objects
DROP – remove existing database objects
RENAME – changes the name of existing database
objects
TRUNCATE – Remove all rows from a database
object with deleting the object
GRANT, REVOKE – manage privileges
AUDIT, NOAUDIT ‐ Audit the database
COMMENT – Add a description about an object to
the dictionary
Used to group a set of DML statements as a single transaction.
Transaction Control COMMIT – Save changes
ROLLBACK – Discard changes
SAVEPOINT – Set a marker in a transaction for
rollback
SET TRANSACTION – Define the properties of the
transaction.
Used to control the properties of a user session (A session if
Session Control the point from which you are connected to the database until
you disconnect)
ALTER SESSION – Control session properties
SET ROLE – Enable/ Disable Roles
Used to manage the properties of the database
System Control ALTER SYSTEM
Oracle Datatypes
When creating tables, a data type must be specified for each column you define. The data types in
Oracle can be categorized into five categories and are listed below.
CHARACTER NUMERIC ROWID BINARY DATE AND TIME
CHAR NUMBER ROWID RAW DATE
VARCHAR2 BINARY_FLOAT UROWID LONG RAW TIMESTAMP
CLOB BINARY_DOUBLE BLOB TIMESTAMP WITH TIMEZONE
LONG FLOAT BFILE TIMESTAMP WITH LOCAL
TIMEZONE
NCHAR INTERVAL YEAR TO MONTH
NVARCHAR2 INTERVAL DAY TO SECOND
NCLOB
CHAR
The CHAR datatype has the following format
CHAR [(size [BYTE | CHAR])]
The CHAR datatype is a fixed length alphanumeric string. We can specify the maximum size ‘size’ in
BYTE or CHAR. If you do not specify BYTE or CHAR, the default is BYTE but in the ASCIII 7 bit system, one
BYTE is equivalent to one CHAR.
If you do not specify any size, the default size is one.
The maximum size of a CHAR is 2000 bytes.
When you create a column of type CHAR, the database will always ensure that the data placed in the
column has the defined length. If the data is shorter than the defined length, it is space padded to the
right. If the data is longer, an error is raised.
NCHAR
The NCHAR datatype is similar to CHAR but it is used to store Unicode character set data. The maximum
size for an NCHAR is 2000 bytes. The NCHAR function has the following format
NCHAR [(size)]
VARCHAR2
The VARCHAR2 data type has the following format
VARCHAR2 (size [BYTE | CHAR])
The VARCHAR2 datatype is a variable length alphanumeric string, which has a maximum size ‘size’.
VARCHAR2 columns require only the amount of space need to store the data, i.e. an empty VARCHAR2
(200) column takes up the same room as an empty VARCHAR2(1) column.
There is no default size for VARCHAR2.
The maximum size for VARCHAR2 is 4000 bytes.
NVARCHAR2
The NVARCHAR2 datatype is similar to the VARCHAR2 but is used to store Unicode variable length data.
The maximum size allowed is 4000 bytes. The NVARCHAR2 has the following format
NVARCHAR2 (size)
NUMBER
The NUMBER datatype has the following format
NUMBER [(p [, s])]
The NUMBER data type can be used to store both fixed point and floating point numbers.
The NUMBER datatypes stores numbers with a precision of ‘p’ digits and a scale of ‘s’ digits. The range
for the precision is between 1 and 38 and the range for the scale is between ‐84 and 127.
Both the precision and scale values are optional, if both are omitted, Oracle assumes the maximum for
each.
If you define a number as NUMBER(4 , 2). The range of values you can store is ‐99.99 to 99.99, i.e.
the number of decimal places is 2, and the number of digits in the integer part is 2 (4 ‐2). Even if we do
not include the decimal part in the number, the largest number which can be stored is 99. If we
attempted to inset 12.125, the actual value stored would be 12.13.
STORED
VALUE DATATYPE EXPLANATION
VALUE
The precision and scale are set to
123.2564 NUMBER 123.2564 the maximum so it can store any
value
NUMBER(6, Since the scale is 2, the decimal part
1234.6789 1234.68
2) is rounded to two digits
12345.123 NUMBER(6,2) ERROR The integer part is too large
123456 NUMBER(6,2) ERROR The integer part is too large
The decimal part is rounded to the
1234.98 NUMBER(6) 1235
next integer
The negative scale rounds the
NUMBER(5,‐
12345.345 12300 number 's' digits left of the decimal
2)
point
The negative scale rounds the
NUMBER(5,‐
1234567 1234600 number 's' digits left of the decimal
2)
point
NUMBER(5,‐
12345678 ERROR The integer part is too large
2)
The use of * in the precision
12345.58 NUMBER(*,1) 12345.6
specifies the default size of 38
Requires a zero after the decimal
0.1 NUMBER(4,5) ERROR
point (5 ‐4 = 1)
Rounded to four digits after the
0.0123457 NUMBER(4,5) 0.01235
decimal point and zero
Stored as it is, only four digits agter
0.09999 NUMBER(4,5) 0.09999
the decimal point and zero
Rounding this value to four digits
0.099996 NUMBER(4,5) ERROR after the decimal and zero results in
0.1, which is outside the range
DATE
The syntax for the DATE format is as follows
DATE
The DATE datatype is used to store date and time information.
The following information is stored in each DATE
Century
Year
Month
Day
Hour
Minute
Second
The DATE datatype occupies 7 bytes of storage space.
The default format to display the date is DD – MON – YY.
If you specify a date value without the time component, the default time is 12am.
The NLS_DATE_FORMAT parameter can be used to display different date formats.
TIMESTAMP
The TIMESTAMP datatype has the following format
TIMESTAMP [ ( p ) ]
The TIMESTAMP data types stores date and time information with fractional second precision. The
difference between DATE and TIMESTAMP is the ability to store fractional seconds up to 9 digits. If no
precision is specified for TIMESTAMP, the default is 6.
The default fractional second precision for DATE is 3 digits.