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:
- 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
- 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 - size: indicates how many characters or digits a column can hold
- not null: indicates whether a column can have a null value or not
- The default value: indicates the default value of a column
- Extra: indicates extra attributes of a column
unsigned, zerofill, auto_increment - The primary key: indicates columns must have unique values
- The foreign key: indicates columns whose values must match the values in the primary key.
Example
- 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.
- To show the tables, we have follows: show tables; comment.
- See, the above comment prompt, the table is displayed successfully.