Ceng301 Dbms Session 9
Ceng301 Dbms Session 9
Session-9
Asst. Prof. Mustafa YENIAD
myeniad@gmail.com
Remember - Data Types
• PostgreSQL supports many data types:
• boolean
• Character types such as char, varchar, and text.
• Numeric types such as integer, floating-point, serial number.
• Temporal types such as date, time, timestamp, and interval
DBMS
https://www.postgresql.org/docs/current/datatype.html
Data Type: boolean
• PostgreSQL supports a single boolean data type: boolean that can have three values:
• True
• False
• null
• In PostgreSQL, the “bool” or”boolean” keyword is used to initialize a Boolean data type.
These data types can hold true, false, and null values.
DBMS
• When queried for these boolean data types are converted and returned according to the
following:
• t to true
• f to false
• space to null
Data Type: boolean
True False
true false
't' 'f'
'true' 'false'
'y' 'n'
'yes' 'no'
DBMS
'1' '0'
INSERT INTO stock(availability) VALUES (TRUE),(FALSE), ('t'), ('y'), ('no'), ('0'), ('1'); # insert some sample data into the stock table
SELECT * FROM stock WHERE availability = 'yes'; # check for the availability of products
SELECT * FROM stock WHERE availability = 'no'; # check for the products that are not available
Data Type: numeric types
• PostgreSQL provides two distinct types of numbers:
1. integers
• There are three kinds of integers in PostgreSQL:
• Small integer (SMALLINT)
• Integer (INT) is synoym of (INTEGER)
• Serial (SERIAL) is the same as integer except that PostgreSQL will automatically generate and
populate values into the serial column.
DBMS
Note: The NUMERIC and DECIMAL types are equivalent in PostgreSQL and upto the SQL standard.
It is recommended to not use the NUMERIC type if precision is not required.
Calculation on NUMERIC values is slower than integers, floats, and double precision.
Data Type: numeric types
• Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte floating-point numbers, and selectable-precision decimals:
up to 131072 digits before the decimal point; up to 16383 digits after the
decimal variable user-specified precision, exact
decimal point
up to 131072 digits before the decimal point; up to 16383 digits after the
numeric variable user-specified precision, exact
decimal point
➢ Use VARCHAR(n) if you want to validate the length of the string (n) before inserting into or updating to a column.
➢ VARCHAR (without the length specifier) and TEXT are equivalent.
• An example of using the PostgreSQL character data type:
DROP TABLE IF EXISTS characters;
CREATE TABLE characters (
id SERIAL PRIMARY KEY,
x CHAR (5),
y VARCHAR (50),
z TEXT);
INSERT INTO characters (x, y, z) VALUES('A','This is a test for varchar','This is a very long text for the PostgreSQL text column');
SELECT * FROM characters;
Creating a table & declaring columns
• In database theory, NULL represents unknown or information missing. NULL is not the same as an empty string or the number zero. To
control whether a column can accept NULL, you use the NOT NULL constraint.
• A column can be assigned a DEFAULT value. When a new row is created and no values are specified for some of the columns, those
columns will be filled with their respective default values.
CREATE TABLE table_name(
column_name_1 data_type NOT NULL,
column_name_2 data_type,
column_name_3 data_type DEFAULT 'default_value',
…);
DBMS
• First, specify the name of the table (table_name) that you want to insert data after the INSERT INTO keywords and a list
of comma-separated columns (column1, column2, ....).
DBMS
• Second, supply a list of comma-separated values in a parentheses (value1, value2, ...) after the VALUES keyword.
Important: The columns and values in the column and value lists must be in the same order.
DROP TABLE IF EXISTS distros;
CREATE TABLE distros(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description VARCHAR(255)
);
INSERT INTO distros (name,description) VALUES('Alma', 'Provide a community-supported enterprise OS');
INSERT INTO distros (name,description) VALUES('Gentoo', 'Optimized for the specific type of computers');
Inserting a new row
RETURNING clause:
• The INSERT statement also has an optional RETURNING clause that returns the information of the inserted row.
• If you want to return the entire inserted row, you use an asterisk (*) after the RETURNING keyword:
INSERT INTO distros (name,description)
VALUES
('Ubuntu', 'Officially released in multiple editions: Desktop, Server, and Core for IoT devices and also Robots')
RETURNING *;
DBMS
• If you want to return just some information of the inserted row, you can specify one or more columns after the
RETURNING clause.
• For example, the following statement returns id of the inserted row:
INSERT INTO distros (name,description)
VALUES
('Debian', 'Popular, free and open-source software, developed by the community-supported Debian Project')
RETURNING id;
Inserting multiple rows
• To insert multiple rows into a table using a single INSERT statement, you use the following syntax:
• First, specify the name of the table that you want to update data after the UPDATE keyword.
• Second, specify columns and their new values after SET keyword. The columns that do not appear in the SET clause retain their original values.
• Third, determine which rows to update in the condition of the WHERE clause.
• The WHERE clause is optional but if you omit the WHERE clause, the UPDATE statement will update all rows in the table!
• When the UPDATE statement is executed successfully, it returns the update count.
UPDATE distros
SET name='RedHat'
WHERE id=4;
• First, specify the name of the table from which you want to delete data after the DELETE FROM keywords.
• Second, use a condition in the WHERE clause to specify which rows from the table to delete.
• The WHERE clause is optional but if you omit the WHERE clause, the DELETE statement will delete all rows in the table!
DBMS
• First, specify the name of the table that you want to drop after the DROP TABLE keywords.
• Second, use the IF EXISTS option to remove the table only if it exists.
• If you remove a table that does not exist, PostgreSQL issues an error. To avoid this situation, you can use the IF EXISTS
DBMS
option.