On 2024-03-24 11:23:22 -0700, David G. Johnston wrote:
> On Sun, Mar 24, 2024 at 11:14 AM Peter J. Holzer <hjp-pg...@hjp.at> wrote:
>     It doesn't. Your statement
> 
>     > CREATE TABLE test1
>     > (
>     > c1 numeric   NULL ,
>     > c2 varchar(36)  NOT NULL ,
>     > CONSTRAINT test1_PK PRIMARY KEY (c1,c2)
>     > ) ;
> 
>     creates the table with both columns being defined as NOT NULL:
> 
> 
> 
> The request is a warning when defining a multi-column table constraint primary
> key if any of the columns comprising said PK are not defined already to be NOT
> NULL.
> 
> Personally, green field at least, I find that to be reasonable.

Frankly, I don't. I see no reason why I should have declare a column
in a PK explicitely as NOT NULL. 

Something like

CREATE TABLE test1
(
    c1 numeric,
    c2 varchar(36),
    CONSTRAINT test1_PK PRIMARY KEY (c1,c2)
) ;

or

create table test2 (
    id serial primary key,
    ...
);

should be totally fine. An explicit NOT NULL here is just noise and
doesn't add value.

I have some sympathy for the idea that an explicit NULL in a column
definition should cause a warning if the resulting column would not in
fact be nullable. But since writing NULL is otherwise exactly equivalent
to writing nothing, even that seems a bit inconsistent and might be
more confusing than helpful. In any case it seems like a very low-value
change to me which should only be done if it's very little effort
(which apparently it isn't).

        hp


-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | h...@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment: signature.asc
Description: PGP signature

Reply via email to