The database for CS50 SQL project, I worked on this project and named as Neighbourly, which is a Local Marketplace Support System. The database that I had designed includes all entities which are necessary to support the process of local businesses, connecting customers, business owners, and delivery agents. The database handles the listing of products and services, order placement, payment tracking, delivery agent assignment, and user's ratings, and system logging. As such, included in the database's scope is:
- Users, like Customers, Business-Owners, Delivery-Agents, including basic information like name, username, password, location, address and role etc.
- Businesses, which include owner information, location & address, operating hours, and the market domains they belong to.
- Products, that are offered by businesses, including the particular product's models, pricing, stock levels, and category they belong.
- Services, that are also offered by businesses, which includes the information about their availability, pricing, duration, and categories.
- Product Orders, the orders are done through and represented by User Carts, which hold information related to order status, items ordered, quantities, price at time of order, and assigned delivery agents or user's self pick (as the system is dedicated to local marketplace support).
- Service Orders, the orders that are requested or booked by the users to receive service, which includes order status, service details, quantity, and price at the time of order.
- Payments, for both product and service orders, the payment section holds information like payment-status, payment-mode, amount-paid or to-be-paid, and paid-on (date or timestamp).
- Ratings and comments, which are provided by users for products and services they have received.
- Operational Locations, including the information about the areas where this system operates and manages its operations.
- Market Domains, to determine in classifying businesses, products, and services.
- System Logs, as tracking is the significant aspect for data movements (Insertions, Deletions, Updations) across core and lookup tables, which extends scope for, analysis for future assessments, and for decision making.
Out of scope for this project involve aspects like direct user-to-user messaging, complex promotional or discount systems, delivery agent commission tracking, detailed inventory management, specific payment gateway operations, and advanced delivery route optimization.
The database will support:
- CRUD operations for users (all roles), businesses, products (and product's models), and services.
- Association of businesses with relevant market domains (products/services).
- Tracking product orders (via carts) through various statuses (pending, confirmed, out-for-delivery, delivered, cancelled, etc.).
- Tracking service orders through various statuses (pending, confirmed, in-progress, completed, cancelled, etc.).
- Managing product stock levels automatically, based on order confirmation and cancellation/returns (with triggers).
- Managing service availability slots automatically based on order confirmation and completion/cancellation (with triggers).
- Assigning delivery agents to product orders (user's carts).
- Recording payments associated with product carts and service orders, including different payment modes and statuses (pending, completed, refunded, etc.).
- Allowing users to rate and comment on products (and product's models) and services they have completed/received.
- Implementing business logic and data integrity through use of column-constraints, table-constraints, foreign-key-constraints and triggers (e.g., user role validation for business ownership, status transition restrictions, password history checks, rating eligibility etc.).
- Logging significant data movements, changes in core tables with a dedicated logs table.
- Provides various pre-defined views for simplified querying of common information patterns (e.g., user counts per location, product/service summaries, active users, pending payments, top sellers etc.).
Entities are captured in SQLite tables with the following schema.
The database includes the following entities:
The locations table holds information about the locations where this system's hubs operates and manages it operations so that the users can use it for digital collaboration with their customers and business owners.
It includes:
id, which specifies the unique ID for each location as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.municipality, which specifies the town, city or village name asTEXT.district, which specifies the district name asTEXTthat the correspondingmunicipalityexist.state, which specifies the state name asTEXTthat the correspondingmunicipalityanddistrictexist in.country, which specifies the country name asTEXTthat the correspondingmunicipality,district, andstateexist in.pincode, which specifies the zip-code or pincode of the location asTEXT, which helps uniquely identifies a location precisely, the reason for storing it as aTEXTis, as some pincodes involve special characters and can be of any length between 4 and 10.
The market_domains table holds information about the different domains of the businesses that are available in most of the markets.
It includes:
id, which specifies the unique ID for each market domain as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.type, which specifies the type of the domain asTEXT, which has few pre-defined values with aCHECKconstraint applied, that determines whether the domain belongs to product, service or both.domain, which specifies the name of the domain asTEXT.
The order_statuses table holds information for the types of statuses for all products and services orders.
It includes:
id, which specifies the unique ID for each order status as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.type, which specifies the type of the order asTEXT, which has few pre-defined values with aCHECKconstraint applied, that determines whether the order belongs to product, service or both.status, which specifies the name of the order-status asTEXT, and each order-status is unique, thusUNIQUEconstraint applied.
The payment_statuses table holds information for the types of statuses for all product and service orders payments.
It includes:
id, which specifies the unique ID for each order's payment status as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.status, which specifies the name of the payment-status asTEXT, and each payment-status is unique, thusUNIQUEconstraint applied.
The payment_modes table holds information for the types of modes that a payment was made through for a product or service order.
It includes:
id, which specifies the unique ID for each order's payment mode as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.mode, which specifies the name of the payment-mode asTEXT, and each payment-mode is unique, thusUNIQUEconstraint applied.
The users table holds information of every user, the user can be a customer, business_owner, and delivery_agent.
It includes:
id, which specifies the unique ID for each user as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.name, which specifies the full-name or actual-name of the user asTEXT.username, which specifies the user's simple identifiable name i.e username asTEXT, and to ensure each and every user has unique usernameUNIQUEconstraint applied, including that aCHECKconstraint also applied to check the length of the username of user's.password, which specifies the user's password that he/she uses to sign in or sign up to the system, at the application level it can be hashed with a specific algorithm, but due to the limitations of SQLite3 I appliedTEXTas it's type, and also appliedCHECKconstraint to check the length of the user's password.age, which specifies the user's age as anINTEGER, and I enforced aCHECKconstraint to check whether theagementioned is valid or not.gender, which specifies the gender of the user asTEXT, and included aCHECKconstraint to validate the gender to be in pre-defined values.phone, which specifies the mobile/phone number of the user's, having aCHECKconstraint to validate the format and length of the user's phone-number, as a phone number contains the special charactersTEXTtype used, and as the phone number must needs to be unique for each and every userUNIQUEconstraint used.email, which specifies the email of the user's asTEXT, and included aCHECKconstraint to validate the format and length of the email, enforced aUNIQUEconstraint to ensure the user's email must be unique to another users, and added aDEFAULTconstraint withNULLvalue, as some user's might not interested to provide the email based on their consent.address, which specifies the address of the user's asTEXT.location_id, which specifies thePRIMARY KEYof thelocationstable, that is being referenced to as the id of the location where theaddressmight correspond to, thus theFOREIGN KEYconstraint links this tolocationstable.user_role, which specifies the role of the user that the system is being used asTEXT, included pre-defined user roles to the schema by usingCHECKconstraint to validate theuser_role.
The businesses table holds the basic information about all the businesses.
It includes:
id, which specifies the unique ID for each business as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.owner_id, which specifies the ID of the user who hasbusiness_ownerrole inuserstable, that is being referenced to as id of the user inusers, , thus theFOREIGN KEYconstraint links this touserstable.name, which specifies the name of the business asTEXT.address, which specifies the address of the business asTEXT.location_id, which specifies thePRIMARY KEYof thelocationstable, that is being referenced to as the id of the location where theaddressmight correspond to, thus theFOREIGN KEYconstraint links this tolocationstable.open_timeandclose_time, the both specifies the timing of the business working hours which has givenNUMERICtype, which is relevant to store time/date type data, and also implementedDEFAULTvalues to maintainNULLfree records.
The business_associations is the associative/junction table that holds the information which business belongs to which domain
It includes:
business_id, which specifiesINTEGERthat references to theidinbusinessestable, to point out the one or more domains maintained by the same business. For example: A restaurant can deliver the food they make as a product(product-based-domain), and the same restaurant can also serve their customers as a service(service-based-domain), thus theFOREIGN KEYconstraint links this tobusinesstable.domain_id, which specifiesINTEGERthat references to theidinmarket_domainstable, that points the actual domain the business is pointing out, thus theFOREIGN KEYconstraint links this tomarket_domainstable.
The products table holds the information of the product's name, business that is selling, and what category or market domain does it belong etc.
It includes:
id, which specifies the unique ID for the product asINTEGER. Thus this column has thePRIMARY KEYconstraint applied.business_id, which specifies the ID asINTEGER, that the business that owns the product, and that ID is being referenced frombusinessestable, thus theFOREIGN KEYconstraint links this tobusinessestable.name, which specifies the name of the product asTEXT.category_id, which specifies the ID of the domain i.e category, which is being referenced frommarket_domainstable, as a product might belong to certain category or domain for extra detail, thus theFOREIGN KEYconstraint links this tomarket_domainstable.UNIQUE("business_id", "name"), which specifies the pair ofbusiness_idand product'snameis needs to beUNIQUE, to avoid the redundancy theUNIQUEconstraint applied.
The product_models table holds the information about the certain product's models/variants.
It includes:
id, which specifies the unique ID for product model asINTEGER. Thus this column has thePRIMARY KEYconstraint applied.product_id, which specifies the ID of the product inproductstable asINTEGER, to identify where the certain model points to which product inproductstable, thus theFOREIGN KEYconstraint links this toproductstable.size, which specifies the size parameter of the product model asTEXT, whereas it can involve the various metrics of size based on products and their models soTEXTis appropriate to apply, and theDEFAULTvalue asNULLis applied as all product-models don't have size parameter.color, which specifies the color parameter of the product model asTEXT, and theDEFAULTvalues asNULLis applied as all product-models don't actually rely on color parameter. So, theTEXTis appropriate to store color based values. (For more in advance we can also store HEX-codes asBIGINTEGERorINTEGER).weight_kg, which specifies the weight parameter of the product model asREAL, and theCHECKconstraint is applied to allow the product-model's weight to be greater than 1 gram and less than 1000 kilo gram, to avoid unrealistic weights as input, for the cases when there is no-need for weight parameter for the product-model'sDEFAULT NULLconstraint used.material, which specifies the type of material that the product-model made up of asTEXT, and similarly toweight_kgfor the cases when there is no-need for material parameter for the product-model'sDEFAULT NULLconstraint applied, and theTEXTtype is appropriate to hold the name-type fields.price, which specifies the price of the product-model asREAL, and aCHECKconstraint applied to make the price greater than 1.available_stock, which specifies the stock that is available for the product-model asINTEGER, and theCHECKconstraint applied to validate the stock not going in to negative values, andDEFAULT 0constraint used incase of incorrect input given.min_order_qty, which specifies the minimum stock to order asINTEGERvalue, aCHECKconstraint is used to specify the minimum order requirement value should not be less than 1, andDEFAULT 1constraint used incase of incorrect input given.mfg_dateandexp_date, the both specifies the dates of product-model's manufacture date and expire date (in case if it has) asNUMERICvalues, andDEFAULT CURRENT_TIMESTAMPconstraint applied in case of not manufacture date given.UNIQUE("product_id", "size", "color", "weight_kg", "material", "price", "mfg_date", "exp_date"), which specifies the set of a product-model for a certain product referenced fromidasproduct_idmust have aUNIQUErecord of product-model, to avoid redundancy theUNIQUEconstraint applied for the set of columns.
The user_carts table holds the information of user's cart that allows user to order 1 or more than a product at a time.
It includes:
id, which specifies the unique ID for user's cart asINTEGER. Thus this column has thePRIMARY KEYconstraint applied.user_id, which specifies the ID of the user asINTEGER, that is being referenced fromuserstable, to point out which user ordered which cart at a particular period of time/days/months, thus theFOREIGN KEYconstraint links this touserstable.ordered_on, which specifies the date of the cart was ordered asNUMERIC, andDEFAULT NULLis applied pending carts hold ordered-on date to be NULL.status_id, which specifies the ID of the status inorder_statusestable, which states the status of the order whether it is delivered, returned, or cancelled etc., hereDEFAULT 0constraint used for the reason it can only be 0 when aDELETEwas made onorder_statusesbased onFOREIGN KEYconstraint.delivery_agent_id, which specifies the ID of the user who is adelivery_agentinuser_roleofuserstable asINTEGER, hereDEFAULT 0points to the imaginarydelivery_agentnamedself-pick-upinuserstable as the name suggests, as this is a local marketplace support system, some users might pick their cart on their own rather than getting it delivered, so for those cases there is a key user inuserstable withid-0, and it is theDEFAULTvalue when adelivery_agentis not assigned for the certain cart, and for theFOREIGN KEYconstraint used to setNULLvalue as thedelivery_agentlefts (DELETE ON"users"table) from the system, thus theFOREIGN KEYconstraint links this touserstable.
The product_orders table holds the information of each cart contains which product_model of a product that is ordered through a user's cart.
It includes:
id, which specifies the unique ID of the order-id for the product-model that is being ordered under certain cart asINTEGER. Thus this column has thePRIMARY KEYconstraint applied.cart_id, which specifies the ID of the cart asINTEGER, that is being referenced fromuser_cartstable to point out to which cart does the certain product-order belongs to, thus theFOREIGN KEYconstraint links this touser_cartstable.product_model_id, which specifies the ID of the product_model that is being ordered in a cart asINTEGER, that is being referenced fromproduct_modelstable, thus theFOREIGN KEYconstraint links this toproduct_modelstable.qty, which specifies the quantity of the product_model that is being ordered asINTEGER, aCHECKconstraint is applied to validate the quantity when being ordering must be greater than or equal to 1.price_at_order, which specifies the price of the product_model asREALwhen it is ordered via a cart, aCHECKconstraint is applied to validate the price needs to be greater than 0.
The cart_payments table holds the information about cart's payments statuses and mode, paid on information, and total price of cart etc.
It includes:
id, which specifies the unique ID of the payment-id for the cart's payment asINTEGER. Thus this column has thePRIMARY KEYconstraint applied.cart_id, which specifies the ID of the cart asINTEGER, that is being referenced fromuser_cartstable to point out to which cart does the certain payment is associated, thus theFOREIGN KEYconstraint links this touser_cartstable.total_price, which specifies the total-price of the cart that is being ordered asREAL, aCHECKconstraint is applied to validate thetotal_priceshould be greater than 0.status_id, which specifies the ID of the payment's status asINTEGER, that is being referenced frompayment_statusestable to map the status of the payment - whether it is pending, completed, cancelled etc., thus theFOREIGN KEYconstraint is applied to link this topayment_statusestable, and aDEFAULT 0value is used in the case ofFOREIGN KEYconstraintON DELETEaction the default value 0 is placed, as the value-0 points to the deleted status in linked table (payment_statuses).mode_id, which specifies the ID of the payment's mode asINTEGER, that is being referenced frompayment_modestable to map the mode of the payment that done or being done - whether it is cash, phonepe, google-pay, etc., thus theFOREIGN KEYconstraint links this topayment_modestable, and aDEFAULT 1value is used in the case ofFOREIGN KEYconstraintON DELETEaction the default value 1 is placed, as the value-1 points to the cash payment-mode in the linked table (payment_modes).paid_on, which specifies the date, when were the payment was made.
The product_ratings table holds the ratings of the product-models that are ordered by the users, if user given rating on it.
It includes:
user_id, which specifies the ID of the user asINTEGER, that is being referenced fromuserstable to point out which user given rating to which product-model, thus theFOREIGN KEYconstraint links this touserstable.product_model_id, which specifies the ID of the product-model asINTEGER, that is being referenced fromproduct_modelstable to point out which product-model was rated, thus theFOREIGN KEYconstraint links this toproduct_modelstable.rating, which specifies the rating on the product-model that the user given asREAL, aCHECKconstraint implemented to check rating should be in between 1.00 AND 5.00.comment, which specifies the comment on the product-model that the user given asTEXT, aCHECKconstraint is used to ensure the comment length should exceed 100 characters.rated_on, which specifies the timestamp (date + time) asNUMERICin which the comment or rating recorded.PRIMARY KEY("user_id", "product_model_id"), this defines a composite primary key using the combination ofuser_idandproduct_model_id. This constraint is applied to uniquely identify each rating record, furthermore it ensures that each combination ofuser_idandproduct_model_idcan only appear once to avoid redundancy.
The services table holds the information of the service's name, business that is providing, what category or market domain does it belong etc., price of service, availability, and estimate work duration for a service etc.
It includes:
id, which specifies the unique ID for the service asINTEGER. Thus this column has thePRIMARY KEYconstraint applied.business_id, which specifies the ID asINTEGER, that the business that provides the service, and that ID is being referenced frombusinessestable, thus theFOREIGN KEYconstraint links this tobusinessestable.name, which specifies the name of the service asTEXT, given thatTEXTis appropriate for the name fields.category_id, which specifies the ID of the domain i.e category, which is being referenced frommarket_domainstable, as a service might belong to certain category or domain for extra detail, thus theFOREIGN KEYconstraint links this tomarket_domainstable.availability, which indicates theBOOLEANvalue, which indicates the availability of the service like whether it is available (1) or not-available (0). Thus aCHECKconstraint applied to validate theavailabilitymust be in pre-defined values.available_slots, which indicates the slots asINTEGER, to showcase how many times whether the service can be booked or performed concurrently, as per maintaining requirement for the available-slots should be more than or equal to 5, for this reason theCHECKconstraint is applied.price, which specifies the price of the service asREAL, aCHECKconstraint is applied to ensure the price is greater than 0.est_wrk_dur_hrs, which specifies theestimated work duration hoursasREAL, which indicates only the value in hours, aCHECKconstraint is applied to validate the work-duration must be greater than or equal to 10 minutes (ROUND(10.0/60.0, 4)).UNIQUE("business_id", "name"), which specifies the pair ofbusiness_idand product'snameis needs to beUNIQUE, to avoid the redundancy theUNIQUEconstraint applied.
The service_orders table holds the information about the services that are ordered by the users
It includes:
id, which specifies the unique ID asINTEGER, that the service-order can be indicated, thus this column has thePRIMARY KEYconstraint.user_id, which specifies the ID of the user asINTEGER, who was ordered/ordering the service, thus theFOREIGN KEYconstraint links this touserstable.service_id, which specifies the ID of the service asINTEGER, that is being ordered/getting ordered by the user, thus theFOREIGN KEYconstraint links this toservicestable.qty, which specifies the quantity of slots asINTEGER, that is booked or requested at the time of being ordered/getting ordered, aCHECKconstraint is applied to validate theqtyshould be greater than or equal to 1 and less than or equal to 5.price_at_order, which specifies the price of the service at the time when that is being ordered/getting ordered asREAL, aCHECKconstraint is applied to validate that price is greater than 0.ordered_on, which specifies the date in which the service is ordered asNUMERICdata.status_id, which specifies the ID asINTEGERof the order-status from theorder_statusestable, thus theFOREIGN KEYconstraint links this toorder_statusestable.
The service_payments table holds the information about the services payments-statuses, date-paid on etc.
It includes:
id, which specifies the unique ID asINTEGERfor the payment associated with a service-order, thus this column has thePRIMARY KEYconstraint.service_order_id, which specifies the ID of the service-order asINTEGER, that is associated to theservice_orderstable, thus theFOREIGN KEYconstraint links this toservice_orderstable.price, which indicates the price of the service-order.status_id, which indicates the ID of the payment-status asINTEGER, that is associated to thepayment_statusestable, thus theFOREIGN KEYconstraint links this topayment_statusestable.mode_id, which indicates the ID of the payment-mode asINTEGER, that is associated to thepayment_modestable, thus theFOREIGN KEYconstraint links this topayment_modestable.paid_on, which indicates the date when the payment was made or done asNUMERIC.
The service_ratings table holds the ratings of the services that are ordered by the users, if user given rating on it.
It includes:
user_id, which specifies the ID of the user asINTEGER, that is being referenced fromuserstable to point out which user given rating to which service, thus theFOREIGN KEYconstraint links this touserstable.service_id, which specifies the ID of the service asINTEGER, that is being referenced fromservicestable to point out which service was rated, thus theFOREIGN KEYconstraint links this toservicestable.rating, which specifies the rating on the service that the user given asREAL, aCHECKconstraint implemented to check rating should be in between 1.00 AND 5.00.comment, which specifies the comment on the service that the user given asTEXT, aCHECKconstraint is used to ensure the comment length should exceed 100 characters.rated_on, which specifies the timestamp (date + time) asNUMERICin which the comment or rating recorded.PRIMARY KEY("user_id", "service_id"), this defines a composite primary key using the combination ofuser_idandservice_id. This constraint is applied to uniquely identify each rating record, furthermore it ensures that each combination ofuser_idandservice_idcan only appear once to avoid redundancy.
As detailed by the diagram:
- A
locationcan be associated with zero, one, or manyusers, and eachusermust be associated with exactly onelocation. Furthermore for businesses, alocationcan contain one, or many businesses. Eachbusinessmust reside in exactly onelocation. - A
usercan own zero, one or many businesses. Eachbusinessmust be owned by exactly oneuser. - A
businesscan be associated with one or manymarket_domainsthrough thebusiness_associationstable. On the other hand, amarket_domaincan categorize one or manybusinessesthrough the same association table. Each entry inbusiness_associationslinks exactly onebusinessto exactly onemarket_domain. - A
businesscan offer (own) zero, one or manyproducts. Eachproductmust be offered by exactly onebusiness. - A
businesscan provide zero, one or manyservices. Eachservicemust be provided by exactly onebusiness. - A
market_domaincan act as a category for zero, one or manyproducts. Eachproductmust belong to exactly onemarket_domain(category). Similar to that, amarket_domaincan act as a category for zero, one or manyservices. Eachservicemust belong to exactly onemarket_domain(category). - A
productcan have zero, one or many specificproduct_models(variants). Eachproduct_modelmust belong to exactly oneproduct. - A
user(customer) can create zero, one or manyuser_carts. Eachuser_cartmust belong to exactly oneproduct. In addition to that, Auser(delivery_agent) can be assigned to deliver zero, one or manyuser_carts. Auser_cartcan be assigned to at most one delivery agent. - A
user_cart(representing an order) can contain one or manyproduct_orders. Eachproduct_ordermust belong to exactly oneuser_cart. - A
product_modelcan include in zero, one or manyproduct_orders. Eachproduct_ordermust refer to exactly oneproduct_model. - An
order_statuscan apply to zero, one or manyuser_carts. Eachuser_cartmust have exactly oneorder_status. - A
user_cartcan have zero or onecart_paymentassociated with it. Eachcart_paymentmust be associated with exactly oneuser_cart. - A
payment_statuscan apply to zero, one or manycart_payments. Eachcart_paymentmust have exactly onepayment_status. - A
payment_modecan be used for zero, one or manycart_payments. Eachcart_paymentmust have exactly onepayment_mode. - A
usercan give zero, one or manyproduct_ratings, but only one rating per specificproduct_model. Eachproduct_ratingmust be give by exactly oneuser. - A
user(customer) can place zero, one or manyservice_orders. Eachservice_ordermust be placed by exactly oneuser. - A
servicecan be part of zero, one or manyservice_orders. Eachservice_ordermust refer to exactly oneservice. - A
order_statuscan apply to zero, one or manyservice_orders. Eachservice_ordermust have exactly oneorder_status. - A
service_ordercan have zero or oneservice_paymentassociate with it. Eachservice_paymentmust be associated with exactly oneservice_order. - A
payment_statuscan apply to zero, one or manyservice_payments. Eachservice_paymentmust have exactly onepayment_status. - A
payment_modecan be used for zero, one or manyservice_payments. Eachservice_paymentuses at most onepayment_mode. - A
usercan give zero, one or manyservice_ratings, but only one rating per specificservice. Eachservice_ratingmust be given by exactly oneuser. - A
servicecan receive zero, one or manyservice_ratings. Eachservice_ratingmust be for exactly oneservice.
The following VIEWs, INDEXes, TRIGGERs are the optimizations that I had made to increase the performance and functionality of the schema.
The views in this schema give a pre-defined result set that will optimize the data access, which in turn they optimize the way of querying.
The following are the views that I came up with:
-
users_in_locations: This view provides information about the number of users in each location, who are categorized by their roles (customers,business_owners,delivery_agents). -
customer_info: This view contains detailed information about eachcustomer, which includes theirname,username,age,gender, andlocation, thus this view don't expose the sensitive information likepassword,address,phone,emailetc.. -
business_owners_info: This view provides detailed information about eachbusiness_owner, which includes theirname,username,age,gender,business-name,working hoursandlocation. -
business_count_per_owner: This views shows the number of businesses owned by each business owner. This view uses the above view (i.e.,business_owners_info), on the other hand, this view can also be aTEMPORARY VIEW. -
delivery_agent_info: This view contains detailed information about eachdelivery_agent, which includes theirname,username,age,gender,phone,location, and the number of order they have dealt with. -
products_info: This view contains detailed information about each product, which includes the business it belongs to (business_id),product-name,domain, and the product's models information. -
services_info: This view contains detailed information about each service, which includes the business it belongs to (business_id),service-name,domain,price, andworking-duration. -
users_products_orders_count: This view shows the number of product orders made by each user. -
user_carts_info: This view provides the detailed information about each user's cart, which includes the ordered date (ordered_on), order status (order_status), payment status (payment_status), anddelivery_agent- information. -
users_service_orders_count: This view shows the number of service orders made by each user. -
service_orders_info: This view contains detailed information about each service order, which includes the user who ordered it,nameof the service,qty(quantity),price_at_order(price), and payment information (fromservice_paymentstable). -
product_based_businesses: This view provides information about product-based businesses, which includes theirname,owner,type,addressandworking-hours. -
service_based_businesses: This view provides information about service-based businesses, which includes theirname,owner,type,addressandworking-hours. -
market_domains_by_locations: This view shows the different market domains available in each location, which are categorized by product-based and service-based businesses. -
available_products_by_locations: This view provides information about the products available in each location. -
available_services_by_locations: This view contains information about the services available in each location. -
products_ratings_summary: This view shows the average (AVG) rating and number of ratings of each product, along with the business and owner information. -
service_ratings_summary: This view contains the average rating (AVG) and number of ratings of each service, along with business and owner information. -
pending_cart_order_payments: This view shows the pending payments for the cart orders, which includes the users and their corresponding cart information. -
pending_service_order_payments: This view contains the pending payments for the service orders, which includes the user and order information. -
active_users_summary_cart_orders: This view provides information about users who have placed orders in their carts in the last 30 days, which includes the ordered date (ordered_on), delivery agent (delivery_agent_id), and order status (order_status). -
active_users_summary_service_orders: This view contains information about users who have place services orders in the last 30 days, which includes the ordered date (ordered_on), delivery agent (delivery_agent_id), and order status (order_status). -
order_frequency_per_user: This view shows the frequency of cart orders and service orders for each user in the last month. -
top_selling_products: This view lists the top 10 products based on total orders and price. -
top_selling_services: This view lists the top 10 services based on total orders and price.
The indexes that are defined in this schema will optimize the query performance, and provides efficient data retrieval and manipulation. The following indexes play a crucial role in enhancing the overall performance and scalability of the system.
The following are the indexes that I came up with:
-
idx_user_roles: This index speeds up queries that retrieve the user details by their role (customer,business_owner,delivery_agent), which enables efficient user management. -
idx_username: This index enables fast lookups when searching for users byusername, which coordinates user authentication and profile management. -
idx_users: This index optimizes queries that filter users byageandgender, which facilitates the demographic analysis. -
idx_locations: This index enhances query performance when searching for locations bymunicipality,state,country, orpincode, which supports efficient location-based services. -
idx_location_pincode: This index provides fast lookups for locations by pincode, facilitating efficient delivery and logistics operations. -
idx_businesses: This index speeds up queries that retrieve business details byowner_id, andname, which enables efficient business management and customer search. -
idx_market_domains: This index optimizes queries that filter businesses by domaintypeorcategory, which supports market analysis and trend based analysis. -
idx_products: This index enables fast lookups for products byname, in which it facilitates efficient product search and discovery. -
idx_product_models: This index speeds up queries that retrieve product model details by attributes likesize,weight,color,priceandmfg_date, in turn this index supports efficient product management. -
idx_user_carts: This index enhances order history lookups and delivery agent tracking, which further enables efficient order management and logistics operations. - Β
idx_user_carts_2: This index enhances the speed when fetching a user's orders with specific status. -
idx_product_orders: This index optimizes queries that filter product orders by quantity (qty), andprice, which supports sales analysis and revenue tracking. -
idx_order_statuses: This index speeds up queries that retrieve order statuses bytypeandstatus, which enables efficient order management and tracking. -
idx_payment_statuses: This index optimizes queries that retrieve payment statuses, which support efficient payment processing and tracking. -
idx_payment_modes: This index enables fast lookups for payment modes, which facilitates in efficient payment processing and transaction management. -
idx_cart_order_payments: This index speeds up queries that retrieve payment data by payment date, which assists in financial analysis and revenue tracking. -
idx_product_ratings: This index optimizes queries that sort product ratings, which in turn enables efficient review management and product ranking. -
idx_services: This index enhances the search performance for services by attributes likename,availability,price, and estimated work duration (est_wrk_dur_hrs), which supports efficient service discovery. -
idx_service_name: This index provides fast lookups for services byname, which facilitates efficinet service searching and discovery. -
idx_service_orders: This index speeds up queries that analyze trending service orders over time, which supports businesses to gain insights and also assist in identifying trend. -
idx_service_payments: This index optimizes queries that retrieve payment data by payment date, which in turn it supports the financial analysis and revenue tracking for service. -
idx_service_ratings: This index enables efficient sorting and filtering of service reviews byrating, which supports review management and service ranking.
I came up with the following triggers the increase the functionality of the schema very much, although most of them can be handled in application level but I had taken a deep consideration on data-integrity and consistency, thus made the following comprehensive triggers for the schema.
Logically, I came up with 4 types of triggers they are:
-
Logging Triggers, triggers that keep track of data movements within database. -
Information about logging.
I came up with a separate, dedicated table calledTABLE-19: logsthat holds and records the data movements with the help of Logging Triggers.TABLE-19:logs
Thelogstable that holds the logs of all tables in the database, which further help in analysis, historical tracking, recording crucial changes etc.
It includes:id, which specifies the unique ID of the log that has recorded by thetriggers. Thus this column is thePRIMARY KEYconstraint.record_id, which specifies the ID of the specific record in target tables when there is a movement in data through operations in it.table, which specifies the name of the table that had affect with data movements, and stores it asTEXT, aCHECKconstraint is applied to validate only valid table names come in input.operation, which specifies the type of operation made on correspondingtable, the operation can beINSERT,UPDATEorDELETE, and this data is stored inTEXT, asCHECKconstraint is applied to validate theoperationvalue is falls under pre-defined values.column, which specifies the name of the column of the correspondingtablethat had affected after/before theoperationit is stored asTEXT.old_value, which specifies the old-value whenever anUPDATE-operationtakes place on atableand this columnold_valueholds the record of old-value of aUPDATE.new_value, which specifies the new-value whenever anUPDATE-operationtakes place on atableand this columnnew_valueholds the record of new-value of aUPDATE.description, which specifies the description of theoperationthat made on target-tableon a certaincolumn.timestamp, which specifies the timestamp of theoperationwhen it was recorded, to keep real-time trackingDEFAULT CURRENT_TIMESTAMPwas applied.
-
Restricting Triggers, triggers that restrict certain operations on certain tables based on certain conditions to maintain integrity and security of the data.
-
Validating Triggers, triggers that validate whether the data is valid enough to get in through operations like
INSERT,UPDATE, andDELETE. -
Automating Triggers, triggers that automate (automatically process data in corresponding tables) certain when certain changes occur in certain table with certain operation.
The following are the triggers tables wise:
log_new_userlog_left_userlog_user_updates
The above triggers are used for logging user's data movements like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_user - automatically logs the bit information of new-users along with a timestamp states that when their account was created. The log_left_user - logs the deletion of users (users who left from the system) by records their username and the timestamp states that when did they actually left. The log_user_updates - tracks the updates of user's information like password, phone,email and user_role to monitor the user activity, thus these triggers assist in maintaining logs which are useful for tracking and monitoring user's activity.
restrict_user_password_update: The trigger that prevents users to reusing previously used passwords, which is dedicated to increase the security by ensuring password uniqueness and reducing the risk of compromised accounts.
log_new_businesslog_closed_businesslog_business_updates
The above triggers are used for logging business's data movements like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_business - automatically logs the creation of new businesses by recording owner_id, business's-name, address, and location_id. The log_closed_business - logs the records of deleted (closed) businesses, again by recording owner_id,business's-name,address, and location_id to track when businesses are closed. The log_business_updates - tracks the updates to business's information like business's-name, address, and owner_id, to monitor business activity, thus these triggers assist in maintaining logs which are useful for tracking and monitoring business's activity.
restrict_business_name_update: The trigger that limits the number of times a business name can be updated to 5, which prevents excessive changes and maintain data consistency.
validate_business_existence: The trigger that prevents duplicate businesses from being created by checking for existing businesses with the samenameandowner_id.
The triggers 2.3.2, 2.3.3 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
-
validate_owner_role_on_insert&validate_owner_role_on_update: These triggers ensures that that onlyuserswith theiruser_roleasbusiness_ownercan be assigned as owners of the businesses on bothINSERTandUPDATEcases separately. Thus these both triggers prevents unauthorized assignments and increase the data integrity. -
validate_business_location: The trigger that ensures that businesses are located in the same location as their owners, which contributes in maintaining data consistency and preventing invalid location assignments. -
validate_business_working_hours: The trigger that ensures that businesses are open for at least 5 hours, this allows the system to maintain a standard for business operations and also prevents invalid working hours. -
validate_business_timings_format: The trigger that validates the format of businessopen_timeandclose_time, by ensuring they are in required format in 24-hour clock notation.
restrict_update_on_business_id: The trigger that restricts direct update to thebusiness_idonbusiness_associationstable, in which it prevents unauthorized changes and maintains the data integrity by ensuring that business associations are not changed without proper authorization.
log_new_productlog_removed_productlog_product_updates
The above triggers are used for logging the data movements in product's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_product - automatically logs the creation of new product, by recording the business_id, and the product's - id. The log_removed_product - logs the deletion of products, and it records the business_id and product's - id to track product removals. The log_product_updates - logs the updates of the product's information like name, category_id, and business_id, to track product activity for ensuring data consistency.
The triggers 4.2.1, 4.2.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
restrict_product_category_id_zero_on_insert&restrict_product_category_id_zero_on_update: These triggers prevents thecategory_idfrom being set to0, which ensures valid category assignments and prevents data inconsistencies uponINSERTandUPDATEoperations, as0ofcategory_idindicatesdeleted-domain/categoryvalue.
The triggers 4.3.1, 4.3.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
validate_product_category_id_on_insert&validate_product_category_id_on_update: These triggers validates thatcategory_idonly belong toproductsundermarket_domainstable uponINSERTandUPDATEoperations, whereas this further assists in maintaining data integrity and prevent invalid category assignments.
log_new_product_modellog_removed_product_modellog_product_model_updates
The above triggers are used for logging the data movements in product-model's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_product_model - logs the creation of new-product model, which captures the product_id, and product-model's - id, which helps in tracking product-model additions. The log_removed_product_model - logs the deletion of product-models, which the records the product_id, and product_model's - id, to track product-model removals. The log_product_model_updates - tracks the updates of the product-model information which includes price (to track trends in price changes), and available_stock (to track the demand of the product-model).
The triggers 5.2.1, 5.2.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
compare_product_model_mfg_exp_date_on_insert&compare_product_model_mfg_exp_date_on_update: These triggers ensures that themfg_date(manufacture-date) is not later thanexp_date(expiration-date) upon bothINSERTandUPDATEoperations, thus these triggers prevents invalid date assignments and maintains data consistency and integrity.
The triggers 5.2.3, 5.2.4 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
validate_format_of_mfg_exp_dates_on_insert&validate_format_of_mfg_exp-dates_on_update: These triggers validates that the formats of themfg_date(manufacture-date) andexp_date(expiration-date) uponINSERTandUPDATEoperations, thus these triggers ensures that they are in required format and prevents inconsistent data.
log_new_servicelog_removed_servicelog_service_updates
The above triggers are used for logging the data movement in service's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_service - logs the creation of new services, which records the business_id, and the service's - id, which tracks the service additions. The log_removed_service - logs the deletion of services, by recording the business_id, and the service's - id, to track service removals. The log_service_updates - tracks the updates on service's information like its category_id (to track the trends in category updates of a business's service or services), availablility (to track the service demand), and price (to track the price trends of the service).
The triggers 6.2.1, 6.2.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
restrict_service_category_id_zero_on_insert&restrict_service_category_id_zero_on_update: These triggers prevents thecategory_idfrom being set to0as it refers to thedeleted-domain/categoryin referenced tablemarket_domainsuponINSERTandUPDATEoperations, thus these triggers ensures the valid category assignments.
The triggers 6.3.1, 6.3.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
validate_service_category_id_on_insert&validate_service_category_id_on_update: These triggers validates thatcategory_idonly belong toservicesundermarket_domainstable uponINSERTandUPDATEoperations, whereas this further assists in maintaining data integrity and prevent invalid category assignments.
log_new_cartlog_removed_cartlog_cart_updates
The above triggers are used for logging the data movement in users-cart's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_cart - logs the creation of new carts, which records the user_id, and the user-cart's - id, which tracks the cart initiations. The log_removed_cart - logs the deletion or abandonment of carts, by recording the user_id, and the user-cart's - id, to track cart removals. The log_cart_updates - tracks the updates on user-cart's information like its status_id (to track the status's updates (transitions) on carts), ordered_on (to observe the trends when the orders hiked and dropped), and delivery_agent_id (to keep track of which delivery-agent was assigned).
-
restrict_cart_status_if_it_not_confirmed: The trigger that prevents updating the cart status tocart-order-delivered,cart-order-self-pick-up,cart-order-returned, orcart-order-cancelledif the cart was never confirmed (cart-order-confirmed) or out-for-delivery (cart-order-out-for-delivery), for ensuring valid status transitions. -
restrict_cart_status_if_it_moves_back_from_confirmed_to_pending: The trigger that restricts changing cart status back tocart-order-pendingafter confirmed (cart-order-confirmed), which is essential for maintaining a logical status flow. -
restrict_delivery_agent_update_on_delivered_orders: The trigger that prevents updatingdelivery_agent_idif the order is alreadycart-order-out-for-deliveryorcart-order-delivered, which is essential to maintain the data consistency. -
restrict_update_on_cart_ordered_date: The trigger that restricts updatingordered_ondate once it is already set, this ensures the data integrity. -
restrict_confirmed_cart_order_deletion: The trigger that prevents deleting user-carts status is confirmed (cart-order-confirmed), out-for-delivery (cart-order-out-for-delivery), delivered (cart-order-delivered), self-picked (cart-order-self-pick-up), returned (cart-order-returned), or cancelled (cart-order-cancelled). Thus this enhances the data consistency. -
restrict_cart_status_confirmed_when_cart_is_empty: The trigger that restricts confirming cart-orders when the cart is empty, thus preventing invalid order-confirmations.
validate_stock_before_cart_confirmed: The trigger that validates stock availability before confirming a cart order (cart-order-confirmed), which ensures that sufficient stock is being ordered for products.
-
auto_update_payment_status_as_cart_status_change: The trigger automatically sets the payment status of correspondinguser_cart'sincart_paymentsstatus topending, when a cart-order is confirmed (cart-order-confirmed), thus this trigger assists in streamlining payment processing. -
auto_update_payment_after_cart_cancelled_if_paid: The trigger automatically updates thecart_idof particularuser_cartsincart_paymentsstatus (i.e.payment_status) torefundedif the cart is cancelled (cart-order-cancelled) after being paid, as this ensures accurate payment status. -
auto_update_available_stock_when_order_is_confirmed: The trigger automatically decreases theavailable_stockinproduct_modelstable, when a product-order is confirmed (cart-order-confirmed), which make maintaining accurate inventory levels. -
auto_increase_stock_on_cancel_return: The trigger that automatically increases theavailable_stockinproduct_modelsafter auser_cartorder is cancelled (cart-order-cancelled) or returned (cart-order-returned), which in further ensures accurate inventory levels.
log_new_product_orderlog_removed_product_orderlog_product_orders_updates
The above triggers are used for logging the data movement in product-order's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_product_order - logs new product orders, which records the product_model_id and cart_id to track the product additions in carts and user order trends. The log_removed_product_order - logs the removed product orders, by recording the product_model_id, and cart_id, to track product removals in carts to assess the demand of the product. The log_product_orders_update - tracks the updates on product-order's information, by tracking the qty (quantity - to track the demand of the product), and price_at_order (to track the discounts and price-trends at orders).
-
restrict_update_price_after_ordered: The trigger prevents updatingprice_at_orderafter the order is confirmed (cart-order-confirmed), out-for-delivery (cart-order-out-for-delivery), delivered (cart-order-delivered), self-picked (cart-order-self-pick-up) or returned (cart-order-returned), thus improves data integrity. -
restrict_qty_update_after_order_confirmed: The trigger restricts updating quantity (qty) after the cart order is confirmed (cart-order-confirmed), out-for-delivery (cart-order-out-for-delivery), delivered (cart-order-delivered) or self-picked (cart-order-self-pick-up), which in further maintains the accurate order information. -
restrict_adding_product_orders_to_confirmed_carts: The trigger prevents adding products to carts that are already confirmed (cart-order-confirmed), out-for-delivery (cart-order-out-for-delivery), delivered (cart-order-delivered), self-picked (cart-order-self-pick-up) or cancelled (cart-order-cancelled), thus ensures valid cart status and their transitions. -
restrict_qty_exceeding_stock: The trigger that restricts ordering products with quantity (qty) which exceeds available-stock (available_stock), thus this trigger prevents overselling and maintains inventory accuracy.
The triggers 8.3.1, 8.3.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
validate_product_order_price_on_insert&validate_product_order_price_on_update: These triggers validates thatprice_at_order(price at the time of order) against the product-model's price (product_models.price) uponBEFORE INSERTandBEFORE UPDATEoperations, which ensures accurate pricing.
The triggers 8.4.1, 8.4.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
-
auto_update_cart_total_price_on_insert&auto_update_cart_total_price_on_update: These triggers that automatically computes/calculates and stores thetotal_priceincart_paymentstable whenever a new product-order (inproduct_orderstable) is added (uponINSERToperation) to the cart or existing product-order's product price is updated (uponUPDATEoperation) in theproduct_orderstable. -
auto_update_cart_total_price_on_delete: The trigger that updatestotal_priceincart_paymentswhen a product order (inproduct_orderstable) is deleted (uponDELETEoperation), which maintains accurate payment information. -
auto_merge_duplicate_product_orders: The trigger that automatically merges duplicate product order (inproduct_orderstable) by updating quantity (qty), in which this trigger also prevents duplicate rows and ensures data consistency.
log_new_service_orderlog_cancelled_service_orderlog_service_orders_updates
The above triggers are used for logging the data movement in service-order's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_service_order - logs new service orders, which records the service_id and user_id to track the service order additions. The log_cancelled_service_order - logs the cancelled or removed service orders, by recording the service_id, and user_id, to track service order removals in order to assess the demand of the service. The log_service_orders_update - tracks the updates on service-order's information, by tracking the status_id (order status - to track the transistions of the service orders), price_at_order (to track the discounts and price-trends at orders), and orderd_on (to assess the demand of orders in particular period of time).
-
restrict_price_update_after_service_order_placed: The trigger that prevents updating price at order (price_at_order) after the service order is placed, which ensures data integrity and prevents unauthorized changes. -
restrict_update_on_quantity_once_service_order_is_started: The trigger that restricts updating quantity (qty) once the service order is confirmed (service-order-confirmed), in-progress (service-order-in-progress) or completed (service-order-completed), which maintains accurate order information. -
restrict_update_on_service_id_if_order_was_placed: The trigger that prevents updatingservice_idafter the service order is placed, which ensures the data consistency and prevents the unauthorized changes. -
restrict_confirmed_service_orders_delete: The trigger that restricts deleting service orders with confirmed (service-order-confirmed), in-progress (service-order-in-progress), completed (service-order-completed), or cancelled (service-order-cancelled) status, which in turn maintains data integrity and prevents accidental deletions. -
restrict_exceeding_available_service_slots: The trigger that restricts ordering services with quantity (qty) exceeding available_slots (available_slots), which prevents overselling and maintaining accurate service availability.
-
validate_price_in_service_orders: The trigger that validates price at order (price_at_order) against the actual serviceprice, which ensures the accurate pricing and prevents unauthorized changes. -
validate_ordering_available_services: The trigger that validates a user is ordering an available (services.availabilityas1) service or not. This prevents orders for unavailable (services.availabilityas0) services.
-
auto_update_slots_on_service_order: The trigger that automatically decreases available slots (services.available_slots) when a service is ordered and confirmed (service-order-confirmed), thus this ensures the accurate service availability. -
auto_restore_slots_on_service_order_completion_or_cancellation: The trigger that automatically increases available slots (services.available_slots) when a service order status is completed (service-order-completed) or cancelled (service-order-cancelled), which maintains accurate service availability. -
auto_update_payment_status_as_service_order_status_change: The trigger that automatically sets payment status (service_payments.status_id) to pending (service-order-pending) when service order is confirmed (service-order-confirmed), which streamlines payment processing. -
auto_refund_payment_after_service_status_on_cancellation: The trigger that auto-refunds payment when service order is cancelled (service-order-cancelled) after being confirmed (service-order-confirmed) and paid (service_payments.status_idasidof'completed'inpayment_statuses), which further ensures accurate payment status.
log_new_cart_paymentlog_removed_cart_paymentlog_cart_payments_updates
The above triggers are used for logging the data movement in cart-payments's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_cart_payment - logs new cart's payment records, which records the cart_id and cart-payments-id to track the payment activity and monitor user transactions. The log_removed_cart_payment - logs the removed cart-payments records, by recording the cart_id, and cart-payments-id, to track payment removals. The log_cart_payments_update - tracks the updates on cart-payment records, which include status_id (to keep track of the payment's status transitions), paid_on (to assess the support of users towards businesses) and mode_id (to assess the popular trends of payment-modes).
The triggers 10.2.1, 10.2.2 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
-
restrict_pending_cart_payment_status_change_on_insert&restrict_pending_cart_payment_status_change_on_update: These triggers prevents setting the payment-status (status_id) to anything other thanpendingwhen the cart-status is pending (cart-order-pending), uponINSERTandUPDATEoperations, thus these triggers ensures a valid payment status transitions. -
restrict_price_mode_update_if_completed_for_cart_payment: The trigger that restricts updating payment-amount (total_price) or mode (mode_id) after payment-status iscompleted, which maintains accurate payment information.
The triggers 10.2.4, 10.2.5 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
-
restrict_setting_payment_completed_on_insert_for_cart_payment&restrict_setting_payment_completed_on_update_for_cart_payment: These triggers prevents setting the payment-status (status_id) tocompleted, if payment hasn't been made (i.e.,paid_onisNULL) uponINSERTandUPDATEoperations, in which these triggers ensures the accurate payment status. -
restrict_confirmed_payments_delete: The trigger that restricts deleting payments that are completed, refunded, or cancelled uponDELETEoperation, thus allows the data integrity to be maintained and prevents accidental deletions.
auto_refund_payment_after_cart_status_return_or_cancelled: The trigger that automatically updates payment-status torefundedwhen a cart order is returned (cart-order-returned) or cancelled (cart-order-cancelled) after being delivered (cart-order-delivered) and paid (i.e.,cart_payments.paid_onis not NULL), thus streamline refund processing.
log_new_service_paymentlog_removed_service_paymentlog_service_payments_updates
The above triggers are used for logging the data movement in service-payments's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_service_payment - automatically logs the new service payment records, which records the service_order_id and user_id (user-who ordered it). The log_removed_service_payment - logs removed service payment records, which captures the service_order_id and user_id (user-who ordered it), both log_new_service_payment and log_removed_service_payment triggers allow us to track the payment activity and monitor user transactions. The log_service_payments_update - tracks the updates to the service-payment records, which it include status_id (to assess the status transitions), paid_on (to assess the hikes and drops of payments made over a period) and mode_id (to assess the popular mode of payment).
restrict_price_mode_update_if_completed_for_service_payment: The trigger that restricts updating (uponUPDATEoperation) payment amount or mode after payment iscompleted, which allows the data to be maintained with accuracy in payment information.
The triggers 11.2.2, 11.2.3 (in
schema.sql) context is similar and are used for bothINSERTandUPDATEoperation
-
restrict_setting_payment_completed_on_insert_for_service_payment&restrict_setting_payment_completed_on_update_for_service_payment: These triggers prevents setting the payment-status (status_id) tocompleted, if payment hasn't been made (i.e.,paid_onisNULL) uponINSERTandUPDATEoperations, in which these triggers ensures the accurate payment status. -
restrict_confirmed_service_payment_deleted: The trigger that prevents deletingcompleted,refunded, orcancelledpayment's statuses for service orders, thus this allows the data integrity to be maintained and prevents accidental deletions.
log_new_product_ratinglog_removed_product_rating
The above triggers are used for logging the data movement in product-rating's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_product_rating - automatically logs the new product ratings, by recording user_id and product_model_id. The log_removed_product_rating - that automatically logs the removed product ratings, which records user_id and product_model_id. Therefore, these two triggers assist in tracking customer feedback and monitor product-model's-sales performance.
-
validate_user_eligibility_to_give_product_rating_on_insert: The trigger that ensures that only users who have ordered and received a product can leave a rating or review, thus preventing unauthorized feedback. -
validate_product_model_rating_correctly_updated: The trigger that prevents users from updating ratings or reviews for the wrong product or user, which ensures that feedback is accurate and trustworthy.
log_new_service_ratinglog_removed_service_rating
The above triggers are used for logging the data movement in service-rating's like when operations (INSERT, UPDATE, DELETE) takes place. The log_new_service_rating - automatically logs the new service ratings, by recording user_id and service_id. The log_removed_service_rating - that automatically logs the removed service ratings, which records user_id and service_id. Therefore, these two triggers assist in tracking customer feedback and monitor product-model's-sales performance.
-
validate_user_eligibility_to_give_service_rating_on_insert: The trigger ensures that only users who have ordered and received a service can give rating or review, hence this prevents unauthorized feedback. -
validate_service_rating_correctly_updated: The trigger that prevents users from updating ratings or reviews for the wrong service or user, which ensures that feedback is accurate and trustworthy.
There are several limitations when compared to the real-world application, and yet I tried my utmost to work on this project to make it aligns with the real-world applications.
The following are the limitations that are observed:
- The current schema assumes a business is owned by only one user account. The partnerships or multiple owners would require changes in the schema.
- The schema doesn't handle complex inventory concepts like batch tracking, stock reservations, or varying service slot durations within a single service.
- The
passwordsin the schema are stored asTEXT, but in a real-world application, they absolutely hashed before storing, whereas the triggers checking password changes history (logstable) works on stored values, which is very insecure if not hashed. - There is no built-in mechanism for communication between users (for example,
customerasking abusiness ownera question).
By,
Deva Manikanta Sala

