Understanding Tables, Rows, and Columns
Imagine you have a big notebook where you keep track of all your favorite toys, video games, and books. This notebook has tables that help you organize everything neatly. Tables are like grids with rows and columns where you can store information. Let’s dive in and understand these in simple terms.
📚 What is a Table?
A table in a database is like a spreadsheet. It’s a structured way to organize and store data. For example, if you want to keep track of your toy collection, you can create a table like this:
Toy ID | Toy Name | Type | Color |
---|---|---|---|
1 | Teddy Bear | Stuffed | Brown |
2 | Remote Car | Electronic | Red |
3 | Puzzle Blocks | Wooden | Rainbow |
Key Points:
- Columns: The vertical lines (e.g., “Toy ID”, “Toy Name”, etc.) are columns. Each column stores one type of information.
- Rows: The horizontal lines (e.g., the first row with “1, Teddy Bear, Stuffed, Brown”) are rows. Each row is one record of data.
🎨 What are Columns?
Columns are like the labels at the top of your table. They tell you what kind of information is stored in that part of the table.
In our example:
Toy ID
: A unique number for each toy.Toy Name
: The name of the toy.Type
: What kind of toy it is.Color
: The toy’s color.
Think of columns as categories that keep everything neat.
👦 What are Rows?
Rows are like the details of one thing on your table. Each row is a single record. It contains information about one item (in our example, one toy).
For example:
- Row 1: Information about the “Teddy Bear”.
- Row 2: Information about the “Remote Car”.
🛠 Working with Tables Using SQL
In a database, we use SQL (Structured Query Language) to create, read, and manage tables.
Creating a Table
Here’s how you can create the toy table:
CREATE TABLE Toys (
ToyID INT PRIMARY KEY,
ToyName VARCHAR(50),
Type VARCHAR(20),
Color VARCHAR(20)
);
This SQL command creates a table named Toys with four columns.
Adding Data to the Table
Now, let’s add some toys to our table:
INSERT INTO Toys (ToyID, ToyName, Type, Color)
VALUES
(1, 'Teddy Bear', 'Stuffed', 'Brown'),
(2, 'Remote Car', 'Electronic', 'Red'),
(3, 'Puzzle Blocks', 'Wooden', 'Rainbow');
Viewing the Data
To see what’s inside your table, use:
SELECT * FROM Toys;
Output:
ToyID | ToyName | Type | Color |
---|---|---|---|
1 | Teddy Bear | Stuffed | Brown |
2 | Remote Car | Electronic | Red |
3 | Puzzle Blocks | Wooden | Rainbow |
Updating Data
Want to change the color of the “Teddy Bear”? Use this:
UPDATE Toys
SET Color = 'Golden'
WHERE ToyID = 1;
Deleting Data
If you want to remove the “Remote Car”:
DELETE FROM Toys
WHERE ToyID = 2;
🧩 Practice Questions
1. Create a “Books” table with columns: BookID
, Title
, Author
, and Genre
.
2. Add three books to the “Books” table.
3. Write a query to find all books written by “J.K. Rowling”.
4. Update the genre of a book to “Fantasy”.
5. Delete a book from the table.
🚀 Interview Questions
Basic:
- What is a table in a database? Answer: A table organizes data into rows and columns, like a spreadsheet.
- What are the columns in a table? Answer: Columns store one type of information, like names or dates.
- What are rows in a table? Answer: Rows are individual records or entries in a table.
Advanced:
- Write a query to add a new column
Price
to the “Toys” table.
ALTER TABLE Toys
ADD Price DECIMAL(5, 2);
- How do you retrieve only the “Toy Name” and “Color” from the “Toys” table?
SELECT ToyName, Color FROM Toys;
- Explain the difference between
DELETE
andDROP
. Answer:DELETE
removes specific rows from a table, whileDROP
deletes the entire table from the database.
Tables, rows, and columns are the backbone of databases. Think of tables as your organized notebooks, columns as the labels, and rows as the entries. Practice creating and managing tables, and soon you’ll be a pro at organizing data!