Skip to content

Commit 74e642f

Browse files
committed
move Views notes to SQL section
1 parent ab2ecc0 commit 74e642f

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

01.SQL.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787

8888
[12. Common table expressions](#12-common-table-expressions)
8989
[13. Recursive common table expressions (CTE)](#13-recursive-common-table-expressions-cte)
90+
[14. Views](#14-views)
9091

9192
---
9293
###### <div style="text-align:right">[table of contents](#table-of-contents)</div>
@@ -1450,4 +1451,52 @@ table suggestions (leader_id, follower_id, depth)
14501451

14511452
---
14521453

1454+
###### <div style="text-align:right">[table of contents](#table-of-contents)</div>
1455+
1456+
## 14. Views
1457+
1458+
fixing mistakes in the design of table creation process where you constantly have to perform eg. JOINS for queries when you should have maybe had combined tables.
1459+
Better way to merge tables together without deleting original tables.
1460+
1461+
A view is a "fake" table that has rows from other tables.
1462+
These can be exact rows or computed values.
1463+
can reference a view in any place where we usually reference a table.
1464+
view doesnt actually create a table or move data around.
1465+
1466+
Views can be created ahead of time,
1467+
can be refferenced in different queries (whereas CTE's can only be reffered to in the query they are attached to)
1468+
1469+
you want to create a view when there is an operation that's logic you will execute often.
1470+
1471+
view all views -> pgadmin - > schemas -> public views
1472+
1473+
```SQL
1474+
--creating a view
1475+
CREATE VIEW tags AS(
1476+
SELECT id, created_at, user_id, post_id, 'photo_tag' AS type FROM photo_tags
1477+
UNION ALL
1478+
SELECT id, created_at, user_id, post_id, 'caption_tag' AS type FROM caption_tags
1479+
);
14531480

1481+
--reference view from another different query
1482+
SELECT username, COUNT(*)
1483+
FROM users
1484+
JOIN tags ON tags.user_id = users.id
1485+
GROUP BY username
1486+
ORDER BY COUNT(*) DESC;
1487+
```
1488+
1489+
##### updating view (change the definition)
1490+
syntax: CREATE OR REPLACE VIEW _ AS
1491+
1492+
```SQL
1493+
CREATE OR REPLACE VIEW recent_posts AS (
1494+
SELECT * FROM posts
1495+
ORDER BY created_at DESC
1496+
LIMIT 15
1497+
)
1498+
```
1499+
##### deleting a view
1500+
```SQL
1501+
DROP VIEW recent_posts;
1502+
```

03.postgreSQL.md

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -466,50 +466,3 @@ Cost for sequential read =
466466
(# rows scanned) * cpu_tuple_cost
467467

468468
---
469-
470-
## Views
471-
fixing mistakes in the design of table creation process where you constantly have to perform eg. JOINS for queries when you should have maybe had combined tables.
472-
Better way to merge tables together without deleting original tables.
473-
474-
A view is a "fake" table that has rows from other tables.
475-
These can be exact rows or computed values.
476-
can reference a view in any place where we usually reference a table.
477-
view doesnt actually create a table or move data around.
478-
479-
Views can be created ahead of time,
480-
can be refferenced in different queries (whereas CTE's can only be reffered to in the query they are attached to)
481-
482-
you want to create a view when there is an operation that's logic you will execute often.
483-
484-
view all views -> pgadmin - > schemas -> public views
485-
486-
```SQL
487-
--creating a view
488-
CREATE VIEW tags AS(
489-
SELECT id, created_at, user_id, post_id, 'photo_tag' AS type FROM photo_tags
490-
UNION ALL
491-
SELECT id, created_at, user_id, post_id, 'caption_tag' AS type FROM caption_tags
492-
);
493-
494-
--reference view from another different query
495-
SELECT username, COUNT(*)
496-
FROM users
497-
JOIN tags ON tags.user_id = users.id
498-
GROUP BY username
499-
ORDER BY COUNT(*) DESC;
500-
```
501-
502-
##### updating view (change the definition)
503-
syntax: CREATE OR REPLACE VIEW _ AS
504-
505-
```SQL
506-
CREATE OR REPLACE VIEW recent_posts AS (
507-
SELECT * FROM posts
508-
ORDER BY created_at DESC
509-
LIMIT 15
510-
)
511-
```
512-
##### deleting a view
513-
```SQL
514-
DROP VIEW recent_posts;
515-
```

0 commit comments

Comments
 (0)