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));
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.,id,name).data_type: The type of data that column will hold (e.g.,INT,VARCHAR).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 namedidthat 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): Thenamecolumn is defined to store variable-length strings up to 50 characters.age INT: Theagecolumn will store integer values representing the employee’s age.department VARCHAR(20): Thedepartmentcolumn 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 Name | Data Type | Size |
|---|---|---|
| id | INT | 11 |
| name | VARCHAR | 50 |
| age | INT | - |
| department | VARCHAR | 20 |
0 Response to How to Create a Simple Table in MySQL: A Beginner’s Guide | MySQL Database Tutorial
Post a Comment