I couldn't find this behavior described in the official MySQL documentation
A few minutes of looking in the MySQL docs found this:
Column values can be given in several ways:
If you are not running in strict SQL mode, any column
not explicitly given a value is set to its default
(explicit or implicit) value. For example, if you specify a
column list that does not name all the columns in the
table, unnamed columns are set to their default values.
Default value assignment is described in Section 11.6,
“Data Type Default Values”. See also Section 1.8.3.3,
“Constraints on Invalid Data”.
If you want an INSERT statement to generate an error unless
you explicitly specify values for all columns that do not
have a default value, you should use strict mode. See
Section 5.1.7, “Server SQL Modes”.
Answer from Mike on serverfault.comI couldn't find this behavior described in the official MySQL documentation
A few minutes of looking in the MySQL docs found this:
Column values can be given in several ways:
If you are not running in strict SQL mode, any column
not explicitly given a value is set to its default
(explicit or implicit) value. For example, if you specify a
column list that does not name all the columns in the
table, unnamed columns are set to their default values.
Default value assignment is described in Section 11.6,
“Data Type Default Values”. See also Section 1.8.3.3,
“Constraints on Invalid Data”.
If you want an INSERT statement to generate an error unless
you explicitly specify values for all columns that do not
have a default value, you should use strict mode. See
Section 5.1.7, “Server SQL Modes”.
Use SQL mode STRICT_ALL_TABLES per http://dev.mysql.com/doc/refman/5.6/en/sql-mode.html#sqlmode_strict_all_tables.
MySQL change column default to not null - Stack Overflow
mysql - Remove default value from NOT NULL column - Database Administrators Stack Exchange
MySQL default value on NULL - Stack Overflow
mysql - Field both NOT NULL and DEFAULT NULL - Database Administrators Stack Exchange
The answer is to use the following (slightly odd) syntax:
ALTER TABLE items ALTER ordering DROP DEFAULT;
When trying to modify a column with ALTER TABLE, there are 4 keywords that can be used, each with different capabilities:
CHANGE [COLUMN]MODIFY [COLUMN]RENAME COLUMNALTER [COLUMN]
CHANGE is a MySQL extension to standard SQL. MODIFY and RENAME COLUMN are MySQL extensions for Oracle compatibility.
ALTER [COLUMN] is standard SQL (I think).
The docs about ALTER TABLE have more details:
Renaming, Redefining, and Reordering Columns
The
CHANGE,MODIFY,RENAME COLUMN, andALTERclauses enable the names and definitions of existing columns to be altered. They have these comparative characteristics:
CHANGE:
- Can rename a column and change its definition, or both.
- Has more capability than
MODIFYorRENAME COLUMN, but at the expense of convenience for some operations.CHANGErequires naming the column twice if not renaming it, and requires respecifying the column definition if only renaming it.- With
FIRSTorAFTER, can reorder columns.
MODIFY:
- Can change a column definition but not its name.
- More convenient than
CHANGEto change a column definition without renaming it.- With
FIRSTorAFTER, can reorder columns.
RENAME COLUMN:
- Can change a column name but not its definition.
- More convenient than
CHANGEto rename a column without changing its definition.
ALTER:
- Used only to change a column default value.
In this case, you have 3 options:
ALTER TABLE items
CHANGE ordering ordering int NOT NULL;
ALTER TABLE items
MODIFY ordering int NOT NULL;
ALTER TABLE items
ALTER ordering DROP DEFAULT ;
try this
use NULL instead of NOT NULL
CREATE TABLE `Foo` (
`id` int NOT NULL ,
`FirstName` varchar(255) NULL ,
`LastName` varchar(255) NULL DEFAULT 'NONE' ,
PRIMARY KEY (`id`)
);
and query like this
use DEFAULT instead of NULL
INSERT INTO Foo ( `FirstName`, `LastName` ) VALUES('FOO', DEFAULT);
When you use NULL it means that you want to set NULL as a value, which can't happen for the NOT NULL column.
You can explicitly insert the default value by using DEFAULT:
INSERT INTO Foo ( `FirstName`, `LastName` ) VALUES('FOO', DEFAULT);
As documented under Data Type Default Values:
If the column can take
NULLas a value, the column is defined with an explicitDEFAULT NULLclause.
(I think they meant implicit, not explicit).
Moreover, as documented under CREATE TABLE Syntax:
If neither
NULLnorNOT NULLis specified, the column is treated as thoughNULLhad been specified.
Therefore, in MySQL the following column definitions are all identical:
columnname type
columnname type NULL
columnname type DEFAULT NULL
columnname type NULL DEFAULT NULL
The choice of which to use is a balance between being explicit, and being concise. Depending on the circumstances, I might use any of the above.
Every database that I've encountered treats NULLs the way that you describe them. When a column accepts NULL values, then not providing a value on INSERT defaults the value to NULL. I believe this is part of the ANSI standard behavior.
As for specifying "NULL" itself. That is a matter of preference. The standard does say that a column allows NULLs unless NOT NULL is specified (at the level of the data definition language). So, the NULL itself is unnecesary, and you have a fourth, equivalent option:
columnname type
NULLs are quite embedded in the SQL language through the ANSI standard. Your third option is the most explicit, but it also looks a bit unusual.
If you are planning on being super-consistent throughout your system, then I would take the path you are on. For every column in every table have NULL or NOT NULL. For all columns that take default values, have the DEFAULT statement.
However, don't expect other people you work with (now or in the future) to readily follow this example. Many would prefer the fourth option in this case simply because it requires less typing and is how all (or almost all) SQL dialects behave.
I have a table that I cannot alter, and I need to add records that don't fill out all the columns. Now, all columns in this table cannot be null. So my issue is I need to put some default data in these columns, but I can't set a default value since I cannot alter the table. For varchar fields, I was just going to put empty strings? But I also have DateTime and TimeStamp, and I don't know what to do with them.
This is for a class where they don't want you to alter the table in any way. They have a bunch of useless columns, and I won't be gathering the data. But I need to fill out all the column values to add a record, and all columns cannot be null.