SELECT WHERE AND OR NOT IN BETWEEN LIKE REGEXP NULL
what
DataBase
is a collection of data stored in a format that can be easily be accessed
DBMS
DataBase Management System
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
Not Relational - NoSQL
NoSQL systems don’t underdstand SQL
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' -- > >= < <= = != <>
ANDORNOT
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'))
INNOT 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 NULLIS 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