How to Create a Table in MySQL

MySQL is Oracle supported open source relational database management system (RDBMS) based on Structured Query Language (SQL). The best thing about MySQL is it runs on different operating systems like Linux, Windows, etc. Using MySQL you can easily create a database in MySQL. The database contains information stored in the form of tables. A table is utilized to format information as rows and columns and utilized for both storing and showing records in the structure format. Here we are learning about how to create a table in MySQL.

Table of Contents

How to Create a Table in MySQL

The table creation command in MySQL requires three things.

1. Name of the table

2. Name of the fields.

3. Definition of each field.

The syntax for table creation in MySQL

CREATE TABLE table_name (column_name column_type);
  1. Name of the table: In the MySQL database, every table has a unique name. The name of the table contains that table name. which is uniquely identified.
  2. Name of the fields: Not only table has a unique name in MySQL but also columns have a name.  The table contains other fields like primary key, unique key, etc.
  3. Definition of each field: You need to define each field while creating a table.

We will create the following table in the world database.

create table country(
   country_id INT NOT NULL AUTO_INCREMENT,
   country_name VARCHAR(100) NOT NULL,
   country-capital VARCHAR(40) NOT NULL,
   independence_day DATE,
   PRIMARY KEY ( country_id )
);

How to Create a Table in MySQL On Linux

You can easily create a table in MySQL in Linux.  The syntax will remain the same as above. You need only to log in to MySQL and run the command of creating a table and it will create your desired table.

root@host# mysql -u root -p
Enter password:*******
mysql> use world;
Database changed
mysql> CREATE TABLE country(
-> country_id INT NOT NULL AUTO_INCREMENT,
-> country_name VARCHAR(100) NOT NULL,
-> country-capital VARCHAR(40) NOT NULL,
-> independence_day DATE,
-> PRIMARY KEY ( country_id )
-> );
Query OK, 0 rows affected (0.10 sec)
Thus you can create a table in MySQL using Linux and workbench.

 

By Sameer

I'm Sameer Bille, a blogger from Mumbai, India. I started MuchTech as a passion.Here at Much Tech I write about Tech Tips,Tricks and how to guide.

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.