From 73d57cc8d06712feb4900e9d54501cffcd7316ef Mon Sep 17 00:00:00 2001 From: yikth Date: Thu, 3 Sep 2020 20:15:00 +0800 Subject: [PATCH] Day 1 Material --- README.md | 323 ++++++++++++++++++++++++++++++++++++++++++ files/sample-data.sql | 173 ++++++++++++++++++++++ 2 files changed, 496 insertions(+) create mode 100644 README.md create mode 100644 files/sample-data.sql diff --git a/README.md b/README.md new file mode 100644 index 0000000..33d6148 --- /dev/null +++ b/README.md @@ -0,0 +1,323 @@ +# Database + +## Tools + +* mySQL + +## MySQL Datatypes + +Ty p e | S i z e | D e s c r i p t i o n +-----|-----|--- +CHAR[Length] | Length bytes | A fixed-length field from 0 to 255 characters long. +VARCHAR(Length) | String length + 1 bytes | A fixed-length field from 0 to 255 characters long. +TINYTEXT | String length + 1 bytes | A string with a maximum length of 255 characters. +TEXT | String length + 2 bytes | A string with a maximum length of 65,535 characters. +MEDIUMTEXT | String length + 3 bytes | A string with a maximum length of 16,777,215 characters. +LONGTEXT | String length + 4 bytes | A string with a maximum length of 4,294,967,295 characters. +TINYINT[Length] | 1 byte | Range of -128 to 127 or 0 to 255 unsigned. +SMALLINT[Length] | 2 bytes | Range of -32,768 to 32,767 or 0 to 65535 unsigned. +MEDIUMINT[Length] | 3 bytes | Range of -8,388,608 to 8,388,607 or 0 to 16,777,215 unsigned. +INT[Length] | 4 bytes | Range of -2,147,483,648 to 2,147,483,647 or 0 to 4,294,967,295 unsigned. +BIGINT[Length] | 8 bytes | Range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 or 0 to 18,446,744,073,709,551,615 unsigned. +FLOAT | 4 bytes | A small number with a floating decimal point. +DOUBLE[Length, Decimals] | 8 bytes | A large number with a floating decimal point. +DECIMAL[Length, Decimals] | Length + 1 or Length + 2 bytes | A DOUBLE stored as a string, allowing for a fixed decimal point. +DATE | 3 bytes | In the format of YYYY-MM-DD. +DATETIME | 8 bytes | In the format of YYYY-MM-DD HH:MM:SS. +TIMESTAMP | 4 bytes | In the format of YYYYMMDDHHMMSS; acceptable range ends inthe year 2037. +TIME | 3 bytes | In the format of HH:MM:SS +ENUM | 1 or 2 bytes | Short for enumeration, which means that each column can haveone of several possible values. +SET | 1, 2, 3, 4, or 8 bytes | Like ENUM except that each column can have more than one ofseveral possible values. + + +## SQL Client + +* Part 1 + +```sql +mysql> show databases; + +mysql> create database test_db; + +mysql> show databases; + +mysql> use test_db; + +mysql> show tables; + +mysql> create table users ( + user_id INT, + name VARCHAR(50), + dob DATE, + mobile_number INT, + ic_number INT +); + +// show table fields design +mysql> describe users; + +// add new column +mysql> alter table users add verify INT; + +// delete column +mysql> alter table users drop column verify; + +// modify field type +mysql> alter table users modify column verify varchar(100); + +// insert record into table +mysql> insert into users (user_id, name, mobile_number) + values (123, 'dixant', 9988222); + +// dob in yyyy-mm-dd +mysql> insert into users (user_id, name, mobile_number, dob) + values (123, 'dixant', 9988222, '1975-12-01'); + +// add multiple records +mysql> insert into users (user_id, name, mobile_number) + values (125, 'john', 99882221), (126, 'harry', 99882223); + +// view table records +mysql> select * FROM users + +//update a record +mysql> update users set dob = '1976-01-01' where user_id = 123; + +// delete record +mysql> delete from users where dob > '1976-01-01'; + +// create new database +mysql> create database ntuc; + +mysql> use ntuc; + +// create grocery table +mysql> create table grocery ( + tag INT, + name VARCHAR(100), + brand VARCHAR(100), + price DECIMAL(10,2), + quantity INT +); + +// rename table +mysql> alter table grocery rename to store; + +``` + +* Part 2 + +```sql +// open database +mysql> use ntuc; + +// import script from .sql file +mysql> source C:\...\data.sql + +mysql> show tables; + +// queries filter order by ascending order +mysql> select * from customer where customer_wallet > 5000 order by customer_wallet order by asc; + +// order by wallet then tolerance +mysql> select * from customer where customer_wallet > 5000 order by customer_wallet desc, customer_tolerance order by asc; + +// only display name& email order by wallet then tolerance +mysql> select customer_name, customer_email from customer where customer_wallet > 5000 order by customer_wallet desc, customer_tolerance order by asc; + + +/// nested queries select within select +mysql> select customer_name from + (select * from customer where customer_wallet > 3000) as temp + where customer_tolerance > 0.5; + +// filter using IN clause +mysql> select * from customer where customer_tolerance IN (0.5, 0.6, 0.7); + +mysql> select * from customer where custome tolerance + IN (select customer_tolerance where customer_tolerance < 0.8); + +// ans for exercise +mysql> select * from transaction where customer_id + in (select customer_id from customer where customer_tolerance < 0.7); +/// + +``` + +* common functions + * COUNT + * SUM + * AVG + * MAX + * MIN + +```sql +// sum +mysql> select SUM(customer_wallet) from customer; + +// average +mysql> select AVG(customer_wallet) from customer; + +/// number of records +mysql> select COUNT(customer_wallet) from customer; + + +``` + +* Others functions + * CURRENT_DATE + * [Complete List](https://www.w3schools.com/sql/sql_ref_mysql.asp) + +* `Group By` Clause + +```sql +// group and sum by type.. +mysql> select customer_type, sum(customer_wallet) from customer group by customer_type; + +// exclude type 2 +mysql> select customer_type, sum(customer_wallet) from customer + where customer_type <> 2 + group by customer_type; + +``` + +* `LIMIT` Clause + * limit the number of record to return + +```sql +// show onlt first 5 records +mysql> select * from customer limit 5; + +``` + +* `LIKE` Clause + * pattern match + * wildcard + + `%` : substitute any number of character + + `_` : substitute only one character + +```sql +// show name start with Z +mysql> select * from customer where customer_name LIKE 'Z%'; + +``` + +* `UNION` Clause + * combine data from different table + * performance optimisation + +```sql +mysql> select customer_id from customer + UNION + select transaction_id from transaction; + +mysql> select seller_name as name from seller + UNION select product_name from product; + +// in comparison with OR, UNION is better performance +mysql> select customer_name from customer where customer_wallet = 6000 UNION + select customer_name from customer where customer_wallet = 8000; +``` + +* `INNER JOIN` clause + * aggregates data from different tables + +```sql +// join [transaction] and [customer] table +mysql> SELECT transaction.transaction_id, transaction.transaction_datetime, customer.customer_name + FROM transaction + INNER JOIN customer + ON customer.customer_id = transaction.customer_id; + +// alias AS to simplify +mysql> SELECT t.transaction_id, t.transaction_datetime, c.customer_name + FROM transaction AS t + INNER JOIN customer AS c + ON c.customer_id = t.customer_id; +``` +* `LEFT JOIN` clause + * return data from first table + +```sql +// alias AS to simplify +mysql> SELECT t.transaction_id, t.transaction_datetime, c.customer_name + FROM transaction AS t + LEFT JOIN customer AS c + ON c.customer_id = t.customer_id + GROUP BY c.customer_id; + +mysql> SELECT c.customer_name, t.transaction_id, t.transaction_datetime + FROM customer AS c + LEFT JOIN transaction AS t + ON c.customer_id = t.customer_id + GROUP BY c.customer_id; +``` + +* `RIGHT JOIN` clause + * return data from first table +```sql +mysql> SELECT t.transaction_id, t.transaction_datetime, c.customer_name + FROM transaction AS t + RIGHT JOIN customer AS c + ON c.customer_id = t.customer_id; + +``` + +* `OUTER JOIN` clause + * 3 types + + LEFT OUTER JOIN + + RIGHT OUTER JOIN + + FULL OUTER JOIN + +* INDEX clause + * speed up search queries +```sql +mysql> CREATE INDEX + ON ; + +``` + +* `PRIMARY KEY` clause + * constraint to force unique values + +```sql +mysql> CREATE TABLE table1( + COL1 INT + COL2 INT) + PRIMARY KEY (COL1); +``` + +* `FOREIGN KEY` clause + * + +```sql +mysql> ALTER TABLE table2 + ADD FOREIGN KEY (COL2) REFERENCE table1(COL2); +``` + +* Practical Exercise #4 + +```sql +// Q1 +mysql> SELECT t.transaction_id, t.transaction_datetime, c.customer_name + FROM transaction AS t + INNER JOIN customer AS c + ON c.customer_id = t.customer_id; + +// Q2 +mysql> select customer_id, max(transaction_datetime) from transaction group by customer_id; + +// Q3 +mysql> select seller_id, avg(transaction_amount) from transaction group by seller_id; + +mysql> select s.seller_id, s.seller_name, avg(t.transaction_amount) FROM seller AS s + RIGHT JOIN transaction AS t + ON s.seller_id = t.seller_id + GROUP BY s.seller_id; + +``` + +PE #4 Answers +1. select c.customer_name, t.transaction_datetime from customer as c left join transaction as t on c.customer_id = t.customer_id; +2. select c.customer_name, max(t.transaction_datetime) from customer as c left join transaction as t on c.customer_id = t.customer_id group by c.customer_id; +3. select s.seller_name, avg(t.transaction_amount) from seller as s left join transaction as t on s.seller_id = t.seller_id group by s.seller_id; diff --git a/files/sample-data.sql b/files/sample-data.sql new file mode 100644 index 0000000..d591b1c --- /dev/null +++ b/files/sample-data.sql @@ -0,0 +1,173 @@ +-- MySQL dump 10.13 Distrib 8.0.19, for osx10.15 (x86_64) +-- +-- Host: 127.0.0.1 Database: test +-- ------------------------------------------------------ +-- Server version 8.0.19 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!50503 SET NAMES utf8mb4 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `customer` +-- + +DROP TABLE IF EXISTS `customer`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `customer` ( + `customer_id` int NOT NULL AUTO_INCREMENT, + `customer_type` tinyint DEFAULT '0', + `customer_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, + `customer_email` varchar(255) DEFAULT NULL, + `customer_wallet` double(11,0) NOT NULL, + `customer_tolerance` double(11,2) NOT NULL, + PRIMARY KEY (`customer_id`) +) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `customer` +-- + +LOCK TABLES `customer` WRITE; +/*!40000 ALTER TABLE `customer` DISABLE KEYS */; +INSERT INTO `customer` VALUES (1,0,'Simone','knsejnf@gmail.com',8000,0.80),(2,1,'Betsy','sdbsakd@gmail.com',10000,0.60),(3,0,'Matthew','dfnsd@gmail.com',9000,0.80),(4,1,'Lisa','skadsla@gmail.com',8000,0.80),(5,1,'Jimmy','bk34d34@gmail.com',8000,0.50),(6,0,'Ximeng','lmkefnew@gmail.com',6000,0.80),(7,1,'Laoxi','mnljewe@gmail.com',6000,0.50),(8,0,'Xiaoxing','asdsa@gmail.com',6000,0.80),(9,1,'Xiaoqi','mkrntc@gmail.com',6000,0.50),(10,0,'Meiqi','msnrgjfn@gmail.com',8000,0.70),(11,0,'Zixuan','kmfehfe@gmail.com',8000,0.70),(12,0,'Xuanyi','knfiurb4r@gmail.com',6000,0.80),(13,1,'Laoxi','mndff473@gmail.com',6000,0.50),(14,0,'Xiaoxing','lknfo4rh@gmail.com',6000,0.80),(15,1,'Xiaoqi','kewnfo4@gmail.com',6000,0.50),(16,0,'Meiqi','i12ednd@gmail.com',8000,0.70),(17,0,'Zixuan','ionqwh3@gmail.com',8000,0.70),(18,0,'Ximeng','kekln44@gmail.com',6000,0.80),(19,1,'Laoxi','lmsxn21@gmail.com',6000,0.50),(20,0,'Xiaoxing','asd233@gmail.com',6000,0.80),(21,2,'Fujing','xvebr4u@gmail.com',6000,0.50),(22,2,'Yammy','nsdknfl@gmail.com',8000,0.50),(23,2,'Dajuan','mxwdnwvd@gmail.com',8000,0.50),(24,2,'Sunnee','nxsasg@gmail.com',6000,0.50),(25,2,'Chaoyue','lmd3nd@gmail.com',6000,0.50),(26,2,'Shanzhi','2wssbahsd@gmail.com',6000,0.50),(27,2,'Zining','sascsax@gmail.com',6000,0.50),(28,2,'Ziting','vaxayew@gmail.com',8000,0.50),(29,2,'Zixuan','bshsdwd@gmail.com',8000,0.50); +/*!40000 ALTER TABLE `customer` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `product` +-- + +DROP TABLE IF EXISTS `product`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `product` ( + `product_id` int NOT NULL AUTO_INCREMENT, + `product_name` varchar(255) DEFAULT NULL, + `product_market_price` double DEFAULT NULL, + PRIMARY KEY (`product_id`) +) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `product` +-- + +LOCK TABLES `product` WRITE; +/*!40000 ALTER TABLE `product` DISABLE KEYS */; +INSERT INTO `product` VALUES (1,'iPhone XS',500),(2,'iPhone XR',600),(3,'Google pixel',500),(4,'Huawei P30 pro',300),(5,'iPhone XS case',10),(6,'iPhone XR case',10),(7,'Google pixel case',15),(8,'Huawei P30 pro case',20); +/*!40000 ALTER TABLE `product` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `seller` +-- + +DROP TABLE IF EXISTS `seller`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `seller` ( + `seller_id` int NOT NULL AUTO_INCREMENT, + `seller_name` varchar(255) DEFAULT NULL, + `seller_wallet` double(11,2) DEFAULT '0.00', + PRIMARY KEY (`seller_id`) +) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `seller` +-- + +LOCK TABLES `seller` WRITE; +/*!40000 ALTER TABLE `seller` DISABLE KEYS */; +INSERT INTO `seller` VALUES (1,'Alice',143600.00),(2,'Bob',29350.00),(3,'Carol',113500.00),(4,'Dave',55900.00),(5,'Eve',49950.00),(6,'Farah',10000.00); +/*!40000 ALTER TABLE `seller` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `stock` +-- + +DROP TABLE IF EXISTS `stock`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `stock` ( + `id` int NOT NULL AUTO_INCREMENT, + `product_id` int NOT NULL, + `product_quality` double(11,2) DEFAULT NULL, + `seller_id` int NOT NULL, + `stock_quantity` int DEFAULT '0', + `stock_cost` double DEFAULT '0', + `stock_price` double DEFAULT '0', + PRIMARY KEY (`id`), + KEY `product_id` (`product_id`), + KEY `seller_id` (`seller_id`), + CONSTRAINT `stock_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`), + CONSTRAINT `stock_ibfk_2` FOREIGN KEY (`seller_id`) REFERENCES `seller` (`seller_id`) +) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `stock` +-- + +LOCK TABLES `stock` WRITE; +/*!40000 ALTER TABLE `stock` DISABLE KEYS */; +INSERT INTO `stock` VALUES (1,1,0.80,1,0,30,500),(2,2,0.70,2,7,20,330),(3,3,0.80,4,50,60,1000),(4,4,0.60,3,38,60,810),(5,5,0.50,5,47,60,1000),(6,6,0.80,1,33,70,900),(7,7,0.70,2,10,60,1000),(8,8,0.80,4,42,60,1000); +/*!40000 ALTER TABLE `stock` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `transaction` +-- + +DROP TABLE IF EXISTS `transaction`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `transaction` ( + `transaction_id` int NOT NULL AUTO_INCREMENT, + `transaction_datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `seller_id` int NOT NULL, + `customer_id` int NOT NULL, + `product_id` int NOT NULL, + `transaction_quantity` int DEFAULT '0', + `transaction_amount` double DEFAULT '0', + PRIMARY KEY (`transaction_id`), + KEY `seller_id` (`seller_id`), + KEY `customer_id` (`customer_id`), + KEY `product_id` (`product_id`), + CONSTRAINT `transaction_ibfk_1` FOREIGN KEY (`seller_id`) REFERENCES `seller` (`seller_id`), + CONSTRAINT `transaction_ibfk_2` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`), + CONSTRAINT `transaction_ibfk_3` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) +) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `transaction` +-- + +LOCK TABLES `transaction` WRITE; +/*!40000 ALTER TABLE `transaction` DISABLE KEYS */; +INSERT INTO `transaction` VALUES (1,'2019-03-08 20:16:00',3,2,2,1,480),(2,'2019-04-11 12:48:57',4,1,3,1,400),(3,'2019-04-11 12:48:57',4,1,7,1,80),(4,'2019-05-16 22:57:07',2,1,5,2,200),(5,'2019-07-17 16:23:27',5,3,4,1,300),(6,'2019-07-18 08:59:37',5,3,8,2,160),(7,'2019-09-01 21:00:52',1,2,6,1,95),(8,'2019-11-30 13:58:52',1,1,6,1,1000),(9,'2019-11-30 13:58:52',3,7,4,2,1950),(10,'2019-11-30 13:58:52',4,10,8,1,1000),(11,'2019-11-30 13:58:52',2,5,7,2,1950),(12,'2019-11-30 13:58:52',1,4,1,3,1500),(13,'2019-11-30 13:58:52',3,2,4,3,2800),(14,'2019-11-30 13:58:52',5,25,5,1,1000),(15,'2019-11-30 13:58:52',1,6,1,3,1500),(16,'2019-11-30 13:58:52',1,11,6,3,2950),(17,'2019-11-30 13:58:52',5,19,5,2,1950),(18,'2019-11-30 13:58:52',2,29,7,1,1000),(19,'2019-11-30 13:58:52',2,13,7,4,3950),(20,'2019-11-30 13:58:52',1,24,6,2,1950),(21,'2019-11-30 13:58:52',1,12,1,3,1500),(22,'2019-11-30 13:58:52',4,21,8,3,2950),(23,'2019-11-30 13:58:52',4,8,8,3,2950),(24,'2019-11-30 13:58:52',1,9,1,5,2450),(25,'2019-11-30 13:58:52',3,15,4,5,4950),(26,'2019-11-30 13:58:52',2,28,7,3,2950),(27,'2019-11-30 13:58:52',3,22,4,2,1950),(28,'2019-11-30 13:58:52',1,20,1,1,500),(29,'2019-11-30 13:58:52',4,23,8,1,1000),(30,'2019-11-30 13:58:52',1,17,1,5,2450),(31,'2019-11-30 13:58:52',1,18,6,1,1000),(32,'2019-11-30 13:58:52',2,16,2,3,900),(33,'2019-11-30 13:58:52',1,3,6,4,3950),(34,'2019-11-30 13:58:52',1,25,6,1,1000),(35,'2019-11-30 13:58:52',1,14,6,5,4950); +/*!40000 ALTER TABLE `transaction` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2020-07-21 17:40:55