This is one example where using prepared statements really saves you some trouble.
In MySQL, in order to insert a null value, you must specify it at INSERT
time or leave the field out which requires additional branching:
INSERT INTO table2 [f1, f2]
VALUES ['String Value', NULL];
However, if you want to insert a value in that field, you must now branch your code to add the single quotes:
INSERT INTO table2 [f1, f2]
VALUES ['String Value', 'String Value'];
Prepared statements automatically do that for you. They know the difference between string[0] ""
and null
and write your query appropriately:
$stmt = $mysqli->prepare["INSERT INTO table2 [f1, f2] VALUES [?, ?]"];
$stmt->bind_param['ss', $field1, $field2];
$field1 = "String Value";
$field2 = null;
$stmt->execute[];
It escapes your fields for you, makes sure that you don't forget to bind a parameter. There is no reason to stay with the mysql
extension. Use mysqli
and it's prepared statements instead. You'll save yourself a world of pain.
Inserting NULL values
The SQL INSERT statement can also be used to insert NULL value for a column.
Example:
Sample table: agents
To add values'A001','Jodi','London','.12','NULL' for a single row into the table 'agents' then, the following SQL statement can be used:
SQL Code:
INSERT INTO agents
VALUES ["A001","Jodi","London",.12,NULL];
SQL insert values in specific columns
The SQL INSERT INTO statement can also be used to insert one or more specific columns for a row. It is required to mention the column[s] name in the SQL query.
Example:
Sample table: agents
To add values'A001','Jodi', and ', 12' against the columns 'agent_code', 'agent_name' and 'commission' into the table 'agents', the following SQL statement can be used:
SQL Code:
INSERT INTO agents
[agent_code,agent_name,commission]
VALUES ["A001","Jodi",.12]
See our Model Database
Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL JOINS [29 Exercises]
- SQL JOINS on HR Database [27 Exercises]
- SQL SUBQUERIES
- SQL SUBQUERIES [39 Exercises]
- SQL SUBQUERIES on HR Database [55 Exercises]
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- JOINS queries on soccer Database [61 Exercises]
- Hospital Database
- Introduction
- BASIC, SUBQUERIES, and JOINS [39 Exercises]
- Employee Database
- BASIC queries on employee Database [115 Exercises]
- SUBQUERIES on employee Database [77 Exercises]
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
Previous: Insert
statement
Next: Inserting the result of a query in another table
SQL: Tips of the Day
Postgres and indexes on foreign keys and primary keys:
PostgreSQL automatically creates indexes on primary keys and unique constraints, but not on the referencing side of foreign key relationships.
Ref: //bit.ly/3UkC7nd
B.3.4.3 Problems with NULL Values
The concept of the NULL
value is a common source of confusion for newcomers to SQL, who often think that NULL
is the same thing as an empty string ''
. This is not the case. For example, the following statements are completely different:
mysql> INSERT INTO my_table [phone] VALUES [NULL];
mysql> INSERT INTO my_table [phone] VALUES [''];
Both statements insert a value into the phone
column, but the first inserts a NULL
value and the second inserts
an empty string. The meaning of the first can be regarded as “phone number is not known” and the meaning of the second can be regarded as “the person is known to have no phone, and thus no phone number.”
To help with NULL
handling, you can use the IS NULL
and
IS
NOT NULL
operators and the IFNULL[]
function.
In SQL, the NULL
value is never true in comparison to any other value, even NULL
. An expression that contains NULL
always produces a NULL
value unless otherwise indicated in the documentation for the
operators and functions involved in the expression. All columns in the following example return NULL
:
mysql> SELECT NULL, 1+NULL, CONCAT['Invisible',NULL];
To search for column values that are NULL
, you cannot use an expr = NULL
test. The following statement returns no rows, because expr =
NULL
is never true for any expression:
mysql> SELECT * FROM my_table WHERE phone = NULL;
To look for NULL
values, you must use the IS NULL
test. The following
statements show how to find the NULL
phone number and the empty phone number:
mysql> SELECT * FROM my_table WHERE phone IS NULL;
mysql> SELECT * FROM my_table WHERE phone = '';
See Section 3.3.4.6, “Working with NULL Values”, for additional information and examples.
You can add an index on a column that can have NULL
values if you are using the MyISAM
, InnoDB
, or MEMORY
storage engine. Otherwise, you must declare an
indexed column NOT NULL
, and you cannot insert NULL
into the column.
When reading data with LOAD
DATA
, empty or missing columns are updated with ''
. To load a NULL
value into a column, use \N
in the data file. The literal word NULL
may also be used under some circumstances. See
Section 13.2.7, “LOAD DATA Statement”.
When using DISTINCT
, GROUP
BY
, or ORDER BY
, all NULL
values are regarded as equal.
When using ORDER BY
, NULL
values are presented first, or last if you specify DESC
to sort in descending order.
Aggregate [group] functions such as
COUNT[]
, MIN[]
, and SUM[]
ignore NULL
values. The exception to this is
COUNT[*]
, which counts rows and not individual column values. For example, the following statement produces two counts. The first is a count of the number of rows in the table, and the second is a count of the number of non-NULL
values in the age
column:
mysql> SELECT COUNT[*], COUNT[age] FROM person;
For some data types, MySQL handles NULL
values in special ways. For example, if you insert NULL
into an
integer or floating-point column that has the AUTO_INCREMENT
attribute, the next number in the sequence is inserted. Under certain conditions, if you insert NULL
into a TIMESTAMP
column, the current date and time is inserted; this behavior depends in part on the server SQL mode [see Section 5.1.11, “Server SQL Modes”] as well as the value of the explicit_defaults_for_timestamp
system variable.