|
9 | 9 | + [Mention system](#mention-system)
|
10 | 10 | + [Hashtag system](#hashtag-system)
|
11 | 11 | + [follower system](#follower-system)
|
| 12 | ++ [creating instagram database](#creating-instagram-database) |
12 | 13 |
|
13 | 14 | ---
|
14 | 15 |
|
@@ -488,3 +489,128 @@ Table followers {
|
488 | 489 | }
|
489 | 490 | ```
|
490 | 491 |
|
| 492 | +--- |
| 493 | + |
| 494 | +###### <div style="text-align:right">[table of contents](#table-of-contents)</div> |
| 495 | + |
| 496 | +## creating instagram database |
| 497 | + |
| 498 | +1. servers -> localhost -> databases -> (right-click create) |
| 499 | +2. open query tool by right clicking on specific database |
| 500 | + |
| 501 | +###### users table |
| 502 | +```SQL |
| 503 | +CREATE TABLE users ( |
| 504 | + id SERIAL PRIMARY KEY, |
| 505 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 506 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 507 | + username VARCHAR(30) NOT NULL, |
| 508 | + bio VARCHAR(400), |
| 509 | + avatar VARCHAR(200), |
| 510 | + phone VARCHAR(25), |
| 511 | + email VARCHAR(40), |
| 512 | + password VARCHAR(50), |
| 513 | + status VARCHAR(15), |
| 514 | + CHECK(COALESCE(phone, email) IS NOT NULL) |
| 515 | +); |
| 516 | +``` |
| 517 | + |
| 518 | +###### posts table |
| 519 | +```SQL |
| 520 | +CREATE TABLE posts ( |
| 521 | + id SERIAL PRIMARY KEY, |
| 522 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 523 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 524 | + url VARCHAR(200) NOT NULL, |
| 525 | + caption VARCHAR(240), |
| 526 | + lat REAL CHECK(lat IS NULL OR (lat >= -90 AND lat <= 90)), |
| 527 | + lng REAL CHECK(lng IS NULL OR (lng >= -180 AND lng <= 180)), |
| 528 | + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE |
| 529 | +); |
| 530 | +``` |
| 531 | + |
| 532 | +###### comments table |
| 533 | +```SQL |
| 534 | +CREATE TABLE comments ( |
| 535 | + id SERIAL PRIMARY KEY, |
| 536 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 537 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 538 | + contents VARCHAR(240) NOT NULL, |
| 539 | + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 540 | + post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE |
| 541 | +); |
| 542 | +``` |
| 543 | + |
| 544 | +###### likes table |
| 545 | +```SQL |
| 546 | +CREATE TABLE likes ( |
| 547 | + id SERIAL PRIMARY KEY, |
| 548 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 549 | + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 550 | + post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE, |
| 551 | + comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE, |
| 552 | + CHECK( |
| 553 | + COALESCE((post_id)::BOOLEAN::INTEGER, 0) |
| 554 | + + |
| 555 | + COALESCE((comment_id)::BOOLEAN::INTEGER, 0) |
| 556 | + = 1 |
| 557 | + ), |
| 558 | + UNIQUE(user_id, post_id, comment_id) |
| 559 | +); |
| 560 | +``` |
| 561 | + |
| 562 | +###### photo_tags |
| 563 | +```SQL |
| 564 | +CREATE TABLE photo_tags ( |
| 565 | + id SERIAL PRIMARY KEY, |
| 566 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 567 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 568 | + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 569 | + post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE, |
| 570 | + x INTEGER NOT NULL, |
| 571 | + y INTEGER NOT NULL, |
| 572 | + UNIQUE(user_id, post_id) |
| 573 | +); |
| 574 | +``` |
| 575 | + |
| 576 | +###### caption_tags |
| 577 | +```SQL |
| 578 | +CREATE TABLE caption_tags ( |
| 579 | + id SERIAL PRIMARY KEY, |
| 580 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 581 | + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 582 | + post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE, |
| 583 | + UNIQUE(user_id, post_id) |
| 584 | +); |
| 585 | +``` |
| 586 | + |
| 587 | +###### hashtags |
| 588 | +```SQL |
| 589 | +CREATE TABLE hashtags ( |
| 590 | + id SERIAL PRIMARY KEY, |
| 591 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 592 | + title VARCHAR(20) NOT NULL UNIQUE |
| 593 | +); |
| 594 | +``` |
| 595 | + |
| 596 | +###### hashtags_post |
| 597 | +```SQL |
| 598 | +CREATE TABLE hashtags_posts ( |
| 599 | + id SERIAL PRIMARY KEY, |
| 600 | + hashtag_id INTEGER NOT NULL REFERENCES hashtags(id) ON DELETE CASCADE, |
| 601 | + post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE, |
| 602 | + UNIQUE(hashtag_id, post_id) |
| 603 | +); |
| 604 | +``` |
| 605 | + |
| 606 | +###### followers |
| 607 | +```SQL |
| 608 | +CREATE TABLE followers ( |
| 609 | + id SERIAL PRIMARY KEY, |
| 610 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 611 | + leader_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 612 | + follower_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 613 | + UNIQUE(leader_id, follower_id) |
| 614 | +); |
| 615 | +``` |
| 616 | + |
0 commit comments