SQL and Datatypes
SQL and Datatypes
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.
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.
CHAR
The CHAR datatype has the following format
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.
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
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.
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
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
Century
Year
Month
Day
Hour
Minute
Second
If you specify a date value without the time component, the default time is 12am.
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.