[MySQL] Note: Row CURD

Peter Chang
HackerNoon.com
6 min readJan 8, 2017

--

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.

Content

  1. Create Row
  2. Query By Condition
  3. Query By Order
  4. Query By limi, offset
  5. Alias, AS
  6. Aggregate Function
  7. *Group By
  8. Update Row
  9. Delete Row
  10. Alert Column (add new column)
  11. *Foreign Key
  12. *Select with JOIN

To have a default Database before operation table inside a Database

1 — Create Row

Insert a row with full column values

Inserting NULL to the auto_increment column results in max_value + 1

To insert a row with values on Missing value or selected columns only

2 — Query By Condition

Show

Comparison Operators — =, <, >

Logical Operators — AND, OR, NOT, XOR

Contain Operators —IN, NOT IN

Between Operators —BETWEEN, NOT BETWEEN

IS NULL, IS NOT NULL

3 — Query By Order

Order in either ascending (ASC) (default) or descending (DESC) order.

4 — Query By limit, offset

5 — Alias, AS

6— Aggregate Functions

COUNT, MAX, MIN, AVG, SUM, STD, GROUP_CONCAT,

7— *Group By

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.

First Example

Multi-Columns

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

8— Update Row

Update All row

Update by Condition

Update Multi-columns

9 — Delete Row

Delete All rows

Delete by Condition

10 — Alert Column (add new column)

11— Create Foreign Key Column

To add a Foreign Key to supplierID columns of the products child table to the suppliers parent table :

  1. add supplierID column with INT type in product table
  2. set all the supplierID of the existing records
  3. set supplierID as a foreign key column, by related products table to parent suppliers table

12— *Select with JOIN

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.

  1. Default JOIN is INNER JOIN in MySQL
  2. set all the supplierID of the existing records
  3. set supplierID as a foreign key column, by related products table to parent suppliers table

You may also like

Very thankful to the community of NTU, who contribute such a good tutorial and valuable examples, which is used in this story as tutorial.

Reference:

https://www.cyberciti.biz/faq/mysql-list-databases/

https://www.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Beginner.html

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--