Languages
[Edit]
EN

PostgreSQL - Insert NULL values

0 points
Created by:
Tehya-Blanchard
324

In this article, we would like to show you how to insert NULL values to the table in PostgreSQL.

Quick solution:

INSERT INTO "table_name"
    ("column1", "column2", "column3", "columnN")
VALUES
    (value1, NULL, NULL, valueN);

Practical example

To show you how to insert NULL values to the table, we will use the following users table:

PostgreSQL - example data used to insert NULL values to the table
PostgreSQL - example data used to insert NULL values to the table

Note:

At the end of this article you can find database preparation SQL queries.

Example

In this example, we will insert rows with NULL values to the users table.

Query

INSERT INTO "users"
	( "name", "surname", "department_id", "salary")
VALUES
	(NULL, NULL, NULL, NULL),
	('Simon', NULL, NULL, NULL),
	('Taylor', 'Martin', NULL, NULL),
	('Andrew', 'Thompson', 1, NULL),
	('Taylor', 'Martin', 2, '2000');

Result:

PostgreSQL - insert NULL values to the table - result
PostgreSQL - insert NULL values to the table - result

Database preparation

create_tables.sql file:

CREATE TABLE "users" (
	"id" SERIAL,
	"name" VARCHAR(50),
	"surname" VARCHAR(50),
	"department_id" INT(10),
    "salary" DECIMAL(15,2),
	PRIMARY KEY ("id")
);

insert_data.sql file:

INSERT INTO "users"
	( "name", "surname", "department_id", "salary")
VALUES
	('John', 'Stewart', 1, '6000'),
	('Chris', 'Brown', 2, '6000'),
	('Kate', 'Lewis', 3, '4000');

PostgreSQL - problems

PostgreSQL - Insert NULL values
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join