First, join queries
1, the connection between two tables in the query have established mutual relations, queries are two or more tables or views.
2, the n-query tables, at least the n-1 connection condition table.
Second, the Cartesian product (likely to cause database downtime)
1, each row in the table refers to the elements of each row are a combination of other tables, there is no connection conditions.
2, assuming there are two tables of data in Table X-A and Table B has Y of data, the Cartesian product of the query, a table will be X * Y data strip.
Third, primary keys, foreign keys
1, the primary keys and foreign keys for maintaining the integrity of a relational database.
2, the primary key: a unique and non-empty, is used to identify a table.
3, foreign keys: means for holding the primary key of another table, the foreign key can be repeated, can be null.
Four, equi-natural connection, the connector, the external connector
Known two tables:
Table student is:
Table student_score as:
1, connected to the equivalent non-equivalent connection:
Non-equivalent connections are generally used to connect the given conditions, written in the WHERE clause, where they meet the conditions, you can connect.
Equijoins refers equal connection where connection condition.
[Format:] SELECT field name 1, field name 2, ..., n-field names the FROM Table 1, Table 2, ..., n-tables the WHERE condition - Examples: equijoins] SELECT * the FROM Student STU, SCO student_score the WHERE stu.id = sco.id - [results as shown below:]
- Examples: Non] equijoins the SELECT * the FROM Student STU, SCO student_score the WHERE stu.id > sco.id - [results as shown below:]
2, natural join:
It is a special equi requires duplicate column between two tables, based on the equivalent connection (for the same column = Comparative) removing duplicate column.
--【格式:】 SELECT 列名1, 列名2 FROM 表1 NATURAL JOIN 表2 --【举例:】 SELECT * FROM student stu NATURAL JOIN student_score --【可以将其理解为:消除了重复的列 ---sco.id】 SELECT stu.id, stu.name, sco.score FROM student stu, student_score sco WHERE stu.id = sco.id
3、内连接:
不能消除重复列,可以通过SECECT挑选字段来决定。基本与等值连接相同,使用ON来指定条件。
--【格式:】 SELECT 列名1, 列名2 FROM 表1 INNER JOIN 表2 ON (条件) --【举例:】 SELECT * FROM student stu INNER JOIN student_score sco ON stu.id = sco.id
注:内连接与等值连接的区别:
(1)等值连接:2个表会先进行笛卡尔乘积运算,生成一个新表格,占据在电脑内存里,当表的数据量很大时,很耗内存,这种方法效率比较低,尽量不用。
(2)内连接:2个表根据共同ID进行逐条匹配,不会出现笛卡尔乘积的现象,效率比较高,优先使用这种方法。
4、外连接
不能消除重复列,可以通过SECECT挑选字段来决定。
分为左外连接,右外连接,全外连接。
(1)左外连接:
以第一个关系为主,在第二个关系中找到满足条件的元素,并把他们连接起来,如果没有对应的元素,则在相应位置上的值为null。
--【格式:】 SELECT 列名1, 列名2 FROM 表1 LEFT OUTER JOIN 表2 ON (条件) --【举例:】 SELECT * FROM student stu LEFT OUTER JOIN student_score sco ON stu.id = sco.id AND stu.name = 'tom'
(2)右外连接
和左外连接类似,以第二个关系为主,在第一个关系中找到满足条件的元素,并把他们连接起来,如果没有对应的元素,则在相应位置上的值为null
--【格式:】 SELECT 列名1, 列名2 FROM 表1 RIGHT OUTER JOIN 表2 ON (条件) --【举例:】 SELECT * FROM student stu RIGHT OUTER JOIN student_score sco ON stu.id = sco.id AND stu.name = 'tom'
(3)全外连接:
全外连接是左外连接和右外连接的组合。
注:mysql中没有全外连接,可以使用UNION关键字 连接 左外连接 与 右外连接 实现。
--【格式:】 SELECT 列名1, 列名2 FROM 表1 FULL OUTER JOIN 表2 ON (条件) --【举例:】 SELECT * FROM student stu FULL OUTER JOIN student_score sco ON stu.id = sco.id AND stu.name = 'tom'
五、子查询
1、子查询指的是当前查询建立在另一个查询的结果上。
2、分类:
(1)单行单列子查询:返回单行单列数据。通常写在WHERE里。
(2)多行单列子查询:返回多行单列数据。通常写在WHERE里。
(3)多行多列子查询:返回多行多列数据。通常写在FROM里,当成一个表来使用。
3、单行单列子查询
【举例:】 SELECT stu.name FROM student stu WHERE stu.id = ( SELECT sco.id FROM student_score sco WHERE sco.score = '98' )
4、多行单列子查询
【举例:】 SELECT stu.name FROM student stu WHERE stu.id IN ( SELECT sco.id FROM student_score sco WHERE sco.score IN ('98', '97', '96') )
5、多行多列子查询
【举例:写在WHERE条件里】 SELECT * FROM student stu WHERE stu.id IN ( SELECT sco.id FROM student_score sco WHERE sco.score IN ('98', '97', '96') )
【举例:写在FROM条件里,当成表来用】 SELECT * FROM student stu, ( SELECT sco.id FROM student_score sco WHERE sco.score IN ('98', '97', '96') ) sco WHERE stu.id = sco.id
六、分页查询
1、 ROWNUM
ROWNUM被称为伪列,实际上是不存在的列,用于返回标识行数据顺序的数字,自1开始,每次确定数据后自动加1。
SELECT ROWNUM,name,id FROM student
2、分页步骤(相比于mysql会略显复杂一些)
(1)先排序。
SELECT * FROM student ORDER BY id desc
(2)再编号。
SELECT ROWNUM rw, stu.* FROM ( SELECT * FROM student ORDER BY id desc ) stu
(3)取范围。
取范围通用套路: 从第start条开始,到第end条结束。 即从第(page -1)*pagesize + 1条开始,到第(page*pagesize)条。 其中page表示第几页,pagesize表示每页的数据。 比如第一页,每页三条记录,那么第一页显示为 1 至 3 条,第二页显示为4 至 6条, 同理…… SELECT * FROM ( SELECT ROWNUM rw, stu.* FROM ( SELECT * FROM student ORDER BY id desc ) stu ) WHERE rw BETWEEN 3 AND 5