0% found this document useful (0 votes)
3 views37 pages

PostgreSQL Data Types & Constraints Guide

This document provides an overview of data types, constraints, and DML statements in PostgreSQL for a DBMS course. It outlines various data types available in PostgreSQL, such as numeric, character, and JSON types, and explains the importance of constraints like NOT NULL, UNIQUE, and FOREIGN KEY. Additionally, it covers DML commands including INSERT and UPDATE for manipulating data in the database.

Uploaded by

callmev2507
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views37 pages

PostgreSQL Data Types & Constraints Guide

This document provides an overview of data types, constraints, and DML statements in PostgreSQL for a DBMS course. It outlines various data types available in PostgreSQL, such as numeric, character, and JSON types, and explains the importance of constraints like NOT NULL, UNIQUE, and FOREIGN KEY. Additionally, it covers DML commands including INSERT and UPDATE for manipulating data in the database.

Uploaded by

callmev2507
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Department of CSE

COURSE NAME: DBMS


COURSE CODE:24AD2103
Topic:
Data Types, Constraints & DML Statements

Session - 8

CREATED BY K. VICTOR BABU


AIM OF THE SESSION
To familiarize students with the basic concept of Data Types in PostgresQL
To aware the students how to define the constraints in the queries
To grasp the students the concept of DML statements

INSTRUCTIONAL OBJECTIVES

This Session is designed to get the enough knowledge to write the DML queries

LEARNING OUTCOMES

At the end of this session, you should be able to write the PostgresQL queries using constraints

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL

Datatypes in SQL are used to define the type of data that can be stored in a column of a table, like, INT, CHAR,
MONEY, DATETIME etc.

Defining a Datatype: Datatypes are defined during the creation of a table in PostgresQL. While creating a
table, it is required to specify its respective datatype and size along with the name of the column.
The syntax to define a datatype on a column of an SQL table is:

CREATE TABLE table_name (column1 datatype, column2 datatype…)

Ex: CREATE TABLE Customers (Name VARCHAR (25), Age INT);

The above query will create a table Customer. The query is defined with two datatypes: VARCHAR and IN

Note: PostgreSQL offers the PL/pgSQL procedural programming language. Additional


functionalities to standard SQL in PostgreSQL include advanced types and user-
defined types, extensions and custom modules, JSON support, and additional options
for triggers and other functionality.
CREATED BY K. VICTOR BABU
BASIC DATA TYPES IN POSTGRESQL
1) Numeric datatype
2) Character datatype
3) Date/time datatype
4) Monetary data type
5) Binary data type
6) Boolean data type
7) Enumerated data type
8) Geometric data type
9) Text search data type
10) UUID data type
11) Network address type
12) JSON data type
13) Bit string type
14) XML data type
15) Range data type
16) Arrays
17) Composite data type
18) Object identifiers type
19) Pseudo data type
CREATED BY K. VICTOR BABU 20) pg-Isn data type
BASIC DATA TYPES IN POSTGRESQL
1. Numeric Datatype: Numeric datatype is used to specify the numeric data into the table. It contains the
following:

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL
2. Character Datatype: The character data types are used to represent the character type values. The below
table contains all Character data types that are supported in PostgreSQL:

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL
3. Date/Time Datatype: The character data types are used to represent the character type values. The below
table contains all Character data types that are supported in PostgreSQL:

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL
4. Monetary Data Type:

5. Binary Data Types:

6. Boolean Data Type:

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL
7. Enumerated Data Type: It is similar to
enum types which are compatible with a
various programming language. The
Enumerated data type is represented in a
table with foreign keys to ensure data
integrity.

8. Geometric Data Types:


Geometric data types represent
two-dimensional spatial objects.
The most fundamental type, the
point, forms the basis for all of
the other types.

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL
9. Text Search Data Type: In PostgreSQL,
the full-text search data type is used to
search over a collection of natural
language documents. We have two
categories of data types that are
compatible with full-text search.

10. UUID Data Types: The UUID stands for Universally Unique identifiers is a
128-bit quantity which is created by an algorithm. It is the best-suited data
type for the primary keys. The UUID is written as a group of lower-case
hexadecimal digits through multiple sets separated by hyphens.

11. Network Address Data Type: The UUID stands for


Universally Unique identifiers is a 128-bit quantity which
is created by an algorithm. It is the best-suited data type
for the primary keys. The UUID is written as a group of
lower-case hexadecimal digits through multiple sets
separated by hyphens. The below table contains
all network address data types that are supported in
PostgreSQL:
CREATED BY K. VICTOR BABU
BASIC DATA TYPES IN POSTGRESQL

12. JSON Data Type: PostgreSQL provides two kinds of data types for storing the JSON (JavaScript Object
Notation) data.
a) Json is an extension of a text data type with JSON validation. In this, we can insert the data quickly, but the
data retrieval is comparatively slow. It saves inputted data just the way it contains the whitespace. It is also
reprocessing on the data retrieval.
b) Jsonb is a binary representation of the JSON data. It is also compatible with indexing and also improves the
whitespace to make the retrieval quicker. In this, the insertion is slow, but the data retrieval is more rapid, and
reprocessing is required on the data retrieval.

13. Bit string Data Type: The bit strings data type contains two categories of strings that are 1's and 0's. The
bitmasks can be stored with the help of these strings. In this, we have two kinds of SQL bit, such as:
a) bit varying(n) and b) bit (n)
Here, n is a positive integer.
CREATED BY K. VICTOR BABU
BASIC DATA TYPES IN POSTGRESQL
14. XML Data Type: In PostgreSQL, the XML data type is used to store the XML data. The function of the XML
data type is to check whether that the input XML is well-formed, and also there are support functions to
perform type-safe operations on it.

15. Range Data Types: These data types are used


to display a range of values of some element
types, known as the range's subtype. It also
signifies several elements of values in a single
range value. In this, we can also create our range
types. In PostgreSQL, we have following built-in
range types:
CREATED BY K. VICTOR BABU
BASIC DATA TYPES IN POSTGRESQL

16. Array Type: The PostgreSQL provides a column of tables as a variable-length and the multidimensional array.
We can create any user-defined base type, built-in, composite, and enumerated type arrays.
 Here, we can perform various operations on arrays such as declare, insert, accessing, modifying, and
searching.

17. Composite Type: In PostgreSQL, the composite data type is used to signify the structure of a row or record
as a list of file names and data types.

18. Pseudo Type: In PostgreSQL, the data types are pseudo types, which are used to contain many special-
purpose entries. And it is used to declare a result type or the function's argument, but it is not compatible to
use as a column data type. The below table contains some of the commonly used pseudo data types in
PostgreSQL:

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL

CREATED BY K. VICTOR BABU


BASIC DATA TYPES IN POSTGRESQL
19. Object Identifier (OIDs) Type: These types of data
types are used as primary keys for several system tables.
The oid type represents an object identifier and
currently implemented as an unsigned four-byte integer.
In huge databases or even in large individual tables, it is
not big enough to offer database-wide individuality.
 Object identifiers are used for references to system
tables. Beyond comparison, the oid type itself has
few operations that can be cast to integer, and can
be manipulated using the standard integer
operators.
 The below table contains all the object identifier
data types that are supported in PostgreSQL:
CREATED BY K. VICTOR BABU
BASIC DATA TYPES IN POSTGRESQL

20. pg_lsn Type: The pg_lsn data type can be used to store Log Sequence Number (LSN) data, a pointer to a
location in the XLOG. It is used to signify the XLogRecPtr and an internal system type of PostgreSQL. The pg_lsn
type is compatible with the standard comparison operators, such as > and =.

Important Points to Remember:


While using the data types, we can refer to the following points:
 If we have an IEEE 754 data source, we can use the float data type
 For integer data type, we can use int.
 Never use char.
 If we want to limit the input, we can apply a text data type.
 When we have vast numbers, we can use bigint only.

CREATED BY K. VICTOR BABU


CONSTRAINTS
 The constraints are used to describe the rules for the data columns in a table. If there is any destruction
between the constraints and the data action, the action is terminated immediately. The constraints make
sure the dependability and the correctness of the data in the database.
 The constraints can be further divided as column level or table level where the table level constraints are
used for the whole table, and the Column level constraints are used only for one column.

Where we use the constraints?


Once constraint is created, it can be added to a table, and we can also disable it temporarily. The constraints
are most commonly used in below areas:
1. To individual columns, we can use the column constraints.
2. When we are creating a table with the help of creating table command, we can also declare the
constraints
3. SQL can discard any value which interrupts the well-defined criteria.
4. All the information related to the constraints is kept in the data dictionary.
5. For one or more columns, we can use the table constraints.
6. All the constraint is allocated a name.
CREATED BY K. VICTOR BABU
CONSTRAINTS
Most commonly used constraints in PostgresQL

CREATED BY K. VICTOR BABU


CONSTRAINTS

1) NOT NULL Constraint: In not-null constraint, a column can hold the Null values by default. If we don't want a
column to have a NULL value, then we need to explain such constraint on this column state that NULL is now not
acceptable for that particular column. It is always created as a column constraint, and it represents unknown
data but it doesn't mean that the data would be null.
Example: To create a new table called Customer, which has the five columns, such as Cust_Id, Cust_ Name,
Cust_Address, Cust_Age, and Cust_Salary.

Output

CREATED BY K. VICTOR BABU


CONSTRAINTS

2) CHECK Constraint: In PostgreSQL, the Check constraint can be defined by a separate name. It is used to
control the value of columns being inserted. It allows us to verify a condition that the value being stored into a
record. If the statement is false, then the data disrupts the constraint and is not saved into the table.
 To create a new table called Customer2, and this table contains five columns.

Output

CREATED BY K. VICTOR BABU


CONSTRAINTS
3) UNIQUE Constraint: The unique constraint is used to maintain the individuality of the values that we store
into a field or a column of the table. It is compatible with a group of column constraints or column constraints
and a table constraint.
When we are using the unique constraint, an index on one or more columns generate automatically. If we add
two different null values into a column in different rows, but it does not interrupt the specification of the
UNIQUE constraint.
 To create a new table called Customer3, which has similar five columns as we created in the above tables.
Output

CREATED BY K. VICTOR BABU


CONSTRAINTS
4) PRIMARY KEY Constraint: It is a field in a table that individually identifies each row or the record in a database
table, and it contains a unique value. A primary key does not hold any null value. And for this, we can also say
that the primary key is a collection of the unique and not-null constraint of a table.
The working of the primary key is similar to a unique constraint. Still, the significant difference between them is
one table can have only one primary key; however, the table can have one or more unique and not-null
constraints.
 To create a new table called Employee, which contains the four columns, such as Emp_Id, Emp_ Name,
Emp_Address, and Emp_Age. Output

CREATED BY K. VICTOR BABU


CONSTRAINTS
5) FOREIGN KEY Constraint: It is a group of columns with values that depend on the primary key benefits from
another table. It is used to have the value in a column or group of columns that must be displayed in the same
column or combination of columns in another table.
In PostgreSQL, the foreign key's values as parallel to actual values of the primary key in the other table; that's
why it is also known as Referential integrity Constraint.
 To create a new table called Employee1, which contains the four columns that are similar to the previous
table.
Output

CREATED BY K. VICTOR BABU


CONSTRAINTS
6) EXCLUSION Constraint: It is used to make sure that any two rows are linked on the specified columns or
statements using the defined operators. In any case, one of these operator evaluations will return null or false.
 To create a new table called Employee, which contains the five columns. And here, we will use an exclude
constraint as well. ERROR

'gist' is the index, and used for creating and


implementation of 'exclusion'
'btree_gist' is a command, for one time in a database.
And after that, it will connect the btree_gist extension
that defines the constraints on basic scalar data types.
If we are using the 'exclusion' constraints, we have to run
the create extension
CREATED BY K. VICTOR BABU
DROPPING CONSTRAINTS
6) DROPPING Constraints: If we want to delete a constraint, then we should remember the name of the
constraints as it is easier for us to drop the constraints directly by its name. Otherwise, we will require to identify
the system-generated name. The following command can be used to find out the names.

The syntax for dropping constraints is as follows:

CREATED BY K. VICTOR BABU


DML STATEMENTS

DML stands for 'Data Manipulation Language' and is typically used to add, retrieve or update data. The
commands used for DML in PostgreSQL are INSERT, UPDATE and DELETE.

1) INSERT: The INSERT command is used to insert new rows into a table. We can insert a single row or
multiple row values at a time into the particular table.
Syntax:
INSERT INTO TABLE_NAME (column1, column2, …… columnN) VALUES (value1, value2, ….. valueN);
For example:
INSERT INTO cseblog (Author, Subject) VALUES (“kbr", "DBMS");

CREATED BY K. VICTOR BABU


DML STATEMENTS

2) UPDATE: The UPDATE command is used to change the present records in a table. To update the selected
rows, we have to use the WHERE clause; otherwise, all rows would be updated.
Syntax:
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE condition;
For example:
UPDATE students SET User_Name = ‘kbr‘ WHERE Student_Id = '3'
3) DELETE: The DELETE command is used to delete all existing records from a table and the WHERE clause
is used to remove the selected records or else, all the data would be eliminated.
Syntax:
DELETE FROM table_name WHERE [condition];
For example:
DELETE FROM cseblog WHERE Author=“kbr";
CREATED BY K. VICTOR BABU
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

1) Create a table an employee and execute DML operations such as insert, delete and
update

2) Create a table student and apply the constraints at column level and table level. Use
constraints such as not null, unique, primary key, foreign key, check, default and create
index.

CREATED BY K. VICTOR BABU


EXAMPLES
1. NOT NULL Constraint: The NOT NULL constraint in a column means that the column cannot store NULL values. For
example,

CREATE TABLE Colleges (


college_id INT NOT NULL,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50)
);

Here, the college_id and the college_code columns of the Colleges table won't allow NULL values.

2. UNIQUE Constraint: The UNIQUE constraint in a column means that the column must have unique value. For example,

CREATE TABLE Colleges (


college_id INT NOT NULL UNIQUE,
college_code VARCHAR(20) UNIQUE,
college_name VARCHAR(50)
);

Here, the value of the college_code column must be unique. Similarly, the value of college_id must be unique as
well as it cannot store NULL values.
CREATED BY K. VICTOR BABU
SUMMARY

 A data type, in programming, is a classification that specifies which type of value a


variable has and what type of mathematical, relational or logical operations can be
applied to it without causing an error.

 SQL constraints are used to specify rules for the data in a table. Constraints are used to
limit the type of data that can go into a table. This ensures the accuracy and reliability of
the data in the table. If there is any violation between the constraint and the data action,
the action is aborted.

 DML statements allow you to query, edit, add, and remove data stored in database
objects. The primary DML commands are SELECT , INSERT , DELETE , and UPDATE . Using
DML statements, you can perform powerful actions on the actual data stored in your
system.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. In the database table, data types describe the kind of ___ that it can contain.

(a) Table
(b) Data
(c) Number
(d) None of the above

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. In the database table, data types describe the kind of ___ that it can contain.

(a) Table
(b) Data Answer: B) Data
(c) Number
(d) None of the above

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. In the database table, data types describe the kind of ___ that it can contain.

(a) Table
(b) Data Answer: B) Data
(c) Number
(d) None of the above

2. How many categories of PostgreSQL data types?

(a) 18
(b) 17
(c) 20
(d) 25

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. In the database table, data types describe the kind of ___ that it can contain.

(a) Table
(b) Data Answer: B) Data
(c) Number
(d) None of the above

2. How many categories of PostgreSQL data types?

(a) 18
(b) 17 Answer: C) 20
(c) 20
(d) 25

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

1. Describe basic datatypes in PostgresQL.

2. List out DML commands in PostgreQL.

3. Analyze constraints with suitable examples.

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

Reference Books:
1. Database System Concepts, Sixth Edition, Abraham Silberschatz, Yale University Henry, F. Korth Lehigh
University, S. Sudarshan Indian Institute of Technology, Bombay.
2. Fundamentals of Database Systems, 7th Edition, RamezElmasri, University of Texas at Arlington, Shamkant
B. Navathe, University of Texasat Arlington.
3. An Introduction to Database Systems by Bipin C. Desai

Sites and Web links:


1. [Link]
2. [Link]
3. [Link] /

CREATED BY K. VICTOR BABU


THANK YOU

Team – Database Management System

CREATED BY K. VICTOR BABU

You might also like