kasimir
kasimir

Reputation: 1554

PostgreSQL: what is the difference between 0 and (0)::smallint as default?

I'm playing around with column types and default values, and came across two columns, both smallint but with different defaults:

  1. has 0
  2. has (0)::smallint

When I change 2 to the value 1 has, both are the same and nothing seems changed. Of course, I googled to find the answer to my question, and found that :: is used for type casting. But I wonder, if it is a default value, why would you need type casting?

Upvotes: 1

Views: 1050

Answers (1)

Mike Organek
Mike Organek

Reputation: 12494

There is no difference, and the casting is not necessary.

# create table somesmallints (a smallint default 0, b smallint default 0::smallint, c smallint default (0)::smallint);

CREATE TABLE
# \d+ somesmallints;
                                  Table "public.somesmallints"
 Column |   Type   | Collation | Nullable |    Default    | Storage | Stats target | Description 
--------+----------+-----------+----------+---------------+---------+--------------+-------------
 a      | smallint |           |          | 0             | plain   |              | 
 b      | smallint |           |          | (0)::smallint | plain   |              | 
 c      | smallint |           |          | (0)::smallint | plain   |              | 

# select 0 = 0::smallint;
 ?column? 
----------
 t
(1 row)

Upvotes: 4

Related Questions