Relational databases store information in tables.
To work with PostgreSQL, students must understand four important terms:
A table is a database object that stores related data in rows and columns.
Example: A university may use a students table to store student information.
A column represents a particular type of information stored in a table.
For example:
Each column has a name and a data type.
A row represents one complete entry in a table.
For example, the information of one student is stored in one row.
A record is another term for a row.
It represents a complete set of related values in a table.
Consider the following students table:
| student_id | student_name | age | department |
|---|---|---|---|
| 101 | Ali | 20 | CS |
| 102 | Ahmed | 21 | IT |
| 103 | Sara | 19 | Mathematics |
| 104 | Ayesha | 22 | English |
Understanding this table:
studentsstudent_idstudent_nameCREATE TABLE students (
student_id INT,
student_name VARCHAR(100),
age INT,
department VARCHAR(50)
);
Explanation:
| Column | Data Type | Purpose |
|---|---|---|
| student_id | INT | Stores student ID |
| student_name | VARCHAR(100) | Stores student name |
| age | INT | Stores age |
| department | VARCHAR(50) | Stores department name |
To view all records:
SELECT * FROM students;
To view only student names:
SELECT student_name
FROM students;
To view student names and ages:
SELECT student_name, age
FROM students;
Consider the following table:
| employee_id | employee_name | salary | department |
|---|---|---|---|
| 1 | Ali | 50000 | IT |
| 2 | Ahmed | 60000 | HR |
| 3 | Sara | 55000 | Finance |
| 4 | Ayesha | 65000 | IT |
Answer the following questions: