You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/25-front-end-development/review-relational-database/6724e3d6e1cb0c1fec3a8e4f.md
-**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
+
CREATETABLEfirst_table();
46
+
```
47
+
48
+
-**`ALTER TABLE ADD COLUMN` Statement**: This statement is used to add a column to an existing table.
-**`ALTER TABLE ADD UNIQUE` Statement**: This statement is used to add a UNIQUE constraint to a column.
117
+
118
+
```sql
119
+
ALTERTABLE 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
+
ALTERTABLE table_name ALTER COLUMN column_name SETNOT 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
+
CREATETABLEcourse_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
+
DELETEFROM 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
+
CREATETABLEbooks_authors (
272
+
author_id INTREFERENCES authors(id),
273
+
book_id INTREFERENCES 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.
-**`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 ONproducts.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 ONproducts.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 ONproducts.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
+
SELECTA.column_name, B.column_name
321
+
FROM table_name A
322
+
JOIN table_name B ONA.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
+
CREATEDATABASEdatabase_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.
0 commit comments