Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL value in multi-column primary key

I've got a table with several columns making up the primary key. The nature of the data stored allows some of these fields to have NULL values. I have designed my table as such:

CREATE TABLE `test` (     `Field1` SMALLINT(5) UNSIGNED NOT NULL,     `Field2` DECIMAL(5,2) UNSIGNED NULL DEFAULT NULL,     PRIMARY KEY (`Field1`, `Field2`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB; 

However, when I run describe test it shows like this:

|| *Field* || *Type*                || *Null* || *Key* || *Default* || *Extra*  || Field1  || smallint(5) unsigned  || NO     || PRI   ||           ||          || Field2  || decimal(5,2) unsigned || NO     || PRI   || 0.00      ||          

And I keep getting an error when inserting a NULL value.

Column 'Field2' cannot be null

Is this because a field that is part of a primary key cannot be null? What are my alternatives besides using, say, '0' for NULL?

like image 483
simbabque Avatar asked Jun 12 '12 17:06

simbabque


People also ask

Can primary key column have NULL values?

A primary key defines the set of columns that uniquely identifies rows in a table. When you create a primary key constraint, none of the columns included in the primary key can have NULL constraints; that is, they must not permit NULL values.

Can multiple primary key be null?

You cannot have a null field as part of a primary key, but you can create a unique composite index which is not a primary key and this can include a null field. An easy way to do this is to create your composite primary key, and then open the Indexes window and turn off the "Primary" property.

Can null be part of composite primary key?

In composite primary key columns you cannot pass null values. Each column defined as a primary key would be validated so that null values are not passed on to them. If you have given a Unique constraint then we have a chance of NULL values being accepted. But in case of primary keys they cannot hold null values.

Can columns in composite key be null?

However a unique key does not automatically imply not null, and can have nulls in a column as long as the values in the populated columns are unique.


2 Answers

From the MySQL documentation :

PRIMARY KEY

A unique index where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one PRIMARY KEY. The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.

https://dev.mysql.com/doc/refman/8.0/en/create-table.html

If Field2 can be NULL, I question why you need it as part of the Primary Key since you then need Field1 to be distinct across all rows. So Field1 by itself should be sufficient as the Primary Key. You could create a different type of index on Field2.

like image 78
Girish Rao Avatar answered Oct 10 '22 17:10

Girish Rao


Primary keys are used to make the column both unique and not null

In order to insert NULL values make field2 as UNIQUE.

Unique constraint makes the field removes duplicates but allows null values

like image 40
Lordferrous Avatar answered Oct 10 '22 19:10

Lordferrous