This is a premium alert message you can set from Layout! Get Now!

Creating a Database and Table

Namaste learners! ๐Ÿ™, So far, you’ve learned what SQL is, its basic syntax, and how data is structured in databases using tables, rows, columns, and data types.

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:

  1. What SQL command creates a new database?
  2. What keyword do you use to define a column with decimal values?
  3. What does PRIMARY KEY do?

๐Ÿ‘‰ 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 (...);
Tags

Post a Comment

0 Comments
Post a Comment
To Top