Lesson 8 of 60 – Tables, Rows & Columns
13%

Tables, Rows & Columns

Tables, rows, and columns are the basic building blocks of a relational database. Understanding these three concepts is essential before learning SQL commands such as SELECT, INSERT, UPDATE, and DELETE.

Note: A table stores data in rows and columns. A row represents one record, while a column represents a particular type of information.

1. What is a Table?

A table is a database object used to store related information in a structured format.

A table is made up of rows and columns.

For example, a students table may look like this:

students

id | name  | age | course
---|-------|-----|--------
1  | Rahul | 21  | Python
2  | Priya | 22  | SQL
3  | Amit  | 20  | Java

Here, students is the name of the table.

2. What is a Row?

A row represents one complete record in a table.

For example:

1 | Rahul | 21 | Python

The above row contains information about one student.

Rows are also commonly called records.

3. What is a Column?

A column represents a particular type or attribute of data in a table.

For example, the students table has the following columns:

  • id
  • name
  • age
  • course

Columns are also commonly called fields.

4. Table Structure

A table can be visualized as a grid containing columns and rows.

id name age course
1 Rahul 21 Python
2 Priya 22 SQL
3 Amit 20 Java

The four headings are columns, while each horizontal entry below the headings represents a row.

5. Table Name

Every table has a name that identifies it within a database.

Examples:

  • students
  • employees
  • products
  • orders
  • customers
  • payments

A good table name should clearly describe the type of information stored in the table.

6. Column Names

Each column should have a meaningful name that describes the data stored in that column.

For example:

students

student_id
student_name
mobile
email
course

Meaningful names make database structures easier to understand and maintain.

7. Data Types of Columns

Every column normally has a defined data type that determines what kind of values it can store.

Common SQL data types include:

  • INT – Whole numbers
  • VARCHAR – Variable-length text
  • DATE – Date values
  • DECIMAL – Exact numeric values
  • BOOLEAN – Boolean values where supported

Example:

student_id INT
name VARCHAR(100)
fee DECIMAL(10,2)
admission_date DATE

8. Creating a Table

The CREATE TABLE statement is used to create a new table.

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    age INT,
    course VARCHAR(100)
);

This creates a table named students with four columns.

9. Inserting Rows

The INSERT INTO statement is used to add rows to a table.

INSERT INTO students
(id, name, age, course)
VALUES
(1, 'Rahul', 21, 'Python');

This statement adds one student record to the table.

10. Inserting Multiple Rows

Multiple rows can be inserted using one INSERT statement.

INSERT INTO students
(id, name, age, course)
VALUES
(1, 'Rahul', 21, 'Python'),
(2, 'Priya', 22, 'SQL'),
(3, 'Amit', 20, 'Java');

This adds three records to the students table.

11. Selecting Data from a Table

The SELECT statement is used to retrieve information from a table.

SELECT * FROM students;

The asterisk * means all columns.

The query returns all rows and columns from the students table.

12. Selecting Specific Columns

Instead of selecting every column, we can specify only the columns we need.

SELECT name, course
FROM students;

This query returns only the name and course columns.

13. Adding a New Column

The ALTER TABLE statement can be used to modify the structure of an existing table.

For example, to add an email column:

ALTER TABLE students
ADD email VARCHAR(150);

The students table now contains an additional email column.

14. Updating a Row

The UPDATE statement is used to modify existing data.

UPDATE students
SET course = 'Django'
WHERE id = 1;

This changes the course of the student whose ID is 1.

Important: Always use a suitable WHERE condition when updating specific records.

15. Deleting a Row

The DELETE statement removes rows from a table.

DELETE FROM students
WHERE id = 3;

This removes the student whose ID is 3.

Warning: A DELETE statement without a WHERE condition can delete all rows from a table.

16. Primary Key Column

A primary key uniquely identifies each row in a table.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    age INT,
    course VARCHAR(100)
);

Here, the id column is the primary key.

Two rows should not have the same primary key value.

17. NULL Values

A NULL value represents missing, unknown, or inapplicable data. It is different from zero and different from an empty string.

For example:

id | name  | email
---|-------|----------------
1  | Rahul | rahul@mail.com
2  | Priya | NULL

In this example, Priya's email value is NULL.

18. Constraints on Columns

Constraints are rules applied to columns to help maintain valid and consistent data.

Common constraints include:

  • PRIMARY KEY
  • FOREIGN KEY
  • NOT NULL
  • UNIQUE
  • DEFAULT
  • CHECK

19. Multiple Tables in a Database

A database normally contains multiple tables for different types of information.

For example, a school database may contain:

  • students
  • teachers
  • courses
  • attendance
  • payments
  • exams

Related tables can be connected using keys.

20. Example of Related Tables

Consider a student management system.

students
----------------
id
name
course_id

courses
----------------
id
course_name
fee

The course_id in the students table can refer to the id in the courses table.

This allows the application to store student and course information separately while maintaining a relationship between them.

21. Table vs Row vs Column

Term Meaning Example
Table Collection of related records students
Row One complete record 1, Rahul, 21, Python
Column One type of information name

22. Table Design Example

Suppose we want to create an employee table.

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(100),
    department VARCHAR(100),
    salary DECIMAL(10,2)
);

This table contains four columns:

  • employee_id
  • employee_name
  • department
  • salary

23. Good Table Design Practices

A well-designed table makes a database easier to maintain and use.

  • Use meaningful table names.
  • Use meaningful column names.
  • Choose suitable data types.
  • Use primary keys where appropriate.
  • Use relationships between related tables.
  • Avoid unnecessary duplicate data.
  • Use appropriate constraints.
  • Keep table structure simple and organized.

24. Real-Life Example

Consider an online shopping website. It may contain tables such as:

  • customers
  • products
  • orders
  • order_items
  • payments

Each table stores a specific type of information. Relationships between these tables allow the application to manage customers, products, orders, and payments together.

25. Complete Example

Here is a simple example showing table creation, inserting data, and retrieving data.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    age INT,
    course VARCHAR(100)
);

INSERT INTO students
(id, name, age, course)
VALUES
(1, 'Rahul', 21, 'Python'),
(2, 'Priya', 22, 'SQL');

SELECT * FROM students;

This example demonstrates the basic relationship between tables, rows, columns, and SQL statements.

📌 Key Points

  • A table stores related data in a structured format.
  • A row represents one record.
  • A column represents a particular type of information.
  • Tables consist of rows and columns.
  • Each column has a suitable data type.
  • CREATE TABLE is used to create a table.
  • INSERT INTO is used to add rows.
  • SELECT is used to retrieve data.
  • UPDATE is used to modify existing records.
  • DELETE is used to remove records.
  • Primary keys uniquely identify records.
  • Multiple tables can be connected using relationships and keys.

🧠 Quick Quiz

Question: What does a row represent in a database table?