I have entered an object to a table in a DataBase. The recognition of the object is by an ID which is being put using identity specification.
right away, when i enter my object i want to take it's ID from the table,
how can i take it's ID if I recognize each object by it's ID (there might be same objects except for their IDs).
Is there a way to take the ID by for example the last entered to the table?
Thanks in advanced
INSERT
INTO mytable (mycol1, mycol2)
VALUES ('val1', 'val2');
SELECT LAST_INSERT_ID();
Related
I am attempting to perform an insert operation inside a select. This is for chaining multiple inserts together that are dependent on each other. As such it looks something like this.
INSERT (Parent)
--SELECT (Return the Child's uuid created and assigned by TRIGGER)
---INSERT (Child)
Every record in the database has a uuid assigned upon insert through a trigger. I have a table for each type of entity I am capturing (e.g. person,address,phone,email).
Solution 1: I can insert a dependent entity such as the phone first, select it's uuid, then include that in the parent entity. In this case "person".
Solution 2: I can create the parent entity, then the dependent, then update the parent after acquiring the dependents uuid.
Solution 3: What I want to do is create the dependents at the same time I am creating the parent, and return their uuid to the parent for the parent record insert. This relies on order of operations to ensure everything is happening in appropriate order.
Because of the issues with mysql array support, I'm fudging a parent/child uuid_array table. As such there are additional operations involved, but the given example is a lot simpler.
Any idea on how to pull this off? Is there a better way to return the uuid?
INSERT INTO person (Name_First,Name_Middle,Name_Last,Phone_UUID_Array)
VALUES (
'John',
'Diddly',
'Doe',
SELECT #last_uuid
INSERT INTO phone (Phone_Number,Phone_Type)
VALUES (
'1-555-555-5555',
(SELECT UUID FROM VIEW_TYPE_CATEGORY WHERE Reference_Type='Personal cell' AND Category='phone')
)
);
Examples of triggers I've created for every table
CREATE TRIGGER uuid_person BEFORE INSERT ON person FOR EACH ROW SET NEW.UUID = UUID();
CREATE TRIGGER last_uuid_person AFTER INSERT ON person FOR EACH ROW SET #last_uuid = NEW.UUID;
CREATE TRIGGER uuid_phone BEFORE INSERT ON phone FOR EACH ROW SET NEW.UUID = UUID();
CREATE TRIGGER last_uuid_phone AFTER INSERT ON phone FOR EACH ROW SET #last_uuid = NEW.UUID;
No, you cannot execute an INSERT "inside" a SELECT, nor can you execute an INSERT inside another INSERT.
You can play around with triggers, but IMHO that's more trouble than it's worth.
I would recommend not doing this with a trigger, but instead generate the uuid and save it in a session variable. Then you can use it in as many subsequent statements as you want.
SELECT UUID() INTO #phone_uuid;
INSERT INTO phone (Uuid,Phone_Number,Phone_Type) VALUES (#phone_uuid, ...);
INSERT INTO person (Name_First,Name_Middle,Name_Last,Phone_UUID_Array)
VALUES ('John', 'Diddly', 'Doe', #phone_uuid);
These session variables are scoped to the session, so you don't have to worry about concurrent clients doing their own work overwriting yours, even if each session is using the same variable names.
Also, I fear from your description that Phone_UUID_Array is a comma-separated list of UUID's. You are probably not going to be happy with that design, for a number of other reasons.
You've got the reference in the wrong direction. Person shouldn't try to have a reference to all its dependent phone numbers. Go the other way -- each row in phone should have a reference to its parent person. That way you don't need to have a comma-separated list, and you can insert them in a more sensible order -- parent first, then one or more phone rows that each references the same parent. You don't need to update parent after inserting the phone.
If you look at facebook's graph API, it seems as though all objects share the same ID space, and all Ids are unique even if they are in different tables.
Is there a feature in MySQL that handles this? (if not, high level idea of how to implement?)
You may want to check out UUID(). It returns a globally unique ID so your IDs will never clash.
To convert it to integer format, you can
UNHEX(REPLACE(UUID(),'-',''))
for storing in a BINARY(16) column.
(Source for converting to integer: Nicholas Sherlock's comment at MySQL reference)
If you have a single master database server, you can create a table called Object that has an integer auto-incrementing primary key and an object type column. Every time you create an object, you insert a row into this Object table first, get the id, then use that id to insert a row into whatever table will hold the object information. So to create an Event:
INSERT INTO Object (object_type) VALUES ('Event')
Get the last insert id, let's say it's 12345.
INSERT INTO Event (id, name, location) VALUES (12345, 'Cookout', 'My back yard')
OR use a single sequence to drive the ID values.
I have a property definition table and second one that holds the actual property values:
table propdef: id, name, description
table props: id, propdefid, userid, value
This way i can dynamically create properties for my users. When I want to update a property for a user I have to check the props table if a propdefid/userid row exists and then either use update or create on this.
Right now I am first querying the db and then deciding on what to do in my java code. Is there a way to do that in the ibatis sqlmap - without extra logic in my java code?
I am using mysql as db.
before you go any farther, you need to stop and read this article: http://tonyandrews.blogspot.com/2004/10/otlt-and-eav-two-big-design-mistakes.html
if you still decide that EAV is the way to go, there is still hope. i can't advise at the ibatis/java level, but i can tell you to look at INSERT ... ON DUPLICATE KEY UPDATE. this change your two statements in to one.
I'm sure this is either totally impossible or really easy:
If I'm creating a table and I want one of the columns to have limited options, it seems that I use either the ENUM or SET value type. But I have to define the possible values at that moment. What if I have another table which has two columns, a primary key column and a data column, and I want the ENUM for my new table to be set to the primary key of the already existing column?
I'm sure I can just write in the values long-hand, but ideally what I need is for new values to be entered into the list table and for the table with the enum column to just accept that the value choices will include anything new added to that list table.
Is this possible without needing to manipulate the structure of the new table each time something is added to the list?
i think this link help :
http://dev.mysql.com/doc/refman/5.0/en/enum.html
have a discussion of it
in the user comments
start :
"In MySQL 5.0, you can convert an enum's values into a dynamically-defined table of values, which then provides effectively a language-neutral method to handle this kind of conversion (rather than relying on PHP, Tcl, C, C++, Java, etc. specific code).
"
he do it with stored PROCEDURE
The easiest way is to use a regular column without contraints. If you're interested in all the current values, use DISTINCT to query them:
select distinct YourColumn from YourTable
That way, you don't have any maintenance and can store whatever you like in the table.
The foreign key table you mention is also a good option. The foreign key will limit the original column. Before you do the actual insert, you run a query to expand the "enum" table:
insert into EnumTable (name)
select 'NewEnumValue'
where not exists (select * from EnumTable where name = 'NewEnumValue')
Not sure what exactly you're trying to achieve btw; limit the column, but automatically expand the choices when someone breaks the limit?
Are they assigned at SubmitChanges? or when a new object is created? If the latter, I would imagine there would be collisons?
If the id field is an autogenerated (identity/guid) field, the id is assigned when the record is inserted into the database. LINQToSQL does a select after insert to get the assigned value and updates it in the object. There are no collisions using identity columns as long as you don't turn on allow identity insert. If the id is not autogenerated, then you will be responsible for creating the id and ensuring that there aren't collisions.