There is a field that is the primary key. This field must not contain "zero" values.
Is there a need for NOT NULL , or is it enough just PRIMARY KEY ?
There is a field that is the primary key. This field must not contain "zero" values.
Is there a need for NOT NULL , or is it enough just PRIMARY KEY ?
Unique fields have one fun feature. Uniqueness is determined by equality. But NULL not equal to NULL ! Therefore, NULL values can be added to a column with uniqueness checking in any number of rows!
And the primary key must uniquely identify each row by the equality of the value of this key . Therefore, the NULL value for the primary key does not make sense . When comparing equality, it simply never matches any string.
This applies to all SQL-compatible databases.
But I will quote the definition of the primary key from dev.mysql.com :
It’s possible to identify each row in a table. It doesn’t contain any
NULLvalues.
A set of columns and an index on this set of columns that can uniquely identify each row in the table. This is a unique index that does not contain
NULLvalues.
So yes, PRIMARY KEY requires its columns to be NOT NULL .
And depending on the syntax used to define the primary key, the NOT NULL indication may be mandatory or optional. In MySQL (apparently) always always.
The syntax for determining the primary key, I met three types:
Right when determining the type of a column, adding a PRIMARY KEY to its type:
CREATE TABLE things ( id integer PRIMARY KEY ) NOT NULL may be optional. But in MySQL, judging by the examples (the documentation did not really help), it is necessary . In PostgreSQL, there is no: it is assumed that the PRIMARY KEY in the column type includes NOT NULL .When defining a primary key as part of a table definition (but not its: columns)
CREATE TABLE things ( id integer NOT NULL, PRIMARY KEY(id) ) NOT NULL obligatory, otherwise the reference to the column in PRIMARY KEY will conflict with the already declared.When determining the primary key on an already created table separately.
NOT NULL mandatory since the table is already defined.NOT NULL does not automatically enlist the column in the primary key, which would be at least strange. - D-sideSource: https://ru.stackoverflow.com/questions/589954/
All Articles
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY- Alexey ShimanskyВ интернете почитал информацию......В инных источниках сказанно,is better - write these same sources. I personally did not understand what you wrote. Perhaps something taken out of context. ....................... the PK field is already NOT NULL if in any editor to create a table: there when indicating that the PK column is ticked automatically and that she is NOT NULL. There is such - Alexey ShimanskyCREATE TABLE MyTable ( Id int NOT NULL PRIMARY KEY,.......orCREATE TABLE MyTable ( Id int NOT NULL, ........еще столбцы, PRIMARY KEY (Id) )......... that is, the column that you deem will be the primary key - write as not null ............... w3schools. com / sql / sql_primarykey.asp → A primary key column cannot contain NULL values - Alex Shimansky