Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate n rows of NULL in PostgreSQL

Tags:

postgresql

I have a table that looks like this:

id, integer, Primary Key, not null
name, character varying
created, timestamp without timezone, not null, default: now()

I want to generate n rows with NULL a name field.

I know that I can do:

INSERT INTO
  employee (name)
VALUES
  (NULL),
  (NULL)...

But I'd prefer to do something like this:

INSERT INTO
  employee (name)
SELECT
  NULL
FROM
  dummy_table_with_n_rows

And I would be able to choose the n.

like image 381
Dan P. Avatar asked May 15 '13 20:05

Dan P.


People also ask

How do I get null records in PostgreSQL?

Example - With INSERT StatementINSERT INTO contacts (first_name, last_name) SELECT first_name, last_name FROM employees WHERE employee_number IS NULL; This PostgreSQL IS NULL example will insert records into the contacts table where the employee_number contains a NULL value.

What is generate series in PostgreSQL?

Generate a series of numbers in postgres by using the generate_series function. The function requires either 2 or 3 inputs. The first input, [start], is the starting point for generating your series. [ stop] is the value that the series will stop at. The series will stop once the values pass the [stop] value.

Do null columns take up space Postgres?

Answer: No, Each null value only uses one bit on disk. If your database table has 30 such default null fields, it would still only be 30 bits per row.

What is Ifnull in PostgreSQL?

PostgreSQL – NULLIF() Function The NULLIF function returns a null value if argument_1 equals to argument_2, otherwise it returns argument_1.


1 Answers

INSERT INTO
  employee (name)
SELECT
  NULL 
FROM
  generate_series(1,10000) i;
like image 178
a_horse_with_no_name Avatar answered Oct 05 '22 19:10

a_horse_with_no_name