no identity crisis
i slipped in a bit to the item
table in my nascent point-of-sale system that i introduced last time that i didn’t explain at all. it’s just a little thing, a column called id
that is an auto-incrementing integer. we need a way to uniquely identify items, and that’s the fallback method for this broken-down php and mysql coder.*
on the other hand, dealing with ringing up customers and putting together orders from distributors, my experience has been that it is good to have a short-hand identifier for products that is not totally opaque like a bare number. you can see that the developers of php point of sale came to the same conclusion by their inclusion of a item_number
field (which is not a number, but we won’t hold that against them). the point-of-sale system we are using currently has a unique identifier for items that they call the code
, and the underlying numeric identifiers in the database are never actually exposed in the interface.
the codes we use to identify products are borrowed almost entirely from the way that our primary distributor identifies products. each code has a two letter prefix that identifies the brand of the product, and then the rest of the identifier is structured differently depending on the brand. another of our distributors uses a fairly similar system with the three-letter prefix separated by a dash. depending on the brand and product, this means that looking up similar products can be straightforward if you just remember a part of the code. for example, i have it baked into my brain that all art alternatives studio canvases have a code starting with 'AA55', so doing searches or reports on just those items means i can just type in that prefix instead of having to navigate a more complicated category system. not all of the brands have codes that are structured that conveniently — products from 3M, for example, have a prefix of 'MT' but the rest of the code is based on a portion of the UPC, and a line like all of the command hooks & clips doesn’t sit within the same numeric range so there’s no one prefix that will come up with just those.
another interesting thing to consider is that an identification scheme based on the brand isn’t stable. not too long ago, chartpak acquired the higgins brand from sanford, which meant in the language of the codes that our distributor uses (and we use), the prefix on the higgins items changed from 'SA' to 'CH'. how we track those sort of changes is something we’ll have to consider later, but it does demonstrate that relying on this code as our primary identifier would be unwise.
but i think the real bottom line is that these identifiers are just a unique opaque identifier for the users of the point-of-sale system, so the system doesn’t need to impose any structure on them. in fact, i’m not sure if i can come up with a reason why they shouldn’t be optional, so i’ve left it open to an item not having a code
.
so here is the updated table with our newly-minted code
field:
CREATE TABLE item (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
code VARCHAR(255),
retail_price DECIMAL(9,2) NOT NULL,
discount_type ENUM('percentage','relative','fixed'),
discount DECIMAL(9,2),
PRIMARY KEY(id),
UNIQUE (code)
);
* so is an auto-incrementing integer really the best primary key to use? it may seem a little more grown-up to use something like a uuid, but while these identifiers may be intended to be hidden, as someone who will almost certainly be looking behind the curtain to run queries against these tables manually, relatively small integers are a whole lot easier to deal with than big hexadecimal ones.
Comments
i guess i feel that with how we price things, the exceptions to our every-day price are rare, so having all of the pricing in another table is just a step of normalization too far. and for us, the exceptions tend to be more rule-based than item-based, more about buying six of a class of items and getting a price break than buying six of any one item and getting a price break.
Add a comment
Sorry, comments on this post are closed.
Generally it's a good idea to break out pricing from the item table. Most of the systems I've worked with have a schema like:
-- the item in our system item(id, code, short_name, retail_price, is_active)
-- where we buy the item from, their code and our cost item_vendor(item_id, vendor_id, vendor_code, cost, is_active)
-- what we sell it for item_pricing(item_id, customer_class, min_qty, sale_price)
That way you can have quantity breaks for customers, and sell at different prices (e.g. wholesale).
Getting away from auto-incrementing integers is generally a good idea as it makes moving the data around a lot easier. The other decision is whether you want to use soft-deletes, which is usually a given.