How to Create Table in a MySQL Database


How to Create Table in a MySQL Database

How to create a table in MySQL:
- In MySQL; data of a business, a company, an organization etc. organized in tables
- To create a table; we take help of create table command

Basic 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(s) separated by comma),
foreign key(columnname(s) separated by comma) references
othertablename(othertablecolumnname(s) separated by comma) ]

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 to specify if not exists clause

2. datatype indicates what type of data a column can hold
char, varchar, text, tinytext, mediumtext, longtext,
blob, medium blob, long blob,
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 null value or not

5. default value indicates default value of a column

6. extra indicates extra attributes of a column
unsigned, zerofill , auto_increment

7. primary key indicates a column(s) must have unique values

8. foreign key indicates a column(s) whose values must match values in a primary key column(s) in another table

Interview Questions: