数据库练习

 

 

create database pcshop;
show databases;
use pcshop;
CREATE TABLE IF NOT EXISTS customers (
    customer_id CHAR(10) NOT NULL,
    firstname VARCHAR(32) DEFAULT NULL,
    lastname VARCHAR(32) DEFAULT NULL,
    city VARCHAR(32) DEFAULT NULL,
    address VARCHAR(128) DEFAULT NULL,
    email VARCHAR(128) DEFAULT NULL,
    PRIMARY KEY (customer_id),
    UNIQUE KEY email (email)
);
show tables;
SELECT 
    *
FROM
    customers;
INSERT INTO customers (customer_id, firstname, lastname, city, address, email) VALUES
('1122334455', 'Ann', 'O''Brien', 'Dublin', '1 Jervis St.', '[email protected]'),
('1231231231', 'John', 'Doe', 'Limerick', NULL, NULL),
('1234567890', 'Paul', 'Murphy', 'Cork', '20 O''Connell St.', NULL),
('9876543210', 'Jack', 'Murphy', 'Galway', '101 O''Connell St.', '[email protected]'),
('9999999999', 'Norah', 'Jones', 'Limerick', '2 Thomas St.', '[email protected]');
CREATE TABLE IF NOT EXISTS laptops (
    model CHAR(4) NOT NULL DEFAULT '',
    speed DOUBLE DEFAULT NULL,
    ram INT(11) DEFAULT NULL,
    hd INT(11) DEFAULT NULL,
    screen DOUBLE DEFAULT NULL,
    price DOUBLE DEFAULT NULL,
    PRIMARY KEY (model)
);
INSERT INTO laptops (model, speed, ram, hd, screen, price) VALUES
('2001', 2, 2048, 240, 20.1, 3673),
('2002', 1.73, 1024, 80, 17, 949),
('2003', 1.8, 512, 60, 15.4, 549),
('2004', 2, 512, 60, 13.3, 1150),
('2005', 2.16, 1024, 120, 17, 2500),
('2006', 2, 2048, 80, 15.4, 1700),
('2007', 1.83, 1024, 120, 13.3, 1429),
('2008', 1.6, 1024, 100, 15.4, 900),
('2009', 1.6, 512, 80, 14.1, 680),
('2010', 2, 2048, 160, 15.4, 2300);
CREATE TABLE IF NOT EXISTS pcs (
    model CHAR(4) NOT NULL,
    speed DOUBLE NOT NULL,
    ram INT(11) NOT NULL,
    hd INT(11) NOT NULL,
    price DOUBLE NOT NULL,
    PRIMARY KEY (model)
);
INSERT INTO pcs (model, speed, ram, hd, price) VALUES
('1001', 2.66, 1024, 250, 2114),
('1002', 2.1, 512, 250, 995),
('1003', 1.42, 512, 80, 478),
('1004', 2.8, 1024, 250, 649),
('1005', 3.2, 512, 250, 630),
('1006', 3.2, 1024, 320, 1049),
('1007', 2.2, 1024, 200, 510),
('1008', 2.2, 2048, 250, 770),
('1009', 2, 1024, 250, 650),
('1010', 2.8, 2048, 300, 770),
('1011', 1.86, 2048, 160, 959),
('1012', 2.8, 1024, 160, 649),
('1013', 3.06, 512, 80, 529);
CREATE TABLE IF NOT EXISTS printers (
    model CHAR(4) NOT NULL DEFAULT '',
    color VARCHAR(5) DEFAULT NULL,
    `type` VARCHAR(10) DEFAULT NULL,
    price DOUBLE DEFAULT NULL,
    PRIMARY KEY (model)
);
INSERT INTO printers (model, color, `type`, price) VALUES
('3001', 'TRUE', 'ink-jet', 99),
('3002', 'FALSE', 'laser', 239),
('3003', 'TRUE', 'laser', 899),
('3004', 'TRUE', 'ink-jet', 120),
('3005', 'FALSE', 'laser', 120),
('3006', 'TRUE', 'ink-jet', 100),
('3007', 'TRUE', 'laser', 200);

CREATE TABLE IF NOT EXISTS products (
    maker CHAR(1) DEFAULT NULL,
    model CHAR(4) NOT NULL DEFAULT '',
    `type` VARCHAR(10) DEFAULT NULL,
    PRIMARY KEY (model)
);
INSERT INTO products (maker, model, `type`) VALUES
('A', '1001', 'pc'),
('A', '1002', 'pc'),
('A', '1003', 'pc'),
('B', '1004', 'pc'),
('B', '1005', 'pc'),
('B', '1006', 'pc'),
('C', '1007', 'pc'),
('D', '1008', 'pc'),
('D', '1009', 'pc'),
('D', '1010', 'pc'),
('E', '1011', 'pc'),
('E', '1012', 'pc'),
('E', '1013', 'pc'),
('E', '2001', 'laptop'),
('E', '2002', 'laptop'),
('E', '2003', 'laptop'),
('A', '2004', 'laptop'),
('A', '2005', 'laptop'),
('A', '2006', 'laptop'),
('B', '2007', 'laptop'),
('F', '2008', 'laptop'),
('F', '2009', 'laptop'),
('G', '2010', 'laptop'),
('E', '3001', 'printer'),
('E', '3002', 'printer'),
('E', '3003', 'printer'),
('D', '3004', 'printer'),
('D', '3005', 'printer'),
('H', '3006', 'printer'),
('H', '3007', 'printer');
CREATE TABLE IF NOT EXISTS sales (
    customer_id CHAR(10) NOT NULL DEFAULT '',
    model CHAR(4) NOT NULL DEFAULT '',
    quantity INT(11) DEFAULT NULL,
    `day` DATE NOT NULL DEFAULT '0000-00-00',
    paid DOUBLE DEFAULT NULL,
    type_of_payment VARCHAR(32) DEFAULT NULL,
    PRIMARY KEY (customer_id , model , `day`)
);
INSERT INTO sales (customer_id, model, quantity, `day`, paid, type_of_payment) VALUES
('1122334455', '2010', 1, '2013-12-19', 2300, 'mastercard credit'),
('1122334455', '3001', 1, '2013-12-18', 99, 'cash'),
('1231231231', '2002', 2, '2013-12-19', 1898, 'visa credit'),
('1231231231', '3002', 1, '2013-12-18', 239, 'cash'),
('1234567890', '1001', 1, '2013-12-20', 1902.6, 'mastercard credit'),
('9876543210', '1007', 1, '2013-12-17', 510, 'visa debit'),
('9876543210', '1007', 3, '2013-12-19', 1530, 'visa debit'),
('9876543210', '2002', 1, '2013-12-17', 949, 'visa debit'),
('9999999999', '1007', 1, '2013-12-20', 459, 'visa credit'),
('9999999999', '3007', 2, '2013-12-20', 360, 'visa credit');


 

 

#进行单表查询

#1)查询所有speed大于2.8的PC信息

SELECT 
    *
FROM
    pcs
WHERE
    speed > 2.8;

#2)查询购买model为1007的购买记录信息

SELECT 
    *
FROM
    sales
WHERE
    model = '1007';

#3)统计2013-12-20购买记录的数量(count)
#聚集函数

SELECT 
    COUNT(*)
FROM
    sales
WHERE
    day = '2013-12-20';

#4)统计2013-12-20购买的总数量(sum)

SELECT 
    model, COUNT(*), MAX(quantity), SUM(quantity)
FROM
    sales
GROUP BY model;

#a) What PC models have a speed of at least 3.00?

SELECT 
    *
FROM
    pcs
WHERE
    speed >= 3.0;

#b) Which makers make laptops with a hard disk of at least 100 GB
#查询哪个供应商供应laptops硬盘至少100GB以上的供应商信息

#--1、做等值连接

SELECT 
    maker
FROM
    products,
    laptops
WHERE
    products.model = laptops.model
        AND hd >= 100;

#--2、自然连接

SELECT 
    maker
FROM
    products
        NATURAL JOIN
    laptops
WHERE
    hd >= 100;

#--3、内连接
SELECT 
    maker
FROM
    products
        inner JOIN
    laptops ON products.model = laptops.model
WHERE
    hd >= 100;

#--4、自然连接

SELECT 
    maker
FROM
    products
        JOIN
    laptops USING (model)
WHERE
    hd >= 100;

#--5、子查询实现
#--不相关子查询(子查询的SQL语句可以单独执行)

SELECT 
    maker
FROM
    products
WHERE
    model IN (SELECT 
            model
        FROM
            laptops
        WHERE
            hd >= 100);

#--相关子查询(子查询的SQL语句不能够单独执行,必须嵌在父查询中,作为父查询的条件执行)

SELECT 
    maker
FROM
    products
WHERE
    EXISTS( SELECT 
            *
        FROM
            laptops
        WHERE
            model = products.model AND hd >= 100);

#--所有子查询都可以写成相关子查询
#--有些相关子查询可以用不相关子查询来代替
#--所有不相关子查询都可以用相关子查询来代替


#c) Find the model number and price of all products (of any type) made by maker B
#查询供应商B所提供的所有产品的产品编号和产品价格。

SELECT 
    model, price
FROM
    (SELECT 
        model, price
    FROM
        pcs UNION SELECT 
        model, price
    FROM
        laptops UNION SELECT 
        model, price
    FROM
        printers) allType
        NATURAL JOIN
    products
WHERE
    maker = 'B';

#d) Find the model numbers of all color laser printers
#9)查找所有的彩色打印机的model numbers。

SELECT 
    model
FROM
    printers
WHERE
    color = 'TRUE' AND type = 'laser';

#e) Find those makers that sell Laptops but not PCs
#10)查找供应laptops但不供应pc的供应商信息
SELECT DISTINCT
    maker
FROM
    products
WHERE
    type = 'laptop'
        AND maker NOT IN (SELECT 
            maker
        FROM
            products
        WHERE
            type = 'pc');

#f) Find those hard-disk sizes that occur in two or more PCs
#查询硬盘大小出现在两种以上PC电脑上的硬盘大小

SELECT DISTINCT
    hd
FROM
    pcs
GROUP BY hd
HAVING COUNT(hd) >= 2;


#a)Find those pairs of PC models that have both the same speed and ram. A pair should be listed only once;  e.g., list (i, j) but not (j, i).
#查询具有相同运行速度和内存的PC电脑编号对每个pc models对只出现一次,即 (i, j)出现,则 (j, i)不出现

#--典型的表的自身连接查询

SELECT 
    a.model, b.model
FROM
    pcs a,
    pcs b
WHERE
    a.speed = b.speed AND a.ram = b.ram
        AND a.model < b.model;

#b)Find the makers who sell exactly three different models of PC
#查询销售三种不同型号PC电脑的供应商

SELECT DISTINCT
    maker
FROM
    products
WHERE
    type = 'pc'
GROUP BY maker
HAVING COUNT(*) >= 3;

#c)Find the makers of PCs with at least  three different speeds

SELECT DISTINCT
    maker
FROM
    products
        NATURAL JOIN
    pcs
GROUP BY maker
HAVING COUNT(speed) >= 3;

#d)Find those makers of at least two  different computers (PCs or laptops) with speeds of at least 2.80
#--想让大家用视图解决问题
#--from子句中应用别名方式给子查询命名名称也可以

SELECT DISTINCT
    maker
FROM
    products
        NATURAL JOIN
    (SELECT 
        model, speed
    FROM
        pcs
    WHERE
        speed >= 2.8 UNION SELECT 
        model, speed
    FROM
        laptops
    WHERE
        speed >= 2.8) atleastSpeed
GROUP BY maker
HAVING COUNT(*) >= 2;

#e)Find the maker(s) of the computer  (PC or laptop) with the highest available speed
#--想让大家用视图解决问题

CREATE VIEW v_pcandlaptop (model , speed) AS
    SELECT 
        model, speed
    FROM
        pcs 
    UNION SELECT 
        model, speed
    FROM
        laptops;

SELECT DISTINCT
    maker
FROM
    products
WHERE
    model IN (SELECT 
            model
        FROM
            (SELECT 
                model, speed
            FROM
                pcs UNION SELECT 
                model, speed
            FROM
                laptops) atleastSpeed
        WHERE
            atleastSpeed.speed >= ALL (SELECT 
                    speed
                FROM
                    pcs UNION SELECT 
                    speed
                FROM
                    laptops));

SELECT DISTINCT
    maker
FROM
    products
WHERE
    model IN (SELECT 
            model
        FROM
            v_pcandlaptop
        WHERE
            speed >= (SELECT 
                    MAX(speed)
                FROM
                    v_pcandlaptop));

你可能感兴趣的:(mysql)