Introduction
In the previous article (Getting Started with SQL queries (last article), I have a detailed explanation of the basic concepts of database queries, as well as a single table query, in this article, the main description of the various connections in SQL and the scope of use, as well as the further interpretation of relational algebra and relational algorithms for the same query in different ways.
Introduction to multiple table joins
In a relational database, a query often involves multiple tables, because very few databases have only one table, and if most queries involve only one table, then that table tends to be lower than the third paradigm, with a large number of redundancies and exceptions.
Therefore, a join is an important means of connecting multiple tables into a single table.
For example, simple two tables connect student tables (Student) and Class (Class) tables, as shown in figure:
After the connection, as shown in figure:
Cartesian product
Cartesian product implementations in SQL are both cross joins (Cross join). All connection modes will be sir. Cartesian product table, Cartesian product is a concept in relational algebra, which represents any combination of data in each of the two tables, and the two tables in the graph are joined as Cartesian product (cross join).
In practical applications, the Cartesian product itself is mostly not practical use, only in two tables connected with the constraints, it will have practical significance, the following see the inner join
INNER JOIN
If you understand it step-by-step, the inner join can be considered as having crossed the two tables first, and then by adding a constraint (SQL in the keyword on) to remove the subset of rows that do not meet the criteria, the result is the inner join. If I add a constraint to the image above,
For the two tables in the opening statement, suppose the query is as follows:
Copy Code code as follows:
SELECT *
From [Class] C
INNER JOIN
[Student] S
On C.classid=s.studentclassid
The above query statement can be interpreted as a partial understanding, first of all, the class table and the student table to cross-connect, generate the following table:
Then, by using the constraints on the back, select only those columns that are equal to the Studentclassid and ClassID (the green part of the image above), and finally, a subset of the selected table
Of course, the constraints on the back of the inner join are not just equals, you can also use comparison operators, including > (greater than), >= (greater than or equal), <= (less than or equal), < (less than),!> (not greater than),!< (not less than), and <> (Not equal to). Of course, the data types of the two columns involved in the restriction condition must match.
For the above query statement, if on the after limit condition is changed from equal to greater than:
Copy Code code as follows:
SELECT *
From [Class] C
INNER JOIN
[Student] S
On C.classid>s.studentclassid
The results are then filtered from the Cartesian product of the first step to a subset of those classid greater than Studentclassid:
Although the table attached above does not have any practical meaning, it is only used as a demo to use:-)
Relational calculus
The concept of the Cartesian product above is a concept in relational algebra, and I mentioned in the previous article that there is a query method for relational calculus. The above relational algebra is distributed understanding, and the above statement derivation process is as follows: "to the table student and class, Match all classid and studentclassid equal rows, select all Columns "
and the relational algorithm, more attention is what I want, such as the same query, using relational algorithm to think of the way is "to find all the students information, including their class information, class ID, student ID, student name"
The SQL query statement using the relational calculus method is as follows:
Copy Code code as follows:
SELECT *
From [Class] C
,
[Student] S
where C.classid=s.studentclassid
Of course, the results returned after the query will not change:
Outer Joins
Let's say the top two tables, students and classes. I added a student named Eric to the student, but for some reason I forgot to fill in the class ID:
When I want to execute such a query: Give me the names of all the students and the classes they belong to: