Creating tables is a fundamental step in designing any relational database. In this chapter, youโ€™ll learn how to create tables in MySQL using SQL commands.


๐Ÿ”ฐ What is a Table?

A table is a structured format for storing data in rows and columns. Each column has a specific data type, and each row represents a record.


โœ… Syntax for Creating a Table

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
);

Example:

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    email VARCHAR(150) UNIQUE,
    birthdate DATE,
    is_active BOOLEAN DEFAULT TRUE
);

๐Ÿ” Explanation of the Example

ColumnData TypeDescription
idINTUnique student ID
AUTO_INCREMENTAutomatically increases ID
PRIMARY KEYUniquely identifies each row
VARCHAR(100)Text field up to 100 characters
email UNIQUEEnsures emails are not duplicated
DATEFor storing birth dates
BOOLEAN DEFAULT TRUEStatus column with default value

๐Ÿ›  Common Data Types

Data TypeDescription
INTInteger
VARCHAR(n)Variable-length string
TEXTLarge text
DATEDate (YYYY-MM-DD)
DATETIMEDate & Time
BOOLEANTrue/False
FLOAT / DOUBLEDecimal values

๐Ÿงฑ Adding Constraints

ConstraintDescription
PRIMARY KEYUniquely identifies a row
NOT NULLDisallows empty values
UNIQUEEnsures all values are different
DEFAULTSets a default value
AUTO_INCREMENTAuto-increments numeric values
FOREIGN KEYLinks tables together

๐Ÿ”— Example with Foreign Key

CREATE TABLE courses (
    course_id INT AUTO_INCREMENT PRIMARY KEY,
    course_name VARCHAR(100) NOT NULL
);

CREATE TABLE enrollments (
    enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    course_id INT,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

๐Ÿ“‹ View Table Structure

DESCRIBE students;

๐Ÿงน Drop Table (Use with Caution)

DROP TABLE students;

๐Ÿ“ Best Practices

  • Use snake_case for table and column names (e.g., first_name)
  • Always define a PRIMARY KEY
  • Use appropriate data types and constraints
  • Use FOREIGN KEYS to maintain relationships

๐ŸŽฏ Summary

TaskCommand
Create TableCREATE TABLE table_name (...);
View StructureDESCRIBE table_name;
Delete TableDROP TABLE table_name;

Creating tables is a fundamental step in designing any relational database. In this chapter, youโ€™ll learn how to create tables in MySQL using SQL commands.


๐Ÿ”ฐ What is a Table?

A table is a structured format for storing data in rows and columns. Each column has a specific data type, and each row represents a record.


โœ… Syntax for Creating a Table

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
);

Example:

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    email VARCHAR(150) UNIQUE,
    birthdate DATE,
    is_active BOOLEAN DEFAULT TRUE
);

๐Ÿ” Explanation of the Example

ColumnData TypeDescription
idINTUnique student ID
AUTO_INCREMENTAutomatically increases ID
PRIMARY KEYUniquely identifies each row
VARCHAR(100)Text field up to 100 characters
email UNIQUEEnsures emails are not duplicated
DATEFor storing birth dates
BOOLEAN DEFAULT TRUEStatus column with default value

๐Ÿ›  Common Data Types

Data TypeDescription
INTInteger
VARCHAR(n)Variable-length string
TEXTLarge text
DATEDate (YYYY-MM-DD)
DATETIMEDate & Time
BOOLEANTrue/False
FLOAT / DOUBLEDecimal values

๐Ÿงฑ Adding Constraints

ConstraintDescription
PRIMARY KEYUniquely identifies a row
NOT NULLDisallows empty values
UNIQUEEnsures all values are different
DEFAULTSets a default value
AUTO_INCREMENTAuto-increments numeric values
FOREIGN KEYLinks tables together

๐Ÿ”— Example with Foreign Key

CREATE TABLE courses (
    course_id INT AUTO_INCREMENT PRIMARY KEY,
    course_name VARCHAR(100) NOT NULL
);

CREATE TABLE enrollments (
    enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    course_id INT,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

๐Ÿ“‹ View Table Structure

DESCRIBE students;

๐Ÿงน Drop Table (Use with Caution)

DROP TABLE students;

๐Ÿ“ Best Practices

  • Use snake_case for table and column names (e.g., first_name)
  • Always define a PRIMARY KEY
  • Use appropriate data types and constraints
  • Use FOREIGN KEYS to maintain relationships

๐ŸŽฏ Summary

TaskCommand
Create TableCREATE TABLE table_name (...);
View StructureDESCRIBE table_name;
Delete TableDROP TABLE table_name;