ORA-02438: Column check constraint cannot reference other columns - oracle

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))
);

Related

Attempt to define a second primary key with IF NOT EXISTS

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;

Adding reference partitioning to oracle table

on the 12c version of oracle I have created T_App table like this:
create table T_APP
(
C_ID NUMBER(34, 0) not null,
C_ACTIVE number(1, 0) not null,
C_APPID varchar2(255 char) not null,
C_CONTACTTYPE varchar2(255 char) not null,
primary key (C_ID)
)
partition by list(C_CONTACTTYPE)
(
partition p1 values ('default')
);
and T_APPUSER table like this:
create table T_APPUSER
(
C_ID NUMBER(34, 0) primary key,
C_ACTIVE number(1, 0) not null,
C_DEVICETOKEN varchar2(255 char),
C_SSOID varchar2(255 char),
F_APP NUMBER(34, 0) not null,
constraint FK_APP
foreign key (F_APP)
references T_APP
)
PARTITION BY REFERENCE (FK_APP);
but when I run the query of creating T_APPUSER I get ERROR!
is there anyThing wrong with my query for creating the T_APPUSER table?

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?

Insert my data from Relational Tables to Object-Relational Tables

I tried with the command insert into select... but it didn't work. How can I load the data from the Relational tables to the Object-Relational tables?
Example for table Categories:
insert into CATEGORIES_NEW select * from Categories;
The above worked,but the similar way doesn't work for the other tables.
Below are all the tables:
Relational Tables
CREATE TABLE Customers (
CUSTOMERID NUMBER NOT NULL,
FIRSTNAME VARCHAR2 (20) NOT NULL,
LASTNAME VARCHAR2 (20) NOT NULL,
ADDRESS1 VARCHAR2 (40) NOT NULL,
ADDRESS2 VARCHAR2 (40),
CITY VARCHAR2 (20) NOT NULL,
STATE VARCHAR2 (20) NOT NULL,
ZIP VARCHAR2 (20) NOT NULL,
COUNTRY VARCHAR2 (20) NOT NULL,
REGION VARCHAR2 (20) NOT NULL,
EMAIL VARCHAR2 (20) NOT NULL,
PHONE VARCHAR2 (20) NOT NULL,
CREDITCARDTYPE VARCHAR2 (20) NOT NULL,
CREDITCARD VARCHAR2 (20) NOT NULL,
CREDITCARDEXPIRATION VARCHAR (10) NOT NULL,
USERNAME VARCHAR2 (20) NOT NULL,
"PASSWORD" VARCHAR2 (20) NOT NULL,
AGE NUMBER NOT NULL,
INCOME NUMBER NOT NULL,
GENDER VARCHAR2 (8) NOT NULL,
PRIMARY KEY (CUSTOMERID));
CREATE TABLE Orders (
ORDERID NUMBER NOT NULL,
ORDERDATE DATE NOT NULL,
CUSTOMERID NUMBER NOT NULL REFERENCES Customers,
NETAMOUNT NUMBER NOT NULL,
TAX NUMBER NOT NULL,
TOTALAMOUNT NUMBER NOT NULL,
PRIMARY KEY (ORDERID));
CREATE TABLE Categories (
"CATEGORY" NUMBER NOT NULL,
CATEGORYNAME VARCHAR (20) NOT NULL,
PRIMARY KEY ("CATEGORY"));
CREATE TABLE Products (
PROD_ID NUMBER NOT NULL ,
"CATEGORY" NUMBER NOT NULL REFERENCES Categories,
TITLE VARCHAR (40) NOT NULL,
ACTOR VARCHAR (40) NOT NULL,
PRICE VARCHAR (40) NOT NULL,
PRIMARY KEY (PROD_ID));
CREATE TABLE Orderlines (
ORDERLINEID NUMBER NOT NULL,
ORDERID NUMBER NOT NULL REFERENCES Orders,
PROD_ID NUMBER NOT NULL REFERENCES Products,
QUANTITY NUMBER NOT NULL,
ORDERDATE VARCHAR2 (80) NOT NULL,
PRIMARY KEY (ORDERLINEID,ORDERID));
Object-Relational Tables
CREATE TYPE Customer_obj AS OBJECT (
CUSTOMERID NUMBER,
CUST_NAME NAME,
CUST_ADDRESS ADDRESS_TYPE,
CUST_CONTACT CONTACT_INFO,
CUST_CREDITCARD CREDITCARD_INFO,
CUST_PERSONALINFO PERSONAL_INFO
);
CREATE TYPE NAME AS OBJECT (
FIRSTNAME VARCHAR2(80) ,
LASTNAME VARCHAR2(80)
);
CREATE TYPE ADDRESS_TYPE AS OBJECT (
ADDRESS VARCHAR2(280) ,
CITY VARCHAR2(80) ,
STATE VARCHAR2(20),
ZIP VARCHAR2(40) ,
COUNTRY VARCHAR2(40) ,
REGION VARCHAR2(40)
);
CREATE TYPE CONTACT_INFO AS OBJECT (
EMAIL VARCHAR2(40),
PHONE VARCHAR2(40)
);
CREATE TYPE CREDITCARD_INFO AS OBJECT (
CREDITCARDTYPE VARCHAR2(40) ,
CREDITCARD VARCHAR2(40) ,
CREDITCARDEXPIRATION VARCHAR2(40)
);
CREATE TYPE PERSONAL_INFO AS OBJECT (
USERNAME VARCHAR2(80) ,
PASSWORD VARCHAR2(80) ,
AGE NUMBER ,
INCOME NUMBER ,
GENDER VARCHAR(20)
);
CREATE TABLE CUSTOMERS_NEW OF Customer_obj(
PRIMARY KEY (CUSTOMERID));
CREATE TYPE Categories_obj AS Object(
CATEGORY NUMBER ,
CATEGORYNAME VARCHAR2(80) );
CREATE TABLE CATEGORIES_NEW OF Categories_obj(
PRIMARY KEY (CATEGORY));
CREATE TYPE Orders_obj AS Object(
ORDERID NUMBER ,
ORDERDATE VARCHAR2(400),
CUSTOMERID NUMBER,
NETAMOUNT NUMBER ,
TAX NUMBER,
TOTALAMOUNT NUMBER
);
CREATE TABLE ORDERS_NEW OF Orders_obj(
PRIMARY KEY (ORDERID),
FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMERS_NEW
);
CREATE TYPE Products_obj AS Object(
PROD_ID NUMBER ,
CATEGORY NUMBER ,
TITLE VARCHAR2 (40) ,
ACTOR VARCHAR2 (40) ,
PRICE VARCHAR2 (40)
);
CREATE TABLE PRODUCTS_NEW OF Products_obj(
PRIMARY KEY (PROD_ID),
FOREIGN KEY (CATEGORY) REFERENCES CATEGORIES_NEW
);
CREATE TYPE Orderlines_obj AS Object(
CUST_ID_INFO ID_INFO,
QUANTITY NUMBER,
ORDERDATE VARCHAR2 (80)
);
CREATE TYPE ID_INFO AS Object (
ORDERLINEID NUMBER ,
ORDERID NUMBER ,
PROD_ID NUMBER
);
CREATE TABLE ORDERLINES_NEW OF Orderlines_obj(
PRIMARY KEY (CUST_ID_INFO.ORDERLINEID,CUST_ID_INFO.ORDERID),
FOREIGN KEY (CUST_ID_INFO.ORDERID) REFERENCES ORDERS_NEW,
FOREIGN KEY (CUST_ID_INFO.PROD_ID) REFERENCES PRODUCTS_NEW
);
Object tables are tricky, and probably not worth the effort. But anyway.
You're working with objects, which means you need to instantiate them. When the object table is defined on a type which just has scalar attributes Oracle is clever enough to implicitly instantiate the object. But when the Type is complex, with attributes which are themselves Types ypu need to instantiate them explicitly.
Here is the insert for CUSTOMERS_NEW
insert into CUSTOMERS_NEW
select Customer_obj (
c.CUSTOMERID ,
c.FIRSTNAME||' '||c.LASTNAME ,
ADDRESS_TYPE(
c.ADDRESS1||' ' ||c.ADDRESS2 ,
c.CITY ,
c.STATE ,
c.ZIP ,
c.COUNTRY ,
c.REGION
),
CONTACT_INFO(
c.EMAIL,
c.PHONE
),
CREDITCARD_INFO (
c.CREDITCARDTYPE ,
c.CREDITCARD ,
c.CREDITCARDEXPIRATION
),
PERSONAL_INFO (
c.USERNAME,
c.PASSWORD,
c.AGE ,
c.INCOME ,
c.GENDER
)
)
from customers ;

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