help creating schema sql for hsqldb -


i quite new database programming , trying create java program access hsqldb(version2.2.5) using hibernate.i have 3 classes in program mapped 3 tables given below.

i wanted create one-to-one mapping between saleorder , bill. also, create schema db, tried sql create statements. 2 tables saleorder , bill have each other's id fk.to avoid dependency error between tables during create,i used alter table add constraint statements

now,i want use drop table statements @ beginning of script.i used following

alter table saleorder drop constraint fk_so_bill; alter table saleorder drop constraint fk_so_buyer; alter table bill drop constraint fk_bill_so; alter table bill drop constraint fk_bill_buyer;  drop table buyer if exists; drop table saleorder if exists; drop table bill if exists; 

this however, causes problem when run first time(since altered tables don't exist.).i not find 'if exists' clause alter table in hsqldb ..so solution?should run create table scripts alone first time,and add alter table,drop table statements shown above thereafter?that doesn't sound clean way.

will grateful suggestions,

sincerely,

jim

the main schema script

create table buyer(    buyer_id bigint not null primary key identity,    name varchar(100) );  create table saleorder(    saleorder_id bigint not null primary key identity,    buyer_id bigint not null,    bill_id bigint,  );  create table bill(   bill_id bigint not null primary key identity,   buyer_id bigint not null,   saleorder_id bigint not null,      ); alter table saleorder add constraint fk_so_bill foreign key(bill_id) references bill(bill_id); alter table saleorder add constraint fk_so_buyer foreign key(buyer_id) references buyer(buyer_id); alter table bill add constraint fk_bill_buyer foreign key(buyer_id) references buyer(buyer_id); alter table bill add constraint fk_bill_so foreign key(saleorder_id) references saleorder(saleorder_id); 

also,i using ant target create schema

<target name="createschema" description="execute schema">             <sql                     driver="${db.driver}"                     url="${db.url}"                     userid="${db.username}"                     password="${db.password}"                     print="yes"                     src="${dir.schema}/${file.schema}"                     caching="false"                     showheaders="true"                     >                 <classpath>                     <path location="${dir.lib}/hsqldb.jar"/>                 </classpath>             </sql>         </target> 

you don't need alter table statements before dropping tables.

use cascade keyword force drop foreign key constraints.

drop table buyer if exists cascade drop table saleorder if exists cascade drop table bill if exists cascade 

can use

drop schema public cascade 

which drops existing objects in schema.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -