ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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..
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.
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:
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:
INSERT INTO option VALUES ('text_color', 'FFFFFF');
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.
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.
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.
CREATE TABLE options (row_check int UNIQUE NOT NULL, ...);
CREATE TRIGGER only_one_row_update BEFORE UPDATE ON options
FOR EACH ROW
IF NEW.id <> 1 THEN
SET NEW.id = 1;
CREATE TRIGGER only_one_row_insert BEFORE INSERT ON options
FOR EACH ROW
IF NEW.id <> 1 THEN
SET NEW.id = 1;
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. :-)
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...