Data manipulation:
DATA TYPES
CONTENTS
The common data types are:
Numeric type
Character type
Date / Time Type
Boolean type
Enumerated type
DATA TYPES
Name Storage Description
type
Smallint 2 bytes Small range integer
Integer 4 bytes Typical choice of
integer
BigInt 8 bytes Large range integer
Decimal Variable User specified
Numeric Variable User specified
Real 4 bytes Variable precision
Double precision 8 bytes Variable precision
Serial 4 bytes Auto incrementing
integer
Bigserial 8 bytes Large auto-
incrementing integer
INTEGER TYPES
Smallint, Integer, BigInt store whole numbers, i.e. without
fractions
Selection depends on range, storage size and performance
Small integer if disk space is at a premium
BigInt if the range of the integer type is insufficient. It is
faster.
PRECISION NUMBERS
Arithmetic on numeric values is slow compared to integers
For storage of monetary amount where precision is required
Numeric is the total count of significant digits in whole
number, that is, both sides of the decimal point
E.g. 235141 had a precision of 6 and a scale of 4
Syntax is : numeric (precision, scale), that is, numeric(6,4)
The precision must be positive
FLOATING TYPE
REAL type has a range of at least 1E-37 (1x10-37) to 1E+37 (1x1037)
Double precision has a range of 1E-307 (1x10-307) to 1E+307 (1x10308)
with a precision of at least 15 digits
Values that are too large or too small will cause an error
Numbers too close to zero that are not representable as distinct from
zero will cause an underflow error
SERIAL TYPES
Serial and Bigserial are a notational convenience for creating
unique identifier columns (similar to AUTO-INCREMENT)
CREATE TABLE tablename (colname SERIAL);
CHARACTER TYPES
Character varying(n) and character(n), where n is a positive integer
Both can store strings up to n characters (not bytes)
An attempt to store a longer string into a column will result in an
error unless the excess characters are all spaces
Varchar(n) alias for character varying(n)
Char(n) alias for character(n)
Character without length specifier is equivalent to character(1)
If character varying is used without a length specifier, the type
accepts strings of any size
Name Description
Char varying(n), varchar(n) Variable length with limit
Character(n), char(n) Fixed length, blank padded
TEXT
Name Description
Text Variable unlimited length
Text – stores strings of any length
Storage requirements for short string (up to 126 bytes) is 1
byte plus the actual string which includes the space padding
in the case of character
Longer strings have 4 bytes of overhead instead of 1
Date / TIME types
Timestamp without time 8 bytes Both date and time
zone
Timestamp with time zone 8 bytes Both date and time
Date 4 bytes Date (no time of day)
Time (without time zone) 8 bytes Time of day (no date 00:00:00)
Time (with time zone) 12 bytes Time of day only, with time zone (00:00:00
+ 1459)
Timestamp is equivalent to timestamp without time
BOOLEAN TYPE
State True/False and unknown which is represented by SQL
TRUE, ‘t’, ‘true’, ‘y’, ‘yes’, ‘on’, 1
FALSE, ‘f’, ‘false’, ‘n’, ‘no’, ‘off’, ‘0’
FOR EXAMPLE
CREATE TABLE test1 (a Boolean, b text);
ENUMERATED TYPE
Comprise a static, ordered set of values
Enum type are created suing the CREATE TYPE command, e.g.
CREATE TYPE mood AS ENUM (‘sad’, ‘ok’, ‘happy’);
Once created, the enum type can be used in the table and function
definition much like any other type:
CREATE TYPE mood AS ENUM (‘sad’, ‘ok’, happy’);
CREATE TABLE person (
Name text;
Current_mood mood);
DEFAULT VALUES
A column can be assigned a default value
If no default value is declared explicitly, the default value is null
CREATE TABLE product(
Product_no integer,
Name text
Price numeric DEFAULT 9.99);
To set a new default value for a column
ALTER TABLE product ALTER COLUM price SET DEFAULT 10.89
To remove any default value use
ALTER TABLE product ALTER COLUM DROP DEFAULT
DATA CONSTRAINTS
Limits the kind of data stored in a table
E.g. column containing price should only accept a positive
number
CREATE TABLE product (
Price numeric DEFAULT 9.90 CHECK (price>0));
DATA CONSTRAINTS
Adding constraints to existing field
ALTER Table product ADD CHECK name <>’’);
This ensures that the name field is not empty
ALTER TABLE product ADD CONSTRAINT some_name UNIQUE
(product_no);
This creates a constraint called some_name which ensures that
product_no fields are unique
ALTER TABLE product ALTER COLUM product_no SET NOT NULL
Product_no is not null
REMOVING A CONSTRAINT
To remove a constraint you need to know its name
ALTER TABLE product DROP constraint some-name;
To drop the not null constraint
ALTER TABLE product ALTER COLUM DROP NOT NULL;
CHANGING DATA TYPES
To convert a column to a different data type
ALTER TABLE products ALTER COLUMN price TYPE numeric (10,2);
To rename a column
ALTER TABLE product RENAME COLUM product_no TO product_number;
To add a column
ALTER TABLE product ADD COLUMN description text
ALTER TABLE name_of_table ADD COLUMN attribute datatype
Remove column
ALTER TABLE product DROP COLUMN description
CHANGING DATA TYPES
To rename a table
ALTER TABLE product RENAME to items;
To give a constraint a separate name
CREATE TABLE product(
Price numeric DEFAULT 9.99 CONSTRAINT positive_price CHECK
(price>0);
CHANGING DATA TYPES
INDEXES
Maintaining an index of a particular column to make searching
easy e.g. product number of a product table
To create an index on the product_id column:
CREATE INDEX product_id_index ON product (product_id)
When the table is modified the index is automatically updated
To remove an index
DROP INDEX product_id_index
CONCLUSION
The common data types are:
Numeric type
Character type
Date / Time Type
Boolean type
Enumerated type