Graphical SQL basics, SQL articles that novices can understand


This article introduces the design philosophy of relational databases: In SQL, everything is a relation.


There are many great design concepts and ideas in the computer age field, such as:
  • In  Unix , everything is a file .

  • In an object-oriented programming language , everything is an object .


Relational databases also have their own design philosophy: in  SQL , everything is a relation .


01

Relational model

The relational model (Relational model) was proposed by Dr. EFCodd in 1970, based on the relational concept in set theory ; both the entity objects in the real world and the links between them are represented by relations . The relationship we see in the database system is a two-dimensional table (Table), which consists of rows (Row) and columns (Column). Therefore, it can also be said that a relational table is a collection of data rows .

eaf717703c24790defd7b5eca416c2e0.jpeg

The relational model consists of three parts : data structure , relational operations , and integrity constraints .


  • The data structure in the relational model is the relational table , including base tables , derived tables (query results) and virtual tables (views).

  • Commonly used relational operations include add , delete , modify , and query (CRUD), using the SQL language. Among them, query operations are the most complex, including Selection, Projection, Union, Intersection, Exception, and Cartesian product.

  • Integrity constraints are used to maintain data integrity or meet the needs of business constraints, including entity integrity (primary key constraints), referential integrity (foreign key constraints), and user-defined integrity (non-null constraints, unique constraints, check constraints and default values).

Our topic today is Relational Operations Language, or SQL.


02

Set-oriented

SQL (Structured Query Language) is the standard language for manipulating relational databases . SQL is very close to English and very simple to use. At the beginning of its design, it takes into account the needs of non-technical personnel. We usually only need to explain the desired result (What) and leave the data processing process (How) to the database management system. So, SQL is the real programming language for people!

Next, let's analyze the various operation statements of the relationship in detail; the purpose is to let everyone understand that SQL is a set-oriented programming language, its operation object is a set, and the result of the operation is also a set .
In a relational database, relations, tables, and collections usually represent the same concept.

03

The following SELECT

is a simple query statement:

SELECT employee_id, first_name, last_name, hire_date
   FROM employees;
Its function is to query employee information from the employees table.
Obviously, we all know that after FROM is a table (relation, collection). Not only that, but the result of the entire query statement is also a table .
So, we can use the above query as a table:












wait. SELECT is called Projection in relational operations . It should be easier to understand by looking at the schematic diagram below.
aebba2160b34100e407fe2c4b11e6ade.jpeg
In addition to SELECT, there are some commonly used  SQL   clauses.
WHERE   is used to specify the conditions for data filtering, which is called Selection in relational operations . The schematic diagram is as follows:
ba88defc733e0cef0b49852631552b01.jpeg
ORDER BY   is used to sort the query results . The schematic diagram is as follows:
cd2f93e1ea673679aa3008302b43afc7.jpeg
In a word, SQL   can complete each A kind of data operation, such as filtering, grouping, sorting, limiting quantity, etc.; the object of all these operations is a relational table , and the result is also a relational table .
ad1e445b73314fd5a2eac2dea4f3d26c.jpeg
Among these relational operations, there is a special one, which is grouping.

04

The GROUP BY

grouping (GROUP BY) operation is different from other relational operations because it changes the structure of the relation. Consider the following example:
SELECT department_id,  count(*), first_name
   FROM employees
  GROUP  BY department_id; The
purpose of this statement is to count the number of employees by department , but there is a syntax error, that is, first_name cannot appear in query list. The reason is that when grouping by department, each department contains multiple employees; it is a logical error to be unable to determine which employee's name needs to be displayed.
So, GROUP BY  changes the structure of the collection elements (data rows) , creating a whole new relationship.
The schematic diagram of the grouping operation is as follows:
c6a6d6bd4b5751051dc6c45003c5775a.jpeg
Still, the result of GROUP BY is a collection.

05

The most obvious manifestation of the collection-oriented feature of UNION

SQL is:
  • UNION (union operation)

  • INTERSECT (intersection operation)

  • EXCEPT/MINUS (subtraction operation)


The role of these set operators is to combine two sets into one set, so the following conditions need to be met:
  • The number and order of fields in the collections on both sides must be the same;

  • The types of corresponding fields in the collections on both sides must match or be compatible.


Specifically, UNION and UNION ALL are used to compute the union of two collections, returning data that appears in either the first query result or the second query result. The difference
between them is that   UNION excludes duplicate data in the result, while UNION ALL retains duplicate data.
The following is a schematic diagram of the UNION operation:
595130c01d359056e97b41db04e03509.jpeg
The INTERSECT  operator is used to return the common part of the two sets , that is, the data that appears in the first query result and the second query result at the same time, and the duplicate data in the result is excluded .
The schematic diagram of INTERSECT operation is as follows:
c6fb36a9e7f02aa4f679b68183875ea3.jpeg
The EXCEPT   or   MINUS   operator is used to return the difference set of two sets , that is, records that appear in the first query result but not in the second query result, and exclude duplicate data in the result .
The schematic diagram of the EXCEPT operator is as follows:
8ac8acea13ba2b5696c6b8ffe42730d0.jpeg
In addition, the DISTINCT   operator is used to eliminate duplicate data , that is, to exclude duplicate elements in the collection.
The relational concept in SQL comes from the set theory in mathematics, so UNION, INTERSECT and EXCEPT come from the union (∪\cup∪), intersection (∩\cap∩) and difference (∖\setminus∖) operations in set theory respectively .
It should be noted that the collection in set theory does not allow duplicate data, but SQL allows it. Therefore, collections in SQL are also called multisets; multisets and collections in set theory are unordered, but SQL can sort query results through the ORDER BY clause.

06

JOIN

In SQL, not only entity objects are stored in relational tables, but also relationships between objects are stored in relational tables. Therefore, when we want to obtain these related data, we need to use another operation: join query (JOIN) .

Common SQL join query types include inner join , outer join , cross join , etc. Among them, the outer join can be divided into left outer join, right outer join and full outer join.
Inner Join (Inner Join) returns the data in the two tables that meet the join conditions . The principle of the inner join is shown in the figure below:
ca48f39a17a245ffa5a64379140a0895.jpeg
Left Outer Join (Left Outer Join) returns all the data in the left table ; for the right table, it returns the data that meets the join conditions data; return null if none.
The principle of the left outer join is shown in the figure below:
fa0f92f09ffb5464d16fd7371c05a27e.jpeg
Right Outer Join (Right Outer Join) returns all the data in the right table ; for the left table, returns the data that meets the join conditions , and returns a null value if not. Right outer join and left outer join can be interchanged , the following two are equivalent:
t1  RIGHT JOIN t2
t2  LEFT JOIN t1Full
Outer Join (Full Outer Join) is equivalent to left outer join plus right outer join , and returns all the data in the left table and right table at the same time; for the data in the two tables that do not meet the join conditions, return null value.
The principle of full outer join is shown in the figure below:
02340e6e90473468c29ffb74bfafd54c.jpeg
cross join is also called Cartesian Product. The cross join of two tables is equivalent to combining all the rows of one table with all the rows of the other table, and the number of the result is the multiplication of the number of rows of the two tables .
The principle of cross-connect is shown in the following figure:
c9a44fe77a188027f498d4b55d8b0928.jpeg
Other types of connections include semi-join (SEMI JOIN) and anti-join (ANTI JOIN).


A set operation merges two collections into a larger or smaller collection; a join query transforms two collections into a larger or smaller collection while obtaining a larger element (more columns). Many times the collection operations can be implemented through connection query , such as:
select & nbsp; department_id
& nbsp; & nbsp; from & nbsp; departments
& nbsp; union
select & nbsp; department_id
& nbsp. ; & nbsp;
from
& nbsp; employees ; e.department_id)
   FROM departments d
   FULL  JOIN employees e  ON (e.department_id = d.department_id); We have
covered many examples of queries, let's look at others data manipulation.

07

DML

DML   stands for Data Manipulation Language, that is, Insert, Update, and Delete . The following is an example insert statement:

CREATE  TABLE  test( id  int);

-- MySQL, SQL Server, etc.
INSERT  INTO  test( id)  VALUES ( 1),( 2),

( 3); -- Oracle
INSERT  INTO  test( id)
( SELECT  1  AS  id  FROM DUAL
UNION ALL
SELECT  2  FROM DUAL
UNION ALL
SELECT   3  FROM DUAL);
we 3 records are inserted through an INSERT statement, or a relational table containing 3 records is inserted. Because, UNION ALL returns a relational table. VALUES also specifies a relational table, and supports the following statements in SQL Server and PostgreSQL:

SELECT *
FROM (
   VALUES( 1),( 2),( 3)
)  test( id);
As we have said before, FROM   is followed by a relational table , so here  VALUES   also the same. Since we often insert a single record, we don't realize that the operation is actually performed in units of tables.
Similarly, UPDATE and DELETE   statements are also operations in units of relational tables ; it's just that we are used to saying that a row of data is updated or several records are deleted.

Guess you like

Origin blog.csdn.net/zhaomengsen/article/details/132124701