vietnamese-engineer-in-japan / workshop

All information related to workshop
3 stars 0 forks source link

MySQL High Performance - Sunday, 2019/06/09 #4

Open chunvv opened 5 years ago

chunvv commented 5 years ago

MySQL High-Performance Workshop on 09 June 2019

chunvv commented 5 years ago

Rules:

chunvv commented 5 years ago

Basic knowledge (Kiểm tra bài cũ):

  1. What is MySQL CONSTRAINTs, list up all constraints?
* NOT NULL
* UNIQUE
* PRIMARY KEY
* FOREIGN KEY
* CHECK
* DEFAULTs

Examples for check:

CREATE TABLE IF NOT EXISTS 
newbook_mast (book_id varchar(15) NOT NULL UNIQUE,          
book_name varchar(50)  ,           
isbn_no varchar(15)  NOT NULL UNIQUE  ,           
cate_id varchar(8)  ,             
aut_id varchar(8) ,             
pub_id varchar(8) ,            
dt_of_pub date ,             
pub_lang varchar(15) ,           
no_page decimal(5,0) 
CHECK(no_page>0) ,            
book_price decimal(8,2) ,             
PRIMARY KEY (book_id)               
);
CREATE TABLE IF NOT EXISTS
newauthor(aut_id varchar(8) NOT NULL , 
aut_name varchar(50) NOT NULL,
country varchar(25) NOT NULL CHECK (country IN ('USA','UK','India')), 
home_city varchar(25) NOT NULL, 
PRIMARY KEY (aut_id,home_city));

chunvv commented 5 years ago
  1. LIKE Operator: What is the meaning of each following queries:

WHERE CustomerName LIKE 'a%' WHERE CustomerName LIKE '%a' WHERE CustomerName LIKE '%or%' WHERE CustomerName LIKE '_r%' WHERE CustomerName LIKE 'a__%' WHERE ContactName LIKE 'a%o'

chunvv commented 5 years ago
  1. What is the differences between union, union all, minus, intersect?
```sql SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City; ``` ```sql ELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER BY City; ```
chunvv commented 5 years ago
  1. Enum and Set: what are the differences?
    • What happens if we insert a value does not exists in Enum?
```sql CREATE TABLE setTest( attrib SET('bold','italic','underline') ); INSERT INTO setTest (attrib) VALUES ('bold'); INSERT INTO setTest (attrib) VALUES ('bold,italic'); INSERT INTO setTest (attrib) VALUES ('bold,italic,underline'); # Check again here ``` ```sql CREATE TABLE enumTest( color ENUM('red','green','blue') ); INSERT INTO enumTest (color) VALUES ('red'); INSERT INTO enumTest (color) VALUES ('gray'); INSERT INTO enumTest (color) VALUES ('red,green'); # Check again here ```
chunvv commented 5 years ago
  1. What is: AUTO_INCREMENT, PRIMARY KEY and SERIAL?
    SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
chunvv commented 5 years ago
  1. MySQL join types?

=> Check one again here

Screen Shot 2019-06-08 at 22 50 37
chunvv commented 5 years ago
  1. What is Triggers?
A trigger is a set of actions that are run automatically when a specified change operation (SQL INSERT, UPDATE, or DELETE statement) is performed on a specified table. Triggers are useful for tasks such as enforcing business rules, validating input data, and keeping an audit trail. ```sql CREATE TABLE contacts ( contact_id INT(11) NOT NULL AUTO_INCREMENT, last_name VARCHAR(30) NOT NULL, first_name VARCHAR(25), birthday DATE, created_date DATE, created_by VARCHAR(30), CONSTRAINT contacts_pk PRIMARY KEY (contact_id) ); ``` ```sql DELIMITER // CREATE TRIGGER contacts_before_insert BEFORE INSERT ON contacts FOR EACH ROW BEGIN DECLARE vUser varchar(50); -- Find username of person performing INSERT into table SELECT USER() INTO vUser; -- Update create_date field to current system date SET NEW.created_date = SYSDATE(); -- Update created_by field to the username of the person performing the INSERT SET NEW.created_by = vUser; END; // DELIMITER ; ```
chunvv commented 5 years ago
  1. What is View in MySQL?
In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. ```sql CREATE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName FROM Customers WHERE Country = "Brazil"; ``` Để rõ hơn thì mình lấy một ví dụ thế này: Giả sử trang Frontend có một Block gọi là hiển thị 10 tin mới nhất, như vậy ta sẽ truy vấn lấy 10 tin và sắp xếp giảm dần theo ID. Nhưng bạn biết trong SQL nó sẽ duyệt toàn bộ bảng rồi mới trả về kết quả và điều này làm cho truy vấn trở nên chậm chạm. Để giải quyết nó thì ta sẽ tạo một View gồm 10 tin mới nhất và lúc hiển thị ra chỉ cần lấy trong View nên tốc độ sẽ nhanh hơn rất nhiều lần.
chunvv commented 5 years ago
  1. What is a temporary table? Performance? Live time?
``` Point: lưu trữ và xử lý kết quả trung gian ``` Screen Shot 2019-06-08 at 23 11 23
chunvv commented 5 years ago
  1. What is heap table in MySQL?
Screen Shot 2019-06-08 at 23 13 43
chunvv commented 5 years ago

ABD (Active Book Dialog): すべての人が、本を好きになるために​

Points:

Refer:

http://www.abd-abd.com/

chunvv commented 5 years ago

昼会:

chunvv commented 5 years ago

ABD result:

From Tai Tran:

BCE954BA-FDE4-44CA-8EAE-655EA52B4426

chunvv commented 5 years ago

From Trung Vu:

953EEC49-E2CE-4246-B595-1FFC9386739C

chunvv commented 5 years ago

From Quang Minh:

04C16475-3B00-487B-926D-6BAFF3F6E4A7

chunvv commented 5 years ago

From Son Tran:

37934485-C5EC-4D43-99A0-52A96B157F46

chunvv commented 5 years ago

From Duc Phan:

BD7B8FB1-D003-466D-90D2-002C3C111210

chunvv commented 5 years ago

From Quy Vu:

A37F4811-0C9F-4AA4-8C01-31310C651B2B