Creating First Table-tutorial
Now that you have created your first database (SchoolDB), it is time to fill it with data and for that, we need tables!
A table is where your actual data lives just like a sheet in Excel, with rows and columns.
Each row is a record (like a student), and each column is a field (like Name, Age, or Grade).
Step 1: Selecting the Database
Before creating a table, always make sure you’re inside the database where you want the table to exist.
Use the USE command
This tells MySQL
I want to work inside the SchoolDB database now.
Let us create a table called Students inside our SchoolDB database.
Output
You are successfully created your first table.
Output
USE SchoolDB;
This tells MySQL
I want to work inside the SchoolDB database now.
Step 2: Writing the CREATE TABLE Command
The basic syntax to create a table is
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ...);
Let us create a table called Students inside our SchoolDB database.
CREATE TABLE Students ( StudentID INT, Name VARCHAR(50), Age INT, City VARCHAR(50));
Explanation
CREATE TABLE Students - Creates a table named Students.
Each line defines a column name and its data type:
- StudentID - INT (whole numbers)
- Name - VARCHAR(50) (text up to 50 characters)
- Age - INT
- City - VARCHAR(50)
Step 3: Viewing Your Table
After creating the table, you can check if it exists using
SHOW TABLES;
Output
+-------------------+| Tables_in_SchoolDB |+-------------------+| Students |+-------------------+
You are successfully created your first table.
Step 4: Checking Table Structure
To see your table’s columns and data types, use
DESCRIBE Students;
Output
Field Type Null Key Default Extra StudentID int YES
NULL
Name varchar(50) YES
NULL
Age int YES
NULL
City varchar(50) YES
NULL
Two Minute Drill
- CREATE TABLE --> Used to create a new table
- Each column must have a name and datatype
- Use SHOW TABLES --> To see all tables in the database
- Use DESCRIBE table_name --> To view table structure
- Always select your database before creating a table (USE database_name;)