Learn How To Create Tables In Mysql Databases.

Before following simple, well commented, step by step guide on how to create tables in mysql databases.Consider visiting the previous tutorial on How to create a Database in MYSQL. This is complusary if you are completely new to database Management or working with databases.

Back to the topic, to ceate a new table with in the database, you use the Mysql command CREATE TABLE. This command is one of th most complex statements in Mysql.

For this tutorial i will base on the database(mybusiness) that i created in the previous tutorial or post.I came up with a random unique name for my database(mybusiness) and it's inside this database that we will pretend creating this table inside.

Let's first look at the basic syntax of creating a table.

Basic syntax
copy
            
CREATE TABLE table_name(
//list_of_columns
column_name dataType(length value)
        
)

The following command will illustrate the syntax of creating a database table. For this case we are going to create a table called employees inside our database mybusiness.Let's come up with a random list of columns that can be inside a users table, in particular employees for this tutorial.

The following are the columns/fields that we are going to create in the table(employees).

  • id
  • first_name - His/Her first name
  • last_name - His/Her last name
  • email - mail of the employee
  • position - rank of the employee in the organization
copy

            
CREATE TABLE employees(
            
 id INT(11) NOT NULL AUTO_INCREMENT;
 first_name VARCHAR(100) NOT NULL;
 last_name VARCHAR(100) NOT NULL;
 email VARCHAR(255) NOT NULL;
 position VARCHAR(100) NOT NULL;
 PRIMARY KEY(id);
        
)

Example explained

First,choose the table name that you want to create after the CREATE TABLE clause, for this case our table is called employees.Remember the name should be unique in the database. By using this simple command SHOW DATABASES it will display all database in the system hence avoiding name repeation and name decision easy.

id - This represents a column called id in the database.

INT - This represents the data type of the column. For this case id column will be an interger data type.

VARCHAR - This represents the data type of the column. For this case any column with VARCHAR will be a variable character(number,text,strings etc) data type.

NOT NULL - This indicates that the field or column cannot be empty.

AUTO_INCREMENT - This will be incremented by 1 whenever a new employee is added to the table.In simple terms it will be automatic.Forexample employee - 1,2,3,4,5,6

PRIMARY KEY - This will make the id field or column a unique field with out duplicates.Forexample, if there is an employee with the id of (1).He or She will be the only employee with that id in the system and no one else.

Alright guys,that's how you can create a table in the mysql database.If this tutorial was very useful to you consider subscribing to my channel.

Subscribe

Search it,Get it

  • LEARN MORE

YOU MAY LIKE THESE POSTS

share