<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alfi Syahri on Medium]]></title>
        <description><![CDATA[Stories by Alfi Syahri on Medium]]></description>
        <link>https://medium.com/@alfisyahri313?source=rss-caf8377c07c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wVTeZqRyabriHOl9.jpg</url>
            <title>Stories by Alfi Syahri on Medium</title>
            <link>https://medium.com/@alfisyahri313?source=rss-caf8377c07c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 22:06:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alfisyahri313/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Designing a Relational Database for a Car Auction Platform]]></title>
            <link>https://medium.com/@alfisyahri313/designing-a-relational-database-for-a-car-auction-platform-6fc608f721ab?source=rss-caf8377c07c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fc608f721ab</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[database-design]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Alfi Syahri]]></dc:creator>
            <pubDate>Sun, 16 Apr 2023 16:51:29 GMT</pubDate>
            <atom:updated>2023-04-17T16:45:44.039Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are planning to build a car auction platform, one of the most critical components you will need is a database. A good database design is essential to ensure the reliability, scalability, and maintainability of your application.</p><p>In this article, we will guide you through the process of designing a relational database for a car auction platform. We will cover the following topics:</p><ul><li>Understanding the requirements</li><li>Creating a data model</li><li>Populating the database</li><li>Analyzing the Table</li></ul><h3>Understanding the Requirements</h3><p>Before designing a database, it’s crucial to understand the requirements of the system. In this case, we are building a car auction platform that allows users to sell and bid on cars. For details, there are several features we want to define:</p><ul><li>Each user should have a name, contact, and location.</li><li>Each user can post used cars and may allow bidding on the cars.</li><li>Each user can see and search for cars in the website.</li><li>If users are interested in the cars, they can bid on the cars. The original poster can accept or reject the bidding.</li></ul><p>Based on this requirement, we can identify the entities and attributes that are necessary for the database.</p><h3>Entities</h3><ul><li>Users: The users who use the platform to sell or bid on cars.</li><li>Locations: The locations where the cars or users are located.</li><li>Products: The cars that are listed for sale on the platform.</li><li>Bids: The bids that users place on the products.</li></ul><h3>Attributes</h3><p><strong>Users Entity</strong></p><p>Users Entity represents the users who use the platform. It should contain name, location, and contacts such as email and phone number. It contains the following columns:</p><ul><li>id: The unique identifier for the user. This is the <strong>primary key</strong> of the table and is an auto-incremented integer.</li><li>name: The name of the user.</li><li>email: The email address of the user.</li><li>phone: The phone number of the user.</li><li>location_id: The identifier of the location where the user is located. This column is a <strong>foreign key</strong> referencing the id column of the Products table.</li></ul><p>We can create the Table for this entity as follows:</p><ul><li>id: integer (primary key)</li><li>name: varchar(255)</li><li>email: varchar(255)</li><li>phone: varchar(255)</li><li>location_id: integer (foreign key referencing the Locations table)</li></ul><p>In SQL, we can run this code</p><pre>CREATE TABLE Users (<br>  id SERIAL PRIMARY KEY,<br>  name VARCHAR(255) NOT NULL,<br>  email VARCHAR(255) NOT NULL,<br>  phone VARCHAR(255) NOT NULL,<br>  location_id INTEGER NOT NULL REFERENCES Locations(id)<br>);</pre><p><strong>Locations Entity</strong></p><p>Locations Entity represents locations where the cars or users are located. It contains a city name with its coordinate.</p><ul><li>id: The unique identifier for the location. This is the <strong>primary key</strong> of the table.</li><li>city_name: The name of the city where the location is located.</li><li>latitude: The latitude of the location.</li><li>longitude: The longitude of the location.</li></ul><p>We can create the Table for this entity as follows:</p><ul><li>id: integer (primary key)</li><li>city_name: varchar(255)</li><li>latitude: decimal(15, 8)</li><li>longitude: decimal(15, 8)</li></ul><p>In SQL, we can run this code</p><pre>CREATE TABLE Locations (<br>  id SERIAL PRIMARY KEY,<br>  city_name VARCHAR(255) NOT NULL,<br>  latitude DECIMAL(15,8) NOT NULL,<br>  longitude DECIMAL(15,8) NOT NULL<br>);</pre><p><strong>Products Entity</strong></p><p>Products Entity represents the cars that are listed for sale on the platform and the status of the product.</p><ul><li>id: The unique identifier for the product. This is the <strong>primary key</strong> of the table and is an auto-incremented integer.</li><li>user_id: The identifier of the user who listed the product. This column is a <strong>foreign key</strong> referencing the id column of the Users table.</li><li>car_brand: The brand of the car.</li><li>car_model: The model of the car.</li><li>car_body_type: The body type of the car.</li><li>car_transmission_type: The transmission type of the car.</li><li>car_year: The year of the car.</li><li>car_color: The color of the car.</li><li>car_mileage: The mileage of the car.</li><li>car_price: The price of the car.</li><li>location_id: The identifier of the location where the car is located. This column is a <strong>foreign key</strong> referencing the id column of the Location table.</li><li>allow_bidding: Whether or not the seller allows bidding on the product.</li><li>product_status: The status of the product (open or closed).</li><li>created_at: The date and time when the product was created.</li></ul><p>We can create the Table for this entity as follows:</p><ul><li>id: integer (primary key)</li><li>user_id: integer (foreign key referencing the Users table)</li><li>car_brand: varchar(255)</li><li>car_model: varchar(255)</li><li>car_body_type: varchar(255)</li><li>car_transmission_type: varchar(255)</li><li>car_year: integer</li><li>car_color: varchar(255)</li><li>car_mileage: integer</li><li>car_price: decimal(15, 2)</li><li>location_id: integer (foreign key referencing the Locations table)</li><li>allow_bidding: boolean</li><li>product_status: enum(‘Open’, ‘Closed’)</li><li>created_at: timestamp</li></ul><p>In SQL, we can run this code</p><pre>CREATE TYPE product_status_enum AS ENUM (&#39;Open&#39;, &#39;Closed&#39;);<br><br>CREATE TABLE Products (<br>   id SERIAL PRIMARY KEY,<br>   user_id INTEGER REFERENCES Users(id) NOT NULL,<br>   car_brand VARCHAR(255) NOT NULL,<br>   car_model VARCHAR(255) NOT NULL,<br>   car_body_type VARCHAR(255) NOT NULL,<br>   car_transmission_type VARCHAR(255) NOT NULL,<br>   car_year INTEGER NOT NULL,<br>   car_color VARCHAR(255) NOT NULL,<br>   car_mileage INTEGER NOT NULL,<br>   car_price DECIMAL(15, 2) NOT NULL,<br>   location_id INTEGER REFERENCES Locations(id) NOT NULL,<br>   allow_bidding BOOLEAN NOT NULL,<br>   product_status product_status_enum NOT NULL,<br>   created_at TIMESTAMP NOT NULL DEFAULT NOW(),<br>);</pre><p><strong>Bids Entity</strong></p><p>Bids Entity represents the bids made by users on the products listed in the Products table. It contains the following columns:</p><ul><li>id: The unique identifier for the bid. This is the <strong>primary key</strong> of the table and is an auto-incremented integer.</li><li>product_id: The identifier of the product that the bid is for. This column is a <strong>foreign key</strong> referencing the id column of the Products table.</li><li>user_id: The identifier of the user who placed the bid. This column is a <strong>foreign key</strong> referencing the id column of the Users table.</li><li>bid_price: The price of the bid.</li><li>bid_status: The status of the bid (pending, accepted, cancelled, or rejected). Pending means the bid has been sent to the platform but doesn’t get any action from the owner of the products. Accepted means the bid has been accepted by product owner as the winning bid. Cancelled means the bid has been cancelled by the user. Rejected means the bid is rejected by product owner.</li><li>created_at: The date and time when the bid was created.</li></ul><p>We can create the Table for this entity as follows:</p><ul><li>id: integer (primary key)</li><li>product_id: integer (foreign key referencing the Products table)</li><li>user_id: integer (foreign key referencing the Users table)</li><li>bid_price: decimal(15, 2)</li><li>bid_status: enum(‘Pending’,’Accepted’,’Cancelled’,’Rejected’ )</li><li>created_at: timestamp</li></ul><p>In SQL, we can run this code</p><pre>CREATE TYPE bid_status_enum AS ENUM (&#39;Pending&#39;, &#39;Accepted&#39;, &#39;Cancelled&#39;, &#39;Rejected&#39;);<br><br>CREATE TABLE Bids (<br>   id SERIAL PRIMARY KEY,<br>   product_id INTEGER REFERENCES Products(id) NOT NULL,<br>   user_id INTEGER REFERENCES Users(id) NOT NULL,<br>   bid_price DECIMAL(15, 2) NOT NULL,<br>   bid_status bid_status_enum NOT NULL,<br>   created_at TIMESTAMP NOT NULL DEFAULT NOW()<br>);</pre><h3>Creating a Data Model</h3><p>Now that we have identified the entities and attributes, we can create a data model. A data model is a visual representation of the entities and their relationships.</p><p>In this case, we have four entities: Users, Locations, Products, and Bids. We can represent them in an entity-relationship diagram (ERD).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*NFneaYPlOXRfUyZEWI6fHA.png" /></figure><p>The diagram shows the relationships between the different tables in the database. The Users table is related to the Locations table through the location_id foreign key column. Similarly, the Products table is related to both the Users and Locations tables through the user_id and location_id foreign key columns, respectively. Finally, the Bids table is related to both the Products and Users tables through the product_id and user_id foreign key columns, respectively.</p><h3><strong>Populating the database</strong></h3><p>We have made some dummy data to populate the database, you can see the details at <a href="https://github.com/alfisyahri313/car-auction-database">https://github.com/alfisyahri313/car-auction-database</a></p><p>Run the code in insert_table.py to populate the database with the dummy data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*rEjzVdD8hGnGa1ORgTrYEw.png" /><figcaption>location table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*vqfEaLUDo0Yj-SznejczZw.png" /><figcaption>users table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ViRgcGxIM4REGDnOpYpcJg.png" /><figcaption>products table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*tc__vs96Ig_nU1vMrvtRQg.png" /><figcaption>bids table</figcaption></figure><h3>Backup the database</h3><p>Backing up a database is an essential task for any organization to ensure data security and avoid data loss. We have backed up the database. You can find the database backup at <a href="https://github.com/alfisyahri313/car-auction-database">https://github.com/alfisyahri313/car-auction-database</a></p><h3>Performing transactional query</h3><p>As we have completed populating the database, we now performing some examples in transactional query:</p><ul><li>Find cars with a car year above 2015</li></ul><pre>SELECT *<br>FROM Products<br>WHERE car_year &gt;= 2015;<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GMiVv7szrlIGudr85x06KA.png" /></figure><ul><li>Add a new bid to the table</li></ul><pre>INSERT INTO bids (product_id, user_id, bid_price, bid_status, created_at)<br>VALUES (49, 24, 230000000, &#39;Pending&#39;, NOW());</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*1eNDeeKDZaJfNMkhG_BJug.png" /><figcaption>before</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*8sMLHwkD9ZpxcILPpwLcuw.png" /><figcaption>after</figcaption></figure><ul><li>Find all products from one account sorted from the newest</li></ul><pre>SELECT id, user_id, car_brand, car_model, car_price, location_id, allow_bidding, product_status, created_at FROM products<br>WHERE user_id = 23 ORDER BY created_at DESC;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TfYoBivC3Q_AC29s3iC8Yg.png" /></figure><ul><li>Find products using a Keyword. It can be a car brand, model, year, or others keyword. We use “Yaris” as Keyword to find the “Yaris” car.</li></ul><pre>SELECT * FROM products<br>WHERE car_brand ILIKE &#39;%Yaris%&#39; <br>   OR car_model ILIKE &#39;%Yaris%&#39; <br>   OR CAST(car_year AS TEXT) ILIKE &#39;%Yaris%&#39; <br>   OR car_body_type ILIKE &#39;%Yaris%&#39; <br>   OR car_transmission_type ILIKE &#39;%Yaris%&#39; <br>   OR car_color ILIKE &#39;%Yaris%&#39;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rPhPOboFiDxgYDjr27MBJw.png" /></figure><ul><li>Find the nearest products to a given city by its location_id. 3273 is the location_id of Bandung.</li></ul><pre>SELECT p.id, p.car_brand, p.car_model, p.car_year, p.car_price, l.city_name,<br>    ROUND(SQRT(POW((l.latitude - c.latitude) * 111.139, 2) + POW((l.longitude - c.longitude) * 111.139, 2)), 2) AS distance_km<br>FROM Products p<br>JOIN Locations l ON p.location_id = l.id<br>JOIN Locations c ON c.id = 3273<br>WHERE p.product_status = &#39;Open&#39;<br>ORDER BY distance_km ASC;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BCJc8LH0Hxx_6JFAyw3Xdw.png" /></figure><h3>Performing analytical query</h3><p>We now performing some examples in analytical query:</p><ul><li>Rank the products based on the number count of bids in those products from the highest</li></ul><pre>SELECT l.city_name, p.car_brand, p.car_model, p.car_year, p.car_price,<br>       AVG(p2.car_price) AS average_car_city<br>FROM products p<br>LEFT JOIN locations l ON p.location_id = l.id<br>LEFT JOIN products p2 ON p.car_brand = p2.car_brand <br>                 AND p.car_model = p2.car_model <br>                 AND p.car_year = p2.car_year <br>                 AND p.location_id = p2.location_id<br>GROUP BY l.city_name, p.car_brand, p.car_model, p.car_year, p.car_price<br>ORDER BY l.city_name, p.car_brand, p.car_model, p.car_year, p.car_price;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WD_hioQEZoPniGsZ5Fl0gA.png" /></figure><ul><li>Comparing car price to average car price in the city</li></ul><pre>SELECT l.city_name, p.car_brand, p.car_model, p.car_year, p.car_price,<br>       AVG(p2.car_price) AS average_car_city<br>FROM products p<br>LEFT JOIN locations l ON p.location_id = l.id<br>LEFT JOIN products p2 ON p.location_id = p2.location_id<br>GROUP BY l.city_name, p.car_brand, p.car_model, p.car_year, p.car_price<br>ORDER BY l.city_name, p.car_brand, p.car_model, p.car_year, p.car_price;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iFkgpc58zTunraHhqvxJeg.png" /></figure><ul><li>From a car model bidding, find a comparison of the date the user placed the bid with the next bid and the price offered. For Example, we used the “Toyota Yaris” car model.</li></ul><pre>SELECT car_model, user_id, bid_date,<br>    LEAD(bid_date) OVER (PARTITION BY user_id ORDER BY bid_date) AS next_bid_date,<br>    bid_price,<br>    LEAD(bid_price) OVER (PARTITION BY user_id ORDER BY bid_date) AS next_bid_price<br>FROM (<br>    SELECT p.car_model, b.user_id, DATE(b.created_at) AS bid_date, b.bid_price<br>    FROM bids b<br>    LEFT JOIN products p ON p.id = b.product_id<br>    WHERE p.car_model = &#39;Toyota Yaris&#39;<br>    ORDER BY user_id, bid_date<br>) sorted_bid_table</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*Rj1zQb8YrNFe6bUbb1KM1w.png" /></figure><ul><li>Comparing the percentage difference of the average car prices based on their models and the average bid prices offered by customers in the last 6 months. <br>- The “Difference” is the gap between the average price of a car model (avg_price) and the average bid price offered for that model (avg_bid_6month). <br>- The “Difference_percent” is the percentage of the calculated difference, which is obtained by dividing the difference by the average car price (avg_price) and then multiplying it by 100%.</li></ul><pre>SELECT *, avg_price-avg_bid_6month AS difference, ((avg_price-avg_bid_6month)/avg_price)*100 AS difference_percent<br>FROM<br>(SELECT p.car_model, AVG(p.car_price) AS avg_price, AVG(b.bid_price) AS avg_bid_6month<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;6 months&#39;<br>GROUP by p.car_model) avg_table</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*TogJNu9VG3veOet5yxljbQ.png" /></figure><ul><li>Creating a window function to show the average bid price in the last 6 months of car model and brand.</li></ul><pre>WITH m_1 AS (SELECT p.car_brand, p.car_model, AVG(b.bid_price) AS m_min_1<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;1 months&#39;<br>GROUP by p.car_brand, p.car_model),<br>m_2 AS (SELECT p.car_brand, p.car_model, AVG(b.bid_price) AS m_min_2<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;2 months&#39;<br>  AND b.created_at &lt; now() - interval &#39;1 month&#39;<br>GROUP by p.car_brand, p.car_model),<br>m_3 AS (SELECT p.car_brand, p.car_model, AVG(b.bid_price) AS m_min_3<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;3 months&#39;<br>  AND b.created_at &lt; now() - interval &#39;2 month&#39;<br>GROUP by p.car_brand, p.car_model),<br>m_4 AS (SELECT p.car_brand, p.car_model, AVG(b.bid_price) AS m_min_4<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;4 months&#39;<br>  AND b.created_at &lt; now() - interval &#39;3 month&#39;<br>GROUP by p.car_brand, p.car_model),<br>m_5 AS (SELECT p.car_brand, p.car_model, AVG(b.bid_price) AS m_min_5<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;5 months&#39;<br>  AND b.created_at &lt; now() - interval &#39;4 month&#39;<br>GROUP by p.car_brand, p.car_model),<br>m_6 AS (SELECT p.car_brand, p.car_model, AVG(b.bid_price) AS m_min_6<br>FROM products p<br>LEFT JOIN bids b ON p.id = b.product_id<br>WHERE b.created_at &gt;= now() - interval &#39;6 months&#39;<br>  AND b.created_at &lt; now() - interval &#39;5 month&#39;<br>GROUP by p.car_brand, p.car_model)<br>SELECT a.car_brand, a.car_model, m_min_6, m_min_5, m_min_4, m_min_3, m_min_2, m_min_1<br>FROM m_1 a<br>LEFT JOIN m_2 b ON a.car_model = b.car_model<br>LEFT JOIN m_3 c ON a.car_model = c.car_model<br>LEFT JOIN m_4 d ON a.car_model = d.car_model<br>LEFT JOIN m_5 e ON a.car_model = e.car_model<br>LEFT JOIN m_6 f ON a.car_model = f.car_model</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aK_TYnVHFq9s0zHRKd7G3w.png" /></figure><p>Note: our dummy data has date only in the last 1 month</p><h3>Conclusion</h3><p>This database design provides a solid foundation for a bidding system application. It includes tables for users, locations, products, and bids, all of which are linked together through foreign keys. With this database, users can create accounts, post products, bid on products, and view the products they are interested in.</p><p>The use of foreign keys ensures data integrity, and the inclusion of attributes such as ‘created_at’ will help to keep track of when records were created. Additionally, the use of enums for attributes such as ‘bid_status’ and ‘product_status’ help to ensure data consistency.</p><p>Overall, this database design is an effective solution for a bidding system application, with a clear and logical structure that will allow for efficient data management and processing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fc608f721ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>