SQL has a number of data types that may be used for attributes:
-
Integers: typically we use
INT/INTEGERwhich are implicitly signed, we may usedUNSIGNED INTEGERfor the unsigned variant.INT(n)specifies the number of digits used. -
Approximate real numbers: use
FLOAT/REAL/DOUBLE, can specify digit precision asFLOAT(n)wherenis the number of bits used to store the mantissa of the floating point number. -
Exact real numbers: use
DECIMAL(i,j)to specify a fixed-point decimal number withibeing the precision (total number of digits to store the number), andjdenoting the position of the point / the scale. -
Strings: We can use a number of types here:
CHAR(n)/CHARACTER(n): fixed length, right padded with spacesVARCHAR(n)/CHAR VARYING(n): varying lengthCLOB/TEXT: character large object
-
Binary data: Again we can use similar notation:
BIT(n): fixed lengthBIT VARYING(n): varying lengthBLOB: binary large object
title: Best practice: hash binary data and store that in the database with the metadata. -
Boolean: use
BOOLEANorBIT(1), choice may vary from implementation to implementation, and in some scenarios it may be `NULL` -
Date and Time: We have multiple different types to specify time:
DATE: made up of year-month-day (“yyyy-mm-dd”)TIME: made up of hour:minute:second (“hh:mm:ss”)TIME(i):TIMEplus i additional digits for fractions of a second (“hh:mm:ss:ii…i”)DATETIME/TIMESTAMP: bothDATEandTIMEcomponents
-
Interval: we can use
INTERVALto specify relative time value as opposed to absolute, can be day/time intervals or year/month intervals. Can be positive or negative when added to or subtracted from an absolute value, the result is an absolute value. -
There are additional domain specific / complex types which aren’t relevant for the course:
- Special Types:
CURRENCY/MONEY - Spatial Types (GIS):
GEOMETRYtype - Enumerated Types:
ENUM("One", "Two", "Three") - Collection Types:
SET/VALUE_MAP
- Special Types: