Oracle SQL Foreign Key Issues - oracle

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.

Related

TRIGGERS_function

please help me to find the solution i have a project .
so my project is about a mobile repair store , we have some tables :
customers, mobiles, repairjob
//customer table
CREATE TABLE CUSTOMERS (
CUSTOMER_CPR NUMBER(9) PRIMARY KEY,
FIRST_NAME VARCHAR2(30) NOT NULL,
MID VARCHAR2(30)
LAST_NAME VARCHAR2(30) NOT NULL,
EMAIL VARCHAR2(30) NOT NULL UNIQUE,
HOME_TEL NUMBER(8) UNIQUE,
HOUSE_NO VARCHAR2(30) NOT NULL,
ROAD_NO VARCHAR2(30) NOT NULL,
BLOCK_NO VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) NOT NULL
);
//mobiles table
CREATE TABLE MOBILES (
MOB_ID NUMBER(8,0) PRIMARY KEY,
MODEL VARCHAR2(30) NOT NULL,
MOB_DECRIPTION VARCHAR2(100),
SERIAL_NUM VARCHAR2(30) UNIQUE,
MAKE VARCHAR2(50),
customer_cpr number(9),
);
//repair job table
CREATE TABLE REPAIR JOB (
JOB_NUM NUMBER primary key,
customer_cpr number(9)
EMP_ID NUMBER(8,0) NOT NULL,
DATE_RECEIVED DATE,
DATE_TO_RETURN DATE,
ITEM_ID NUMBER
);
so my question is to make a trigger function , we have assumed that the customer cannot have more than 3 repair jobs or can't fix more than 3 mobiles at one time until it finishes all. so how i make a triiger function ?

ORA-00904: : invalid identifier error fix in oracle

this one works
create table reservation(
reservation_num number(6) constraint reservation_num_pk primary key,
rdate date,
payment_status varchar(6) constraint payment_status_ck check(payment_status in('paid','unpaid')),
seat_num number(6),
flight_num number(3)
)
but this one fails
create table reservation(
reservation_num number(6) constraint reservation_num_pk primary key,
date date,
payment_status varchar(6) constraint payment_status_ck check(payment_status in('paid','unpaid')),
seat_num number(6),
flight_num number(3)
)
Date is a reserved word. If you really want to use it as column name put it in double quotes
create table reservation(
reservation_num number(6) constraint
reservation_num_pk primary key,
"date" date,
payment_status varchar(6) constraint payment_status_ck
check(payment_status in('paid','unpaid')),
seat_num number(6),
flight_num number(3)
)

Table using Foreign Key Oracle

I am having a problem linking two tables in oracle 11g express addition. I have looked on this site for assistance, but I am unable to find what I need.
My BOOKS table was loaded up using the following statement:
CREATE TABLE STATES(
ST VARCHAR2(2) PRIMARY KEY,
STATES VARCHAR2(20) NOT NULL
);
The CUSTOMERS table that I am trying to load is written as such:
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
FOREIGN KEY ST REFERENCES STATES(ST)
);
However, when I run the statement I get the following error:
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
I believe that the error is in the FOREIGN KEY portion of the statement, as I can load the table without that statement.
You have syntax error while declaring foreign key constraint
Correct one:
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
CONSTRAINT FK_CUSTOMERS FOREIGN KEY (ST) REFERENCES STATES(ST)
);
or
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
FOREIGN KEY (ST) REFERENCES STATES(ST)
);

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?

Unable to use composite key as foreign key

CREATE TABLE departments
( department_id number(10) NOT NULL,
department_name varchar2(50) NOT NULL,
department_code varchar2(50) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id, department_code)
);
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id, department_code)
REFERENCES departments(department_id,department_code));
Your employees table doesn't have a department_code field, so the FOREIGN KEY (department_id, department_code) part is trying to use something that doesn't exist. Which is what the error you get from running the second statement tells you:
ERROR at line 8:
ORA-00904: "DEPARTMENT_CODE": invalid identifier
You would either have to include the department code in that table, which would denormalise the data; or change the primary key on departments to just department_id, which would be much more normal anyway, i.e.:
CREATE TABLE departments
( department_id number(10) NOT NULL,
department_name varchar2(50) NOT NULL,
department_code varchar2(50) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id)
);
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id)
REFERENCES departments(department_id));
It doesn't usually make sense to have a composite primary key like that, where there is a single column that looks like it should be unique anyway. You wouldn't expect to have the same department_id with two department_code values.
But if you do have a legitimate reason to have a composite primary key then all columns in that key will have to be duplicated on the child tables and their foreign key constraints:
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
department_code varchar2(50) NOT NULL,
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id, department_code)
REFERENCES departments(department_id,department_code));
Table EMPLOYEES created.
Which means that whenever you insert a record into that table you will have to supply both the ID and code for an existing department, of course.

Resources