DEV Community

Cover image for MySQL WorkBench: Create, alter & Drop
BrendahKiragu
BrendahKiragu

Posted on

MySQL WorkBench: Create, alter & Drop

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Task 1: Connect to the MySQL Workspace
  4. Task 2: Create a Database and a Table
  5. Task 3: Modify a Table
  6. Task 4: Delete a Table and Database
  7. Conclusion

Introduction

Managing relational databases effectively requires an understanding of fundamental operations such as creating, viewing, altering, and deleting databases and tables. This guide walks you through these essential operations using MySQL Workspace.

By the end of this guide, you will be able to:

  • Create databases and tables using the CREATE statement.
  • View available databases and tables using the SHOW statement.
  • Alter the structure of tables using the ALTER statement.
  • Delete databases and tables using the DROP statement.

Prerequisites

Before you begin, ensure that:

  • You have MySQL installed on your system.
  • MySQL Workbench is installed for database management.
  • You have the necessary user privileges to create and modify databases.

MySQL workbench landing page

Task 1: Connect to the MySQL Workspace

  1. Open MySQL Workbench.
  2. Click on Database in the menu bar and select Manage Connections.
  3. Click New and enter the following details:
    • Connection Name: World_Workspace
    • Hostname: localhost (or your server address)
    • Username: root

MySQL new connection window

  1. Click Test Connection to verify the setup.
  2. Once the connection is successful, click OK and then Connect.

Task 2: Create a Database and a Table

Step 1: Check Available Databases

Run the following query to display all available databases:

SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode

MySQL work

Step 2: Create a New Database

To create a database named company, execute:

CREATE DATABASE company;
Enter fullscreen mode Exit fullscreen mode

Verify the creation by running:

SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode

available databases

Step 3: Create a Table

To store employee data, create a table named employees with the following structure:

CREATE TABLE company.employees (
  `EmployeeID` INT AUTO_INCREMENT PRIMARY KEY,
  `FirstName` VARCHAR(50) NOT NULL,
  `LastName` VARCHAR(50) NOT NULL,
  `Department` VARCHAR(50) NOT NULL,
  `Salary` DECIMAL(10,2) NOT NULL,
  `HireDate` DATE NOT NULL
);
Enter fullscreen mode Exit fullscreen mode

dashboard showing the command for creating table employees with columns

Step 4: Verify Table Creation

To verify that the employees table was created, specify the database and check its tables:

USE company;
SHOW TABLES;
Enter fullscreen mode Exit fullscreen mode

image showing employees table in the company database

To list all columns in the employees table, run:

SHOW COLUMNS FROM company.employees;
Enter fullscreen mode Exit fullscreen mode

Task 3: Modify a Table

If you need to add a new column for email addresses, use the ALTER TABLE statement:

ALTER TABLE company.employees ADD COLUMN Email VARCHAR(100) NOT NULL;
Enter fullscreen mode Exit fullscreen mode

Verify the change:

SHOW COLUMNS FROM company.employees;
Enter fullscreen mode Exit fullscreen mode

Image showing the added email column

Task 4: Delete a Table and Database

Step 1: Drop a Table

To delete the employees table:

DROP TABLE company.employees;
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify Table Deletion

SHOW TABLES FROM company;
Enter fullscreen mode Exit fullscreen mode

shows the update company database without the deleted employees table

Step 3: Drop the Database

To delete the company database:

DROP DATABASE company;
Enter fullscreen mode Exit fullscreen mode

Verify database deletion:

SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You have successfully performed key database table operations:

  • Created databases and tables.
  • Viewed databases and tables using SHOW commands.
  • Altered a table column using ALTER TABLE.
  • Deleted tables and databases using DROP.

Top comments (0)