@@ -196,6 +196,23 @@ CREATE TABLE unique_cats (cat_id INT AUTO_INCREMENT NOT NULL PRIMARY KEY
196196- ` AUTO_INCREMENT ` will automatically increase the value for each entry.
197197 - It will appear under ` EXTRA ` when ` DESC ` is used.
198198
199+ It's possible to use two primary keys to make sure there's no duplicates of the
200+ pair of those columns:
201+ ``` sql
202+ -- Likes Table.
203+ -- Ensures one photo will not have the same user liking multiple times
204+ -- as both the photo_id and user_id pair must be unique in an entry
205+ CREATE TABLE likes (
206+ photo_id INT NOT NULL ,
207+ user_id INT NOT NULL ,
208+ created_at TIMESTAMP DEFAULT NOW(),
209+ FOREIGN KEY (photo_id) REFERENCES photos(id),
210+ FOREIGN KEY (user_id) REFERENCES users(id),
211+ PRIMARY KEY (user_id, photo_id)
212+ );
213+
214+ ```
215+
199216## CRUD
200217
201218- __ C__ reate
@@ -1053,7 +1070,7 @@ CREATE TABLE customers (
10531070 id INT NOT NULL AUTO_INCREMENT,
10541071 first_name,
10551072 last_name,
1056- email,
1073+ email UNIQUE ,
10571074 PRIMARY KEY(id)
10581075);
10591076
@@ -1319,3 +1336,124 @@ GROUP BY reviewers.id
13191336ORDER BY STATUS;
13201337```
13211338
1339+ # Section 14: Instagram Database Clone
1340+
1341+ ## General Notes
1342+
1343+ - If you will not be referencing a table, you don't always need to store an ` id ` .
1344+ - Ask questions before you start working with the data.
1345+
1346+ ## Designing the Schema
1347+
1348+ A few things we need to store:
1349+
1350+ - Users
1351+ - Photos
1352+ - Comments
1353+ - Likes
1354+ - Hashtags
1355+ - Followers / followees
1356+
1357+ ## Tagging Solutions:
1358+
1359+ The best solution is a combination of 1 and 2.
1360+
1361+ ### Solution 1:
1362+
1363+ ![ Tagging Solution One] ( /assets/tag_sol_1.png )
1364+
1365+ - Faster with uncommon tags.
1366+
1367+ ### Solution 2:
1368+
1369+ ![ Tagging Solution Two] ( /assets/tag_sol_2.png )
1370+
1371+ ### Solution 3:
1372+
1373+ ![ Tagging Solution Three] ( /assets/tag_sol_3.png )
1374+
1375+ - Fastest if working with common tags that are used often.
1376+ - Not as fast with uncommon tags
1377+
1378+ ## Creating The Tables
1379+
1380+ ``` sql
1381+ CREATE DATABASE ig_clone ;
1382+ USE ig_clone;
1383+
1384+ -- Users Table
1385+ CREATE TABLE users (
1386+ id INT AUTO_INCREMENT PRIMARY KEY ,
1387+ username VARCHAR (255 ) UNIQUE,
1388+ created_at TIMESTAMP DEFAULT NOW()
1389+ );
1390+
1391+ -- Photos Table
1392+ CREATE TABLE photos (
1393+ id INT AUTO_INCREMENT PRIMARY KEY ,
1394+ image_url VARCHAR (255 ) NOT NULL ,
1395+ user_id INT NOT NULL ,
1396+ created_at TIMESTAMP DEFAULT NOW(),
1397+ FOREIGN KEY (user_id) REFERENCES users(id)
1398+ );
1399+
1400+ -- Comments Table
1401+ CREATE TABLE comments (
1402+ id INT AUTO_INCREMENT PRIMARY KEY ,
1403+ comment_text VARCHAR (255 ) NOT NULL ,
1404+ user_id INT NOT NULL ,
1405+ photo_id INT NOT NULL ,
1406+ created_at TIMESTAMP DEFAULT NOW(),
1407+ FOREIGN KEY (user_id) REFERENCES users(id),
1408+ FOREIGN KEY (photo_id) REFERENCES photos(id)
1409+ );
1410+
1411+ -- Likes Table
1412+ CREATE TABLE likes (
1413+ photo_id INT NOT NULL ,
1414+ user_id INT NOT NULL ,
1415+ created_at TIMESTAMP DEFAULT NOW(),
1416+ FOREIGN KEY (photo_id) REFERENCES photos(id),
1417+ FOREIGN KEY (user_id) REFERENCES users(id),
1418+ PRIMARY KEY (user_id, photo_id)
1419+ );
1420+
1421+ -- Followers Table
1422+ -- Who follows who
1423+ CREATE TABLE follows (
1424+ follower_id INT NOT NULL ,
1425+ followee_id INT NOT NULL ,
1426+ created_at TIMESTAMP DEFAULT NOW(),
1427+ FOREIGN KEY (follower_id) REFERENCES users(id),
1428+ FOREIGN KEY (followee_id) REFERENCES users(id),
1429+ PRIMARY KEY (follower_id, followee_id)
1430+ );
1431+
1432+ -- Tags Table
1433+ CREATE TABLE tags (
1434+ id INT AUTO_INCREMENT PRIMARY KEY ,
1435+ tag_name VARCHAR (255 ) UNIQUE,
1436+ created_at TIMESTAMP DEFAULT NOW()
1437+ );
1438+
1439+ -- Photo Tags Table
1440+ -- Associate a tag with a photo
1441+ CREATE TABLE photo_tags (
1442+ photo_id INT NOT NULL ,
1443+ tag_id INT NOT NULL ,
1444+ FOREIGN KEY (photo_id) REFERENCES photos(id),
1445+ FOREIGN KEY (tag_id) REFERENCES tags(id),
1446+ PRIMARY KEY (photo_id, tag_id)
1447+ );
1448+ ```
1449+
1450+ ## Inserting The Values
1451+
1452+ [ Values] ( /static/ig_clone_data.sql )
1453+
1454+ ## Having
1455+
1456+ Acts like ` WHERE ` , but takes grouped data and allows for filtering based off a
1457+ clause.
1458+ - This is necessary, because ` WHERE ` comes __ before__ ` GROUP BY ` , which means
1459+ it's impossible to filter the data using ` WHERE ` when trying to also group.
0 commit comments