db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bernt M. Johnsen" <Bernt.John...@Sun.COM>
Subject Re: "create table if not exists" ?
Date Tue, 17 Mar 2009 14:02:45 GMT
E.g. like this (I hope the SQLState is correct):

try {
    stmt.executeUpdate("create table t (i integer)");
} catch (SQLException e) {
    if (!e.getSQLState().equals("X0Y32") {
        throw e;
    }
}


Alternatively, use the JDBC MetaData interface to check if the table
is there. Note that the MySQL "IF EXISTS" is not standard SQL, and as
such not portable (but of course, a bit convenient).




>>>>>>>>>>>> George H wrote (2009-03-17 13:37:51):
> I don't think it supports it according to
> http://www.ibm.com/developerworks/library/os-ad-trifecta4/index.html
> You may have to run a drop table line before every create table.
> Either that or in your java code to catch an SQL exception and then
> run the create table sql.
> --
> George H
> george.dma@gmail.com
> 
> 
> 
> On Tue, Mar 17, 2009 at 1:25 PM, bigblop <fedevaps@yahoo.dk> wrote:
> >
> > I have this line i a derby script:
> >
> > create table derbyDB(num int, addr varchar(40));
> >
> > But the second time I run the script I get:
> >
> > Table/View 'DERBYDB' already exists in Schema 'APP'. [SQL State=X0Y32, DB
> > Errorcode=20000]
> >
> > In mySQL I could use IF NOT EXISTS like:
> >
> >
> > create table if not exists derbyDB(num int, addr varchar(40));
> >
> > But I get an error when doing that with Derby. Is there something equivalent
> > in derby to IF NOT EXISTS ?
> > --
> > View this message in context: http://www.nabble.com/%22create-table-if-not-exists%22---tp22558480p22558480.html
> > Sent from the Apache Derby Users mailing list archive at Nabble.com.
> >
> >

-- 
Bernt Marius Johnsen, Staff Engineer
Database Technology Group, Sun Microsystems, Trondheim, Norway

Mime
View raw message