pixabay.com
Morden APPs means: 90% CRUD, user interface, and reports, and 10% complex code evaluation for a dozen languages.
There is 2.31 billion social media users over the world, we all love listening and sharing, we love platform. The fact is 90% of operation happens in platform is about DB/Cache CRUD, this story has collected 12 most comment used MySQL ROW operation, to help understanding and finding all we need about ROW.
mysql> **SHOW DATABASES;**mysql> **USE yourdatabse;**mysql> CREATE TABLE IF NOT EXISTS products (productID INT UNSIGNED NOT NULL AUTO_INCREMENT,productCode CHAR(3) NOT NULL DEFAULT '',name VARCHAR(30) NOT NULL DEFAULT '',quantity INT UNSIGNED NOT NULL DEFAULT 0,price DECIMAL(7,2) NOT NULL DEFAULT 99999.99,quantity INT UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY (productID));
Insert a row with full column values
mysql> **INSERT INTO products VALUES (1001, 'PEN', 'Pen Red', 5000, 1.23);**Query OK, 1 row affected (0.00 sec)
Inserting NULL to the auto_increment column results in max_value + 1
mysql> **INSERT INTO products VALUES (NULL, 'PEN', 'Pen Red', 5000, 1.23);**Query OK, 1 row affected (0.00 sec)
To insert a row with values on Missing value or selected columns only
mysql> **INSERT INTO products (productCode, name, quantity, price) VALUES ('PEC', 'Pencil 2B', 10000, 0.48),;**Query OK, 1 row affected (0.00 sec)
Show
List all the rows of the **_specified columns_**mysql> SELECT * FROM products;
List all the rows of the **_specified columns_**mysql> SELECT productID, name FROM products
mysql> SELECT * FROM products WHERE price >1;
mysql> SELECT * FROM products WHERE productCode = 'PEN';
mysql> SELECT * FROM products WHERE productCode = 'PEN' AND price >1;
mysql> SELECT * FROM products WHERE name IN ('Pen Red', 'Pen Black');
mysql> SELECT * FROM products WHERE name IN ('Pen Red', 'Pen Black');
mysql> SELECT * FROM products WHERE productCode IS NULL;
Order in either ascending (ASC) (default) or descending (DESC) order.
mysql> SELECT * FROM products WHERE price >1 ORDER BY price DESC;
mysql> SELECT * FROM products WHERE price >1 ORDER BY price DESC, quantity;
mysql> SELECT * FROM products LIMIT 2,1;//limit =2//skip =1
mysql> SELECT productID AS ID, price AS `Unit Price`
mysql> SELECT * FROM products ORDER BY ID;
COUNT, MAX, MIN, AVG, SUM, STD, GROUP_CONCAT,
mysql> SELECT COUNT(*) AS `Count` FROM products**;**
+-------+| Count |+-------+| 5 |+-------+
GROUP BY is a very cool weapon in generating organised result and sort out data from entangle columns.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.
mysql> SELECT name, ANY_VALUE(price) FROM products GROUP BY name;
mysql> SELECT productCode, MAX(price) AS `Highest Price`, MIN(price) AS `Lowest Price`FROM productsGROUP BY productCode;
+-------------+---------------+--------------+| productCode | Highest Price | Lowest Price |+-------------+---------------+--------------+| PEC | 0.49 | 0.48 || PEN | 1.25 | 1.23 |+-------------+---------------+--------------+
mysql> 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 productsGROUP BY productCode;
mysql> UPDATE products SET price = price * 1.1;
mysql> UPDATE products SET quantity = quantity - 100 WHERE name = 'Pen Red';
mysql> UPDATE products SET quantity = quantity + 50, price = 1.23 WHERE name = 'Pen Red';
mysql> DELETE FROM products;
mysql> DELETE FROM products WHERE name = 'Pen Red';
mysql> ALTER TABLE productsADD COLUMN supplierID INT UNSIGNED NOT NULL;
To add a Foreign Key to supplierID
columns of the products
child table to the suppliers
parent table :
mysql> ALTER TABLE productsADD COLUMN supplierID INT UNSIGNED NOT NULL;
mysql> UPDATE products SET supplierID = 501;
mysql> ALTER TABLE productsADD FOREIGN KEY (supplierID) REFERENCES suppliers (supplierID);
Magic JOIN(foreign key) is my favourite part of MySQL. As I have been using MongoDB for long, its relational query has problem in blood. There was one time, we has having a big refactor project which include schema refactor, one of the biggest pain is turning 1–1 relation to 1–n or n-n relation, it is so much time consuming.
I love foreign key and select, which make n-n relation as the best thing since sliced bread. Combo JION-table1-table2-result multi complicated hit by just one SQL query.
JOIN is 1 click with 30 HIT
SQL joins are used to combine rows from two or more tables. Default JOIN is INNER JOIN in MySQL, which given keywords then selects all rows from both tables as long as there is a match between the columns in both tables.
mysql> SELECT products.name, price, suppliers.nameFROM productsJOIN suppliers ON products.supplierID = suppliers.supplierIDWHERE price < 5;
Very thankful to the community of NTU, who contribute such a good tutorial and valuable examples, which is used in this story as tutorial.
https://www.cyberciti.biz/faq/mysql-list-databases/
https://www.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Beginner.html