SQL zero-based introductory learning (14)

Part 1: SQL zero-based introductory learning (thirteen)

SQL NULL value

A NULL value represents missing unknown data.
By default, table columns can hold NULL values.
If a column in a table is optional, then we can insert new records or update existing records without adding a value to that column. This means that the field will be stored with a NULL value.
NULL values ​​are treated differently than other values.
NULL is used as a placeholder for unknown or inapplicable values.
Note: NULL and 0 cannot be compared; they are not equivalent.

SQL NULL value handling

Consider the "Persons" table below:
insert image description here
Suppose the "Address" column in the "Persons" table is optional. This means that if a record is inserted without a value in the "Address" column, the "Address" column will be saved with a NULL value.
So how do we test for NULL values?
You cannot test for NULL values ​​using comparison operators such as =, <, or <>.
We have to use IS NULL and IS NOT NULL operators.

SQL IS NULL

How do we select only records with NULL values ​​in the "Address" column?

We have to use the IS NULL operator:

SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NULL

The result set looks like this:
insert image description here
Tip: Always use IS NULL to find NULL values.

SQL IS NOT NULL

How do we select only records that do not have NULL values ​​in the "Address" column?
We have to use the IS NOT NULL operator:

SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NOT NULL

The result set looks like this:
insert image description here

SQL NULL function

SQL ISNULL(), NVL(), IFNULL(), and COALESCE() functions

Look at the "Products" table below:
insert image description here
Suppose "UnitsOnOrder" is optional and can contain NULL values.
We use the following SELECT statement:

SELECT ProductName,UnitPrice*(UnitsInStock+UnitsOnOrder)
FROM Products

In the above example, if any "UnitsOnOrder" value is NULL, then the result is NULL.
Microsoft's ISNULL() function is used to specify how to handle NULL values.
The NVL(), IFNULL(), and COALESCE() functions can also achieve the same result.
Here, we want the NULL value to be 0.
Below, if "UnitsOnOrder" is NULL, it will not affect the calculation, because ISNULL() returns 0 if the value is NULL:
SQL Server / MS Access

SELECT ProductName,UnitPrice*(UnitsInStock+ISNULL(UnitsOnOrder,0))
FROM Products

Oracle
Oracle does not have an ISNULL() function. However, we can use the NVL() function to achieve the same result:

SELECT ProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0))
FROM Products

MySQL
MySQL also has a function similar to ISNULL(). It works a little differently than Microsoft's ISNULL() function, though.
In MySQL, we can use the IFNULL() function as follows:

SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0))
FROM Products

Or we can use the COALESCE() function as follows:

SELECT ProductName,UnitPrice*(UnitsInStock+COALESCE(UnitsOnOrder,0))
FROM Products

SQL common data types

Every column in a database table requires a name and a data type. Each column in a database table is required to have a name and a data type.

When creating an SQL table, the SQL developer must decide what type of data each column in the table will store. A data type is a label that is a guide for SQL to understand what type of data each column expects to store, and it also identifies how SQL interacts with the stored data.

The following table lists common data types in SQL:
insert image description here
insert image description here

SQL Data Types Quick Reference Manual

However, different databases offer different options for data type definitions.

The following table shows common names for some data types on various database platforms:
insert image description here
Note: In different databases, the same data type may have different names. Even with the same name, dimensions and other details may differ! Please always check the documentation!

SQL data types for various databases

Data types and ranges used by Microsoft Access, MySQL, and SQL Server.

Microsoft Access data types

insert image description here

MySQL data types

In MySQL, there are three main types: Text (text), Number (number), and Date/Time (date/time) types.

Text type:
insert image description here
Number type:
insert image description here
Note: The above size does not represent the specific length stored in the database, such as int(4) does not only store 4-length numbers.

In fact, it doesn't matter how much storage space int(size) occupies. int(3), int(4), and int(8) all take up 4 btyes of storage space on the disk. Int(M) is the same data type as int, except that it is displayed to the user a little differently.

For example:

1、int的值为10 (指定zerofill)

int(9)显示结果为000000010
int(3)显示结果为010

It’s just that the displayed lengths are different, and they all take up four bytes of space.

Date type:
insert image description here
Even though DATETIME and TIMESTAMP return the same format, they work very differently. In an INSERT or UPDATE query, TIMESTAMP automatically sets itself to the current date and time. TIMESTAMP also accepts different formats, such as YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD.

SQL Server data types

String Type:
insert image description here
Number Type:
insert image description here
Date Type:
insert image description here
Other Data Types:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44006731/article/details/129238054