Paramclasses Academy - Blog

Blog Details

How to create a table in MySQL

How to create a table in MySQL

October 10, 2024

To create a table in MySQL, you can use the CREATE TABLE statement. Here's a simple guide on how to create a table:

Basic Syntax:

CREATE TABLE table_name
( column1 datatype constraints,
column2 datatype constraints,
... columnN datatype constraints
);
table_name: The name you want to give to the table.

  • column1, column2, ... columnN: The names of the columns in the table.
  • datatype: The type of data the column will hold (e.g., INT, VARCHAR, DATE).
  • constraints: Additional rules you can apply to the column, like NOT NULL, PRIMARY KEY, etc.

    Example: Creating a employees table :

    CREATE TABLE employees ( employee_id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100), hire_date DATE, salary DECIMAL(10, 2) );

     
  • employee_id: A unique ID for each employee, set to automatically increment with each new entry.
  • first_name and last_name: Employee names with a max length of 50 characters, both required (NOT NULL).
  • email: Employee email address with a max length of 100 characters.
  • hire_date: The date the employee was hired.
  • salary: A decimal field to store the employee’s salary, allowing up to 10 digits with 2 decimals.

     

  •  

Copyright © Paramwebinfo Academy.All Rights Reserved

Designed by PARAMWEBINFO