LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-2013, 09:21 PM   #1
jason_m
Member
 
Registered: Jun 2009
Posts: 33

Rep: Reputation: 12
Database model - unique names across several tables


I'm looking for any general suggestions on how to best represent some data. This can be either at the database level or the application level. I'm open to any suggestions here, my current approach seems rather cumbersome.

I'm building a user interface for a retirement system model. The model allows you to build up a benefit formula from various parts: constants, expressions, averages, lookup tables, to name a few. The user interface allows you to create and edit a constant, an expression, and so on. Every model part has a name, all other necessary data fields are specific to each part type (constants have a name and a value, expressions have a name and a expression string, averages have a couple other fields, etc.). One thing I would like enforced - either at the application level or the database level, is that part name be unique across all part types (i.e. it is not allowed to have a constant named Foo and an average named Foo).

I'm struggling to easily represent this at the database level. My current appraoch (reflected in the subject line) is to have a table of "names", and then a table for each part type. The part type tables have a foreign key relationship with the names table. Something like this (I'm using sqlite3 at the moment, but would like as generic a solution as possible):

(type_id would work like: 1 for constant, 2 for expression, ...)

Code:
create table tbl_names (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  type_id INTEGER NOT NULL
);

create table tbl_constant (
  id INTEGER PRIMARY KEY,
  name_id INTEGER NOT NULL UNIQUE,
  value REAL NOT NULL,
  FOREIGN KEY(name_id) REFERENCES tbl_names(id)
);

create table tbl_expression (
  id INTEGER PRIMARY KEY,
  name_id INTEGER NOT NULL UNIQUE,
  expr TEXT,
  FOREIGN KEY(name_id) REFERENCES tbl_names(id)
);
This sort of works. It isn't perfect. There is nothing stopping me from creating a name "Foo" with type_id=1 (we'll give it id=1), and then creating a constant and an expression record, both with name_id=1. The application knowns the name "Foo" belongs to a constant because of the type_id=1, but still, this seems like a downside of my current approach.

A bigger downside is that this is just a total pain to model in the application. I've been working on a desktop application using Qt, and can sort of hack it together using Qt's model/view framework, but it feels cumbersome. Now I'm playing with models in django and am revisiting the issue all over again. It looks like maybe an inline formset would work with my existing database structure, but I haven't played around enough to get it working. So, before I dive in much further, I wanted to stop and get some opinions.

How can I better/best model this data to:
- enforce as clean of data as possible at the database level
- make application-level modeling as simple as possible

I imagine there could be some trade-off between these two objectives, so maybe I can't max them both out. This is a personal project, so there aren't really and hard and fast requirements that I can or cannot change.

Thanks in advance.
 
Old 05-31-2013, 05:12 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If
Quote:
part name be unique across all part types
then that's what must be matched using FOREIGN KEY ie 'name' column, not id.
 
Old 05-31-2013, 07:57 AM   #3
jason_m
Member
 
Registered: Jun 2009
Posts: 33

Original Poster
Rep: Reputation: 12
What advantages does that offer?

Now I'm going to get into some additional details of how things are currently working (not to say they _have_ to work this way - that's why I came to the forum). Consider a model part that references another model part. Parts of type average do this. An average is stored currently stored in a table that looks something like:

Code:
create table tbl_average (
    id INTEGER PRIMARY KEY,
    name_id INTEGER UNIQUE NOT NULL,
    underlying_name_id INTEGER NOT NULL,
    avg_period INTEGER NOT NULL,
    FOREIGN KEY(name_id) REFERENCES names(id),
    FOREIGN KEY(underlying_name_id) REFERENCES names(id)
);
name_id associates the average part with a name. underlying_name_id associates it with another name in the model which acts as the thing to average.

If I were to instead make the foreign key relationship on name rather than name_id, then one downside is renaming things just got harder. If the underlying_name_id_ pointed to a part called "Foo", and then I decided to rename "Foo" to be "Bar", not only would I have to update the names table, but I would have to check every part-type specific table for references to "Foo" and update those as well.
 
Old 05-31-2013, 08:49 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This isn't a direct answer to your question, but might still solve your problem. I know just enough SQL and relational database technique to be dangerous, so when I want to build a database that is non-trivial (by my standards, at least), I use a web application framework to develop the database indirectly by using the dumbed-down methods that are provided through the API. I like Django for this, although there are numerous others. Writing code in Python that is simple to understand using the well documented API allows me to:
  • create the database tables easily
  • test and play with the database using a built-in web interface or with a general purpose database browser (I use postgresql & pgadmin)
  • modify/update the database structure until I've got it right
  • populate the database with initial data, if that's appropriate
Once I've got a functioning database, I can continue to use the web framework to complete the application, or I can build the application independently, but still retaining the database structure created by the framework. I find that the embodied expertise in the framework results in a database that is properly created, and includes all sorts of details that I wouldn't ordinarily know about, and that always works as expected.

--- rod.

Last edited by theNbomr; 05-31-2013 at 08:52 AM.
 
Old 05-31-2013, 10:59 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quite frankly, I think you would be better off implementing that sort of logic in a programming-language attached to the application. I suspect that you are unintentionally building-in more flexibility (hence, complexity) than the business objective actually requires.

A benefits-formula consists of some algorithm, which might be composed of sub-algorithms, and the inputs to the algorithm(s) may be constants or patient-related variables. I would model all of this, except for the variables, in modular code that the users cannot directly modify. (The algorithms for obtaining the necessary variable-values would also be in code.)

There are a number of issues to consider here: complexity, maintainability, and, in the case of changes over time, the auditability of past and current records.
 
  


Reply



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
django: get all unique values from model field stateless Programming 0 04-19-2013 02:44 AM
LXer: Startup Offers Unique Delivery Model For Open-Source Software LXer Syndicated Linux News 0 12-30-2005 02:31 AM
create unique user names sabapathys Linux - Software 1 05-12-2005 05:50 AM
create unique user names sabapathys Linux - Newbie 1 05-12-2005 04:09 AM
Unique class names (C++) ToothlessRebel Programming 6 12-25-2004 12:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration