First Database Table

Create a simple example

First Table

  1. This will be a table that really should be split into many tables, this is just for getting used to creating tables and doing simple queries
  2. Table Name: products
  3. Fields
    • product_id
    • product_name
    • product_desc
    • product_price (may not belong in here?)
    • product_category (really does not belong in here)
    • product_quantity (may also not belong here)
  4. Design Table
    • which storage engine to use (for MySQL use InnoDB, but there are others)
    • storage engine determines what features exist for the table (FKs, triggers etc)
    • Default Character Set (utf8 and collation utf8_general_ci or utf8_bin, utf8mb4 and utf8mb4_general_ci)
    • MySQL Character Sets
    • For Full utf8 Support you need to use utf8mb4 and utf8mb4_unicode_ci
      • utf8mb4 Character Set Mysql.com
      • comparision of utf8 character sets and why to use utf8mb4_unicode_ci
      • How to support full Unicode in MySQL Databases
      • MySQL UTF8 Character Sets and Collations Explained

        /* specify WHICH database to use */
        /* Obviously, put in YOUR database name, which you can find with */
        SHOW DATABASES;
        
        use STUusername;
        
        CREATE TABLE zproducts(
        product_id INT UNSIGNED PRIMARY KEY auto_increment,
        prod_name varchar(30),
        prod_desc varchar(255),
        price decimal(10,2),
        prod_cat varchar(20),
        quantity INT,
        SKU INT UNSIGNED,
        UNIQUE KEY(SKU)
        ) engine=InnoDB 
        Default charset utf8mb4 
        collate=utf8mb4_unicode_ci;
        
        /* Lists tables in the present database */
        show tables;
        
        /* Shows fields and field types for a table */
        describe zproducts;
        
        /* Gives the SQL code to create the table
        It will be one complete statement,
        even if you have used a mix of CREATE,
        ALTER and so onto originally create the table
        
        Also includes a AUTO_INCREMENT=7 
         which specifies the PK value of the 
         next insert if you use auto_increment */
        
        show create table zproducts;
        

zproducts table description

To Do an Insert

zproducts list of records

Simple SELECT queries


    select * from zproducts;

    select prod_name,quantity,price from zproducts;

    select prod_name,quantity,price,
    quantity*price as Total 
    from zproducts;

    select sum(quantity*price) as Total 
    from zproducts;

UPDATE Example

    UPDATE zproducts 
    set prod_desc="An Electricians Friend"
    WHERE product_id=4;

    # *WARNING* if you forget the where clause
    # you change the entire table

    # Having to know the product_id is not very human friendly so a better update would be:
    UPDATE zproducts 
    set prod_desc="An electrician's best friend" 
    WHERE prod_name = "electrical tape";

    # Oddly, there is a limitation on using subqueries (a query within a query) with updates and deletes, you can not use a table in the subquery, that is the one being updated, so this fails
    # Note, it is really overkill to use a subquery here regardless

    UPDATE zproducts
    set prod_desc="An Electricians Friend"
    WHERE product_id = 
    (SELECT a.product_id FROM zproducts as a where a.prod_name = "electrical tape");

    # Now, if you nest that table in yet another query, you can pull it off, but it is very ugly
    # and each time you use "zproducts" you have to give it an alias (as a, as b) which loads
    # another copy into memory. Really inefficient
    UPDATE zproducts
    set prod_desc="An Electricians Friend"
    WHERE product_id = 
    (select PID FROM (SELECT a.product_id as PID FROM zproducts as a 
                      where a.prod_name = "electrical tape") as b);

    # The cleaner way to do this, would be to create a function like "getprodid()" that takes the
    # name and returns a product_id, but the simple where clause using the "prod_name" is the way to go

SELECTS WITH WHERE FILTERS

    SELECT prod_name,quantity,price
    FROM zproducts
    WHERE price < 20;

    SELECT prod_name,quantity,price
    FROM zproducts
    WHERE price like "%97";

DELETE Examples

Deleting multiple records at once

Using the IN() function

    # You can use the IN()  operator to match multiple values

    DELETE FROM zproducts where product_id IN (5,6,7);   # similar to using a series of "OR" statements

USING BETWEEN AND

    #  You can also do a range using  BETWEEN  AND

    DELETE FROM zproducts where product_id BETWEEN 5 AND 10;  # this includes the end points, so 5,6,7,8,9,10 will get deleted
    # But those will only be deleted if they EXIST obviously, otherwise values that don't match in the range are ignored

USING WHERE

    DELETE FROM zproducts where product_id >= 5 and product_id <=10;  # same effect as the BETWEEN 5 AND 10

Page last updated on 20230829

HTML 5 Validation CSS 3 Validation Web Accessibility EValuator