ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • DML ) insert,select,update query remind example
    Mysql 2017. 11. 15. 16:24
    728x90

    ----------------   INSERT   ------------------------


    -- Insert a row with all the column values


    INSERT INTO products VALUES 

    (1001, 'PEN', 'Pen Red', 5000, 1.23);



    -- INSERT multiple rows in one command


    INSERT INTO products VALUES

             (NULL, 'PEN', 'Pen Blue',  8000, 1.25),

             (NULL, 'PEN', 'Pen Black', 2000, 1.25);


    -- Missing value for the auto_increment column also results in max_value + 1


    INSERT INTO products (productCode, name, quantity, price) VALUES

             ('PEC', 'Pencil 2B', 10000, 0.48),

             ('PEC', 'Pencil 2H', 8000, 0.49);



    -- Missing columns get their default values

    INSERT INTO products 

    (productCode, name) VALUES ('PEC', 'Pencil HB');




    -- 2nd column (productCode) is defined to be NOT NULL

    INSERT INTO products values 

    (NULL, NULL, NULL, NULL, NULL);







    ----------------   SELECT   ------------------------


    -- List all rows for the specified columns

    SELECT name, price FROM products;

    +-----------+-------+

    | name      | price |

    +-----------+-------+

    | Pen Red   |  1.23 |

    | Pen Blue  |  1.25 |

    | Pen Black |  1.25 |

    | Pencil 2B |  0.48 |

    | Pencil 2H |  0.49 |

    +-----------+-------+


    -- List all rows of ALL the columns. The wildcard * denotes ALL columns

    SELECT * FROM products;

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1001 | PEN         | Pen Red   |     5000 |  1.23 |

    |      1002 | PEN         | Pen Blue  |     8000 |  1.25 |

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    |      1004 | PEC         | Pencil 2B |    10000 |  0.48 |

    |      1005 | PEC         | Pencil 2H |     8000 |  0.49 |

    +-----------+-------------+-----------+----------+-------+


    2) SELECT without Table



    SELECT 1+1;

    +-----+

    | 1+1 |

    +-----+

    |   2 |

    +-----+




    SELECT NOW();

    +---------------------+

    | NOW()               |

    +---------------------+

    | 2012-10-24 22:13:29 |

    +---------------------+



    2-1) Multiple columns


    SELECT 1+1, NOW();

    +-----+---------------------+

    | 1+1 | NOW()               |

    +-----+---------------------+

    |   2 | 2012-10-24 22:16:34 |

    +-----+---------------------+



    3) Comparison Operators


    SELECT name, price FROM products WHERE price < 1.0;

    +-----------+-------+

    | name      | price |

    +-----------+-------+

    | Pencil 2B |  0.48 |

    | Pencil 2H |  0.49 |

    +-----------+-------+



    SELECT name, quantity FROM products WHERE quantity <= 2000;

    +-----------+----------+

    | name      | quantity |

    +-----------+----------+

    | Pen Black |     2000 |

    +-----------+----------+


    SELECT name, price FROM products WHERE productCode = 'PEN';

    +-----------+-------+

    | name      | price |

    +-----------+-------+

    | Pen Red   |  1.23 |

    | Pen Blue  |  1.25 |

    | Pen Black |  1.25 |

    +-----------+-------+


    4) String Pattern Matching - LIKE and NOT LIKE


    For strings, in addition to full matching using operators like '=' and '<>', 

    we can perform pattern matching using operator LIKE (or NOT LIKE) with wildcard characters. 

    The wildcard '_' matches any single character; '%' matches any number of characters (including zero). 

    For example,


    'abc%' matches strings beginning with 'abc';

    '%xyz' matches strings ending with 'xyz';

    '%aaa%' matches strings containing 'aaa';

    '___' matches strings containing exactly three characters; and

    'a_b%' matches strings beginning with 'a', followed by any single character, followed by 'b', followed by zero or more characters.


    SELECT name, price FROM products WHERE name LIKE 'PENCIL%';

    +-----------+-------+

    | name      | price |

    +-----------+-------+

    | Pencil 2B |  0.48 |

    | Pencil 2H |  0.49 |

    +-----------+-------+


    SELECT name, price FROM products WHERE name LIKE 'P__ %';

    +-----------+-------+

    | name      | price |

    +-----------+-------+

    | Pen Red   |  1.23 |

    | Pen Blue  |  1.25 |

    | Pen Black |  1.25 |

    +-----------+-------+


    5) Arithmetic Operators


    Operator Description

    + Addition

    - Subtraction

    * Multiplication

    / Division

    DIV Integer Division

    % Modulus (Remainder)



    6) Logical Operators - AND, OR, NOT, XOR


    - You can combine multiple conditions with boolean operators AND, OR, XOR. 

    You can also invert a condition using operator NOT. For examples,


    SELECT * FROM products WHERE quantity >= 5000 AND name LIKE 'Pen %';

    +-----------+-------------+----------+----------+-------+

    | productID | productCode | name     | quantity | price |

    +-----------+-------------+----------+----------+-------+

    |      1001 | PEN         | Pen Red  |     5000 |  1.23 |

    |      1002 | PEN         | Pen Blue |     8000 |  1.25 |

    +-----------+-------------+----------+----------+-------+


    SELECT * FROM products WHERE quantity >= 5000 AND price < 1.24 AND name LIKE 'Pen %';

    +-----------+-------------+---------+----------+-------+

    | productID | productCode | name    | quantity | price |

    +-----------+-------------+---------+----------+-------+

    |      1001 | PEN         | Pen Red |     5000 |  1.23 |

    +-----------+-------------+---------+----------+-------+


    SELECT * FROM products WHERE NOT (quantity >= 5000 AND name LIKE 'Pen %');

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    |      1004 | PEC         | Pencil 2B |    10000 |  0.48 |

    |      1005 | PEC         | Pencil 2H |     8000 |  0.49 |

    +-----------+-------------+-----------+----------+-------+


    7) IN, NOT IN


    - You can select from members of a set with IN (or NOT IN) operator. 

    This is easier and clearer than the equivalent AND-OR expression.


    SELECT * FROM products WHERE name IN ('Pen Red', 'Pen Black');

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1001 | PEN         | Pen Red   |     5000 |  1.23 |

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    +-----------+-------------+-----------+----------+-------+


    8) BETWEEN, NOT BETWEEN


    - To check if the value is within a range, you could use BETWEEN ... AND ... operator. 

    Again, this is easier and clearer than the equivalent AND-OR expression.


    SELECT * FROM products 

           WHERE (price BETWEEN 1.0 AND 2.0) AND (quantity BETWEEN 1000 AND 2000);

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    +-----------+-------------+-----------+----------+-------+


    9) IS NULL, IS NOT NULL


    SELECT * FROM products WHERE productCode IS NULL;

    Empty set


    SELECT * FROM products WHERE productCode = NULL;

    -- This is a common mistake. NULL cannot be compared.


    9) ORDER BY Clause


    SELECT * FROM southwind.products WHERE name LIKE 'Pen %' ORDER BY price DESC;

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1002 | PEN         | Pen Blue  |     8000 |  1.25 |

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    |      1001 | PEN         | Pen Red   |     5000 |  1.23 |

    +-----------+-------------+-----------+----------+-------+


    SELECT * FROM products WHERE name LIKE 'Pen %' ORDER BY price DESC, quantity;

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    |      1002 | PEN         | Pen Blue  |     8000 |  1.25 |

    |      1001 | PEN         | Pen Red   |     5000 |  1.23 |

    +-----------+-------------+-----------+----------+-------+


    - You can randomize the returned records via function RAND(), e.g.,

    SELECT * FROM products ORDER BY RAND();


    10) LIMIT Clause


    SELECT * FROM products ORDER BY price LIMIT 2;

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1004 | PEC         | Pencil 2B |    10000 |  0.48 |

    |      1005 | PEC         | Pencil 2H |     8000 |  0.49 |

    +-----------+-------------+-----------+----------+-------+


    - Skip the first two rows and display the next 1 row

    SELECT * FROM products ORDER BY price LIMIT 2, 1;

    +-----------+-------------+---------+----------+-------+

    | productID | productCode | name    | quantity | price |

    +-----------+-------------+---------+----------+-------+

    |      1001 | PEN         | Pen Red |     5000 |  1.23 |

    +-----------+-------------+---------+----------+-------+


    11) AS - Alias


    SELECT productID AS ID, productCode AS Code,

                  name AS Description, price AS `Unit Price`  -- Define aliases to be used as display names

           FROM products

           ORDER BY ID;  -- Use alias ID as reference

    +------+------+-------------+------------+

    | ID   | Code | Description | Unit Price |

    +------+------+-------------+------------+

    | 1001 | PEN  | Pen Red     |       1.23 |

    | 1002 | PEN  | Pen Blue    |       1.25 |

    | 1003 | PEN  | Pen Black   |       1.25 |

    | 1004 | PEC  | Pencil 2B   |       0.48 |

    | 1005 | PEC  | Pencil 2H   |       0.49 |

    +------+------+-------------+------------+


    12) Function CONCAT()


    - You can also concatenate a few columns as one (e.g., joining the last name and first name) using function CONCAT(). 

    For example,


    SELECT CONCAT(productCode, ' - ', name) AS `Product Description`, price FROM products;

    +---------------------+-------+

    | Product Description | price |

    +---------------------+-------+

    | PEN - Pen Red       |  1.23 |

    | PEN - Pen Blue      |  1.25 |

    | PEN - Pen Black     |  1.25 |

    | PEC - Pencil 2B     |  0.48 |

    | PEC - Pencil 2H     |  0.49 |

    +---------------------+-------+








    -----------------------  Producing Summary Reports   ------------------------------------


    1) DISTINCT


    - A column may have duplicate values, we could use keyword DISTINCT to select only distinct values. 

    We can also apply DISTINCT to several columns to select distinct combinations of these columns. For examples,


    SELECT price FROM products;

    +-------+

    | price |

    +-------+

    |  1.23 |

    |  1.25 |

    |  1.25 |

    |  0.48 |

    |  0.49 |

    +-------+


    SELECT DISTINCT price AS `Distinct Price` FROM products;

    +----------------+

    | Distinct Price |

    +----------------+

    |           1.23 |

    |           1.25 |

    |           0.48 |

    |           0.49 |

    +----------------+


    2) GROUP BY Clause

    - The GROUP BY clause allows you to collapse multiple records with a common value into groups. For example,


    SELECT * FROM products ORDER BY productCode, productID;

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1004 | PEC         | Pencil 2B |    10000 |  0.48 |

    |      1005 | PEC         | Pencil 2H |     8000 |  0.49 |

    |      1001 | PEN         | Pen Red   |     5000 |  1.23 |

    |      1002 | PEN         | Pen Blue  |     8000 |  1.25 |

    |      1003 | PEN         | Pen Black |     2000 |  1.25 |

    +-----------+-------------+-----------+----------+-------+


    SELECT * FROM products GROUP BY productCode;

           -- Only first record in each group is shown

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1004 | PEC         | Pencil 2B |    10000 |  0.48 |

    |      1001 | PEN         | Pen Red   |     5000 |  1.23 |

    +-----------+-------------+-----------+----------+-------+


    - GROUP BY by itself is not meaningful. 

    It is used together with GROUP BY aggregate functions (such as COUNT(), AVG(), SUM()) to produce group summary.


    3) GROUP BY Aggregate Functions: COUNT, MAX, MIN, AVG, SUM, STD, GROUP_CONCAT


    - Function COUNT(*) returns the number of rows selected


    SELECT COUNT(*) AS `Count` FROM products;

           -- All rows without GROUP BY clause

    +-------+

    | Count |

    +-------+

    |     5 |

    +-------+


    SELECT productCode, COUNT(*) FROM products GROUP BY productCode;

    +-------------+----------+

    | productCode | COUNT(*) |

    +-------------+----------+

    | PEC         |        2 |

    | PEN         |        3 |

    +-------------+----------+


    SELECT productCode, COUNT(*) AS count 

           FROM products 

           GROUP BY productCode

           ORDER BY count DESC;

    +-------------+-------+

    | productCode | count |

    +-------------+-------+

    | PEN         |     3 |

    | PEC         |     2 |

    +-------------+-------+


    - Besides COUNT(), there are many other GROUP BY aggregate functions such as AVG(), MAX(), MIN() and SUM(). For example,


    SELECT MAX(price), MIN(price), AVG(price), STD(price), SUM(quantity)

           FROM products;

           -- Without GROUP BY - All rows

    +------------+------------+------------+------------+---------------+

    | MAX(price) | MIN(price) | AVG(price) | STD(price) | SUM(quantity) |

    +------------+------------+------------+------------+---------------+

    |       1.25 |       0.48 |   0.940000 |   0.371591 |         33000 |

    +------------+------------+------------+------------+---------------+


    SELECT productCode, MAX(price) AS `Highest Price`, MIN(price) AS `Lowest Price`

           FROM products

           GROUP BY productCode;

    +-------------+---------------+--------------+

    | productCode | Highest Price | Lowest Price |

    +-------------+---------------+--------------+

    | PEC         |          0.49 |         0.48 |

    | PEN         |          1.25 |         1.23 |

    +-------------+---------------+--------------+


    SELECT productCode, MAX(price), MIN(price),

                  AVG(price) AS Average, 

          STD(price) AS `Std Dev`,

                  SUM(quantity)

           FROM products

           GROUP BY productCode;

    +-------------+------------+------------+-------------+-------------+---------------+

    | productCode | MAX(price) | MIN(price) | Average     | Std Dev     | SUM(quantity) |

    +-------------+------------+------------+-------------+-------------+---------------+

    | PEC         |       0.49 |       0.48 |    0.485000 |    0.005000 |         18000 |

    | PEN         |       1.25 |       1.23 |    1.243333 |    0.009428 |         15000 |

    +-------------+------------+------------+-------------+-------------+---------------+


    SELECT productCode, MAX(price), MIN(price),

                  CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`,

                  CAST(STD(price) AS DECIMAL(7,2)) AS `Std Dev`,

                  SUM(quantity)

           FROM products

           GROUP BY productCode;

    -- Use CAST(... AS ...) function to format floating-point numbers

    -- mysql cast() 함수는 type 을 변경하는 함수이다.

    +-------------+------------+------------+---------+---------+---------------+

    | productCode | MAX(price) | MIN(price) | Average | Std Dev | SUM(quantity) |

    +-------------+------------+------------+---------+---------+---------------+

    | PEC         |       0.49 |       0.48 |    0.49 |    0.01 |         18000 |

    | PEN         |       1.25 |       1.23 |    1.24 |    0.01 |         15000 |

    +-------------+------------+------------+---------+---------+---------------+


    4) HAVING clause ** group by 에서는 having 만 가능 where 은 안됨 !!


    - HAVING is similar to WHERE, but it can operate on the GROUP BY aggregate functions; 

    whereas WHERE operates only on columns.


    SELECT

              productCode AS `Product Code`,

              COUNT(*) AS `Count`,

              CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`

           FROM products 

           GROUP BY productCode

           HAVING Count >=3;

              -- CANNOT use WHERE count >= 3

    +--------------+-------+---------+

    | Product Code | Count | Average |

    +--------------+-------+---------+

    | PEN          |     3 |    1.24 |

    +--------------+-------+---------+


    5) WITH ROLLUP


    - The WITH ROLLUP clause shows the summary of group summary, e.g.,


    SELECT 

              productCode, 

              MAX(price), 

              MIN(price), 

              CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`,

              SUM(quantity)

           FROM products

           GROUP BY productCode

           WITH ROLLUP;        -- Apply aggregate functions to all groups

    +-------------+------------+------------+---------+---------------+

    | productCode | MAX(price) | MIN(price) | Average | SUM(quantity) |

    +-------------+------------+------------+---------+---------------+

    | PEC         |       0.49 |       0.48 |    0.49 |         18000 |

    | PEN         |       1.25 |       1.23 |    1.24 |         15000 |

    | NULL        |       1.25 |       0.48 |    0.94 |         33000 |

    +-------------+------------+------------+---------+---------------+








    -------------------------- UPDATE ----------------------------------


    -- To modify existing data, use UPDATE ... SET command, with the following syntax:

    UPDATE tableName SET columnName = {value|NULL|DEFAULT}, ... WHERE criteria


    -- Increase the price by 10% for all products

    UPDATE products SET price = price * 1.1;

       

    SELECT * FROM products;

    +-----------+-------------+-----------+----------+-------+

    | productID | productCode | name      | quantity | price |

    +-----------+-------------+-----------+----------+-------+

    |      1001 | PEN         | Pen Red   |     5000 |  1.35 |

    |      1002 | PEN         | Pen Blue  |     8000 |  1.38 |

    |      1003 | PEN         | Pen Black |     2000 |  1.38 |

    |      1004 | PEC         | Pencil 2B |    10000 |  0.53 |

    |      1005 | PEC         | Pencil 2H |     8000 |  0.54 |

    +-----------+-------------+-----------+----------+-------+


    -- Modify selected rows

    UPDATE products SET quantity = quantity - 100 WHERE name = 'Pen Red';

       

    SELECT * FROM products WHERE name = 'Pen Red';

    +-----------+-------------+---------+----------+-------+

    | productID | productCode | name    | quantity | price |

    +-----------+-------------+---------+----------+-------+

    |      1001 | PEN         | Pen Red |     4900 |  1.35 |

    +-----------+-------------+---------+----------+-------+


    -- You can modify more than one values

    UPDATE products SET quantity = quantity + 50, price = 1.23 WHERE name = 'Pen Red';

       

    SELECT * FROM products WHERE name = 'Pen Red';

    +-----------+-------------+---------+----------+-------+

    | productID | productCode | name    | quantity | price |

    +-----------+-------------+---------+----------+-------+

    |      1001 | PEN         | Pen Red |     4950 |  1.23 |

    +-----------+-------------+---------+----------+-------+


Designed by Tistory.