[DB] RDBMS MySQL

·

7 min read

[DB] RDBMS MySQL

Purchase RDS at AWS and set MySQL

  1. Create MySQL RDS instance from AWS

    1. Remember the master name & password
  2. Set inbound rules to allow any IP addresses to access

  3. Copy the endpoint

  4. Open VSCode and install MySQL extension

  5. Add connection


SQL (Structured Query Language)

Unlike non-relational databases where data formats were free-form, relational databases have a concept of "tables". Using these tables, data is managed in a format similar to Excel, and we can stack the desired data in this table.

NoSQL vs. SQL (RDB)

  • collection <-> table

  • documents <-> record

  • mongoose ODM (Object Document Mapper) Library for MongoDB <-> sequelize ODM Library for MySQL

Scaling in DB

MySQL

  • Advantages:

    • Clear definition of schema ensures data integrity.

    • Each data can be stored only once through relationships, without duplication.

  • Disadvantages:

    • Relatively less flexible, and the data schema must be known and planned in advance. (Modifying it later may be difficult or impossible.)

    • Very complex queries can be created with many JOIN statements.

    • Horizontal scaling is difficult, and typically only vertical scaling is possible. This means that there may be some growth limitations in terms of processing capacity.

  • When to use it?

    • If related data changes relatively frequently in multiple parts of an application (which would require modifying multiple collections if using NoSQL):

    • A clear schema is important, and the data structure should not undergo dramatic changes.

NoSQL

  • Advantages:

    • High flexibility due to the absence of a fixed schema. This means that stored data can be adjusted and new "fields" can be added at any time.

    • Data is stored in the format required by the application, resulting in faster data retrieval speeds.

    • Vertical and horizontal scaling is possible, allowing the database to handle all read/write requests generated by the application.

  • Disadvantages:

    • Due to its flexibility, decision-making on data structure can be delayed (since it doesn't need to be planned and decided immediately).

    • If copied data is modified, multiple collections and documents must be modified.

  • When to use it?

    • If the exact data requirements are unknown or if data with relationships frequently changes (requires frequent modifications):

    • If data is frequently read but not frequently modified (i.e. doesn't require modifying dozens of documents with a single change):

    • If the database needs to be horizontally scaled (i.e. handling large amounts of data, high read/write throughput)

Types of SQL

DDL (Data Definition Language)

DDL (Data Definition Language) is a language used to define data, and is used to create, modify, or delete tables and databases. It is used for actions related to the structure of data.

  • CREATE : to create DATABASE, TABLE, VIEW, or INDEX
CREATE DATABASE dbName;
CREATE TABLE tableName
{
 columnName columnAttribute
}
  • DROP: to remove DATABASE, TABLE, VIEW, or INDEX
DROP DATABASE dbName;
DROP TABLE tableName;
  • ALTER: to change the attributes of DATABASE, or TABLE.
ALTER DATABASE dbName changeCondition;
ALTER TABLE tableName ADD changeCondition;
ALTER TABLE tableName DROP changeCondition;
ALTER TABLE tableName MODIFY changeCondition;
ALTER TABLE tableName RENAME changeCondition;
...

DML (Data Manipulation Language)

DML (Data Manipulation Language) is a language used to manipulate data in a database, and is used for actions such as storing, deleting, modifying, and retrieving data. It is used for actions related to the content of data.

  • SELECT: to search through the DB
SELECT columnName FROM tableName [WHERE condition];
  • INSERT: to insert the data into the DB
INSERT INTo tableName (columnName) VALUES (value);
  • DELETE: to delete the data that meets the condition from the table
DELETE FROM tableName [WHERE condition];
  • UPDATE: to update the data that meets the condition from the table
UPDATE tableName SET columnName = value [WHERE condition];

DCL (Data Control Language)

It is important to understand the TRANSACTION concept for using DCL.

DCL (Data Control Language) is a set of syntax used for database authorization and permission-related tasks. It is used to set permissions for specific users to access the database.

  • COMMIT: to notify DB task has been done successfully
START TRANSACTION;
...
COMMIT;
  • ROLLBACK: to notify DB task has been done unsuccessfully
START TRANSACTION;
...
ROLLBACK;
  • GRANT: to grant permission to a specific user
GRANT [permission] ON objectName TO user;
  • REVOKE: to cancel granted permission to a specific user
REVOKE [permission] ON objectName FROM user;

DQL (Data Query Language)

DQL(Data Query Language) is a language that allows querying within a predetermined schema, with SELECT being the most representative example of DQL. DQL is sometimes considered as a part of DML.

  • WHERE: to filter the data that meets the condition
SELECT columnName FROM tableName WHERE condition;
  • DISTINCT: to remove the duplicate values
SELECT DISTINCT columnName FROM tableName WHERE condition;
  • GROUP BY: to make values as a group
SELECT region, SUM(sales_amount) as total_sales
FROM sales
GROUP BY region;
  • ORDER BY: to set the values in order (ASC - default / DESC)
SELECT name, age, salary
FROM employees
WHERE department = 'Sales'
ORDER BY salary DESC;

TCL (Transaction Control Language)

TCL (Transaction Control Language) is a set of commands in SQL that allow the management of transactions, including committing or rolling back changes made by DML statements.

The COMMIT and ROLLBACK statements, which are used to make permanent changes or undo changes made during a transaction, are often categorized as TCL commands.

SQL Constraints

Constraints are rules that manage restrictions between each column and prevent data violations, ensuring the integrity of the database.

Constraints are concepts created to maintain the database's integrity and ensure that data is in a flawless and perfect state.

Primary Key Constraints

A primary key is used to indicate information that can uniquely identify the data in a table. (REQUIRED)

CREATE TABLE User (
    userId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name   varchar(255)
);

NULL Constraints

The NULL attribute is a condition that allows or disallows NULL values in a specific column.

CREATE TABLE User (
    userId int(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name   varchar(255) NOT NULL
);

Unique Constraints

The UNIQUE constraint is a condition that prevents duplicate values in a specific column, ensuring that each value in the column is unique.

CREATE TABLE User (
    userId int(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name   varchar(255) NOT NULL UNIQUE
);

Foreign Key Constraints

The foreign key constraint is a constraint used when establishing a relationship between tables in a database.

CREATE TABLE Garden (
    gardenId int(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    address  varchar(255) NOT NULL
);

CREATE TABLE GardenPlants (
    gardenPlantsId int(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    GardenId       int(11)      NOT NULL,
    name           varchar(255) NOT NULL,
    FOREIGN KEY (GardenId) REFERENCES Garden (gardenId)
);

The types of referential integrity constraints are:

  • CASCADE
FOREIGN KEY (UserId) REFERENCES Users(userId)
    ON DELETE CASCADE
    ON UPDATE CASCADE;
  • NO ACTION
FOREIGN KEY (UserId) REFERENCES Users(userId)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;
  • SET NULL
FOREIGN KEY (UserId) REFERENCES Users(userId)
    ON DELETE SET NULL
    ON UPDATE SET NULL;
  • SET DEFAULT
FOREIGN KEY (UserId) REFERENCES Users(userId)
    ON DELETE SET DEFAULT
    ON UPDATE SET DEFAULT;

CHECK Constraints

A constraint that allows only data that meets certain conditions to be inserted. Basic operators, comparison operators, IN, NOT IN, and other operators can be used in the conditions.

CREATE TABLE employees (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50),
  emp_age INT,
  emp_gender CHAR(1),
  emp_salary DECIMAL(10,2),
  emp_hire_date DATE,
  CONSTRAINT chk_emp_age CHECK (emp_age >= 18 AND emp_age <= 65),
  CONSTRAINT chk_emp_gender CHECK (emp_gender IN ('M', 'F')),
  CONSTRAINT chk_emp_salary CHECK (emp_salary > 0)
);

SELECT JOIN Operator

The JOIN operator is an operator that connects two tables based on their common data, allowing them to be queried as a single table.

In the above ERD, the User table and the Posts table have a 1:N relationship because one user can write multiple posts. However, since the UserId column in the Posts table can be used to identify which user wrote each post, we can use a JOIN operation to output the email of the corresponding user based on the foreign key UserId.

SELECT p.postId, p.title, p.content, u.email
FROM Posts as p
JOIN Users as u 
    ON p.UserId = u.userId;

More Resources