Database 101

Database?

The tool help us store and retrive
DATA & INFOMATION

When you should use Database?

When shouldn't use DB

File

  • Easy to use
  • Hard to search

Database

  • Hard to use
  • Easy to programing

The data flow

Raw data

Processing

Store in database

Query, analysis

Data presentation

Raw data

Processing

Store data

Query, analysis

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

SELECT COUNT(ProductID)
FROM Products;

!https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join
!https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_count

Data presentation

Data flow in EyeQ?

Design database

Conceptual data model of Magento1.9 (UML)

Design database

 Conceptual data mode

Logical data model

 Physical data model

 Conceptual data model

Determining data to be stored
(Detail of data)

Determining data relationships between tables

Result of this step is : UML, entity relationship

QA: So why we should break data to tables?

Example: Customer & Shipping Address

Benefits:

Avoid duplicate data
More flexibaal

Easy to maintain and scale up
Cons:
Some time it make more complex to query data

4 address column vs address table

Logical data model

Translate conceptual data model to DBMS schema!

Entity–relationship model

CREATE TABLE `CheckInOut` (
  `UserFullCode` int(11) DEFAULT NULL,
  `UserEnrollNumber` int(11) DEFAULT NULL,
  `TimeDate` varchar(50) DEFAULT NULL,
  `TimeStr` varchar(50) DEFAULT NULL,
  `TimeType` varchar(10) DEFAULT NULL,
  `TimeSource` varchar(4) DEFAULT NULL,
  `MachineNo` int(11) DEFAULT NULL,
  `CardNo` varchar(60) DEFAULT NULL,
  KEY `ospos_checkinout_ibfk_1` (`UserFullCode`),
  CONSTRAINT `ospos_checkinout_ibfk_1` FOREIGN KEY (`UserFullCode`) REFERENCES `ospos_people` (`person_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Document model
{

    "_id" : ObjectId("5bf7d071441dac019ff79232"),
    "user" : ObjectId("5bf7d071441dac019ff7922f"),
    "imageId" : "visitorImageId1",
    "oldFaceId" : "exampleVisitor1",
    "newFaceId" : "exampleVisitor1",
    "actionType" : "action Example1",
    "createdAt" : ISODate("2018-06-01T17:00:00.000Z"),
    "__v" : 0,
    "updatedAt" : ISODate("2018-11-23T10:03:35.389Z")
}

Models

Which modal that I should choose and why?

  • Relational model
  • Entity–relationship model
  • Enhanced entity–relationship model
  • Object model
  • Document model
  • Entity–attribute–value model
  1. Choose data model
  2. Conceptual data
  3. Implement

Physical data model

The feature of DBMS!

Q&A

Warm up

Unilever muốn xây dựng hệ thống phát hiện các hành động bất thường để tăng cường an ninh.
Đây là kết quả của việc phân tích yêu cầu từ khách hàng:
 

  • Có nhiều camera quan sát, mỗi camera có địa chỉ IP kết nối, vị trí của camera
  • Mỗi khi có sự kiện bất thường xảy ra, cần lưu lại thông tin camera nào ghi lại, loại hành động bất thường, một đoạn video của hành động, thời điểm xảy ra hành động

What data we should store in database?  🤔

Camera

  • name
  • rtsp
  • location

Alert

  • camera
  • type
  • storagePath of video
  • time

Previous Question

  • Sự giống và khác nhau giữa các data model?
  • Data model là gì?
  • Tại sao lại chọn data modal đó?

Data model ( mô hình dữ liệu ) là tập các khái niệm để mô tả cấu trúc của CSDL, cũng như các ràng buộc trên cơ sở dữ liệu ấy.

  • Entity–relationship model
    ( Mô hình thực thể kết hợp )
  • Relational model
    ( Mô hình quan hệ )
  • Object model
    ( Mô hình đối tượng )
  • Document Object model
    ( Mô hình đối tượng tài liệu )

Some types of data model

Tại sao lại chọn data model đó?

Về bản chất, mục tiêu chung của data model là tổ chức cách lưu trữ dữ liệu.
Sự khác nhau của các model là ở cách tổ chức dữ liệu.
Một data model được được chọn thì phải phù hợp nhất với bài toán đang cần giải quyết.

MongoDB: truy xuất dữ liệu nhanh, dễ mở rộng

MySql: Dữ liệu toàn vẹn, ràng buộc chặt chẽ

FaceID and ImageID

Coming up

  • Relational model
  • Basic concepts
  • Design your first database
  • SQL language
  • Database in action

Relational model

Mô hình dữ liệu quan hệ được Edgar Frank Codd đề xuất vào năm 1970. Do tính chất đơn giản và xây dựng trên nền tảng toán học vững chắc nên mô hình quan hệ được sử dụng rộng rãi từ thập niên 80 cho tới nay.

Nhiều hệ thống đang sử dụng mô hình quan hệ như Oracle của Oracle, Sql Server của Microsoft.

Các hệ thống thực tế đang sử dụng mô hình này: TIKI, Lazada...

SQL Language

SQL ( Structured Query Language ) là ngôn ngữ truy vấn mang tính cấu trúc.

Nó được thiết kế để quản lý dữ liệu trong một hệ thống quản lý cơ sở dữ liệu quan hệ (RDBMS).

SQL Language

Table : là một bảng dữ liệu 2 chiều được đặt tên, có một số cột và một số dòng dữ liệu.

Column : là tập các thuộc tính của bảng đó

Row : là một bộ dữ liệu trong một bảng

Primary key : Khoá chính là một loại ràng buộc để định danh duy nhất mỗi row trong table

Khóa ngoại : của một table được xem như con trỏ trỏ tới khóa chính của table khác.

Design your first database!

Database in action

https://sqliteonline.com/

 

SQL Language

https://www.w3schools.com/sql/default.asp

Database101

By Bùi Minh Vũ

Database101

  • 270