Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

MySQL Foundation: Core Syntax and Database Management Operations

Tech Nov 25 13

SQL Core Syntax Guidelines

  1. SQL statements are typically written line-by-line for readability, though whitespace doesn’t affect execution as long as valid terminators are present.
  2. A required statement terminator defaults to semicolon (;), but can also be \g (same as semicolon) or \G (displays query results vertically for large datasets).
  3. Syntax mirrors simplified natural language to lower learning barriers.
  4. Wrap reserved words, identifiers with spaces/special characters, or case-sensitive identifiers (on case-sensitive OS) in backticks (`) to avoid parser conflicts.

Core SQL Operation Templates

-- General structure creation syntax
CREATE [STRUCTURE_TYPE] [STRUCTURE_NAME] [STRUCTURE_DEFINITION_CLAUSE];

-- List all existing accessible structures of a specific type
SHOW [PLURAL_STRUCTURE_TYPE];

-- Retrieve full creation details and metadata for a single structure
SHOW CREATE [STRUCTURE_TYPE] [STRUCTURE_NAME];

-- Core DML (Data Manipulation Language) for tables (always use WHERE unless full table changes are intentional)
INSERT INTO [TARGET_TABLE] ([COL1], [COL2], ...) VALUES ([VAL1], [VAL2], ...);
SELECT [COL_LIST | *] FROM [SOURCE_TABLE];
UPDATE [TARGET_TABLE] SET [COL1] = [NEW_VAL1], [COL2] = [NEW_VAL2], ...;
DELETE FROM [TARGET_TABLE];

Key Takeaways for Core Syntax

  • SQL prioritizes readability but enforces strict terminator and escaping rules.
  • Basic operations map to MySQL’s object hierarchy: database-level, table-level, and data-level.
  • Never omit WHERE in UPDATE/DELETE outside of controlled testing scenarios.

Database-Level SQL Operations

These manage top-level database objects, including creation, inspection, selection, configuration, and permanent deletion.

1. Create a New Database

-- Full safe creation syntax with optional parameters
CREATE DATABASE [IF NOT EXISTS] [DB_NAME]
[CHARACTER SET | CHARSET] [CHARSET_NAME]
[COLLATE] [COLLATION_NAME];

Examples

  1. Minimal safe creation (prevents duplicate DB errors)
CREATE DATABASE IF NOT EXISTS sample_db_basic;
  1. Create with emoji-friendly UTF8MB4 charset (modern production default)
CREATE DATABASE IF NOT EXISTS sample_db_utf8mb4
CHARSET utf8mb4;
  1. Create with explicit charset and MySQL 8.0+ default collation
CREATE DATABASE IF NOT EXISTS sample_db_custom_collated
CHARSET utf8mb4
COLLATE utf8mb4_0900_ai_ci;

Key Takeaways for DB Creation

  • Database names follow MySQL identifier rules: alphanumeric + underscores only, case-insensitive (most systems), no leading digits, snake_case preferred.
  • Optional IF NOT EXISTS avoids common setup errors.
  • Each DB creates a dedicated directory in the MySQL data storage path, with a metadata file storing charset/collation.
  • Production databases are pre-configured once whenever possible.

2. Inspect Existing Databases

-- List all databases accessible to the current user
SHOW DATABASES;

-- Show full creation SQL and DB-specific metadata
SHOW CREATE DATABASE [DB_NAME];

Examples

SHOW DATABASES;
SHOW CREATE DATABASE sample_db_utf8mb4;

Key Takeaways for DB Inspection

  • User permissions strictly limit visible databases.
  • SHOW CREATE DATABASE is useful for verifying settings or replicating DBs.

3. Select an Active Database

-- Set default DB context for all subsequent statements
USE [DB_NAME];

Example

USE sample_db_utf8mb4;

Key Takeaways for Active DB Selection

  • Eliminates need for explicit [DB_NAME].[TABLE_NAME] qualification in table/data commands.
  • Can switch active DBs at any time with another USE command.
  • Full qualification is required if no active DB is selected.

4. Modify Database Configuration

Modern MySQL versions do not support direct DB renaming; use this workflow instead: create a new DB, migrate all tables/data, drop the old DB.

-- Alter only charset and collation (irreversible for existing tables without conversion)
ALTER DATABASE [IF EXISTS] [DB_NAME]
[CHARACTER SET | CHARSET] [NEW_CHARSET_NAME]
[COLLATE] [NEW_COLLATION_NAME];

Examples

  1. Update charset of a basic DB to UTF8MB4
ALTER DATABASE IF EXISTS sample_db_basic
CHARSET utf8mb4;
  1. Update both charset and collation of a custom DB
ALTER DATABASE IF EXISTS sample_db_custom_collated
CHARSET utf16le
COLLATE utf16le_general_ci;

Key Takeaways for DB Modification

  • Collation must always be compatible with the selected charset.
  • Production DBs are rarely modified; recreate with corrected settings after backing up data.

5. Drop (Permanently Delete) a Database

-- Delete DB and ALL contents (tables, views, data, triggers, etc.) IRREVERSIBLY
DROP DATABASE [IF EXISTS] [DB_NAME];

Example

DROP DATABASE IF EXISTS sample_db_basic;

Key Takeaways for DB Deletoin

  • Always create a full backup before dropping any DB, especially in production.
  • Optional IF EXISTS prevents accidental errors if the DB is already gone.
  • Drops remove the DB’s entire directory from the MySQL data storage path.
Tags: MySQL

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.