How to Create a Simple Table in MySQL: A Beginner’s Guide | MySQL Database Tutorial

How to Create a Simple Table in MySQL: A Beginner’s Guide | MySQL Database Tutorial

Creating a table in MySQL is a fundamental skill for working with databases. In this tutorial, we will walk through the basic steps to create a simple table in MySQL, complete with column names, data types, and sizes. 

Steps to Create a Table in MySQL

To create a table, we use the CREATE TABLE statement. The basic syntax looks like this: 

CREATE TABLE TABLE_NAME(
Column_Name1  Data_Type (SIZE),
Column_Name2  Data_Type (SIZE),
Column_Name3  Data_Type (SIZE),
.............................
.............................
Column_Name(n)  Data_Type (SIZE));
Remainder:

Don't copy paste my code , if you really want to Learn SQL  or MySQL you have to write code by Yourselves.   

 

Here’s a breakdown of the syntax:

  • CREATE TABLE: The command to create a new table in the database.
  • table_name: The name you want to give to your table (e.g., employees).
  • column_name: The name of the column in the table (e.g., idname).
  • data_type: The type of data that column will hold (e.g., INTVARCHAR).
  • size: Optional. It specifies the size of the data type (for example, VARCHAR(50) for a string of up to 50 characters).

Example: Creating an Employees Table

Let’s create a simple table called employees with the following columns:

  • id: an integer that will hold the employee's ID number.
  • name: a string (up to 50 characters) for the employee's name.
  • age: an integer for the employee’s age.
  • department: a string (up to 20 characters) for the department the employee belongs to.

Let’s create a simple table called employees with the following columns:

CREATE TABLE employees (
  id INT (11),
  name VARCHAR (50),
  age INT,
  department VARCHAR (20)
);

Explanation of the Example:

  • id INT(11): Defines a column named id that will store integer values. The (11) is a display width for the integer, which is optional and not a limit on the number size.
  • name VARCHAR(50): The name column is defined to store variable-length strings up to 50 characters.
  • age INT: The age column will store integer values representing the employee’s age.
  • department VARCHAR(20): The department column will store a string of up to 20 characters.

Example Result:

After executing the above SQL statement, MySQL will create the employees table in your database. If you view the table's structure, you will see something like this:

Column NameData TypeSize
idINT11
nameVARCHAR50
ageINT-
departmentVARCHAR20

Note :
Data types are case-insensitive, but it’s common to use uppercase for table and column names, and lowercase for data types. 


Next Post:

We will post about MySQL Data Type , Stay Tuned with us.

0 Response to How to Create a Simple Table in MySQL: A Beginner’s Guide | MySQL Database Tutorial

Post a Comment