1. How does SQL99 use connections, and what is the difference from SQL92?
Cross connection : It is actually a Cartesian product, we use CROSS JOIN.
Get the Cartesian product of the two tables player and team:
SELECT * FROM player CROSS JOIN team
Multi-table cross join:
SELECT * FROM table1 CROSS JOIN table2 CROSS JOIN table3
Natural join : Automatically query all the same fields in the two joined tables, and then perform an equal value join.
SELECT colName1, colName2 FROM player NATURAL JOIN team
ON connection : used to specify the connection conditions we want.
SELECT column list FROM Table 1 JOIN Table 2 ON connection conditions
USING connection : You can specify a field with the same name in the data table to perform an equivalent connection.
SELECT column list FROM table 1 JOIN table 2 USING (common column)
External connection : Generally, OUTER is omitted and not written.
Left outer join: LEFT JOIN or LEFT OUTER JOIN
SELECT * FROM player LEFT JOIN team ON player.team_id = team.team_id
Right outer join: RIGHT JOIN or RIGHT OUTER JOIN
SELECT * FROM player RIGHT JOIN team ON player.team_id = team.team_id
Full outer join: FULL JOIN or FULL OUTER JOIN
SELECT * FROM player FULL JOIN team ON player.team_id = team.team_id
ps: MySQL does not support full outer join. The result of a full outer join = matched data in the left and right tables + unmatched data in the left table + unmatched data in the right table.
Self-join : Treat a table as two different instances to perform more complex data analysis or query.
SELECT column list FROM table alias 1, table alias 2 WHERE connection condition
name | effect |
---|---|
inner join | Query the data rows that meet the connection conditions between multiple tables. Including: equijoin, non-equivalent join, self-join. |
outer join | Returns all records from one table, and matching rows from another table. Including: left outer join, right outer join, full join. |
cross connect | Returns the combination of each row in the left table with each row in the right table. |
2. What is the role of attempts in SQL and how does it work?
View: a virtual table that does not have data itself. On the one hand, views can help us use part of the table instead of all tables. On the other hand, views can also formulate different query views for different users.
As a virtual table, a view encapsulates the interface between the underlying layer and the data table, and is equivalent to the data result set of one or more tables.
Create a view:
CREATE VIEW view_name AS
SELECT colName1, colName2
FROM table
WHERE condition
嵌套视图:
CREATE VIEW player_above_above_avg_height AS
SELECT player_id, height
FROM player
WHERE height > (SELECT AVG(height) FROM player_above_avg_height)
Modify the view:
ALTER VIEW view_name AS
SELECT colName1, colName2
FROM table
WHERE condition
Delete a view:
DROP VIEW view_name
Use views to simplify SQL operations:
- Complete complex connections
- Format the data
- Using views and calculated fields
3. What is a stored procedure and how often is it used in actual projects?
Views are virtual tables and usually do not directly operate on the underlying data table, while stored procedures are programmed SQL and can directly operate on the underlying data table. A stored procedure can be said to be a collection of statements composed of SQL statements and flow control statements.
Define a stored procedure:
CREATE PROCEDURE 存储过程名称([参数列表])
BEGIN
需要执行的语句
END
Delete stored procedure: DRPO PROCEDURE stored procedure name
Update stored procedure: ALTER PROCEDURE stored procedure name
Parameter Type | Whether to return | effect |
---|---|---|
IN | no | Pass parameters into the stored procedure, and the value of the parameter is modified during the stored procedure and cannot be returned. |
OUT | yes | Put the result calculated by the stored procedure into this parameter, and the caller can get the return value. |
INOUT | yes | The combination of IN and OUT is not only used for the incoming parameters of the stored procedure, but also can put the calculation results into the parameters, and the caller can get the return value. |