Built-in data types in SQL
In addition to the basic data types, the SQL standard supports several data types:
-
DATE
Dates in
YYYY-MM-DD
format- e.g.,
DATE '2001-11-15'
- e.g.,
-
TIME
Time of day in hours, minutes, and seconds
- e.g.,
TIME '09:00:30'
,TIME '09:00:30.75'
- e.g.,
-
TIMESTAMP
Date plus the time of day
- e.g.,
TIMESTAMP '2001-11-15 09:00:30.75'
- e.g.,
-
INTERVAL
Period of time
- e.g.,
INTERVAL '1' day
- Subtracting a
DATE
/TIME
/TIMESTAMP
from another gives anINTERVAL
value INTERVAL
values can be added toDATE
/TIME
/TIMESTAMP
values
- e.g.,
Large-object types
Many current-generation database applications need to store attributes that can be large. SQL provides large-object data types for character data (CLOB
) and binary data (BLOB
). Here, ‘lob’ stands for ‘Large OBject.’
When a query returns a large object, a pointer is returned, rather than the large object itself.
User-defined types
SQL provides the notion of distinct types. The CREATE TYPE
clause can be used to define new types.
a
: Name of the new typet
: Pre-existing type
Before user-defined types were added to SQL, SQL had similar but subtly different notion of domain, which can add integrity constraints to an underlying type.
d
: Name of the new domaint
: Pre-existing type
There are two significant differences between types and domains:
- Domains can have constraints specified on them, and can have default values defined for variables of the domain type, whereas user defined types cannot have constraints of default values specified on them.
- Domains are not strongly typed.
Index creation
Many queries reference only a small proportion of the records in a table. Thus, it is inefficient for the system to read every record to find a record with particular value.
An index on an attribute of a relation is a data structure that allows the database system to find those tuples in the relation that have a specified value for that efficiently, without scanning through all the tuples of the relation.
i
: Name of the indexr
: RelationA_i
: Attributes
Example
The above query can be executed by using the index to find the required record, without looking at all records of student.