1

BACKGROUND

I am attempting to create a table that will have a single column and single row with boolean value. It will be either set True or False by some external application.

CODE

BEGIN;

CREATE TABLE my_table (
  my_flag BOOL DEFAULT TRUE
);


END;


SELECT * FROM my_table

ISSUE

When i do SELECT * FROM my_table i expect it to return one row with the value TRUE except it returns the table and row but with no value. I am not sure why that is?

2
  • 1
    You don't insert anything into the table, so obviously the select returns no rows either Commented Oct 29, 2020 at 21:29
  • SELECT COUNT(*) FROM my_table => 0 Commented Oct 29, 2020 at 21:33

2 Answers 2

2

Pretty sure it is not returning a row:

CREATE TABLE my_table (
  my_flag BOOL DEFAULT TRUE
);

select * from my_table ;
 my_flag 
---------
(0 rows)

The DEFAULT only applies when you INSERT a row:

insert into my_table values (default);
INSERT 0 1
test(5432)=> select * from my_table ;
 my_flag 
---------
 t
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

1

The table is empty. At least one row has to be added. Below example of creating table using CTaS:

CREATE TABLE my_table (my_flag) AS SELECT TRUE;

db<>fiddle demo

2 Comments

This is actaully very neat!
Side note: can you any place where i can practice my sql skills ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.