Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaDB SQL command reduntant?

Correct me if I am wrong, but this SQL command:

create table MYTABLE (ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1))

does not need the NOT NULL part, as a primary key is suppose, by default, to be not null.

Isn't this reduntant?

(I'm not secure to just test it and agree in the result, programming is full of surprises in long-term)

I'm using JavaDB/Derby.

like image 643
The Student Avatar asked Apr 06 '26 10:04

The Student


1 Answers

Yes, a primary key is a combination of a unique index and a non-null constraint. The latter is defined by the SQL99 standard feature E141-08.

It seems that in an older version of Derby it was not possible to create primary keys unless the columns were also declared nullable:

> PRIMARY KEY does not imply NOT NULL. Derby issues error message:

ij> create table tab (i integer primary key);
ERROR 42831: 'I' cannot be a column of a primary key or unique key because it
can contain null values.

This was a bug and it has been fixed.

like image 184
Mark Byers Avatar answered Apr 09 '26 00:04

Mark Byers