MySQL Database Table and Data Operations Guide
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
- Create table within a specific database
CREATE TABLE companydb.employees (
employee_name VARCHAR(100)
);
- Activate database first, then create table with multiple columns
USE companydb;
CREATE TABLE staff (
employee_name VARCHAR(100),
department VARCHAR(50),
hire_date DATE
);
- 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
- List all tables in current database
SHOW TABLES;
- List tables in specific database
SHOW TABLES FROM companydb;
- Pattern matching for table names
SHOW TABLES LIKE '%staff%';
- View table creation statement
SHOW CREATE TABLE employees;
Output Formatting
\gterminator displays results horizontally\Gterminator 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
- Rename table
RENAME TABLE employees TO worker_list;
- 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
- Append column at end of table
ALTER TABLE worker_list ADD salary DECIMAL(10,2);
- 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
- Insert column at beginning
ALTER TABLE worker_list ADD employee_id INT FIRST;
- 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
- Full column insertion
INSERT INTO worker_list VALUES (1, 'Alice Johnson', 'Engineering', '2023-01-15', 'alice@company.com', '555-0100');
- 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
- Retrieve all columns
SELECT * FROM worker_list;
- Retrieve specific columns
SELECT employee_name, department FROM worker_list;
- 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
- Update specific record
UPDATE worker_list SET department = 'Product' WHERE employee_id = 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
- Data retrieval operations typically constitute over 90% of database interactions
- Always inspect table structure before performing any operations
- Avoid modifying table structures after data population
- Design table schemas comprehensively before implementation
- Use explicit column lists instead of wildcard seletcion in production environments