Oracle 6张表联合查询实战(oracle6张表查询)

在Oracle中使用联合查询可以将多个表中的数据合并在一起,方便我们进行数据分析和处理。本文将介绍如何在Oracle中使用联合查询实现数据的统计和查询。

我们需要创建6张表,以便后续的演示操作。代码如下:

CREATE TABLE employee

(

emp_id INTEGER PRIMARY KEY,

emp_name VARCHAR2(50) NOT NULL,

emp_salary INTEGER NOT NULL,

emp_department VARCHAR2(50) NOT NULL

);

CREATE TABLE department

(

department_id INTEGER PRIMARY KEY,

department_name VARCHAR2(50) NOT NULL

);

CREATE TABLE project

(

project_id INTEGER PRIMARY KEY,

project_name VARCHAR2(50) NOT NULL

);

CREATE TABLE employee_project

(

emp_id INTEGER NOT NULL,

project_id INTEGER NOT NULL,

hours_worked INTEGER NOT NULL,

FOREIGN KEY (emp_id) REFERENCES employee(emp_id),

FOREIGN KEY (project_id) REFERENCES project(project_id)

);

CREATE TABLE employee_department

(

emp_id INTEGER NOT NULL,

department_id INTEGER NOT NULL,

FOREIGN KEY (emp_id) REFERENCES employee(emp_id),

FOREIGN KEY (department_id) REFERENCES department(department_id)

);

CREATE TABLE employee_salary_hike

(

emp_id INTEGER NOT NULL,

hike_amount INTEGER NOT NULL,

hike_date DATE NOT NULL,

FOREIGN KEY (emp_id) REFERENCES employee(emp_id)

);

以上代码创建了6个表,其中employee表存储员工信息,department表存储部门信息,project表存储项目信息,employee_project表存储员工参与的项目信息,employee_department表存储员工所在部门信息,employee_salary_hike表存储员工晋升信息。

接下来,我们需要将这6个表联合起来,便于查询和统计。代码如下:

SELECT e.emp_name,

d.department_name,

p.project_name,

ep.hours_worked,

esh.hike_amount

FROM employee e

JOIN employee_department ed ON e.emp_id = ed.emp_id

JOIN department d ON d.department_id = ed.department_id

JOIN employee_project ep ON e.emp_id = ep.emp_id

JOIN project p ON p.project_id = ep.project_id

JOIN employee_salary_hike esh ON e.emp_id = esh.emp_id

ORDER BY e.emp_name;

以上代码将6个表联合起来,以员工姓名作为排序规则进行查询。通过使用JOIN语句,我们将6个表中员工的工作部门、参与项目信息和晋升信息合并起来,方便我们进行数据分析和统计。

通过以上操作,我们可以看到Oracle中联合查询的强大功能,不仅可以方便地进行数据查询、筛选和筛选,还可以通过联合多张表来实现复杂的数据关联和查询。


数据运维技术 » Oracle 6张表联合查询实战(oracle6张表查询)