It is very easy to create a table in MySQL. You need to use the Create table statement to create a table in MySQL. Please check below example
create table table_name( column1 datatype, column2 datatype, column3 datatype );
There are three main elements in creating a table:
1. Table name: Name you want to give to your table. You can select any name.
2. column: Column name in your table.
3. datatype: Type of data you are going to store in that column.
But we can add more elements to a table.
let’s do a live example with a brief explanation:
create table students( roll_no int NOT NULL AUTO_INCREMENT, student_name varchar(255) NOT NULL, student_address varchar(255) NOT NULL, PRIMARY KEY (roll_no) );
Let’s explain these above statements:
- Table name: students
- columns: roll_no, student_name, student_address
- datatype: int, varchar
- Not Null: NOT NULL is used to prevent a user to create a record with a NULL value. If a user will try to create a record with NULL value then MySQL will display an error message.
- AUTO_INCREMENT: Field Attribute AUTO_INCREMENT tells MySQL to go ahead and add the next available number to the id field.
- PRIMARY KEY: Keyword PRIMARY KEY is used to define a column as a primary key.