Now it’s time to learn how to create your own database and tables using the CREATE statement — the real first step in building anything with SQL! ๐
๐ฆ What is a Database?
A database is a container that holds one or more tables, along with other elements like views, stored procedures, etc.
In simple words, it’s like a folder, and tables are files inside it.
๐ง SQL Command: CREATE DATABASE
To create a new database in SQL, use:
CREATE DATABASE school;
✅ This will create a new database named school.
Note: Some systems (like MySQL) require you to switch to the database using:
USE school;
๐ SQL Command: CREATE TABLE
After creating the database, the next step is to create a table where you’ll store the data.
Let’s reuse our example from earlier and create a students table inside the school database:
CREATE TABLE students (id INT PRIMARY KEY,name VARCHAR(50),age INT,course VARCHAR(30),marks DECIMAL(5,2));
๐ Breakdown of this table:
| Column | Data Type | Purpose |
|---|---|---|
id |
INT |
Unique student ID |
name |
VARCHAR(50) |
Student's name |
age |
INT |
Student's age |
course |
VARCHAR(30) |
Name of the course (e.g., SQL) |
marks |
DECIMAL(5,2) |
Score in decimal format |
๐งช Full Example: Creating DB + Table
CREATE DATABASE school;USE school;CREATE TABLE students (id INT PRIMARY KEY,name VARCHAR(50),age INT,course VARCHAR(30),marks DECIMAL(5,2));
Boom! You now have a students table inside a school database — ready to store data. ๐ฏ
⚠️ Common Mistakes to Avoid
| Mistake | Tip |
|---|---|
Forgetting USE |
Always select the right database before creating tables. |
| Duplicate Table Name | Make sure the table name is unique inside the database. |
| Wrong Data Type | Choose the correct data type based on the kind of data. |
๐ง Quiz Time!
Let’s see how much you remember:
- What SQL command creates a new database?
- What keyword do you use to define a column with decimal values?
- What does
PRIMARY KEYdo?
๐ Comment your answers below on the blog!
✅ Summary
| Concept | SQL Command |
|---|---|
| Create a DB | CREATE DATABASE dbname; |
| Use a DB | USE dbname; |
| Create a Table | CREATE TABLE tablename (...); |