LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-30-2008, 02:09 PM   #1
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Rep: Reputation: 30
strange things I would like to do with mysql


Hello,
I have a piece of code that should work with a mysql database and,
needing a configuration file for this app, I would like to test if
I could use mysql to store my options. I would like to setup a
table named options with all the fields that interest me.
So the question : is possible to force the behavior of a table of the database to have only 1 record?
I know this question could be terribly idiot but I am curious..
Thanks,
tano
 
Old 05-30-2008, 03:25 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,958

Rep: Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672
Either grant the user read-only access to that table (ensuring that the 1 record in there won't get added to), or don't add any other records. If you want to just read options from a table, don't put any functions in your code that write to that table.

There may be some other way to do it, but "not writing more to it" seems the simplest.
 
Old 05-30-2008, 03:35 PM   #3
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
Yes, it is obvious. But I would like to know if exists something that prevent the queries
to add records (exists a MAX_ROWS option in create table but seems that could be
ignorated)
 
Old 05-30-2008, 03:43 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You aren't really doing it the best way, but it should work. A better way to do it would be to have your option table and have parameter field and a value field:

Code:
+-----------+
| option    |
+-----------+
| parameter |
| value     |
+-----------+
Now you can add options at a later date without having to alter your table. Now, suppose that you have an option for background color that you are calling "bg_color" and you want to know what it is set to. You would run:

Code:
SELECT value FROM option WHERE parameter='bg_color';
If later on you decide it would be cool to also be able to set the text color you could do something like:

Code:
INSERT INTO option VALUES ('text_color', 'FFFFFF');
HTH

Forrest
 
Old 05-30-2008, 06:01 PM   #5
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
I know. But I know also that I had not to add more properties. It is a very small
project, not expandible, so I would like to do in *this way*, i.e. only one row.
The scheme you presented is flexible, it is the same adopted for radius attributes
on the sql database I am working on, but I need only 1 option, and I would not
store it in a file or create a structure in the db that I do not use for anything else.
 
Old 05-30-2008, 09:14 PM   #6
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
For starters, let me echo forrestt's sentiment that this isn't really a good idea. Aside from lacking flexibility, it's fighting the nature of SQL. SQL is set-oriented, and in general it just doesn't make much sense to restrict a set to only being able to have one member. In such cases, you're looking for a singleton, not a set, and so a relational table isn't the way to model it. So I certainly do not recommend this.

That said, it's still possible. In standard SQL, you could do this with a column that has both a UNIQUE constraint and a CHECK constraint, e.g.
Code:
CREATE TABLE options (row_check int UNIQUE NOT NULL CHECK (row_check = 1), ...);
That's it - simple and elegant. The bad news is, you can't use that solution because MySQL doesn't support CHECK constraints.

However, on the up side, you can emulate a CHECK constraint with triggers. In this case, you would need both an update and insert trigger.
Code:
CREATE TABLE options (row_check int UNIQUE NOT NULL, ...);

delimiter //

CREATE TRIGGER only_one_row_update BEFORE UPDATE ON options 
FOR EACH ROW 
   BEGIN 
      IF NEW.id <> 1 THEN 
         SET NEW.id = 1; 
      ENDIF; 
   END;//

CREATE TRIGGER only_one_row_insert BEFORE INSERT ON options 
FOR EACH ROW 
   BEGIN 
      IF NEW.id <> 1 THEN 
         SET NEW.id = 1; 
      ENDIF; 
   END;//

delimiter ;
That ought to do the trick.
 
Old 05-31-2008, 03:45 AM   #7
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
Well, the prologue is that I have to use MySQL as a relational database. Moreover, I need
a configuration file, *SO* I would like to insert data in a table in this way. I am not
one of the mad person that the night howl at the moon and the day use databases in trick and
perverse way. :-)
 
Old 05-31-2008, 08:51 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
You might as well use the same cfg file you use to tell the prog how to login to the DB... unless its always supplied on the cmd line.
A DB table for 1 row is definitely overkill, and as pointed out, a little messy to setup. IOW, you're fighting the basic design of a DB ie its supposed to have multiple rows...
 
Old 06-01-2008, 03:25 AM   #9
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
The config for db are stored in the php file and fixed.
The option can be changed at run-time.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange things with Pidgin. MheAd Linux - Software 4 10-28-2007 05:22 PM
??? Very strange things appening to my Debian ??? win2suse Linux - Software 3 04-12-2007 07:05 PM
Strange things (for me) on apache log stormrider_may Linux - Security 4 02-20-2006 06:23 AM
UT2004 and INI. strange things jerome23 Linux - Games 0 05-23-2005 05:09 AM
strange things to the monitor porous Linux - General 4 10-29-2003 09:05 AM


All times are GMT -5. The time now is 07:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration