473,902 Members | 6,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is better - one field or eight - mysql bit testing

Hi Folk

I have to store up to eight boolean bits of information about an item
in my database.

e.g.

[ ] with restaurant
[ ] drive-through facility
[ ] yellow windows
[ ] wifi factilities
etc...

There are three ways of storing this information in my mysql database
A. add eight fields (tiny integer)
B. add one tiny integer and create a function in PHP that can translate
the number stored into a eight boolean values (the bits)
C. create a table which list
ItemID
Associated characteristics
In C, you will only list the characteristics that are true for the item
listed.

Option B is the most efficient in MySql, but would you recommend it
when creating a PHP website. The problem is that the user needs to
enter them with a nice webform, etc...

What do you reckon.

TIA

- Nicolaaas

May 14 '06
39 3250
FYI - The proper term for a table that solves a many-to-many
relationship is called an 'intersection tabel'

items(item_id, name);
items2chars(ite m_id, char_id); <-- intersection table
characteristics (char_id, name);

In any event, don't store the characteristics in a single field. Put
them in seperate fields, or create a characteristics table with an
intersection table.

If a characteristics is only a boolean value, and you don't foresee
adding/removing attributes, then using fields would work fine (though
it may make a couple of complicated queries a little tricky).
If characteristics have attributes themselves, then you need to use the
intersection method.
If characteristics you foresee characteristics beinga added/removed, it
would be better to use the intersection method.

May 14 '06 #11
Kenneth Downs wrote:
Bent Stigsen wrote:


The answer is D, implementing two tables.
One table with the characteristics , and one table containing two foreign
keys making the association between characteristics and the "items" (what
that might be).

Anybody working with databases must have a reasonable method for altering
table structures as a regular event and a reasonable way to synchronize
structures and the code that works with them. Not having this will cost,
and all solutions that seek to re-invent physical implementation produce
burdens worse than the disease.


I disagree. If you properly plan your installation, you will not need to change
your tables. Over the years I've designed hundreds of databases; most of them
have never been changed.

Having to alter a database layout either means you've had a significant change
in the database needs, or, more likely, you didn't design it properly in the
first place.
The solution you present may be correct in his case, but it verges on the
dreaded 'abstraction' of the E-A-V system, and if that is so it would be a
cure worse than the disease. Whether it is a valid cross-reference or an
instance of E-A-V would require knowing more about the system.

Not at all. In fact, it makes things much easier to handle in the long run.
When you have a multi-to-multi connections, as in this case, a third table is
almost always the way to go.

But I would repeat that any 'abstraction' made in an attempt to avoid table
structure changes is going to fail. It fails because you give up what the
server can do for you and end up spending your time reinventing an RDMBS
server.


I completely disagree. Again, many times I've abstracted things yet made good
use of the server's abilities.

Abstraction only means you're separating the database structure from the program
logic. This is generally considered a good thing, especially as you get into
more complicated projects..

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 15 '06 #12
Richard Levasseur wrote:
FYI - The proper term for a table that solves a many-to-many
relationship is called an 'intersection tabel'

Yes intersection tabel (sic) is one name for them, but describes a subset of
link tables. Link table is another, mutli-to-multi-link table is a third. The
term "link table" has been around since the 1970's. I first heard "intersecti on
table" in the late 1990's.
items(item_id, name);
items2chars(ite m_id, char_id); <-- intersection table
characteristics (char_id, name);

In any event, don't store the characteristics in a single field. Put
them in seperate fields, or create a characteristics table with an
intersection table.

Yes, that is the way to use a link table. You also need to set up foreign keys
on both columns in the link table.
If a characteristics is only a boolean value, and you don't foresee
adding/removing attributes, then using fields would work fine (though
it may make a couple of complicated queries a little tricky).
If characteristics have attributes themselves, then you need to use the
intersection method.
If characteristics you foresee characteristics beinga added/removed, it
would be better to use the intersection method.


--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 15 '06 #13
Kenneth Downs wrote:
Bent Stigsen wrote:

The answer is D, implementing two tables.
One table with the characteristics , and one table containing two foreign
keys making the association between characteristics and the "items" (what
that might be).
Anybody working with databases must have a reasonable method for altering
table structures as a regular event and a reasonable way to synchronize
structures and the code that works with them. Not having this will cost,
and all solutions that seek to re-invent physical implementation produce
burdens worse than the disease.

The solution you present may be correct in his case, but it verges on the
dreaded 'abstraction' of the E-A-V system, and if that is so it would be a
cure worse than the disease. Whether it is a valid cross-reference or an
instance of E-A-V would require knowing more about the system.


Hmmm, I don't really see it as such. Arguably a step in that direction, but
still good old relational tables to me, and very much a reasonable method
to anticipate changes in schema/data.

But I would repeat that any 'abstraction' made in an attempt to avoid
table
structure changes is going to fail. It fails because you give up what the
server can do for you and end up spending your time reinventing an RDMBS
server.


Perhaps I don't quite understand you here, but I don't think I am
reinventing anything. Using an intermediate table to represent a
many-to-many relationship is hopefully quite a common practice, and by no
means an abuse of any relational database, but rather something they are
extremely good at.
/Bent
May 15 '06 #14
Jerry Stuckle wrote:
Kenneth Downs wrote:
Bent Stigsen wrote:


The answer is D, implementing two tables.
One table with the characteristics , and one table containing two foreign
keys making the association between characteristics and the "items" (what
that might be).

Anybody working with databases must have a reasonable method for altering
table structures as a regular event and a reasonable way to synchronize
structures and the code that works with them. Not having this will cost,
and all solutions that seek to re-invent physical implementation produce
burdens worse than the disease.


I disagree. If you properly plan your installation, you will not need to
change
your tables. Over the years I've designed hundreds of databases; most of
them have never been changed.


Sorry to hear that.

Having to alter a database layout either means you've had a significant
change in the database needs, or, more likely, you didn't design it
properly in the first place.


Or your customer loved it, their business is growing, and they've got more
stuff for you to do.

Cheers,

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec )ure(Dat)a(.com )
May 15 '06 #15
Carved in mystic runes upon the very living rock, the last words of
Colin McKinnon of comp.lang.php make plain:
Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
windandwaves of comp.lang.php make plain:
I have to store up to eight boolean bits of information about an
item in my database.

There are three ways of storing this information in my mysql
database A. add eight fields (tiny integer)
B. add one tiny integer and create a function in PHP that can
translate


I'd go with B, just my personal preference.


Its a BAD idea. SQL has no visibility of it. It's not normalized and
it doesn't scale. In fairness certain types of search can be done very
fast, but they're unlikely to be particularly common.


Yeah, you're right. That'll teach me to post quickie answers without
thinking about it. I used such an approach in an application once, and
that was what I was thinking about, but the thing I used it for, there
was never more than 20 - 30 items and never any need to query it.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 15 '06 #16
Bent Stigsen wrote:

Hmmm, I don't really see it as such. Arguably a step in that direction,
but still good old relational tables to me, and very much a reasonable
method to anticipate changes in schema/data.

But I would repeat that any 'abstraction' made in an attempt to avoid
table
structure changes is going to fail. It fails because you give up what
the server can do for you and end up spending your time reinventing an
RDMBS server.


Perhaps I don't quite understand you here, but I don't think I am
reinventing anything. Using an intermediate table to represent a
many-to-many relationship is hopefully quite a common practice, and by no
means an abuse of any relational database, but rather something they are
extremely good at.


As I said, in this case it may be valid.

It would be a move toward using E-A-V if and only if it were done
specifically to avoid structure changes. That would flag a mindset that
would tend towards making the data more and more difficult to work with,
trading the effort of regular development and use for the effort of
modifying the table structures.

I can only argue here from experience. A flag is a property of the entity
being recorded in the table. That means by default it is a column in a
table, along with other flags. This is the simplest possible arrangement
and anything else carries a higher ongoing cost. The default position is
to have it a column in the table. In most cases, if the customer wants
another flag, that's another column. If a developer is trying to avoid
structure changes because of cost, then that developer needs to seriously
look at their development tools (or coding habits), anything which drives
you away from the natural use of tables is not your friend.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec )ure(Dat)a(.com )
May 15 '06 #17
Kenneth Downs wrote:
Jerry Stuckle wrote:

Kenneth Downs wrote:
Bent Stigsen wrote:

The answer is D, implementing two tables.
One table with the characteristics , and one table containing two foreign
keys making the association between characteristics and the "items" (what
that might be).
Anybody working with databases must have a reasonable method for altering
table structures as a regular event and a reasonable way to synchronize
structures and the code that works with them. Not having this will cost,
and all solutions that seek to re-invent physical implementation produce
burdens worse than the disease.

I disagree. If you properly plan your installation, you will not need to
change
your tables. Over the years I've designed hundreds of databases; most of
them have never been changed.

Sorry to hear that.


My customers aren't. It's because the databases were designed properly in the
first place.
Having to alter a database layout either means you've had a significant
change in the database needs, or, more likely, you didn't design it
properly in the first place.

Or your customer loved it, their business is growing, and they've got more
stuff for you to do.


And if it had been designed properly in the first place, you would have planned
for the growth.

You can't plan for all possible changes. But a good designer can plan ahead for
most of the potential changes and incorporate them in the database early.

A good DBA plans ahead. Those who say you should plan on changing your database
either don't plan ahead or, more likely, are just hackers throwing together code
to fix the current problem.

For instance - try a database with over 75 tables, all interrelated and linked
with foreign keys. Over 500K LOC working with this database. Changing a table,
even to add a column, is NOT necessarily a minor change. Lots for code to check
through.

And adding a new table can really cause problems. This is one successful
project I managed several years ago.

Even in my web sites I need to change virtually none of my database structures
after they've gone live - because I've planned ahead.
Cheers,

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 15 '06 #18
Jerry Stuckle wrote:
Kenneth Downs wrote:
Jerry Stuckle wrote:

Kenneth Downs wrote:
[snip]Anybody working with databases must have a reasonable method for
altering table structures as a regular event and a reasonable way to
synchroni ze
structure s and the code that works with them. Not having this will
cost, and all solutions that seek to re-invent physical implementation
produce burdens worse than the disease.
I disagree. If you properly plan your installation, you will not need to
change
your tables. Over the years I've designed hundreds of databases; most of
them have never been changed.

Sorry to hear that.


My customers aren't. It's because the databases were designed properly in
the first place.


Sadly not all customers appreciate that. All some care about is the <beep>
dessert topping. I find that a trifle annoying. I utterly refuse to play
along with that. Oh what fun I would have, being a ruthless dictator. chop.
chop.
/Bent
May 15 '06 #19
Bent Stigsen wrote:
Jerry Stuckle wrote:

My customers aren't. It's because the databases were designed properly in
the first place.

Sadly not all customers appreciate that. All some care about is the <beep>
dessert topping. I find that a trifle annoying. I utterly refuse to play
along with that. Oh what fun I would have, being a ruthless dictator. chop.
chop.
/Bent


Bent,

Unfortunately, that's true. Some have to go through a messy (and expensive)
change cycle to become believers.

At the same time, I've found most databases can be designed with change in mind
very quickly. Just properly normalizing the database does wonders. And in a
case like the one which started this discussion, thinking ahead - yes, now we
have eight options. But would we ever want to add more? If so, a link table is
the way to go.

Sometimes it's not obvious. But understanding the data and how it will be used
is critical to proper database design.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 15 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14950
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
13
3544
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to make a query that selects from a table as desribed below .. I have a table (Volunteer) that has a member field (memnumber) and a number of fields that are headed in various categories and are yes/no formated
11
4446
by: pmarisole | last post by:
I am trying to use the vbscript "split" function on a multi-select field. I am trying to do a mass update of several records at a time. I am getting an error and I'm not sure what to do. Here is the code if someone could help... strID = split(request.form("proj"), ", ") projstat = split(request.form("rojstat"),",") impr = split(request.form("impr"),",") idate =...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11279
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10870
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.