MySQL Tutorial – Creating a Table in MySQL

How to create a database table in MySQL?

After we have created our database the next thing to do is to create a table. To create a table, use the CREATE TABLE command. We are going to name our table as “employee_record”.

Note: in order to create table, first we are going to choose on which database we are going to insert the table.

Issue the command:

USE employee;

Our table consists of the following field: (id, f_name, l_name, position, age, salary, email)

To create the employee table, issue the following command:

 CREATE TABLE employee_record
 (
 id int unsigned not null auto_increment primary key,
 f_name varchar(20),
 l_name varchar(20),
 position varchar(30),
 age int,
 salary int,
 email varchar(60)
 );

Note: if you have encoded the command correctly, it will display success message:

Query OK, 0 rows affected (0.11 sec)

Now that we’ve created the table, issue the command to display the list of tables.

SHOW TABLES;

Result:

+--------------------+
| Tables_in_employee |
+--------------------+
| employee_record    |
+--------------------+
1 row in set (0.00 sec)

To display the lists all the column names along with their column types of the table. Use the DESCRIBE command:

DESCRIBE employee_record;

Result:

+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(10) unsigned |      | PRI | 0       | auto_increment |
| f_name | varchar(20)      | YES  |     | NULL    |                |
| l_name | varchar(20)      | YES  |     | NULL    |                |
|position| varchar(30)      | YES  |     | NULL    |                |
| age    | int(11)          | YES  |     | NULL    |                |
| salary | int(11)          | YES  |     | NULL    |                |
| email  | varchar(60)      | YES  |     | NULL    |                |
+--------+------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
, , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.