[MDEV-8801] New behavior on creating a multi-part PK with a nullable part does not seem reasonable Created: 2015-09-15 Updated: 2015-09-21 Resolved: 2015-09-21 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Documentation |
| Affects Version/s: | 10.1 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Minor |
| Reporter: | Elena Stepanova | Assignee: | Ian Gilfillan |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Description |
|
Consider the following CREATE statement:
Sonce c2 is a part of a PK, it cannot really be nullable. Earlier versions of MariaDB and MySQL would silently convert it into a not-nullable column with default value '0':
MySQL 5.7 is stricter in this regard, and refuses to execute the CREATE statement:
At least it's understandable and clearly explained. MariaDB 10.1, however, converts the column to not-nullable, but does not set a default value. So, farther attempt to insert into the table without providing explicit values for c2 fails with a very confusing warning (or error, if strict mode is enabled):
I don't know what the intentional behavior was, but it surely could not be this? The change appeared in 10.1 tree with this commit:
|
| Comments |
| Comment by Sergei Golubchik [ 2015-09-15 ] |
|
I believe our behavior is exactly what SQL standard (2003) says it should be. SQL-2003, Part II, “Foundation” says:
Explanation: all PRIMARY KEY columns are automatically converted to NOT NULL.
Explanation: there is no concept of “no default value” in the standard, instead a column always has an implicit default value of NULL. On insertion it might fail the NOT NULL constraint though. MariaDB and MySQL instead mark such a column as “not having a default value”. The end result is the same — a value must be specified explicitly or an INSERT will fail. Thus, I believe, MariaDB behaves in a standard compatible manner — being part of a PRIMARY KEY, the nullable column gets an automatic NOT NULL contraint, on insertion one must specify a value for such a column. MariaDB before 10.1 and MySQL before 5.7 were automatically assigning a default value of 0 — this behavior was non-standard. MySQL 5.7 refuses to convert a nullable column to NOT NULL — this is also against the SQL standard. |
| Comment by Elena Stepanova [ 2015-09-15 ] |
|
Ian, could you please find a proper place in the KB to document it? We will have questions in future, it's better to refer to documentation while answering them. |
| Comment by Ian Gilfillan [ 2015-09-21 ] |
|
This has now been documented at https://mariadb.com/kb/en/mariadb/multi-part-primary-keys-with-nullable-columns/ |