NULL value in multi-column primary key NULL value in multi-column primary key mysql mysql

NULL value in multi-column primary key


From the MySQL documentation :

A PRIMARY KEY is 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.

http://dev.mysql.com/doc/refman/5.1/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.


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


Primary key states that column mustn't have NULL values. So columns used for defining composite primary key isn't going to be NULL.

Also Oracle server compares the combination of all columns used in a composite primary key definition. If your all columns existing data (say x,y) matched with newly adding row, it will raise error of Unique Constraint Violated.

Moreover,look at this thread:What's wrong with nullable columns in composite primary keys?.

This link provides valuable information regarding possibility of NULLABLE columns in composite key!