Creating and Managing Databases and Tables

Intermediate

๐Ÿ—๏ธ Creating & Managing Databases and Tables in MySQL

In MySQL, databases serve as containers for related tables.


๐Ÿ› ๏ธ Creating a Database

CREATE DATABASE my_database;
USE my_database;

Use the CREATE DATABASE statement to initialize a new database, then USE to set it as the current context.


๐Ÿ“‹ Creating Tables

Define columns with specific data types and constraints:

CREATE TABLE users (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100)
);
  • NOT NULL: Ensures a value must be provided
  • PRIMARY KEY: Ensures unique identification

๐Ÿ”ง Managing Tables

  • โœ๏ธ ALTER TABLE: Modify table structure
  • โŒ DROP TABLE: Delete a table
  • ๐Ÿ”„ RENAME TABLE: Change a tableโ€™s name

๐Ÿ“ Proper management ensures structured data and facilitates maintenance, backups, and scalability.