So what's wroing with:
create table unique_names (id identity, name varchar not null unique)
You can even make the a column of type varchar the primary key. I did.
On Mon, Oct 22, 2012 at 7:18 PM, Brent Ellwein <brent....@gmail.com> wrote:
Hello,
I am trying to have a unique String column for my H2 database implementation. As this is not natively supported, I'm walking around in the weeds a little bit, and store the hash of the string in an indexed column in the table. For insert, I have a two-part check.
1) see if a row exists with the String value using an SQL select statement, if so return the generated ID for the already existing row.
2) insert the new row and then return the resulting generated ID.
This seems to work fine with a combination of a select statement and a followup insert statement. BUT what I would like to do is write a stored procedure which does it all at once and then returns the id value quickly. I was able to write this function. However, I cannot seem to cache the prepared statement in my function call, and thus generate the statement for each call. The end result is that the stored procedure executes more slowly than running several calls through a client with cached stored procedures.
Thus my question is, is there a way to cache a prepared statement from within a stored procedure? Is there a better approach to the problem which I have not yet thought of?
Thanks you for your input.
--Brent
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/P4Pa9RQ1EgMJ.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database+unsubscribe@googlegroups.com.
On Tue, Oct 23, 2012 at 12:15 AM, Rami Ojares <rami....@gmail.com> wrote:I am looking at the list of types supported by h2What is the difference between varchar and text. H2 specifically rejects using unique or primarykey for text fields.
http://www.h2database.com/html/datatypes.html
I don't see type text mentioned.
I remember that type from mysql though.
Anyway, if you don't know what type text is why do you want to use it?
Just use varchar.Consider using merge statement
> Also, this does not solve the problem of caching the preparedStatement in the function.
http://www.h2database.com/html/grammar.html#merge
I'd say yes, to both suggestions. varchar is a standard SQL type suppored by H2, and while H2 may support that type in some of its compatibility modes, it also may not
Just make you string colump a primary key, and use MERGE INTO ...
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
--
Vasile Rotaru
varchar is a nicer solution than text, thank you for that suggestion, however merge doesn't really work for my application. If the inset fails, then the merge will throw an exception rather than return the generated row ID.
What I need is to get the ID for the row back if it already exists. I think that what I need is a conditional insert which will always return the row ID. From what I have been able to read so far this is not a supported feature of H2 which lead me down the stored procedure road. But to make the procedure faster it would help to cache the prepared statement, rather than recompile them on each call.
The string example is just one case of a more complicated system of conditional insertion steps. Any other suggestions?
On Monday, October 22, 2012 3:40:24 PM UTC-6, Vasile Rotaru wrote:On Tue, Oct 23, 2012 at 12:15 AM, Rami Ojares <rami....@gmail.com> wrote:I am looking at the list of types supported by h2What is the difference between varchar and text. H2 specifically rejects using unique or primarykey for text fields.
http://www.h2database.com/html/datatypes.html
I don't see type text mentioned.
I remember that type from mysql though.
Anyway, if you don't know what type text is why do you want to use it?
Just use varchar.Consider using merge statement
> Also, this does not solve the problem of caching the preparedStatement in the function.
http://www.h2database.com/html/grammar.html#merge
I'd say yes, to both suggestions. varchar is a standard SQL type suppored by H2, and while H2 may support that type in some of its compatibility modes, it also may not
Just make you string colump a primary key, and use MERGE INTO ...
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
--
Vasile Rotaru
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/cCEgFiqzwKoJ.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
The string case is only one instance of this conditional insert pattern. Some other cases have more complex unique pairs of keys, where a generated ID is used to identify the row. I have not been able to get the merge to return anything, but rather throw exceptions with my test cases thus far. I use the JDBC get generated ID apis in other parts of the application, but it doesn't apply for the merge command as I get an exception instead of a result set. Maybe there is something that I'm missing, but I haven't yet been able to get that to work.
merge into File (filepath, filesize, datemodified) key (filepath) values (?,?,?)
--
Vasile Rotaru
--
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database...@googlegroups.com.
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.
Hi,
You would need a unique index on the column "email". For H2, you could use a combination of:
insert into employee(email) select ? from dual where not exists(select * from employee where email=?);
select id from employee where email=?
This could be written as a user defined function. This should work for other databases as well. For H2, you could use "merge into employee(email) key(email) values(?)" as well.
Regards,
Thomas
You received this message because you are subscribed to a topic in the Google Groups "H2 Database" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/h2-database/ejdytiTCvbA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to h2-database...@googlegroups.com.