Skip to content

Commit ec51058

Browse files
feat: add relational databases review (freeCodeCamp#59307)
Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com>
1 parent e071900 commit ec51058

File tree

1 file changed

+379
-1
lines changed

1 file changed

+379
-1
lines changed

curriculum/challenges/english/25-front-end-development/review-relational-database/6724e3d6e1cb0c1fec3a8e4f.md

Lines changed: 379 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,385 @@ dashedName: review-relational-database
77

88
# --description--
99

10-
Description placeholder
10+
## Introduction to Relational Databases
11+
12+
- **Relational Databases**: Organize data into related tables made of rows and columns. Each row represents a record, and each column represents an attribute of the data.
13+
- **Advantages of Relational Databases**: Scalable, widely applicable across domains (e.g., healthcare, business, gaming), and structured to maintain reliable data.
14+
- **Common Use Cases**: Web development, inventory systems, e-commerce, healthcare, and enterprise applications.
15+
16+
## Key Concepts
17+
18+
- **Schema**: A relational database requires a schema that defines its structure—tables, columns, data types, constraints, and relationships.
19+
- **Primary Keys**: Unique identifiers for each row in a table. They are essential for data integrity and are used to relate records between tables via foreign keys.
20+
- **Foreign Keys**: References to primary keys in another table, used to link related data across tables.
21+
- **Relationships**: By connecting tables through primary and foreign keys, you can structure normalized data and perform meaningful queries.
22+
- **Entity Relationship Diagrams (ERDs)**: Visualize how entities (tables) relate to each other in a database schema.
23+
- **Data Integrity**: Enforced using keys and data types. Ensures consistency and accuracy of stored data.
24+
25+
## SQL Basics
26+
27+
- **Queries**: Requests to retrieve specific data from the database.
28+
29+
```sql
30+
SELECT * FROM dogs WHERE age < 3;
31+
```
32+
33+
- **WHERE clause**: Filter results based on conditions. Use comparison operators like `<`, `=`, `>`, etc.
34+
- **Select with ORDER BY**: Retrieve and sort results based on a column.
35+
36+
```sql
37+
SELECT columns FROM table_name ORDER BY column_name;
38+
```
39+
40+
## Table Operations
41+
42+
- **`CREATE TABLE` Statement**: This statement is used to create a new table in a database.
43+
44+
```sql
45+
CREATE TABLE first_table();
46+
```
47+
48+
- **`ALTER TABLE ADD COLUMN` Statement**: This statement is used to add a column to an existing table.
49+
50+
```sql
51+
ALTER TABLE table_name ADD COLUMN column_name DATATYPE;
52+
```
53+
54+
- **`ALTER TABLE DROP COLUMN` Statement**: This statement is used to drop a column from an existing table.
55+
56+
```sql
57+
ALTER TABLE table_name DROP COLUMN column_name;
58+
```
59+
60+
- **`ALTER TABLE RENAME COLUMN` Statement**: This statement is used to rename a column in a table.
61+
62+
```sql
63+
ALTER TABLE table_name RENAME COLUMN column_name TO new_name;
64+
```
65+
66+
- **`DROP TABLE` Statement**: This statement is used to drop an entire table from the database.
67+
68+
```sql
69+
DROP TABLE table_name;
70+
```
71+
72+
- **`ALTER DATABASE RENAME` Statement**: This statement is used to rename a database.
73+
74+
```sql
75+
ALTER DATABASE database_name RENAME TO new_database_name;
76+
```
77+
78+
- **`DROP DATABASE` Statement**: This statement is used to drop an entire database.
79+
80+
```sql
81+
DROP DATABASE database_name;
82+
```
83+
84+
## Constraints & Data Integrity
85+
86+
- **`ALTER TABLE ADD COLUMN` with Constraint**: This statement is used to add a column with a constraint to an existing table.
87+
88+
```sql
89+
ALTER TABLE table_name ADD COLUMN column_name DATATYPE CONSTRAINT;
90+
```
91+
92+
- **`NOT NULL` Constraint**: This constraint ensures that a column cannot have NULL values.
93+
94+
```sql
95+
column_name VARCHAR(50) NOT NULL
96+
```
97+
98+
- **`ALTER TABLE ADD PRIMARY KEY` Statement**: This statement is used to add a primary key constraint to a table.
99+
100+
```sql
101+
ALTER TABLE table_name ADD PRIMARY KEY(column_name);
102+
```
103+
104+
- **`ALTER TABLE DROP CONSTRAINT` Statement**: This statement is used to drop a constraint from a table.
105+
106+
```sql
107+
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
108+
```
109+
110+
- **`ALTER TABLE ADD COLUMN` with Foreign Key**: This statement is used to add a foreign key column that references another table.
111+
112+
```sql
113+
ALTER TABLE table_name ADD COLUMN column_name DATATYPE REFERENCES referenced_table_name(referenced_column_name);
114+
```
115+
116+
- **`ALTER TABLE ADD UNIQUE` Statement**: This statement is used to add a UNIQUE constraint to a column.
117+
118+
```sql
119+
ALTER TABLE table_name ADD UNIQUE(column_name);
120+
```
121+
122+
- **`ALTER TABLE ALTER COLUMN SET NOT NULL` Statement**: This statement is used to set a NOT NULL constraint on an existing column.
123+
124+
```sql
125+
ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;
126+
```
127+
128+
- **`INSERT` Statement with NULL Values**: This statement demonstrates how to insert NULL values into a table.
129+
130+
```sql
131+
INSERT INTO table_name(column_a) VALUES(NULL);
132+
-- or
133+
INSERT INTO table_name(column_b) VALUES('value'); -- if column_a allows nulls
134+
```
135+
136+
- **Composite Primary Key**: This constraint defines a primary key that consists of multiple columns.
137+
138+
```sql
139+
CREATE TABLE course_enrollments (
140+
student_id INT,
141+
course_id INT,
142+
PRIMARY KEY (student_id, course_id)
143+
);
144+
```
145+
146+
## Data Manipulation (CRUD)
147+
148+
- **`INSERT` Statement**: This statement is used to insert a single row into a table.
149+
150+
```sql
151+
INSERT INTO table_name(column_1, column_2) VALUES(value1, value2);
152+
```
153+
154+
- **`INSERT` Statement with Omitted Columns**: This statement shows how to insert values without explicitly listing the column names, relying on the default column order in the table.
155+
156+
```sql
157+
INSERT INTO dogs VALUES ('Gino', 3);
158+
```
159+
160+
- **`INSERT` Statement with Multiple Rows**: This statement is used to insert multiple rows into a table in a single operation.
161+
162+
```sql
163+
INSERT INTO dogs (name, age) VALUES
164+
('Gino', 3),
165+
('Nora', 2);
166+
```
167+
168+
- **`UPDATE` Statement**: This statement is used to update existing data in a table based on a condition.
169+
170+
```sql
171+
UPDATE table_name SET column_name=new_value WHERE condition;
172+
```
173+
174+
- **`DELETE` Statement**: This statement is used to delete rows from a table based on a condition.
175+
176+
```sql
177+
DELETE FROM table_name WHERE condition;
178+
```
179+
180+
## Data Types
181+
182+
- **`NUMERIC` Data Type**: This data type is used to store precise decimal numbers with a specified precision and scale.
183+
184+
```sql
185+
price NUMERIC(10, 2)
186+
```
187+
188+
- **`TEXT` Data Type**: This data type is used to store variable-length character strings with no specific length limit.
189+
190+
```sql
191+
column_name TEXT
192+
```
193+
194+
- **`INTEGER` Data Type**: This data type is used to store whole numbers without decimal places.
195+
196+
```sql
197+
units_sold INTEGER
198+
```
199+
200+
- **`SMALLINT` and `BIGINT` Data Types**: These are variants of INTEGER with smaller and larger ranges respectively.
201+
202+
- **`SERIAL` Data Type**: This data type is used to create auto-incrementing integer columns in PostgreSQL.
203+
204+
```sql
205+
id SERIAL
206+
```
207+
208+
- **`AUTO_INCREMENT` Attribute**: This attribute is used in MySQL to create auto-incrementing integer columns.
209+
210+
```sql
211+
id INT AUTO_INCREMENT
212+
```
213+
214+
- **`VARCHAR` Data Type**: This data type is used to store variable-length character strings with a specified maximum length.
215+
216+
```sql
217+
name VARCHAR(50)
218+
```
219+
220+
- **`DATE` Data Type**: This data type is used to store date values (year, month, day).
221+
222+
```sql
223+
event_date DATE
224+
```
225+
226+
- **`TIME` Data Type**: This data type is used to store time values (hour, minute, second).
227+
228+
```sql
229+
start_time TIME
230+
```
231+
232+
- **`TIMESTAMP` Data Type**: This data type is used to store date and time values, optionally with time zone information.
233+
234+
```sql
235+
event_timestamp TIMESTAMP
236+
event_timestamp TIMESTAMP WITH TIME ZONE
237+
```
238+
239+
- **`BOOLEAN` Data Type**: This data type is used to store true/false values.
240+
241+
```sql
242+
is_active BOOLEAN
243+
```
244+
245+
## Database Relationships
246+
247+
- **Types of Relationships**: These are the different ways tables can be related to each other in a relational database.
248+
- One-to-one
249+
- One-to-many
250+
- Many-to-one
251+
- Many-to-many
252+
- Self-referencing (recursive)
253+
254+
- **One-to-One Relationship**: This relationship type means that each record in one table corresponds to exactly one record in another table.
255+
256+
```md
257+
One employee is assigned exactly one vehicle.
258+
Tables: employees, vehicles
259+
```
260+
261+
- **One-to-Many Relationship**: This relationship type means that one record in a table can be associated with multiple records in another table.
262+
263+
```md
264+
One customer can have many orders.
265+
Tables: customers → orders
266+
```
267+
268+
- **Many-to-Many Relationship via Junction Table**: This relationship type is implemented using a junction table that contains foreign keys from both related tables.
269+
270+
```sql
271+
CREATE TABLE books_authors (
272+
author_id INT REFERENCES authors(id),
273+
book_id INT REFERENCES books(id)
274+
);
275+
```
276+
277+
- **Self-Referencing Relationships**: This relationship type occurs when a table references itself, creating a hierarchical structure.
278+
279+
```md
280+
An employee table where each employee may report to another employee.
281+
```
282+
283+
## Advanced SQL (Joins)
284+
285+
- **`INNER JOIN` Statement**: This join returns only the rows that have matching values in both tables.
286+
287+
```sql
288+
SELECT *
289+
FROM products
290+
INNER JOIN sales ON products.product_id = sales.product_id;
291+
```
292+
293+
- **`FULL OUTER JOIN` Statement**: This join returns all rows from both tables, including unmatched rows from either table.
294+
295+
```sql
296+
SELECT *
297+
FROM products
298+
FULL OUTER JOIN sales ON products.product_id = sales.product_id;
299+
```
300+
301+
- **`LEFT OUTER JOIN` Statement**: This join returns all rows from the left table and matching rows from the right table.
302+
303+
```sql
304+
SELECT *
305+
FROM products
306+
LEFT JOIN sales ON products.product_id = sales.product_id;
307+
```
308+
309+
- **`RIGHT OUTER JOIN` Statement**: This join returns all rows from the right table and matching rows from the left table.
310+
311+
```sql
312+
SELECT *
313+
FROM products
314+
RIGHT JOIN sales ON products.product_id = sales.product_id;
315+
```
316+
317+
- **`SELF JOIN` Statement**: This join is used to join a table with itself to compare rows within the same table.
318+
319+
```sql
320+
SELECT A.column_name, B.column_name
321+
FROM table_name A
322+
JOIN table_name B ON A.related_column = B.related_column;
323+
```
324+
325+
- **`CROSS JOIN` Statement**: This join returns the Cartesian product of two tables, combining every row from the first table with every row from the second table.
326+
327+
```sql
328+
SELECT *
329+
FROM table1
330+
CROSS JOIN table2;
331+
```
332+
333+
## PostgreSQL Specific Commands
334+
335+
- **`psql` Login Command**: This command is used to log in to PostgreSQL with specific username and database.
336+
337+
```bash
338+
psql --username=freecodecamp --dbname=postgres
339+
```
340+
341+
- **`\l` Command**: This command lists all databases in the PostgreSQL instance.
342+
343+
```sql
344+
\l
345+
```
346+
347+
- **`CREATE DATABASE` and `\c` Commands**: These commands are used to create a new database and connect to it.
348+
349+
```sql
350+
CREATE DATABASE database_name;
351+
\c database_name
352+
```
353+
354+
- **`\d` Command**: This command lists all tables in the current database.
355+
356+
```sql
357+
\d
358+
```
359+
360+
- **`\d table_name` Command**: This command displays the schema/structure of a specific table.
361+
362+
```sql
363+
\d table_name
364+
```
365+
366+
- **`\q` Command**: This command exits the PostgreSQL client.
367+
368+
```bash
369+
\q
370+
```
371+
372+
## Relational vs Non-Relational
373+
374+
- **Non-Relational (NoSQL) Databases**: Store unstructured or semi-structured data. Do not require a rigid schema and are more flexible for evolving data models.
375+
- **Choosing Between Relational and Non-Relational**: Depends on the nature of your data and application requirements.
376+
- **Relational vs Non-Relational**: Choose relational for structured data and consistency; NoSQL for flexibility and fast-changing data.
377+
378+
## Popular RDBMS Systems
379+
380+
- **MySQL**: Open-source, reliable, widely used in web development, supported by a large community.
381+
- **PostgreSQL**: Open-source, advanced, extensible. Supports custom data types and server-side programming.
382+
- **SQLite**: Lightweight, file-based, serverless. Ideal for small applications.
383+
- **Microsoft SQL Server**: Proprietary, enterprise-grade database.
384+
- **Oracle Database**: Commercial RDBMS known for large-scale performance and scalability.
385+
386+
## Best Practices
387+
388+
- **Naming Convention**: Use `snake_case` (e.g., `delivery_orders`) for table and column names.
11389

12390
# --assignment--
13391

0 commit comments

Comments
 (0)