0% found this document useful (0 votes)
4 views

6 SQL – Data Types and Constrains in SQL

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.

Uploaded by

itmanarif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

6 SQL – Data Types and Constrains in SQL

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.

Uploaded by

itmanarif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Day – 6 :

Data types and


Constrains in SQL

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.

• NOT NULL Constraint


• UNIQUE Constraint
• PRIMARY KEY Constraint
• FOREIGN KEY Constraint
• CHECK Constraint
• DEFAULT Constraint
• EXCLUSION Constraint
Assigning Constraints

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.

You might also like