The document provides an overview of data types and constraints in SQL, detailing various numeric, character, date and time, and boolean data types along with their uses. It also explains PostgreSQL constraints, including NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and EXCLUSION, which ensure data accuracy and integrity. Examples of assigning constraints to specific data fields are included to illustrate their application.
The document provides an overview of data types and constraints in SQL, detailing various numeric, character, date and time, and boolean data types along with their uses. It also explains PostgreSQL constraints, including NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and EXCLUSION, which ensure data accuracy and integrity. Examples of assigning constraints to specific data fields are included to illustrate their application.
Course created by Satish Dhawale | www.skillcourse.in
Numeric Data Types
Data Type Description Example Use
INTEGER Stores whole numbers. Employee IDs, Age
SERIAL Auto-incrementing integer. Primary key auto-increment
BIGINT Stores large integers. Population, Large counts
Stores exact numbers with
NUMERIC(p, s) Financial data (salary) precision (p) and scale (s).
Stores floating-point numbers
REAL Scientific calculations (single precision).
Stores double-precision floating-
DOUBLE PRECISION High-precision computations point numbers. Example Character Data Types
Data Type Description Example Use
Fixed-length string of n CHAR(n) Employee codes characters. Variable-length string up Names, Email VARCHAR(n) to n characters. addresses Descriptions, TEXT Unlimited-length string. Comments Example Date and Time Data Types
Data Type Description Example Use
Stores date (year, month, DATE Birthdate, Hire date day). Stores time (hour, minute, TIME Appointment times second). TIMESTAMP Stores date and time. Order timestamps Stores date and time with TIMESTAMPTZ Global event tracking timezone info. INTERVAL Stores duration of time. Duration between events Example Boolean Data Type
Data Type Description Example Use
Stores TRUE, FALSE, Flags, Active
BOOLEAN or NULL. status PostgreSQL Constraints: In PostgreSQL, constraints are rules enforced on data in tables to ensure accuracy, consistency, and integrity. They define conditions that the data must meet and are applied to columns or tables during table creation or modification.
PRIMARY KEY: student_id uniquely identifies each student.
NOT NULL: name cannot be NULL UNIQUE: email must be unique. CHECK: age must be 18 or older. DEFAULT: registration_date defaults to the current time. FOREIGN KEY Constraint The FOREIGN KEY constraint ensures that values in a column match values in another table’s primary key, maintaining referential integrity.