LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   strange things I would like to do with mysql (http://www.linuxquestions.org/questions/programming-9/strange-things-i-would-like-to-do-with-mysql-645881/)

tanoatlq 05-30-2008 01:09 PM

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

TB0ne 05-30-2008 02:25 PM

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.

tanoatlq 05-30-2008 02:35 PM

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)

forrestt 05-30-2008 02:43 PM

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

tanoatlq 05-30-2008 05:01 PM

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.

AdaHacker 05-30-2008 08:14 PM

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.

tanoatlq 05-31-2008 02:45 AM

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. :-)

chrism01 05-31-2008 07:51 PM

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...
;)

tanoatlq 06-01-2008 02:25 AM

The config for db are stored in the php file and fixed.
The option can be changed at run-time.


All times are GMT -5. The time now is 10:56 AM.