sql脚本
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50719
Source Host : localhost:3306
Source Schema : my_db
Target Server Type : MySQL
Target Server Version : 50719
File Encoding : 65001
Date: 03/10/2022 10:22:42
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for category
-- ----------------------------
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
`cid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`cname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of category
-- ----------------------------
INSERT INTO `category` VALUES ('c001', '家电');
INSERT INTO `category` VALUES ('c002', '鞋服');
INSERT INTO `category` VALUES ('c003', '化妆品');
INSERT INTO `category` VALUES ('c004', '汽车');
-- ----------------------------
-- Table structure for products
-- ----------------------------
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`pid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`pname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`price` int(11) NULL DEFAULT NULL,
`flag` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`category_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`pid`) USING BTREE,
INDEX `category_id`(`category_id`) USING BTREE,
CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of products
-- ----------------------------
INSERT INTO `products` VALUES ('p001', '小米电视机', 5000, '1', 'c001');
INSERT INTO `products` VALUES ('p002', '格力空调', 3000, '1', 'c001');
INSERT INTO `products` VALUES ('p003', '美的冰箱', 4500, '1', 'c001');
INSERT INTO `products` VALUES ('p004', '篮球鞋', 800, '1', 'c002');
INSERT INTO `products` VALUES ('p005', '运动裤', 200, '1', 'c002');
INSERT INTO `products` VALUES ('p006', '短袖', 300, '1', 'c002');
INSERT INTO `products` VALUES ('p007', '冲锋衣', 2000, '1', 'c002');
INSERT INTO `products` VALUES ('p008', '神仙水', 800, '1', 'c003');
INSERT INTO `products` VALUES ('p009', '大宝', 200, '1', 'c003');
-- ----------------------------
-- View structure for products_category_view
-- ----------------------------
DROP VIEW IF EXISTS `products_category_view`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `products_category_view` AS select `p`.`pid` AS `pid`,`p`.`pname` AS `pname`,`p`.`price` AS `price`,`p`.`flag` AS `flag`,`p`.`category_id` AS `category_id`,`c`.`cid` AS `cid`,`c`.`cname` AS `cname` from (`products` `p` left join `category` `c` on((`p`.`category_id` = `c`.`cid`)));
SET FOREIGN_KEY_CHECKS = 1;
1.视图是一张虚表(建立在table之上的)
2.首先建立一张表,在表的基础上,指定的列映射成一个视图
3.映射:就是SELECT 查询语句(过滤掉安全隐患列的数据),把他查到的数据作为视图的数据进行映射
4.数据来源还是对应的table(是视图的基表)
语法格式:
create view 视图名称 [字段名称列表] as select语句; -- []表示可选项
create view 视图名称 e_id,e_user,e_age as select(id,user,age);
as:表示视图要执行的操作(就是表示视图映射表中的数据)
1.创建视图
# 1.查询所有商品分类详情
SELECT * FROM
products p LEFT JOIN category c on p.category_id = c.cid;
# 2.基于SELECT查询语句,将查询到的结果作为视图进行映射
CREATE VIEW products_category_view
AS
SELECT * FROM
products p LEFT JOIN category c on p.category_id = c.cid;
2.查询视图中的数据
直接将视图名称作为一张表的名称进行操作,注意:视图只有查询操作,和表查询操作一摸一样
。
1.需求:查询各个分类下商品的平均价格
SELECT
cname AS '商品分类名称',
avg(p.price) AS '分类平均价格'
FROM products p
LEFT JOIN category c
ON p.category_id = c.cid
GROUP BY c.cname;
SELECT
cname AS '商品分类名称',
avg(price) AS '分类平均价格'
FROM
products_category_view
GROUP BY cname;
2.需求:查询鞋服分类下最贵的商品的全部信息
# 查出最贵的价格
SELECT
max(p.price) AS '最贵商品'
FROM
products p
LEFT JOIN
category c
ON p.category_id = c.cid
WHERE
c.cname = '鞋服';
# 查出最贵商品的全部信息
SELECT *
FROM
products p
LEFT JOIN
category c
ON p.category_id = c.cid
WHERE
c.cname = '鞋服'
AND
p.price = (
SELECT max(p.price) AS '最贵商品'
FROM products p LEFT JOIN category c ON p.category_id = c.cid
WHERE c.cname = '鞋服'
);
SELECT
*
FROM
products_category_view pcv
WHERE
pcv.cname = '鞋服'
AND
pcv.price = (SELECT MAX(price)
FROM products_category_view
WHERE cname = '鞋服');
1.视图建立在表的基础上,表中存储的数据,视图只是做数据的展示。
2.通过视图不可以做数据的更新操作。
3.可以直接删除视图,不会有任何影响,表如果直接删除,数据就会别永久删除,还会影响给与此表创建的所有输出。
1.权限控制时可以使用:
2.简化复杂的多表查询: