Blank variables and INT columns?
I have a column in a table which is has the data type of an integer. What I had in mind was that values added into that column will be either 0 - N, or just blank as in an empty variable (see below), but I'm not sure that this is possible?
if($resource) {
$resource = $id - 2;
} else {
$resource = "";
}
$result = mysql_query("INSERT INTO table (...,resource,...) VALUE开发者_JAVA技巧S (...,'$resource',...)");
If not, could I instead use the data type of VARCHAR, and then say:
if($resource) {
$resource = $id - 2;
} else {
$resource = "INVALID";
}
In that case, is there any conversion functions I'd have to do when extracting values from the column resource
, or would numbers automatically be treated as integers?
If the field should be "white" for any reason, I think you should mark it as NULLABLE and use word NULL (not INVALID)
If a column is a number, never use a varchar, you will loose a lot of things (also, an int is smaller than a varchar)
EDIT 1: Code snippet to allow null values on column:
ALTER TABLE mytable MODIFY mycolumn INT;
If you specify it as:
ALTER TABLE mytable MODIFY mycolumn INT NOT NULL;
It will be not nullable, so it should be nullable by default if you didn't declare it differently
EDIT 2: Important note, the column must not be UNIQUE otherwise the value will be not nullable!
精彩评论