Date Check Constraint in Oracle - oracle

CREATE TABLE TEMP_STUDENT
(STUDID NUMBER(8,0) NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR2(25),
LAST_NAME VARCHAR2(25),
ZIP VARCHAR2(5) FOREIGN KEY REFERENCES ZIPCODE(ZIP),
REGISTRATION_DATE DATE NOT NULL CHECK (REGISTRATION_DATE > TO_DATE('1-JAN-2000 00:00:00','DD-MON-YYYY HH24:MI:SS')))
What is wrong in this, please help. I am getting error: 'missing right parenthisis'

The issue is with your FOREIGN KEY REFERENCES check.
You want this:
create table zipcode
(
zip varchar2(5) primary key
);
CREATE TABLE TEMP_STUDENT
(STUDID NUMBER(8,0) NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR2(25),
LAST_NAME VARCHAR2(25),
ZIP VARCHAR2(5),
REGISTRATION_DATE DATE NOT NULL CHECK (REGISTRATION_DATE > TO_DATE('1-JAN-2000 00:00:00','DD-MON-YYYY HH24:MI:SS')),
constraint foreign key REFERENCES ZIPCODE(ZIP));

Related

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

ORA-02291: integrity constraint (SQL_HLZTBRUASDUURQGIOAYPNRAFC.EMPLOYEE_ID) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721

I am new to sql and have been trying to figure out what im doing wrong when adding the foreign keys. I am able to create all the tables and everything up to the point where i go to input values for the SALES table. I get an error :
ORA-02291: integrity constraint
(SQL_HLZTBRUASDUURQGIOAYPNRAFC.EMPLOYEE_ID) violated - parent key not
found ORA-06512: at "SYS.DBMS_SQL", line 1721:
Can anyone look over my code and help me with what to change?
CREATE TABLE EMPLOYEE
(EMPLOYEE_ID char(10) PRIMARY KEY,
EMPLOYEE_NAME varchar(30),
Address varchar(50),
PHONE number(10),
HIRING_DATE date not null,
SALARY number(6))
CREATE TABLE PRODUCTS
(PRODUCT_ID char(2) PRIMARY KEY,
PRICE CHAR(5),
TYPE varchar(10),
PRODUCT_NAME varchar(30),
AUTHOR varchar(20))
CREATE TABLE CUSTOMER
(CUSTOMER_ID char(5) PRIMARY KEY,
CUSTOMER_NAME varchar(30),
PHONE number(7),
ADDRESS varchar(30))
CREATE TABLE INVENTORY
(PRODUCT_NAME varchar(20) PRIMARY KEY,
PRODUCT_ID char(2),
UNIT_PRICE number(4),
SHELF_LOCATION varchar(2),
CURRENT_INVENTORY number(3),
MONTHLY_PURCHASES number(3))
CREATE TABLE SALES
(TRANSACTION_ID char(5) PRIMARY KEY,
PRODUCT_ID char(5),
SELL_DATE date not null,
CUSTOMER_ID char(5),
UNITS_SOLD number(2),
EMPLOYEE_ID char(10),
SALES_AMOUNT decimal(10,2),
CONSTRAINT CUSTOMER_ID
FOREIGN KEY (CUSTOMER_ID)
REFERENCES CUSTOMER(CUSTOMER_ID),
CONSTRAINT PRODUCT_ID
FOREIGN KEY(PRODUCT_ID)
REFERENCES PRODUCTS(PRODUCT_ID),
CONSTRAINT EMPLOYEE_ID
FOREIGN KEY (EMPLOYEE_ID)
REFERENCES EMPLOYEE(EMPLOYEE_ID))
When inserting the code below is when I get an error:
INSERT INTO SALES
VALUES('1','42','01-JAN-2015','4269','3','5678901234','135.00’)
The employee_id 5678901234 doesn't exist in the EMPLOYEE table.
Which causes the foreign key constraint on EMPLOYEE_ID to complain.
Those foreign key's are there to ensure referential integrity after all.
(The constraint is used to give a name to it)
So add the user with that id to the EMPLOYEE table first.
An extra advice.
It's safer to list the column names in the INSERT statement.

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.

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.

FOREIGN KEY Connection

So I have table customers and table bookings
I want to add a Foreign Key to the script so that
the CustID from customers can make a column in bookings and connect.
create table customers(
CustID INT NOT NULL,
CustomerName VARCHAR2(25),
CustomerAddress VARCHAR2(50),
CustomerPhone NUMBER(10),
CONSTRAINT pk_cust PRIMARY KEY (CustID) );
and
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers(CustID)
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25));
error;
StartDate VARCHAR2(25),
*
ERROR at line 10:
ORA-00907: missing right parenthesis
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25),
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers(CustID));
You have to declare the primary key and other constraints after you finish declaring the columns.
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers1 (CustID),
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25));
see it carefully -- you Have to add comma after this CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers1 (CustID),

Resources