I would like to start a simple wikepedia database system using postgreSQL with the following,
There are three tables users, categories and articles. In which users and articles are one-to-may through users id primary key. While categories is also one-to-may with articles by categories primary key. Please help me with the schema.
DROP DATABASE IF EXISTS wiki_db;
CREATE DATABASE wiki_db;
--- CREATE SCHEMA
-- CREATE SCHEMA wiki;
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
CREATE TABLE categories (
category_id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE articles (
article_id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
--- user_id has to be defined
user_id INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP,
-- category_id has to be defined
category_id INTEGER NOT NULL,
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE
);
SET search_path TO wiki;
-- Insert sample users
INSERT INTO users (name, email, password)
VALUES
('John Doe', 'john.doe@example.com', 'password1'),
('Jane Smith', 'jane.smith@example.com', 'password2'),
('Bob Johnson', 'bob.johnson@example.com', 'password3');
-- Insert sample categories
INSERT INTO categories (name)
VALUES
('Technology'),
('Sports'),
('Politics');
-- Insert sample articles
INSERT INTO articles (title, content, user_id, category_id)
VALUES
('The Latest Technology Trends', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 1, 1),
('The Top 10 Sports Moments of the Year', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 2, 2),
('The Latest Political Developments', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 3, 3);
+----------+ +------------+
| users | | articles |
+----------+ +------------+
| id |------->| id |
| username | | title |
| email | | content |
| password | | created_at |
+----------+ | user_id |
+------------+
+-----------+ +------------+
| categories| | articles |
+-----------+ +------------+
| id |------> | id |
| name | | title |
+-----------+ | content |
| created_at |
| user_id |
+------------+
+---------------------+
| categories_articles |
+---------------------+
| category_id |
| article_id |
+---------------------+
-- Insert sample users
INSERT INTO users (name, email, password)
VALUES
('John Doe', 'john.doe@example.com', 'password1'),
('Jane Smith', 'jane.smith@example.com', 'password2'),
('Bob Johnson', 'bob.johnson@example.com', 'password3');
-- Insert sample categories
INSERT INTO categories (name)
VALUES
('Technology'),
('Sports'),
('Politics');
-- Insert sample articles
INSERT INTO articles (title, content, user_id, category_id)
VALUES
('The Latest Technology Trends', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 1, 1),
('The Top 10 Sports Moments of the Year', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 2, 2),
('The Latest Political Developments', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 3, 3);
SELECT * FROM articles WHERE user_id = <user_id>;
SELECT * FROM articles WHERE category_id = <category_id>;
SELECT articles.title, articles.content, users.name AS author, categories.name AS category
FROM articles
JOIN users ON articles.user_id = users.user_id
JOIN categories ON articles.category_id = categories.category_id;
INSERT INTO categories_articles (category_id, article_id) VALUES (2, 1);
UPDATE categories_articles SET category_id = 3 WHERE article_id = 1;
DELETE FROM categories_articles WHERE category_id = 2 AND article_id = 1;
Note that when you insert or update a row in the categories_articles table, you need to make sure that the category_id and article_id values you are inserting or updating correspond to valid categories and articles that exist in the categories and articles tables.
SELECT articles.title, articles.content
FROM articles
JOIN categories_articles ON articles.id = categories_articles.article_id
WHERE categories_articles.category_id = 2;
This query selects the title and content columns from the articles table, and joins it with the categories_articles table on the id and article_id columns. The WHERE clause filters the results to only include articles that belong to the category with ID 2.
Return to Top