Skip to content

Instantly share code, notes, and snippets.

@WatheqAlshowaiter
Created April 12, 2023 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WatheqAlshowaiter/615f99fbbbd5755e2c03397ef257af41 to your computer and use it in GitHub Desktop.
Save WatheqAlshowaiter/615f99fbbbd5755e2c03397ef257af41 to your computer and use it in GitHub Desktop.

Clauad+

Question

an SQL PC builder schema like pcpartpicker.com to ensure compatibility for each component. If the component is selected, other incompatible components don't appear (or we can toggle that).

the components are mother board, CPU, etc

convert above requirements to SQL

Answer

Here is a SQL schema for a PC builder app like pcpartpicker.com:

CREATE TABLE components (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  type VARCHAR(255) NOT NULL  -- motherboard, CPU, etc.
);

CREATE TABLE compatibilities (
  component1_id INT,
  component2_id INT,
  FOREIGN KEY (component1_id) REFERENCES components(id),
  FOREIGN KEY (component2_id) REFERENCES components(id)
);

CREATE TABLE builds (
  id INT PRIMARY KEY,
  name VARCHAR(255)
);

CREATE TABLE build_components (
  build_id INT,
  component_id INT,
  FOREIGN KEY (build_id) REFERENCES builds(id),
  FOREIGN KEY (component_id) REFERENCES components(id)
);

-- Query to get compatible components
SELECT * FROM components
WHERE type = 'CPU'  -- get all CPUs
AND id IN ( -- that are compatible with...
  SELECT component2_id
  FROM compatibilities
  WHERE component1_id = {motherboard_id}  -- the selected motherboard
)

-- When a component is selected for a build, filter out incompatible components
INSERT INTO build_components (build_id, component_id)
VALUES ({build_id}, {selected_component_id});

UPDATE components
SET disabled = 1  -- disable incompatible components
WHERE type = 'CPU'  -- looking at CPUs
AND id NOT IN ( -- that are not compatible with
  SELECT component2_id
  FROM compatibilities
  WHERE component1_id = {motherboard_id} -- the selected motherboard
)
AND id NOT IN ( -- and are not already in the build
  SELECT component_id
  FROM build_components
  WHERE build_id = {build_id}
);

This schema defines:

  • A components table to store all PC components
  • A compatibilities table to store compatible component pairs
  • A builds table to store saved PC builds
  • A build_components table to track the components in each build

It then shows some sample queries to:

  • Get compatible components for a selected component
  • Disable incompatible components when a component is selected for a build

This achieves the desired functionality of filtering components based on compatibility and selections. Let me know if you have any other questions!

Question

can I add more than 2 components and ensure that every component is compatible with all previous componemts that are selected before

Answer

Good question! To ensure every newly selected component is compatible with all previously selected components in a build, you can do the following:

When a component is first selected for a build, disable all incompatible components as before:

INSERT INTO build_components (build_id, component_id)
VALUES ({build_id}, {selected_component_id});

UPDATE components
SET disabled = 1
WHERE id NOT IN (
  SELECT component2_id
  FROM compatibilities
  WHERE component1_id = {selected_component_id}
) AND id != {selected_component_id};

    For each subsequent component selection, disable it if it's incompatible with any previously selected component:

INSERT INTO build_components (build_id, component_id)
VALUES ({build_id}, {selected_component_id});

UPDATE components
SET disabled = 1
WHERE id NOT IN (
  SELECT component2_id
  FROM compatibilities
  WHERE component1_id IN (
    SELECT component_id
    FROM build_components
    WHERE build_id = {build_id}
  )
) AND id != {selected_component_id};

This queries the build_components table to get the IDs of all previously selected components for the build. It then checks if the newly selected component is compatible with any of those, and disables it if not.

So in summary, the logic is:

  • When first component is selected, disable incompatible components
  • For each subsequent component, disable if incompatible with any previously selected component
  • This will ensure each new selection is compatible with the full set of currently selected components in the build. Let me know if you have any other questions!

GPT4

Question

an SQL PC builder schema like pcpartpicker.com to ensure compatibility for each component. If the component is selected, other incompatible components don't appear (or we can toggle that).

the components are the motherboard, CPU, CPU Cooler, Memory, storage, Video Card, Case, Power Supply, etc

I want to add more than 2 components and ensure that every component is compatible with all previous components that are selected before

convert the above requirements to SQL then give me an SQL example of adding 3 components with ensure that are compatible.

Answer

To create a PC builder schema similar to pcpartpicker.com, you'll first need to design the database schema with the necessary tables and relationships to represent the components and their compatibility. Then, you can write SQL queries to retrieve, filter, and ensure compatibility between the components.

Here's a possible schema design:

  • components: Stores general information about each component.
  • component_types: Represents the different types of components (e.g., motherboard, CPU, etc.).
  • compatibility_rules: Defines compatibility rules between component types.
  • compatibility: Lists specific compatible pairs of components.
-- Tables for components and their types
CREATE TABLE component_types (
    id INT PRIMARY KEY,
    type_name VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE components (
    id INT PRIMARY KEY,
    component_type_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    details TEXT,
    FOREIGN KEY (component_type_id) REFERENCES component_types(id)
);

-- Tables for compatibility rules and specific compatibility
CREATE TABLE compatibility_rules (
    id INT PRIMARY KEY,
    component_type1_id INT NOT NULL,
    component_type2_id INT NOT NULL,
    description TEXT,
    FOREIGN KEY (component_type1_id) REFERENCES component_types(id),
    FOREIGN KEY (component_type2_id) REFERENCES component_types(id)
);

CREATE TABLE compatibility (
    id INT PRIMARY KEY,
    compatibility_rule_id INT NOT NULL,
    component1_id INT NOT NULL,
    component2_id INT NOT NULL,
    FOREIGN KEY (compatibility_rule_id) REFERENCES compatibility_rules(id),
    FOREIGN KEY (component1_id) REFERENCES components(id),
    FOREIGN KEY (component2_id) REFERENCES components(id)
);

Now, let's consider an example of adding three components (a CPU, a motherboard, and a memory module) and ensuring compatibility between them.

Let's assume you have the following data in your tables:

  • component_types:
    • Motherboard
    • CPU
    • Memory
  • components:
    • Motherboard A (Type 1)
    • Motherboard B (Type 1)
    • CPU X (Type 2)
    • CPU Y (Type 2)
    • Memory M (Type 3)
    • Memory N (Type 3)
  • compatibility_rules:
    • CPU and Motherboard
    • Memory and Motherboard
  • compatibility:
    • Rule 1: Motherboard A and CPU X
    • Rule 1: Motherboard B and CPU Y
    • Rule 2: Motherboard A and Memory M
    • Rule 2: Motherboard B and Memory N

Now, let's say you want to find a compatible set of components including Motherboard A (ID 1), CPU X (ID 3), and Memory M (ID 5). You can write an SQL query to ensure compatibility:

SELECT c1.id AS motherboard_id, c2.id AS cpu_id, c3.id AS memory_id
FROM components AS c1
JOIN compatibility AS comp1 ON c1.id = comp1.component1_id
JOIN components AS c2 ON comp1.component2_id = c2.id
JOIN compatibility AS comp2 ON c1.id = comp2.component1_id
JOIN components AS c3 ON comp2.component2_id = c3.id
WHERE c1.id = 1 AND c2.id = 3 AND c3.id = 5
AND comp1.compatibility_rule_id = 1
AND comp2.compatibility_rule_id = 2;

This query will return a row containing the IDs of the selected components if they are compatible. If no row is returned, it means the components are not compatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment