1. Create the database SaleSys
Second , create 3 tables in the database SaleSys
- Brand information table ( brand )
- BrandId -- brand number, integer, auto-growth, primary key
- BrandName -- brand name, character type, unique constraint
- Product table ( Item )
- ItemId -- item number, integer, auto-growth, primary key
- ItemName -- product name, character type, non-empty, unique
- ItemBirth -- production date, non-null
- ItemPrice -- price, decimal, non-null, check constraints cannot be negative
- BrandId -- brand ID, non-null, foreign key
- Sales table ( sale )
- SaleId -- sales number, integer, auto-increment, primary key
- ItemId -- item number, the foreign key is not empty
- SaleNum -- sales quantity, the check constraint must be greater than 0
- SaleDate -- transaction time, not empty
4. The reference data of the three tables are as follows
Third, complete the following query questions
---------------------------------------------------------------------
--1 . Find the maximum price in the table
--2 . Find the maximum price of each brand
--3 . Find the brand number and price that the brand's maximum price exceeds
--4 . Find the minimum price in the table
--5 . Find the minimum price of each brand
--6 . Find the brand number and price whose minimum price is less than that of the brand
--7 . For the production date after -08-08 , the minimum price of each brand is less than
--8 , find the total value of the price in the table
--9 , find the total value of each brand
--10 , find the total value of the brand between to
--11 . Find the average price of all products
--12 , find the average value of each brand
--13 . Find out which brand average is 40 , 50 , 60
--14 . How many kinds of products are there in total?
--15 . Find out how many products each brand has
--16 . The number of products of each brand is between 1 and 2
---------------------------------------------------------------------
--1 . Query the Item table, and calculate the maximum price and minimum price of the product whose brand number is 1
--2 . Query the Item table and count how many products there are in total
--3 . Query the Item table and count the maximum price and total number of products whose production date is before 2015-08-09
--4 . Query the Sales table, and calculate the average sales volume of the product with the product number 1
--5 . Query the Sale table, and count the largest commodity number and the smallest commodity number
--6 . Query the Brand table and count how many brands there are
--7 . Query the Brand table, and count the number of brands with the word " water " in the brand
--8 . Query the Item table, and calculate the total price of the products whose price is above 100 yuan
---------------------------------------------------------------------
- 1. Statistical sales table to obtain the total sales volume, sales records, and average sales volume
-- 2. Count the average, maximum, and minimum sales volume of products with sales dates between 2015-08-08 and 2015-09-10
-- 3. Count the number of brands with products under the brand
-- 4 . Find out which products have the average value of the products whose brand number is 2 is 40 , 50 , 60
-- 5. The total value of the products whose brand code is
-- 6. Find the brand whose total brand value is greater than
-- 一, 创建数据库SaleSys
-- 1,删除数据库
DROP DATABASE IF EXISTS SaleSys;
-- 2,创建数据库
CREATE DATABASE SaleSys;
-- 3,修改数据库编码方式和字符集排列顺序
ALTER DATABASE SaleSys CHARACTER SET utf8 COLLATE utf8_bin;
-- 4,使用数据库
USE SaleSys;
-- 5,查看当前所有数据库
SHOW DATABASES;
-- 二, 在数据库SaleSys中创建3张表
-- 1, 品牌信息表(brand)
DROP TABLE IF EXISTS brand;
CREATE TABLE brand(
BrandId INT PRIMARY KEY AUTO_INCREMENT, -- 品牌编号,整型,自动增长,主键
BrandName VARCHAR(20) UNIQUE -- 品牌名称,字符型, 唯一约束
);
-- 2, 商品表(Item)
DROP TABLE IF EXISTS Item;
CREATE TABLE Item(
ItemId INT PRIMARY KEY AUTO_INCREMENT, -- 商品编号,整型,自动增长,主键
ItemName VARCHAR(20) UNIQUE NOT NULL, -- 商品名称,字符型,非空,唯一
ItemBirth DATETIME NOT NULL, -- 生产日期,非空
ItemPrice FLOAT NOT NULL CHECK(ItemPrice>=0), -- 价格,小数,非空,检查约束不能是负数
BrandId INT,
FOREIGN KEY (BrandId) REFERENCES brand(BrandId) -- 品牌编号,非空,外键
);
-- 3, 销量表(sale)
DROP TABLE IF EXISTS sale;
CREATE TABLE sale(
SaleId INT PRIMARY KEY AUTO_INCREMENT, -- 销售编号,整型,自动增长,主键
ItemId INT NOT NULL, -- 商品编号,外键非空
SaleNum INT NOT NULL CHECK(SaleNum>=0), -- 销售数量,检查约束必须大于0
SaleDate DATETIME NOT NULL,
FOREIGN KEY (ItemId) REFERENCES Item(ItemId) -- 成交时间,非空
);
-- 三添加数据
-- 1,添加品牌表brand数据
INSERT INTO brand VALUES( DEFAULT,'戴尔');
INSERT INTO brand VALUES( DEFAULT,'华硕');
INSERT INTO brand VALUES( DEFAULT,'联想');
INSERT INTO brand VALUES( DEFAULT,'美化');
INSERT INTO brand VALUES( DEFAULT,'苹果');
SELECT * FROM brand;
-- 2,添加商品表item数据
INSERT INTO Item VALUES(DEFAULT,'电脑','2013-10-20 09:30:12',3999,1);
INSERT INTO Item VALUES(DEFAULT,'耳机','2014-05-21 21:32:54',44,2);
INSERT INTO Item VALUES(DEFAULT,'路由器','2014-05-21 21:32:54',55,2);
INSERT INTO Item VALUES(DEFAULT,'猫','2014-05-21 21:32:54',22,1);
INSERT INTO Item VALUES(DEFAULT,'键盘','2014-05-21 21:32:54',45,3);
INSERT INTO Item VALUES(DEFAULT,'可乐','2012-07-06 15:35:24',3.5,3);
INSERT INTO Item VALUES(DEFAULT,'手机','2015-04-13 14:34:55',3,4);
INSERT INTO Item VALUES(DEFAULT,'数据线','2015-05-19 08:20:52',79,5);
SELECT * FROM Item;
-- 3,添加销售表sale数据
INSERT INTO sale VALUES(DEFAULT,1,2,'2015-05-29 16:21:53');
INSERT INTO sale VALUES(DEFAULT,1,50,'2015-05-29 19:15:23');
INSERT INTO sale VALUES(DEFAULT,2,50,'2015-05-29 19:15:23');
INSERT INTO sale VALUES(DEFAULT,2,20,'2015-05-29 19:15:23');
INSERT INTO sale VALUES(DEFAULT,2,50,'2015-05-29 19:15:23');
INSERT INTO sale VALUES(DEFAULT,3,16,'2015-05-29 21:40:08');
INSERT INTO sale VALUES(DEFAULT,3,5,'2015-05-29 12:06:35');
INSERT INTO sale VALUES(DEFAULT,5,12,'2015-05-29 09:00:45');
SELECT * FROM sale;
-- 四,完成如下查询题目
-- -------------------------------------------------------------------
-- 1、求表中最大的价格
SELECT MAX(ItemPrice) 最大的价格 FROM item;
SELECT ItemPrice 最大的价格 FROM item ORDER BY ItemPrice DESC LIMIT 1;
-- 2、求每个品牌的最大价格
SELECT BrandId 品牌编号,MAX(ItemPrice) 最大的价格 FROM item GROUP BY BrandId;
-- 3、求品牌最大的价格超过50的品牌编号和价格
SELECT BrandId 品牌编号,MAX(ItemPrice) 最大的价格 FROM Item
GROUP BY BrandId HAVING MAX(ItemPrice)>=50;
-- 4、求表中最小的价格
SELECT MIN(ItemPrice) 最小的价格 FROM Item;
SELECT ItemPrice 最小的价格 FROM item ORDER BY ItemPrice LIMIT 1;
-- 5、求每个品牌的最小价格
SELECT BrandId 品牌编号,MIN(ItemPrice) 最小的价格 FROM item GROUP BY BrandId;
-- 6、求品牌最小的价格小于10的品牌编号和价格
SELECT BrandId 品牌编号,MIN(ItemPrice) 最小的价格 FROM item
GROUP BY BrandId HAVING MIN(ItemPrice)<10;
-- 7、求生产日期在2013-08-08之后的,每个品牌最小的价格小于50的
SELECT BrandId 品牌编号,MIN(ItemPrice) 最小的价格
FROM item WHERE ItemBirth>'2013-08-08'
GROUP BY BrandId HAVING MIN(ItemPrice)<50;
-- 8、求表中价格的合计值
SELECT SUM(ItemPrice) 价格的合计值 FROM Item;
-- 9、求每个品牌的合计值
SELECT BrandId 品牌编号,SUM(ItemPrice) 价格的合计值 FROM Item GROUP BY BrandId;
-- 10、求品牌合计值在到40-100之间
SELECT BrandId 品牌编号,SUM(ItemPrice) 价格的合计值 FROM Item
GROUP BY BrandId HAVING SUM(ItemPrice) BETWEEN 40 AND 100;
-- 11、求所有产品的价格平均值
SELECT AVG(ItemPrice) 价格平均值 FROM Item;
-- 12、求每一个品牌的平均值
SELECT BrandId, AVG(ItemPrice) 每一个品牌的平均值 FROM Item GROUP BY BrandId;
-- 13、求品牌平均值是40、50、60的有哪些
SELECT BrandId 品牌编号 FROM Item GROUP BY BrandId HAVING AVG(ItemPrice) IN (40, 50, 60);
-- 14、求一共有多少种产品
SELECT COUNT(DISTINCT ItemName) 产品数 FROM Item;
-- 15、求每个品牌有多少产品
SELECT BrandId 品牌编号, COUNT(ItemName) 产品数 FROM Item GROUP BY BrandId;
-- 16、每个品牌产品数在1到2之间的
SELECT BrandId 品牌编号, COUNT(ItemName) 产品数 FROM Item GROUP BY BrandId HAVING COUNT(ItemName) BETWEEN 1 AND 2;
-- -------------------------------------------------------------------
-- 1、 查询Item表,统计出品牌编号为1的商品的最大价格和最低价格是多少
SELECT MAX(ItemPrice)最大价格,MIN(ItemPrice)最低价格 FROM Item WHERE BrandId = 1;
-- 2、 查询Item表,统计出一共有多少商品
SELECT COUNT(ItemId) 商品数 FROM Item;
-- 3、 查询Item表,统计出生产日期在2015-08-09之前的商品的最大价格和商品总数
SELECT MAX(ItemPrice) 最大价格, COUNT(ItemId) 商品总数 FROM Item WHERE ItemBirth<'2015-08-09';
-- 4、 查询Sale表,统计出商品号为1的商品的销售平均销量
SELECT AVG(SaleNum) AS 销售平均销量 FROM Sale WHERE ItemId=1;
-- 5、 查询Sale表,统计出最大商品编号和最小商品编号
SELECT MAX(ItemId) 最大商品编号, MIN(ItemId) 最小商品编号 FROM Item;
-- 6、 查询Brand表,统计有多少品牌
SELECT COUNT(*) 品牌数 FROM Brand;
-- 7、 查询Brand表,统计出品牌中有"水"字的品牌有多少中
SELECT COUNT(*) 有“水”字的品牌 FROM Brand WHERE BrandName LIKE '%水%';
-- 8、 查询Item表,统计出价格在100元之上的商品的合计价格
SELECT SUM(ItemPrice) 合计价格 FROM Item WHERE ItemPrice > 100;
-- -------------------------------------------------------------------
-- 1、统计销售表,获取销售总量、销售记录数、销售平均量各是多少
SELECT SUM(SaleNum) 销售总量,COUNT(*) 销售记录数,AVG(SaleNum) 销售平均量 FROM sale;
-- 2、统计出销售日期在2015-08-08到2015-09-10之间的产品的销售数量平均值、最大值、最低值
SELECT AVG(SaleNum) 销售数量平均值,MAX(SaleNum) 销售数量最大值, MIN(SaleNum) 销售数量最低值
FROM sale
WHERE SaleDate BETWEEN '2015-08-08' AND '2015-09-10';
-- 3、统计出品牌下有商品的品牌数量
SELECT COUNT(DISTINCT BrandName) AS 有商品的品牌数量 FROM Brand;
-- 4、求品牌编号为2的商品的平均值是40、50、60的有哪些商品
SELECT Item.ItemId 商品编号,Item.ItemName 商品名称
FROM Item
JOIN sale ON Item.ItemId = sale.ItemId
WHERE BrandId = 4 AND ItemPrice IN (40,50,60);
-- 5、品牌编号为2的产品合计值
SELECT SUM(ItemPrice) 产品合计值
FROM Item
WHERE BrandId = 2;
-- 6、求品牌合计值在大于1000的品牌
SELECT BrandName 品牌名称
FROM Brand
WHERE BrandId=
(SELECT BrandId FROM Item GROUP BY BrandId HAVING SUM(ItemPrice) >1000);