SQL zero-based introductory learning (thirteen)

Previous article(SQL zero-based introductory learning (12))

SQL Views (Views)

Views are tables of visualizations.

SQL CREATE VIEW statement

In SQL, a view is a table based on the visualization of the result set of an SQL statement.

Views contain rows and columns, just like a real table. Fields in a view are fields from actual tables in one or more databases.

You can add SQL functions, WHERE, and JOIN statements to views, and you can present data as if it came from a single table.

SQL CREATE VIEW syntax

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

Note: Views always show the latest data! Whenever a user queries a view, the database engine reconstructs the data by using the view's SQL statements.

SQL CREATE VIEW instance

The sample database Northwind has some views installed by default.

The view "Current Product List" lists all products in use (not discontinued) from the "Products" table. This view is created using the following SQL:

CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No

We can query the above view like this:

SELECT * FROM [Current Product List]

Another view of the Northwind sample database selects all products in the Products table that have a unit price higher than the average unit price:

CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

We can query the above view like this:

SELECT * FROM [Products Above Average Price]

Another view of the Northwind sample database calculates the total sales for each category in 1997. Note that this view picks up data from another view called "Product Sales for 1997":

CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName

We can query the above view like this:

SELECT * FROM [Category Sales For 1997]

We can also add conditions to the query. Now, we just need to look at the total sales for the "Beverages" class:

SELECT * FROM [Category Sales For 1997]
WHERE CategoryName='Beverages'

SQL update view

You can update views using the following syntax:

SQL CREATE OR REPLACE VIEW 语法
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

Now we want to add a "Category" column to the "Current Product List" view. We will update the view with the following SQL:

CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName,Category
FROM Products
WHERE Discontinued=No

SQL Server

ALTER VIEW [ schema_name . ] view_name [ ( column [ ,...n ] ) ] 
[ WITH <view_attribute> [ ,...n ] ] 
AS select_statement 
[ WITH CHECK OPTION ] [ ; ]

<view_attribute> ::= 
{ 
    [ ENCRYPTION ]
    [ SCHEMABINDING ]
    [ VIEW_METADATA ]     
} 

schema_name: The name of the schema to which the view belongs.
view_name: The view to change.
column: The names of one or more columns (separated by commas) that will be part of the specified view.

SQL undo view

You can delete a view with the DROP VIEW command.

SQL DROP VIEW syntax
DROP VIEW view_name

SQL Date function

SQL date (Dates)

Note When we are dealing with dates, perhaps the hardest task is to ensure that the format of the inserted date matches the format of the date column in the database.

As long as all your data contains is the date part, running the query should be fine. However, when the time part is involved, the situation is a bit more complicated.

Before discussing the complexities of date queries, let's take a look at the most important built-in date handling functions.

MySQL Date function

The following table lists the most important built-in date functions in MySQL:
insert image description here

SQL Server Date function

The following table lists the most important built-in date functions in SQL Server:
insert image description here

SQL Date data type

MySQL uses the following data types to store dates or date/time values ​​in the database:
DATE - Format: YYYY-MM-DD
DATETIME - Format: YYYY-MM-DD HH:MM:SS
TIMESTAMP - Format: YYYY-MM-DD HH: MM:SS
YEAR - Format: YYYY or YY

SQL Server uses the following data types to store date or date/time values ​​in the database:
DATE - Format: YYYY-MM-DD
DATETIME - Format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - Format: YYYY-MM-DD HH :MM:SS
TIMESTAMP - format: unique number

Note: When you create a new table in the database, you need to choose the data type for the column!
For a list of all available data types, please visit our complete data type reference.

SQL date handling

Note If no time part is involved then we can easily compare two dates!
Suppose we have the following "Orders" table:
insert image description here
Now, we want to select the records whose OrderDate is "2008-11-11" from the above table.

We use the following SELECT statement:

SELECT * FROM Orders WHERE OrderDate='2008-11-11'

The result set looks like this:
insert image description here
Now, suppose the "Orders" table looks like this (note the time part in the "OrderDate" column):
insert image description here
If we use the same SELECT statement as above:

SELECT * FROM Orders WHERE OrderDate='2008-11-11'


SELECT * FROM Orders WHERE OrderDate='2008-11-11 00:00:00'

Then we will get no result! Because there is no "2008-11-11 00:00:00" date in the table. If there is no time part, the default time is 00:00:00.

Tip: If you want to keep your queries simple and more maintainable, don't use the time part in your dates!

Guess you like

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