Oracle Study Notes 005 (join query)

Here Insert Picture Description

The following table built for the presentation of the statement to connect

--课程表
CREATE TABLE course(
    id VARCHAR2(32),
    c_name VARCHAR2(32) NOT NULL,
    semester NUMBER DEFAULT 1 CHECK(semester IN (1, 2)),
    category VARCHAR2(32),
    CONSTRAINT pk_course_id PRIMARY KEY(id)
    )
    
COMMIT ON TABLE course.id IS '编号'; 
COMMIT ON TABLE course.c_name IS '课程名称';
COMMIT ON TABLE course.semester IS '课程学期';
COMMIT ON TABLE course.category IS '课程类别';

--学生表
CREATE TABLE student(
    id VARCHAR(32),
    s_name VARCHAR2(32),
    c_name VARCHAR2(32) NOT NULL,
    semester NUMBER DEFAULT 1 CHECK(semester IN (1, 2)),
    grade NUMBER DEFAULT 0,
    CONSTRAINT pk_student_id PRIMARY KEY(id)
    )
    
COMMIT ON TABLE student.id IS '学生id';
COMMIT ON TABLE student.name IS '学生姓名';
COMMIT ON TABLE student.c_name IS '选课';
COMMIT ON TABLE student.semester IS '学期';

CREATE TABLE teacher(
    id VARCHAR2(32),
    t_name VARCHAR2(32),
    course_id VARCHAR2(32),
    CONSTRAINT pk_teacher_id PRIMARY KEY (id)
    );

COMMENT ON TABLE teacher.id IS '学生标号';
COMMENT ON TABLE teacher.t_name IS '老师姓名';
COMMENT ON TABLE teacher.course_id IS '授课课程编号';


INSERT INTO course VALUES ('001','高等代数',1,'理学');
INSERT INTO course VALUES ('002','高等代数',2,'理学');
INSERT INTO course VALUES ('003','数学分析',1,'理学');
INSERT INTO course VALUES ('004','数学分析',2,'理学');
INSERT INTO course VALUES ('005','人文历史',1,'史学');
INSERT INTO course VALUES ('006','人文历史',2,'史学');
INSERT INTO course VALUES ('007','大学英语',1,'外语');
INSERT INTO course VALUES ('008','大学英语',2,'外语');



INSERT INTO student VALUES ('0001','ToM1','高等代数','1',59);
INSERT INTO student VALUES ('0002','ToM2','数学分析','2',85);
INSERT INTO student VALUES ('0003','ToM3','人文历史','2',0);
INSERT INTO student VALUES ('0004','ToM4','大学英语','1',79);
INSERT INTO student VALUES ('0005','ToM5','数学分析','2',65);
INSERT INTO student VALUES ('0006','ToM6','人文历史','1',66);
INSERT INTO student VALUES ('0007','ToM7','大学英语','1',19);
INSERT INTO student VALUES ('0008','ToM8','数学分析','1',100);


INSERT INTO teacher VALUES('01','Jack1','001');
INSERT INTO teacher VALUES('02','Jack2','001');
INSERT INTO teacher VALUES('03','Jack3','003');
INSERT INTO teacher VALUES('04','Jack4','002');
INSERT INTO teacher VALUES('05','Jack5','008');
INSERT INTO teacher VALUES('06','Jack6','005');
INSERT INTO teacher VALUES('07','Jack7','007');
INSERT INTO teacher VALUES('08','Jack8','006');
INSERT INTO teacher VALUES('09','Jack9','004');

DROP TABLE course;
DROP TABLE student;
DROP TABLE teacher;

SELECT * FROM course;
SELECT * FROM student;
SELECT * FROM teacher;

1. Cartesian connection

  • Conditions generated Cartesian product
  • Omitted connection condition
  • Join condition is invalid
  • All rows in all tables are interconnected
  • To avoid a Cartesian set, may be added in effective connection conditions WHERE
  • In the actual operating environment, should be avoided Descartes Collection
  • Cartesian join query returns the data record is more than one multiple relational tables, included in the student table has eight records, curriculum there are seven records, the returned Cartesian product is the result of 8x7 = 56 recording, number of tables, the corresponding product will have returned records.
Case presentation
--先分别查询每一个表的信息
SELECT * FROM student;
SELECT * FROM course;
SELECT* FROM teacher;

--查询学生以及课程的所有信息(不加入任何条件,)
SELECT * FROM student,course;

--查询学生、课程、以及教师的信息
SELECT * FROM student,course,teacher

Student enrollment information
Here Insert Picture Description
Course information Student
Here Insert Picture Description
teachers teach information
Here Insert Picture Description
Cartesian product of student enrollment and course information in
Here Insert Picture Description
student enrollment, course grades, teachers teaching three tables of Cartesian product
Here Insert Picture Description

2. natural connection

Connection principle
  • Connection principle: two tables contain the same column name, and have the same column value (column total)
  • I.e., with no connection among the Cartesian equivalent
Case presentation
-- 适用于连接的表只有一个同名列时(上述语句无法执行,有多个同名列)
SELECT * FROM student NATURAL JOIN course;

--JOIN...USING...当有多个同名列时使用(指定相同列名中的某一个)
SELECT * FROM student NATURAL JOIN course USING (c_name)

--使用过滤条件(课程名称为大学英语的)
SELECT * FROM student JOIN course USING (c_name) WHERE c_name='大学英语';
--条件为第一学期
SELECT * FROM student JOIN course USING (semester) WHERE semester=1;

--JOIN...ON...适用于两个表中存在或者不存在同名列时使用
SELECT * FROM teacher JOIN course ON teacher.course_id = course.id;

--其他查询(查询史学类学科成绩大于60分的学生的id,姓名,学期,成绩,课程id,课程名称,课程类别)

SELECT 
	student.id AS student_id,
	student.s_name,
	student.semester,
    student.grade,
	course.id AS ciurse_id,
	course.c_name,
	course.category
		FROM student,course
			WHERE student.grade > 60
			AND course.category = '史学';

More of the same column names naturally join query results
Here Insert Picture Description
using filters results (English)
Here Insert Picture Description
conditions for the first semester
Here Insert Picture Description
JOIN ... ON ... query results
Here Insert Picture Description
other results

Here Insert Picture Description

3. The outer connector (Should query result that no significant difference can be copied to build the table above statement in the record itself several contrast difference)

External connection can be divided into three main
  • Left outer join
  • Right outer join
  • Full outer join
Left outer join
  • Primary table plus the data did not match the basis equijoins
--两种写法结果一样
-- 第一种常规写法
SELECT * FROM student LEFT OUTER JOIN course ON student.semester = course.semester;

--Oracle支持的另一种写法
SELECT * FROM student,course WHERE student.semester = course.semester(+);

Here Insert Picture Description

Right outer join
  • Do not do too much to explain, usage, and left outer join, like, the only difference is explained become a key RIGHT
  • Oracle supports another wording is: SELECT * FROM student, course WHERE student.semester (+) = course.semester;
Full outer join
  • Full external connection is based on non-matching data equijoins left and right tables are coupled
-- 常规写法
SELECT * FROM student FULL OUTER JOIN course
ON student.semester = course.semester;

--Oracle支持的另一种写法
SELECT  student.*,course.*
	FROM student
		LEFT OUTER JOIN course
		ON student.semester = course.semester
		UNION
			SELECT student.*,course.*
			FROM course
			LEFT OUTER JOIN student
			ON student.semester = course.semester;

Here Insert Picture Description

4. the connections (equivalent connections)

  • The most common (results not do much to explain)
  • SELECT * FROM student,course WHERE student.semester = course.semester;

The connection is not equivalent

  • In addition the equal sign with the main operator, such as: <>,>, <,> =, <=, LIKE, IN, BETWEEN ... AND
-- 查询学生成绩在60---100分之间的学生的信息(两个语句等价)
SELECT * FROM student JOIN course ON student.grade BETWEEN 60 AND 100;
SELECT * FROM student,course WHERE 60< student.grade AND student.grade<=100;

--查询成绩表和教师授课表中的课程id相等的课程信息和授课教师信息
SELECT * FROM course JOIN teacher ON teacher.course_id LIKE course.id;

Student achievement information in 60-100 minutes
Here Insert Picture Description
Here Insert Picture Description


While studying records, if any deficiencies welcome message pointing ...

Published 63 original articles · won praise 1 · views 2024

Guess you like

Origin blog.csdn.net/qq_45061361/article/details/104866974