How to Create a Table in MySQL Workbench

How to Create a Table in MySQL Workbench

In this article, we will see how to create a table in MySQL workbench. In MySQL, data of a business, a company, an organization, etc. organized in tables. To create a table in MySQL, we take the help of create table command.

Syntax:

create table [if not exists ] tablename(
columnname datatype [(size) not null default value extra,
columnname datatype(size) not null default value extra,
...
primary key(columnname separated by comma),
foreign key(coumnname separated b comma) references
othertablename(othertablecolumnname separated by comma)]
);

Note:

  1. While creating a table; If you know that the table that you want to create does not already exist in the database then you need not specify if not exists clause
  2. Datatype : indicates what type of data a column can hold
    char, varchar, text, tinytext, mediumtext, longtext, blob, mediumblob, longblob,
    boolean,
    tinyint, smallint, mediumint, int, bigint,
    float, double, decimal,
    date, datetime, time, timestamp
  3. size: indicates how many characters or digits a column can hold
  4. not null: indicates whether a column can have a null value or not
  5. The default value: indicates the default value of a column
  6. Extra:  indicates extra attributes of a column
    unsigned, zerofill, auto_increment
  7. The primary key: indicates columns must have unique values
  8. The foreign key: indicates columns whose values must match the values in the primary key.

Example

how to create a table in mysql workbench

  1. Here, we have created a table using the above syntax. First, we have to tell, “create table” and then we gave the table name tbl_student.  Inside the parameter, we have given column names “id” and “int” datatype. Then column name “name” and char datatype.
  2. To show the tables, we have follows: show tables; comment.
  3. See, the above comment prompt, the table is displayed successfully.

Leave a Reply