Attempt to define a second primary key with IF NOT EXISTS - h2

Why is this statement failing despite the "IF NOT EXISTS" clause?
org.h2.jdbc.JdbcSQLSyntaxErrorException: Attempt to define a second primary key; SQL statement:
ALTER TABLE QRTZ_CALENDARS
ADD CONSTRAINT IF NOT EXISTS PK_QRTZ_CALENDARS PRIMARY KEY ( SCHED_NAME, CALENDAR_NAME ) [90017-212]
H2 is run in memory mode jdbc:h2:mem:quartz;INIT=RUNSCRIPT FROM 'classpath:tables_h2.sql'
CREATE TABLE IF NOT EXISTS QRTZ_CALENDARS (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR (200) NOT NULL ,
CALENDAR IMAGE NOT NULL
);
If I move the PK definition into table definition the error goes away.
The script is
CREATE TABLE IF NOT EXISTS QRTZ_CALENDARS (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR (200) NOT NULL ,
CALENDAR IMAGE NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_CRON_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
CRON_EXPRESSION VARCHAR (120) NOT NULL ,
TIME_ZONE_ID VARCHAR (80)
);
CREATE TABLE IF NOT EXISTS QRTZ_FIRED_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
ENTRY_ID VARCHAR (95) NOT NULL ,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
INSTANCE_NAME VARCHAR (200) NOT NULL ,
FIRED_TIME BIGINT NOT NULL ,
SCHED_TIME BIGINT NOT NULL ,
PRIORITY INTEGER NOT NULL ,
STATE VARCHAR (16) NOT NULL,
JOB_NAME VARCHAR (200) NULL ,
JOB_GROUP VARCHAR (200) NULL ,
IS_NONCONCURRENT BOOLEAN NULL ,
REQUESTS_RECOVERY BOOLEAN NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_PAUSED_TRIGGER_GRPS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_GROUP VARCHAR (200) NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_SCHEDULER_STATE (
SCHED_NAME VARCHAR(120) NOT NULL,
INSTANCE_NAME VARCHAR (200) NOT NULL ,
LAST_CHECKIN_TIME BIGINT NOT NULL ,
CHECKIN_INTERVAL BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_LOCKS (
SCHED_NAME VARCHAR(120) NOT NULL,
LOCK_NAME VARCHAR (40) NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_JOB_DETAILS (
SCHED_NAME VARCHAR(120) NOT NULL,
JOB_NAME VARCHAR (200) NOT NULL ,
JOB_GROUP VARCHAR (200) NOT NULL ,
DESCRIPTION VARCHAR (250) NULL ,
JOB_CLASS_NAME VARCHAR (250) NOT NULL ,
IS_DURABLE BOOLEAN NOT NULL ,
IS_NONCONCURRENT BOOLEAN NOT NULL ,
IS_UPDATE_DATA BOOLEAN NOT NULL ,
REQUESTS_RECOVERY BOOLEAN NOT NULL ,
JOB_DATA IMAGE NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_SIMPLE_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
REPEAT_COUNT BIGINT NOT NULL ,
REPEAT_INTERVAL BIGINT NOT NULL ,
TIMES_TRIGGERED BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_SIMPROP_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
STR_PROP_1 VARCHAR(512) NULL,
STR_PROP_2 VARCHAR(512) NULL,
STR_PROP_3 VARCHAR(512) NULL,
INT_PROP_1 INTEGER NULL,
INT_PROP_2 INTEGER NULL,
LONG_PROP_1 BIGINT NULL,
LONG_PROP_2 BIGINT NULL,
DEC_PROP_1 NUMERIC(13,4) NULL,
DEC_PROP_2 NUMERIC(13,4) NULL,
BOOL_PROP_1 BOOLEAN NULL,
BOOL_PROP_2 BOOLEAN NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_BLOB_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
BLOB_DATA IMAGE NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
JOB_NAME VARCHAR (200) NOT NULL ,
JOB_GROUP VARCHAR (200) NOT NULL ,
DESCRIPTION VARCHAR (250) NULL ,
NEXT_FIRE_TIME BIGINT NULL ,
PREV_FIRE_TIME BIGINT NULL ,
PRIORITY INTEGER NULL ,
TRIGGER_STATE VARCHAR (16) NOT NULL ,
TRIGGER_TYPE VARCHAR (8) NOT NULL ,
START_TIME BIGINT NOT NULL ,
END_TIME BIGINT NULL ,
CALENDAR_NAME VARCHAR (200) NULL ,
MISFIRE_INSTR SMALLINT NULL ,
JOB_DATA IMAGE NULL
);
ALTER TABLE QRTZ_CALENDARS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_CALENDARS PRIMARY KEY
(
SCHED_NAME,
CALENDAR_NAME
);
ALTER TABLE QRTZ_CRON_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_CRON_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_FIRED_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_FIRED_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
ENTRY_ID
);
ALTER TABLE QRTZ_PAUSED_TRIGGER_GRPS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_PAUSED_TRIGGER_GRPS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_SCHEDULER_STATE ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_SCHEDULER_STATE PRIMARY KEY
(
SCHED_NAME,
INSTANCE_NAME
);
ALTER TABLE QRTZ_LOCKS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_LOCKS PRIMARY KEY
(
SCHED_NAME,
LOCK_NAME
);
ALTER TABLE QRTZ_JOB_DETAILS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_JOB_DETAILS PRIMARY KEY
(
SCHED_NAME,
JOB_NAME,
JOB_GROUP
);
ALTER TABLE QRTZ_SIMPLE_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_SIMPLE_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_SIMPROP_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_SIMPROP_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_CRON_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_CRON_TRIGGERS_QRTZ_TRIGGERS FOREIGN KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) REFERENCES QRTZ_TRIGGERS (
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) ON DELETE CASCADE;
ALTER TABLE QRTZ_SIMPLE_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_SIMPLE_TRIGGERS_QRTZ_TRIGGERS FOREIGN KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) REFERENCES QRTZ_TRIGGERS (
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) ON DELETE CASCADE;
ALTER TABLE QRTZ_SIMPROP_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_SIMPROP_TRIGGERS_QRTZ_TRIGGERS FOREIGN KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) REFERENCES QRTZ_TRIGGERS (
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) ON DELETE CASCADE;
ALTER TABLE QRTZ_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_TRIGGERS_QRTZ_JOB_DETAILS FOREIGN KEY
(
SCHED_NAME,
JOB_NAME,
JOB_GROUP
) REFERENCES QRTZ_JOB_DETAILS (
SCHED_NAME,
JOB_NAME,
JOB_GROUP
);
COMMIT;
The error only happens when concurrent connections are established.
If I set the pool size to 1 it does not occur.
If I set the pool size to 2 I see it logged once.
If I set the pool size to 3 I see it logged twice.
The error seems to be gone when the connection pool retries.
It all looks like a concurrency bug in H2

IF NOT EXISTS checks only presence of constraint with exactly the same name. This exception indicates that your table already has a primary key constraint with the same or other set of columns, but with different name.
If you're sure that this table either has a primary key constraint on these two columns with unknown name or doesn't have a primary key constraint at all, you can replace your command with the following one:
EXECUTE IMMEDIATE
CASE WHEN NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = 'QRTZ_CALENDARS' AND CONSTRAINT_TYPE = 'PRIMARY KEY')
THEN 'ALTER TABLE QRTZ_CALENDARS ADD CONSTRAINT PK_QRTZ_CALENDARS PRIMARY KEY (SCHED_NAME, CALENDAR_NAME)'
ELSE ''
END;

Related

ORA-02438: Column check constraint cannot reference other columns

CREATE TABLE event (
event_id NUMBER(6) NOT NULL check(event_id > 0),
event_title VARCHAR2(100 CHAR) NOT NULL,
event_team VARCHAR2(1 CHAR) default 'N' NOT NULL check(event_team
IN('Y', 'N')),
no_per_team NUMBER(2) default 1 NOT NULL check((event_team = 'N' AND
no_per_team = 1) OR (event_team = 'Y' AND no_per_team > 1)),
event_gender VARCHAR2(1) NOT NULL check(event_gender IN ('M', 'F')),
og_id NUMBER(3) NOT NULL,
sport_id NUMBER(3) NOT NULL
);
Error report:
ORA-02438: Column check constraint cannot reference other columns
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constraint.
You cannot reference another column when declaring CHECK constrain on a column. Define it at the table level instead.
CREATE TABLE event (
event_id NUMBER(6) NOT NULL check(event_id > 0),
event_title VARCHAR2(100 CHAR) NOT NULL,
event_team VARCHAR2(1 CHAR) default 'N' NOT NULL check(event_team
IN('Y', 'N')),
no_per_team NUMBER(2) default 1 NOT NULL ,
event_gender VARCHAR2(1) NOT NULL check(event_gender IN ('M', 'F')),
og_id NUMBER(3) NOT NULL,
sport_id NUMBER(3) NOT NULL,
CONSTRAINT CHECK_NO_P check((event_team = 'N' AND
no_per_team = 1) OR (event_team = 'Y' AND no_per_team > 1))
);
Move column check constraint to a table check constraint (as Oracle told you):
CREATE TABLE event
(
event_id NUMBER (6) NOT NULL CHECK (event_id > 0),
event_title VARCHAR2 (100 CHAR) NOT NULL,
event_team VARCHAR2 (1 CHAR)
DEFAULT 'N'
NOT NULL
CHECK (event_team IN ('Y', 'N')),
no_per_team NUMBER (2) DEFAULT 1 NOT NULL,
event_gender VARCHAR2 (1) NOT NULL CHECK (event_gender IN ('M', 'F')),
og_id NUMBER (3) NOT NULL,
sport_id NUMBER (3) NOT NULL,
--
CONSTRAINT ch_npt CHECK
( ( event_team = 'N'
AND no_per_team = 1)
OR ( event_team = 'Y'
AND no_per_team > 1))
);

C# Error ORA 00907: Missing Right Parenthesis

My error:
ORA 00907: Missing Right Parenthesis
My code:
CREATE TABLE TBL_TD_USER
(
USER_ID INTEGER(10) NOT NULL,
USER_NAME VARCHAR2(20) NOT NULL,
PASSWORD VARCHAR2(20) NOT NULL,
CREATED_BY VARCHAR2(20) NOT NULL,
CREATED_DATE DATE NOT NULL,
MODIFIED_BY VARCHAR2(20) NOT NULL,
MODIFIED_DATE DATE NOT NULL,
IS_ACTIVE CHARACTER(1) NOT NULL,
DESCRIPTION VARCHAR2(200) NOT NULL,
CONSTRAINT TBL_TD_USER PRIMARY KEY (USER_ID)
);
This works:
CREATE TABLE TBL_TD_USER
(
USER_ID INTEGER NOT NULL ,
USER_NAME VARCHAR2(20) NOT NULL,
PASSWORD VARCHAR2(20) NOT NULL,
CREATED_BY VARCHAR2(20) NOT NULL,
CREATED_DATE DATE NOT NULL,
MODIFIED_BY VARCHAR2(20) NOT NULL,
MODIFIED_DATE DATE NOT NULL,
IS_ACTIVE VARCHAR2(1) NOT NULL,
DESCRIPTION VARCHAR2(200) NOT NULL,
CONSTRAINT TBL_TD_USER PRIMARY KEY (USER_ID)
);
There's no variable type named CHARACTER in oracle, as an alternative, you to use VARCHAR2 with length value in parenthesis.
You can use INTEGER type without length.
demo

how to insert using timestamp oracle

I made a 'Kereta' table:
CREATE TABLE Kereta (
ID_Kereta NUMBER(3) CONSTRAINT kr_id_kr_pk PRIMARY KEY,
Nama_Kereta VARCHAR2(30) CONSTRAINT kr_nama_kr_nn NOT NULL,
Jam_Keberangkatan TIMESTAMP CONSTRAINT kr_jk_nn NOT NULL,
Jam_Tiba TIMESTAMP CONSTRAINT kr_jt_nn NOT NULL,
Stasiun_Asal VARCHAR2(20) CONSTRAINT kr_sa_nn NOT NULL,
Stasiun_Tujuan VARCHAR2(20) CONSTRAINT kr_st_nn NOT NULL
)
Then i tried to insert 13.00 to Jam_Keberangkatan field. I can't insert one by one because of NOT NULL constraint, what should i do?

PL SQL trigger When Insert a table insert into another Table

I attached two table below I need a trigger when insert do_chd table in same time insert in do_chd_hist table And DO_MST table to DO_MST_HIST TAbLE
create table do_mst
( fdo_no varchar2(14) not null,
fdo_date date not null,
code_db varchar2(11),
db_name varchar2(25),
code_sdb varchar2(11),
sdb_name varchar2(25),
code_sst varchar2(11),
sst_name varchar2(25),
constraint fdo_no_pk primary key(fdo_no)
);
create table do_mst_hist
( fdo_no varchar2(14) not null,
fdo_date date not null,
code_db varchar2(11),
db_name varchar2(25),
code_sdb varchar2(11),
sdb_name varchar2(25),
code_sst varchar2(11),
sst_name varchar2(25),
constraint fdo_no_pk primary key(fdo_no)
);
create table do_chd
( fdo_no varchar2(14) not null,
itemcode varchar2(11) not null,
name varchar(27) not null,
unit_price varchar2(11),
req_qty number(11) not null,
total_price number(14),
unit varchar2(7),
constraint fdo_no_fk foreign key(fdo_no)
references do_mst(fdo_no)
);
create table do_chd_hist
( fdo_no varchar2(14) not null,
itemcode varchar2(11) not null,
name varchar(27) not null,
unit_price varchar2(11),
req_qty number(11) not null,
total_price number(14),
unit varchar2(7),
constraint fdo_no_fk foreign key(fdo_no)
references do_mst(fdo_no)
);
Can anybody help me to write this trigger?
While I agree with what being said in the comments, I don't think there's a reason to think twice about writing up a create trigger syntax, and I'm happy to show you the solution to your problem:
CREATE OR REPLACE TRIGGER do_chd_insert_trg
AFTER INSERT do_chd
FOR EACH ROW
DECLARE
sal_diff number;
BEGIN
insert into do_chd_hist
values (new.fdo_no, new.itemcode, new.name, new.unit_price, new.req_try, new.total_price, new.unit);
END;
/
You can use this syntax to generate the trigger to the second table, after changing the respectable column names.
You can read more about oracle trigger syntax here -
https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm

Oracle SQL Foreign Key Issues

I am trying to create tables as well as references but keep getting the following error:
ERROR at line 4:
ORA-00907: missing right parenthesis
...and have searched high and low with no clear way on how to resolve this. I have the following:
drop table financing_plans;
CREATE TABLE financing_plans (plan_id CHAR(10) PRIMARY KEY,
institution VARCHAR2(15) NOT NULL,
Loan_type VARCHAR2(10) NOT NULL,
min_down NUMBER(10,2) NOT NULL,
max_loan_amount NUMBER(10,2) NOT NULL,
Percentage NUMBER(10,2) NOT NULL,
Max_term NUMBER(10,25) NOT NULL);
drop table sale_financings;
CREATE TABLE sale_financings (sale_id CHAR(10) PRIMARY KEY,
Down_pay VARCHAR2(25) NOT NULL,
Loan_term VARCHAR2(18) NOT NULL,
FOREIGN KEY ("plan_uid") REFERENCES financing_plans(plan_id)
FOREIGN KEY ("sale_uid") REFERENCES sales(sale_id));
drop table sales;
CREATE TABLE sales (sale_id CHAR(10) PRIMARY KEY,
Salesperson_ID VARCHAR2(25) NOT NULL,
Cust_ID VARCHAR2(10) NOT NULL,
VIN VARCHAR2(10) NOT NULL,
Gross_sale_price NUMBER(10,2) NOT NULL,
Mileage NUMBER(10,2) NOT NULL,
sale_date DATE,
Vehicle_status VARCHAR2(10) NOT NULL);
Any solution, anyone?
Thanks,
You have to create columns for the foreign key at first, and then create the foreign keys:
CREATE TABLE financing_plans (
plan_id CHAR(10) PRIMARY KEY
, institution VARCHAR2(15) NOT NULL
, Loan_type VARCHAR2(10) NOT NULL
, min_down NUMBER(10,2) NOT NULL
, max_loan_amount NUMBER(10,2) NOT NULL
, Percentage NUMBER(10,2) NOT NULL
, Max_term NUMBER(10,25) NOT NULL
);
CREATE TABLE sales (
sale_id CHAR(10) PRIMARY KEY
, Salesperson_ID VARCHAR2(25) NOT NULL
, Cust_ID VARCHAR2(10) NOT NULL
, VIN VARCHAR2(10) NOT NULL
, Gross_sale_price NUMBER(10,2) NOT NULL
, Mileage NUMBER(10,2) NOT NULL
, sale_date DATE
, Vehicle_status VARCHAR2(10) NOT NULL
);
CREATE TABLE sale_financings (
sale_id CHAR(10) PRIMARY KEY
, Down_pay VARCHAR2(25) NOT NULL
, Loan_term VARCHAR2(18) NOT NULL
, plan_id char(10) not null
, sale_uid char(10) not null
, CONSTRAINT constraint_name_fk FOREIGN KEY (plan_id) REFERENCES financing_plans(plan_id)
, constraint constraint_name_fk2 foreign key (sale_uid) references sales(sale_id)
);
It looks like you're creating the FK reference to sales(sale_id) in sale_financings before you create the sales table. Also, I would check to be sure table exits before dropping it.

Resources