mysql

what I will learn?

  • Retrieve insert update delete
  • Tables Relationships Joins Subquires RegularExpressions
  • SELECT WHERE AND OR NOT IN BETWEEN LIKE REGEXP NULL

what

  1. DataBase
  • is a collection of data stored in a format that can be easily be accessed
  1. DBMS
  • DataBase Management System
  1. Relational
  • Customers - Orders - Products
  • Structured Query Language
    • e.g.
      1
      2
      3
      4
      SELECT *
      FROM prosucts
      WHERE category = 'food'
      ORDER BY price
  • RDBMS
    • MySQL
    • SQL Server
    • Oracle
  1. Not Relational - NoSQL
  • NoSQL systems don’t underdstand SQL
  1. SQL & SEQUEL
  • SQL:
    • Structured
    • Query
    • Languauge
  • SEQUEL:
    • Structured
    • English
    • Query
    • Languauge

Install MySQL and Workbench

Connect MySQL

mysql -u root -p

DataBase Operations

Create DataBase

CREATE DATABASE db1

Drop DataBase

DROP DATABASE db1

Choose DataBase to use

USE db1

DataBase Table Operations

  • DataBase Table Record Relationship row column
1
2
3
4
5
6
USE sql_store;

SELECT *
FROM customers
-- WHERE customer_id = 1
ORDER BY first_name

Create Table

1
2
3
4
5
6
7
8
CREATE TABLE `order_statuses` (
`order_status_id` tinyint(4) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`order_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `order_statuses` VALUES (1,'Processed');
INSERT INTO `order_statuses` VALUES (2,'Shipped');
INSERT INTO `order_statuses` VALUES (3,'Delivered');

Drop Table

DROP TABLE order_statuses

SELECT Clause

  • SELECT
1
2
3
4
5
6
7
SELECT
last_name,
first_name,
points,
(points+10)*100 AS 'discount factor'
FROM customers
ORDER BY points
  • DISTINCT:remove duplicate
1
2
SELECT DISTINCT state
FROM customers
  • WHERE: give a condition
1
2
3
4
5
SELECT *
FROM customers
-- WHERE points > 3000
WHERE state <> 'VA'
-- > >= < <= = != <>
  • AND OR NOT
1
2
3
4
SELECT *
FROM customers
-- WHERE birth_date >='1900-01-01' OR (points > 1000 AND state = 'VA')
WHERE NOT (birth_date < '1900-01-01' AND (points > 1000 AND state = 'VA'))
  • IN NOT IN
1
2
3
4
5
SELECT *
FROM customers
-- WHERE state = 'VA' OR state = 'FL' OR state = 'GA'
WHERE state IN ('VA','FL','GA')
-- WHERE state NOT IN ('VA','FL','GA')
  • BETWEEN...AND...
1
2
3
4
SELECT *
FROM products
-- WHERE quantity_in_stock >=50 AND quantity_in_stock <= 100
WHERE quantity_in_stock BETWEEN 50 AND 100
  • LIKE - % _ REGEXP
    • %:pattern any number of chars
    • _:pattern single char
1
2
3
4
5
6
7
8
9
10
SELECT *
FROM products
WHERE name LIKE 'P%'
-- WHERE name LIKE '%p%'
-- WHERE name LIKE '%y'
-- WHERE name LIKE '_y'
-- WHERE name REGEXP '^P'
-- WHERE name REGEXP 'e$'
-- WHERE name REGEXP 'e$|^P'
-- WHERE name REGEXP 'P[ogh]'
  • IS NULL IS NOT NULL
1
2
3
SELECT *
FROM orders
WHERE shipped_date IS NULL
  • ORDER BY
1
2
3
SELECT *
FROM order_items
ORDER BY quantity,unit_price DESC
1
2
3
SELECT quantity,unit_price
FROM order_items
ORDER BY 1,2 DESC
  • LIMIT
1
2
3
4
5
SELECT *
FROM products
ORDER BY quantity_in_stock DESC
LIMIT 0,3
-- will get first 3 record which stock most

-INNER JOIN:JOIN...ON... 读取匹配的数据

1
2
3
4
5
-- with one Database
SELECT order_id,o.customer_id,first_name,last_name
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
1
2
3
4
5
6
7
8
-- with two Database
USE sql_store;

SELECT order_id,oi.product_id,p.unit_price,p.name
FROM order_items oi
JOIN sql_inventory.products p
ON oi.product_id = p.product_id

1
2
3
4
5
6
7
-- with self table
USE sql_hr;

SELECT *
FROM employees e
JOIN employees m
ON e.reports_to = m.employee_id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- join multiple table
USE sql_store;

SELECT
o.order_id,
o.order_date,
c.first_name,
c.last_name,
os.order_status_id,
os.name
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
JOIN order_statuses os
ON o.status - os.order_status_id
- Outer Join:`LEFT JOIN...ON...`
  • Compound Join Conditions
1
2
3
4
5
6
7
USE sql_store;

SELECT *
FROM order_items oi
JOIN order_item_notes oin
ON oi.order_id = oin.order_Id
AND oi.product_id = oin.product_Id
  • Implpicit Join Syntax
1
2
3
SELECT *
FROM orders o,customers c
WHERE o.customer_id = c.customer_id
  • OUTER JOIN
    • INNER JOIN: 左连接 就是会读取左表的所有数据
    • OUTER JOIN:右连接,就是会读取右表的所有数据
1
2
3
4
5
6
7
8
9
USE sql_store;

SELECT
c.customer_id,
c.first_name,
o.order_id
FROM orders o
RIGHT JOIN customers c
ON c.customer_id = o.customer_id
  • CROSS JOIN
1
2
3
SELECT *
FROM orders o,customers c
-- o的每一条记录都会和c的每一条记录合并
1
2
3
SELECT *
FROM orders o
CROSS JOIN customers c
  • USING Clause
1
2
3
4
5
6
7
8
9
10
USE sql_store;

SELECT
c.customer_id,
c.first_name,
o.order_id
FROM orders o
RIGHT JOIN customers c
-- ON c.customer_id = o.customer_id
USING (customer_id)
1
2
3
4
5
SELECT *
FROM order_items oi
JOIN order_item_notes oin
-- ON oi.order_id = oin.order_id AND oi.product_id = oin.product_id
USING(order_id,product_id)
  • NATURAL JOIN:去掉
1
2
3
4
5
6
7
8
USE sql_store;

SELECT
c.customer_id,
c.first_name,
o.order_id
FROM orders o
NATURAL JOIN customers c
  • UNION:连接两个以上的 Select 结果组合到一个结果集合中,默认会删除重复记录,且结果列数必须一样,否则报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
USE sql_store;

SELECT
order_id,
order_date,
'Archived' AS status
FROM orders
WHERE order_date < '2019-01-01'

UNION
-- UNION ALL 会保留重复项

SELECT
order_id,
order_date,
'Active' AS status
FROM orders
WHERE order_date >= '2019-01-01'

INSERT INTO...VALUES...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
INSERT INTO customers(
first_name,
last_name,
birth_date,
address,
ciry,
state
)
VALUES (
'Join',
'Smith',
'1990-01-01'
'address',
'city',
'CA'
)
  • Inserting Multiple Rows
1
2
3
4
5
6
USE sql_store;

INSERT INTO shippers (name)
VALUES ('Shipper1'),
('Shipper2'),
('Shihpper3')
  • Insert Hierarchical Rows

Column Attributes

  1. VARCHAR
  2. INT
  3. DATE

Create a copy to a table

1
2
3
4
5
USE sql_store;

CREATE TABLE orders_archived AS
SELECT * FROM orders
-- 主键消失了 得额外加主键

Update a single row

1
2
3
4
UPDATE invoices
SET payment_total = 10,
payment_date='2019-03-11'
WHERE invoice_id = 3

Update multiple rows

1
2
3
4
UPDATE invoices
SET payment_total = 10,
payment_date='2019-03-11'
WHERE invoice_id IN (3,4)

Using Subqueries in Updates

1
2
3
4
5
6
7
8
UPDATE invoices
SET payment_total = 10,
payment_date='2019-03-11'
WHERE client_id =
(SELECT client_id
FROM clients
WHERE name='Vinte'
)

DELETE ROWS

1
2
DELETE FROM invoices
WHERE invoice_id=1

Restore the database

archived your sql file and reimport your sql file

MySQL Function

IFNULL

  • SELECT IFNULL(var1,var2) : 如果 var1 不为空,返回 var1,否则返回 var2
1
2
3
4
5
6
7
SELECT
(SELECT DISTINCT
Salary
FROM
Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1) AS SecondHighestSalary;

Reference

https://programmingwithmosh.com/wp-content/uploads/2019/03/SQL-Cheat-Sheet.pdf

声明:本站所有内容仅供个人学习娱乐笔记所用,如涉侵权,请联系删除

Master of SQL

Database design

Security

Writing complex quires

Transactions

Events