Core Operations in MySQL: DDL, DML, and DQL

A database (DB) serves as a repository for storing and managing data. A Database Management System (DBMS) is software designed to manipulate and administer databases. Relational databases (RDBMS) are built on the relational model, organizing data into interconnected two-dimensional tables. This structure ensures uniform data storage, simplifying maintenance, and employs SQL (Structured Query Language) for operations, providing a standardized approach suitable for complex queries.

SQL statements can be written in single or multiple lines, terminated with a semicolon. Spaces and indentation enhance readability. In MySQL, SQL keywords are case-insensitive. Comments include single-line (-- comment or # comment) and multi-line (/* comment */).

Category Full Name Description
DDL Data Deifnition Language Defines database objects like databases, tables, and columns.
DML Data Manipulation Language Handles insertion, deletion, and modification of table data.
DQL Data Query Language Retrieves records from database tables.
DCL Data Control Language Manages database users and access permissions.

Database Opeartions with DDL

Managing Databases

  • List all databases:
    SHOW DATABASES;
    
  • Identify the current database:
    SELECT DATABASE();
    
  • Switch to a specific database:
    USE db_name;
    
  • Create a new database (optional existence check):
    CREATE DATABASE IF NOT EXISTS db_name;
    
  • Remove a database:
    DROP DATABASE IF EXISTS db_name;
    

Table Operations

  • Create a table with columns, constraints, and comments:
    CREATE TABLE table_name (
        col1 INT NOT NULL COMMENT 'ID field',
        col2 VARCHAR(50) DEFAULT 'N/A' COMMENT 'Description',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ) COMMENT 'Sample table';
    
  • List tables in the current database:
    SHOW TABLES;
    
  • View table structure:
    DESCRIBE table_name;
    
  • Retrieve the creation statement:
    SHOW CREATE TABLE table_name;
    
  • Modify table schema:
    ALTER TABLE table_name ADD new_col DECIMAL(10,2);
    ALTER TABLE table_name MODIFY col1 BIGINT;
    ALTER TABLE table_name CHANGE old_col new_col CHAR(20);
    ALTER TABLE table_name DROP COLUMN col2;
    RENAME TABLE table_name TO new_table_name;
    
  • Delete a table:
    DROP TABLE IF EXISTS table_name;
    

Constraints

Constraints enforce rules on table fields to ensure data accuracy and integrity.

Constraint Description Keyword
Not Null Prevents NULL values in a field. NOT NULL
Unique Ensures all values in a field are distinct. UNIQUE
Primary Key Uniquely identifies each row; must be non-NULL and unique. PRIMARY KEY (often with AUTO_INCREMENT)
Default Provides a fallback value if none is specified. DEFAULT value
Foreign Key Establishes links between tables for consistency. FOREIGN KEY

Data Types

MySQL supports numeric, string, and date/time types. String and date values must be enclosed in quotes.

Numeric Type Storage (bytes) Signed Range Unsigned Range Purpose
TINYINT 1 -128 to 127 0 to 255 Small integers
SMALLINT 2 -32,768 to 32,767 0 to 65,535 Medium integers
MEDIUMINT 3 -8,388,608 to 8,388,607 0 to 16,777,215 Larger integers
INT 4 -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 Standard integers
BIGINT 8 -9.22×10¹⁸ to 9.22×10¹⁸ 0 to 1.84×10¹⁹ Very large integers
FLOAT 4 Approximately ±3.4×10³⁸ N/A Single-precision floating-point
DOUBLE 8 Approximately ±1.8×10³⁰⁸ N/A Double-precision floating-point
DECIMAL(M,D) Variable Depends on precision (M,D) N/A Exact fixed-point numbers
String Type Max Size Description
CHAR(N) 255 characters Fixed-length string, padded with spaces.
VARCHAR(N) 65,535 characters Variable-length string.
TEXT 65,535 bytes Long text data.
Date/Time Type Format Description
DATE YYYY-MM-DD Date only.
TIME HH:MM:SS Time only.
DATETIME YYYY-MM-DD HH:MM:SS Combined date and time.
TIMESTAMP YYYY-MM-DD HH:MM:SS Automatic timestamp on update.
YEAR YYYY Year value.

Tags: MySQL database sql DDL DML

Posted on Sun, 05 Jul 2026 17:07:45 +0000 by tommyinnn