您现在的位置是:首页 >其他 >SQL笔记-视图创建网站首页其他

SQL笔记-视图创建

123的故事 2024-10-16 12:01:04
简介SQL笔记-视图创建

一、视图创建代码注释说明

Oracle

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • CREATE VIEW:创建视图的语法关键字;
  • view_name:视图的名称;
  • SELECT:查询关键字;
  • column1, column2, ...:需要查询的列名;
  • FROM:查询来源;
  • table_name:查询的表名;
  • WHERE:查询条件;
  • condition:查询的条件。

MySQL

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • CREATE VIEW:创建视图的语法关键字;
  • view_name:视图的名称;
  • SELECT:查询关键字;
  • column1, column2, ...:需要查询的列名;
  • FROM:查询来源;
  • table_name:查询的表名;
  • WHERE:查询条件;
  • condition:查询的条件。

SQL Server

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • CREATE VIEW:创建视图的语法关键字;
  • view_name:视图的名称;
  • SELECT:查询关键字;
  • column1, column2, ...:需要查询的列名;
  • FROM:查询来源;
  • table_name:查询的表名;
  • WHERE:查询条件;
  • condition:查询的条件。

PostgreSQL

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • CREATE VIEW:创建视图的语法关键字;
  • view_name:视图的名称;
  • SELECT:查询关键字;
  • column1, column2, ...:需要查询的列名;
  • FROM:查询来源;
  • table_name:查询的表名;
  • WHERE:查询条件;
  • condition:查询的条件。

二、多表视图创建的写法及结果

Oracle

以下是在Oracle数据库中创建多表视图的示例代码:

-- 建立部门表
CREATE TABLE department (
  dept_id NUMBER(10) PRIMARY KEY,
  dept_name VARCHAR2(50) NOT NULL
);

-- 插入数据
INSERT INTO department VALUES (1, '部门1');
INSERT INTO department VALUES (2, '部门2');
INSERT INTO department VALUES (3, '部门3');

-- 建立员工表
CREATE TABLE employee (
  emp_id NUMBER(10) PRIMARY KEY,
  emp_name VARCHAR2(50) NOT NULL,
  dept_id NUMBER(10) NOT NULL,
  salary NUMBER(10, 2) NOT NULL,
  CONSTRAINT fk_dept_id FOREIGN KEY (dept_id) REFERENCES department (dept_id)
);

-- 插入数据
INSERT INTO employee VALUES (101, '张三', 1, 8000);
INSERT INTO employee VALUES (102, '李四', 1, 9000);
INSERT INTO employee VALUES (103, '王五', 2, 7000);
INSERT INTO employee VALUES (104, '赵六', 3, 10000);

-- 创建多表视图
CREATE VIEW emp_dept_salary AS
SELECT e.emp_name, d.dept_name, e.salary
FROM employee e
JOIN department d
ON e.dept_id = d.dept_id;

创建完成后,可以使用以下语句查询视图的内容:

SELECT * FROM emp_dept_salary;

执行结果如下:

EMP_NAMEDEPT_NAMESALARY
张三部门18000.00
李四部门19000.00
王五部门27000.00
赵六部门310000.00

MySQL

以下是在MySQL数据库中创建多表视图的示例代码:

-- 建立部门表
CREATE TABLE department (
  dept_id INT PRIMARY KEY,
  dept_name VARCHAR(50) NOT NULL
);

-- 插入数据
INSERT INTO department VALUES (1, '部门1');
INSERT INTO department VALUES (2, '部门2');
INSERT INTO department VALUES (3, '部门3');

-- 建立员工表
CREATE TABLE employee (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50) NOT NULL,
  dept_id INT NOT NULL,
  salary DECIMAL(10, 2) NOT NULL,
  FOREIGN KEY (dept_id) REFERENCES department (dept_id)
);

-- 插入数据
INSERT INTO employee VALUES (101, '张三', 1, 8000);
INSERT INTO employee VALUES (102, '李四', 1, 9000);
INSERT INTO employee VALUES (103, '王五', 2, 7000);
INSERT INTO employee VALUES (104, '赵六', 3, 10000);

-- 创建多表视图
CREATE VIEW emp_dept_salary AS
SELECT e.emp_name, d.dept_name, e.salary
FROM employee e
JOIN department d
ON e.dept_id = d.dept_id;

创建完成后,可以使用以下语句查询视图的内容:

SELECT * FROM emp_dept_salary;

执行结果如下:

emp_namedept_namesalary
张三部门18000.00
李四部门19000.00
王五部门27000.00
赵六部门310000.00

SQL Server

以下是在SQL Server数据库中创建多表视图的示例代码:

-- 建立部门表
CREATE TABLE department (
  dept_id INT PRIMARY KEY,
  dept_name VARCHAR(50) NOT NULL
);

-- 插入数据
INSERT INTO department VALUES (1, '部门1');
INSERT INTO department VALUES (2, '部门2');
INSERT INTO department VALUES (3, '部门3');

-- 建立员工表
CREATE TABLE employee (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50) NOT NULL,
  dept_id INT NOT NULL,
  salary DECIMAL(10, 2) NOT NULL,
  FOREIGN KEY (dept_id) REFERENCES department (dept_id)
);

-- 插入数据
INSERT INTO employee VALUES (101, '张三', 1, 8000);
INSERT INTO employee VALUES (102, '李四', 1, 9000);
INSERT INTO employee VALUES (103, '王五', 2, 7000);
INSERT INTO employee VALUES (104, '赵六', 3, 10000);

-- 创建多表视图
CREATE VIEW emp_dept_salary AS
SELECT e.emp_name, d.dept_name, e.salary
FROM employee e
JOIN department d
ON e.dept_id = d.dept_id;

创建完成后,可以使用以下语句查询视图的内容:

SELECT * FROM emp_dept_salary;

执行结果如下:

emp_namedept_namesalary
张三部门18000.00
李四部门19000.00
王五部门27000.00
赵六部门310000.00

PostgreSQL

以下是在PostgreSQL数据库中创建多表视图的示例代码:

-- 建立部门表
CREATE TABLE department (
  dept_id INT PRIMARY KEY,
  dept_name VARCHAR(50) NOT NULL
);

-- 插入数据
INSERT INTO department VALUES (1, '部门1');
INSERT INTO department VALUES (2, '部门2');
INSERT INTO department VALUES (3, '部门3');

-- 建立员工表
CREATE TABLE employee (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50) NOT NULL,
  dept_id INT NOT NULL,
  salary DECIMAL(10, 2) NOT NULL,
  FOREIGN KEY (dept_id) REFERENCES department (dept_id)
);

-- 插入数据
INSERT INTO employee VALUES (101, '张三', 1, 8000);
INSERT INTO employee VALUES (102, '李四', 1, 9000);
INSERT INTO employee VALUES (103, '王五', 2, 7000);
INSERT INTO employee VALUES (104, '赵六', 3, 10000);

-- 创建多表视图
CREATE VIEW emp_dept_salary AS
SELECT e.emp_name, d.dept_name, e.salary
FROM employee e
JOIN department d
ON e.dept_id = d.dept_id;

创建完成后,可以使用以下语句查询视图的内容:

SELECT * FROM emp_dept_salary;

执行结果如下:

emp_namedept_namesalary
张三部门18000.00
李四部门19000.00
王五部门27000.00
赵六部门310000.00
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。