Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

MySQL Database Table and Data Operations Guide

Tools Nov 21 10

SQL Table (Field) Operations

1. Creating Database Tables

Creating a database table involves defining the table structure with fields based on business requirements. Tables and fields are interdependent—they must be created together.

Syntax

CREATE TABLE [database_name.]table_name (
    column_name column_data_type,
    column_name column_data_type
) table_options;

Implementation

  1. Create table within a specific database
CREATE TABLE companydb.employees (
    employee_name VARCHAR(100)
);
  1. Activate database first, then create table with multiple columns
USE companydb;
CREATE TABLE staff (
    employee_name VARCHAR(100),
    department VARCHAR(50),
    hire_date DATE
);
  1. Configure table options during creation
CREATE TABLE managers (
    manager_name VARCHAR(100)
) ENGINE=InnoDB CHARSET=utf8mb4;

Key Points

  • Tables require a database context—either explicit prefix or USE statement
  • Each field requires a name and data type
  • Table options include storage engine, character set, and collation
  • InnoDB serves as the default storage engine supporting transactions and foreign keys
  • MyISAM, an alternative engine, lacks transaction support
  • Duplicate existing table structure: CREATE TABLE new_table LIKE source_database.source_table;

2. Displaying Database Tables

Query existing tables within a database using SHOW statements.

Implementation

  1. List all tables in current database
SHOW TABLES;
  1. List tables in specific database
SHOW TABLES FROM companydb;
  1. Pattern matching for table names
SHOW TABLES LIKE '%staff%';
  1. View table creation statement
SHOW CREATE TABLE employees;

Output Formatting

  • \g terminator displays results horizontally
  • \G terminator displays results vertically for better readability

3. Inspecting Table Structure

Examine column definitions and metadata within a table.

Syntax

DESC table_name;
DESCRIBE table_name;
SHOW COLUMNS FROM table_name;

Example

DESC employees;

Output

Field Type Null Key Default Extra
employee_name varchar(100) YES NULL
department varchar(50) YES NULL
hire_date date YES NULL

4. Modifying Table Properties

Alter table names and configuration options.

Implementation

  1. Rename table
RENAME TABLE employees TO worker_list;
  1. Modify character set
ALTER TABLE worker_list CHARSET=utf8;

5. Modifying Table Columns

Adjust column definitions after table creation.

Operations Include

  • Adding new columns
  • Renaming existing columns
  • Changing column data types
  • Removing columns
  • Repositioning columns

5.1. Adding Columns

Syntax

ALTER TABLE table_name ADD [COLUMN] column_name column_type [column_attributes] [column_position];

Implementation

  1. Append column at end of table
ALTER TABLE worker_list ADD salary DECIMAL(10,2);
  1. Add column with explicit column keyword
ALTER TABLE worker_list ADD COLUMN email VARCHAR(100);

5.2. Column Positioning

Place columns at specific positions within table structure.

  • First position: FIRST
  • After specific column: AFTER existing_column_name

Implementation

  1. Insert column at beginning
ALTER TABLE worker_list ADD employee_id INT FIRST;
  1. Insert column after another column
ALTER TABLE worker_list ADD phone VARCHAR(20) AFTER employee_name;

5.3. Renaming Columns

Syntax

ALTER TABLE table_name CHANGE old_column_name new_column_name column_type [attributes] [position];

Example

ALTER TABLE worker_list CHANGE salary compensation DECIMAL(10,2);

5.4. Changing Column Type and Attributes

Syntax

ALTER TABLE table_name MODIFY column_name new_type [attributes] [position];

Example

ALTER TABLE worker_list MODIFY phone VARCHAR(25) AFTER employee_id;

5.5. Removing Columns

Syntax

ALTER TABLE table_name DROP column_name;

Example

ALTER TABLE worker_list DROP compensation;

Warning: Column deletion removes all associated data permanently.


SQL Data Operations

1. Inserting Data

Add new records into tables permanentyl.

Methods

  • Full column insertion: Provide values for all columns
  • Partial column insertion: Specify target columns explicitly

Syntax

INSERT INTO table_name VALUES (value_list);
INSERT INTO table_name (column_list) VALUES (value_list);

Implementation

  1. Full column insertion
INSERT INTO worker_list VALUES (1, 'Alice Johnson', 'Engineering', '2023-01-15', 'alice@company.com', '555-0100');
  1. Partial column insertion
INSERT INTO worker_list (employee_id, employee_name, department) VALUES (2, 'Bob Smith', 'Marketing');

2. Retrieving Data

Query existing data from tables.

Syntax

SELECT */column_list FROM table_name [WHERE conditions];

Implementation

  1. Retrieve all columns
SELECT * FROM worker_list;
  1. Retrieve specific columns
SELECT employee_name, department FROM worker_list;
  1. Conditional retrieval
SELECT * FROM worker_list WHERE department = 'Engineering';

3. Updating Data

Modify existing field values.

Syntax

UPDATE table_name SET column = new_value [, column = new_value] [WHERE condition];

Implementation

  1. Update specific record
UPDATE worker_list SET department = 'Product' WHERE employee_id = 1;
  1. Update multiple fields
UPDATE worker_list SET department = 'Sales', phone = '555-0199' WHERE employee_name = 'Bob Smith';

Caution: Omitting WHERE clause updates all records.

4. Deleting Data

Remove records from tables permanently.

Syntax

DELETE FROM table_name [WHERE condition];

Implementation

DELETE FROM worker_list WHERE employee_id = 2;

Warning: Deletion is irreversible. Always use WHERE conditions to target specific records.


Operational Best Practices

  1. Data retrieval operations typically constitute over 90% of database interactions
  2. Always inspect table structure before performing any operations
  3. Avoid modifying table structures after data population
  4. Design table schemas comprehensively before implementation
  5. Use explicit column lists instead of wildcard seletcion in production environments

Related Articles

Installing CocoaPods on macOS Catalina (10.15) Using a User-Managed Ruby

System Ruby on macOS 10.15 frequently fails to build native gems required by CocoaPods (for example, ffi), leading to errors like: ERROR: Failed to build gem native extension checking for ffi.h... no...

Resolve PhpStorm "Interpreter is not specified or invalid" on WAMP (Windows)

Symptom PhpStorm displays: "Interpreter is not specified or invalid. Press ‘Fix’ to edit your project configuration." This occurs when the IDE cannot locate a valid PHP CLI executable or when the debu...

Capturing Android Screenshots and Screen Recordings with ADB

Two practical ways to grab images and videos from an Android device: Mirror the phone display to a computer and use desktop tools for screenshots and GIFs Use ADB commands (no UI mirroring required)...

Leave a Comment

Anonymous

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