问题
I want to insert null
in a column in a table.
Which one represents null? undef
or empty string ''
.
Which one should be used and Why? I know about defined
and that I can check it.
But I am looking more from perspective of database.
Which one represents null
more properly?
Update: I am using DBI
module.
回答1:
DBI uses undef
to represent a SQL NULL
. The empty string represents an empty string.
However, some databases don't make a distinction between NULL
and the empty string. Oracle is a particular offender here. DBI can't do anything about that.
回答2:
Assuming Perl DBI, undef
represents a SQL NULL
.
An empty string in Perl represents exactly the same (i.e. an empty string) in SQL.
回答3:
Assuming you are using the DBI module, and are using bound arguments (since if you were constructing the SQL by hand, you would use NULL
in the string):
Undefined values, or
undef
, are used to indicate NULL values.
See the perldoc for DBI
If you aren't using DBI directly (e.g. you are using DBIx::Class or some other ORM) then you will probably find that it inherits the behaviour of DBI for this.
来源:https://stackoverflow.com/questions/12708633/which-one-represents-null-undef-or-empty-string