Oracle Batch Database - oracle

I'm trying to run my databases but STUDENTS AND BATCHES tables are giving the error: 'ORA-00942: table or view does not exist'. I've tried dropping them in order, but I can't tell what exactly the problem is.
Forgive me if the answer is obvious, I'm new to batches
DROP TABLE students;
DROP TABLE batches;
DROP TABLE courses;
DROP TABLE faculty;
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);

Create your faculty and courses tables first. Since batches depends on one of those tables, you would want to have those tables created first.
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Example: http://sqlfiddle.com/#!4/91909c/1 shows the sequence of table creation

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

ORA-02253 ORA-02253: constraint specification not allowed here

CREATE TABLE XX_EMP_COL_CONST
(
EMP_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_PK PRIMARY KEY,
ENAME VARCHAR2(100) CONSTRAINT XX_EMP_COL_CONST_UK1 UNIQUE,
SALARY NUMBER NOT NULL,
GENDER CHAR (1) CONSTRAINT XX_EMP_COL_CONST_CHQ CHECK (GENDER IN ('M','F')),
DEPT_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_FK1 REFERENCE departments(department_id)
);
You can try (you made just a simple mistake: the S after REFERENCE):
CREATE TABLE XX_EMP_COL_CONST (
EMP_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_PK PRIMARY KEY
,ENAME VARCHAR2(100) CONSTRAINT XX_EMP_COL_CONST_UK1 UNIQUE
,SALARY NUMBER NOT NULL
,GENDER CHAR(1) CONSTRAINT XX_EMP_COL_CONST_CHQ CHECK (GENDER IN ('M', 'F'))
,DEPT_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_FK1 REFERENCES departments(department_id)
);
You can see it on http://sqlfiddle.com/#!4/4e896f/1

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.

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