No, the constraint name is completely unpredictable. If you want your names to be consistent, you can name them correctly by applying a predictable / repeatable name manually. I have no idea how you would do this in the code you have, but in T-SQL instead of:
CREATE TABLE dbo.foo(bar INT PRIMARY KEY);
CREATE TABLE dbo.blat(bar INT FOREIGN KEY REFERENCES dbo.foo(bar));
(The above end up with constraints having names like PK__foo__DE90ECFF6CF25EF6
and FK__blat__bar__1B1EE1BE
.)
You would say:
CREATE TABLE dbo.foo(bar INT, CONSTRAINT PK_foo PRIMARY KEY (bar));
CREATE TABLE dbo.blat(bar INT, CONSTRAINT fk_foobar FOREIGN KEY(bar)
REFERENCES dbo.foo(bar));
My question is this, is there a way to dynamically generate Object 3 through MySQL.
When I hear "generate," I tend to interpret that as "insert," but I think you're asking whether MySQL could dynamically select and return the appropriate related records to the application based on certain criteria.
If that's what you're asking, then it seems you have correctly surmised that one sensible way to implement that is indeed with stored procedures...
Also I've read that I shouldn't use stored procedures, and instead do everything with code
Nonsense, I say. Disregard that advice. Stored procedures, used properly, can be used to build an excellent interface between the database tables and the application.
One aspect in particular seems to suit your request:
MySQL supports a very useful extension that enables the use of regular SELECT statements (that is, without using cursors or local variables) inside a stored procedure. The result set of such a query is simply sent directly to the client. Multiple SELECT statements generate multiple result sets, so the client must use a MySQL client library that supports multiple result sets. -- http://dev.mysql.com/doc/refman/5.5/en/stored-routines-syntax.html
Of course, like any other tool, there are inevitably people who come up with ways of misusing them, but this doesn't reflect on the suitability of the tool when properly applied.
But a single call to the database from your application could retrieve the primary object and related data, with stored procedures.
I was thinking of creating a separate table top hold primary keys of related objects
That sounds like you're describing a junction table...
but I'm thinking there is probably a better way of doing that.
If the relationship among objects is many-to-many, then this is the appropriate way of expressing that relationship... but not when it isn't.
Best Answer
Since the tables use the MyISAM storage engine, I have two suggestions.
SUGGESTION #1 : Use Order of Insertion
The simplest way to do that would be arbitrarily insert the main object first for the given group.
Going forward, you would run queries like these:
Main object_id for every group
Main object for every group by name
Epilogue
To ensure the first rule,
1 object belongs to 1 and only 1 group
, add this indexHaving a unique index on those two columns along with
object_id
definitely prevents insertion of anobject_id
twice in a group. This unique would speed upGROUP BY
queries like the ones I mentioned. It would also speed up the collection of the objects in a group like the following:and
SUGGESTION #2 : Use two auto_increments within the same table
All queries in
SUGGESTION #1
still apply, but the design is a little different.Make the design the
object
table as follows:MyISAM would handle the incremental values of all
object_id
pergroup_object_id
. At present, only the MyISAM storage engine has this capability. I have discussed this before:Apr 21, 2012
: How can you have two auto-incremental columns in one table?Apr 30, 2013
: Issues converting MyISAM table to InnoDB (auto column issue)Give it a Try !!!
UPDATE 2013-08-28 02:05 EDT
Remember I said
EXAMPLE
Let's say you ran the following to create the tables
and load the tables with
You will have this data:
If you run my queries, note how you get the main object from each group
Note how the main inserted object for each group has the minimum object_id for that group.
When you apply the rule I first mentioned, simply look for the minimum
object_id
for any givengroup_object_id
from theobject
table.YOUR LAST EDIT
Adding a flag
If you already have data in the object table and want to assign a random object as the main object, you could add a flag called
main
asINT NOT NULL DEFAULT 0
to the object table. Assuming responsibility, you would have to check to see, upon insertion, if a group already has a main object. If so, you would update themain
flag of the previous main object to 0. Then, you could update another object'smain
to 1.Rearranging to change main object
If you try adhering to my rule, first insertion is main, here is what you can do so that you can choose which object becomes first in the group:
EXAMPLE
Let's say from my example, that you want take
OBJ_Z
inGROUP2
and make it the new main object. You would do this:If you run this code, here is the new output
Note that
OBJ_Z
is the main object because it is the first in the group. Simply query for the name of the object you want to be main in the first line and then run all the code. I have tried repeatedly, and it works.Give it a Try !!!