Oracle APEX master detail page creation: ORA-06531 error - sql

I am creating a flock management application with APEX with the following PL/SQL scheme:
-- SHEEP table
create table sheep (
sheep_id number(*,0) generated by default on null as identity,
eid varchar2(6) not null,
sex varchar2(6) not null,
name varchar2(45),
breed varchar2(255) not null,
birth date,
sheep_state varchar2(45) not null,
lambs number(*,0) not null,
lambs_sets number(*,0) not null,
markings varchar2(4000),
note varchar2(4000),
constraint sheep_pk primary key (sheep_id)
);
-- EVENT table
create table event (
event_id number(*,0) generated by default on null as identity,
sheep_id varchar2(6) not null,
name varchar2(255) not null,
event_date date not null,
description varchar2(4000) not null,
location varchar2(45) not null,
constraint event_pk primary key (event_id)
);
-- SALE table
create table sale (
sale_id number(*,0) generated by default on null as identity,
sheep_id varchar2(6) not null,
price number(*,2) not null,
sale_date date not null,
location varchar2(45) not null,
note varchar2(4000),
constraint sale_pk primary key (sale_id)
);
In this scheme, the SALE and EVENT tables reference the EID field of the SHEEP table (unique identifier of 5 numbers and one letter - representing the yellow tag they have in their ears). I am trying to create a Master Detail page that would link a sheep to specific events (what happened to the sheep) and a sales history (if it has been sold, when, where, for how much).
I tried to use both the Create Application Wizard and the Create Page Wizard but each time I get the same error:
Ajax call returned server error ORA-06531: Reference to uninitialized collection for Execute PL/SQL Code.
It still lets me continue in the wizard but when I want to select the details tables (SALE & EVENT), they don't even show up in selecting list (cf here).
Does someone have an idea? I am quite new to APEX and I might have missed something. Also, if you have any recommendations considering my relational scheme (change to make, improvements for efficiency), don't hesitate to tell me as well.
Thanks in advance!

There is no relationship between event and sheep or between sale and sheep. You state the SALE and EVENT tables reference the EID field of the SHEEP table but the database doesn't know about that, because that relationship has not been defined by way of a foreign key. A foreign key in a table references the primary key of the other table, you can't reference another column (like the EID), even if that is unique.
In your script, make sheep_id NUMBER in all tables and create a foreign key in SALE and EVENT. Like this:
-- SHEEP table
create table sheep (
sheep_id number(*,0) generated by default on null as identity,
eid varchar2(6) not null,
sex varchar2(6) not null,
name varchar2(45),
breed varchar2(255) not null,
birth date,
sheep_state varchar2(45) not null,
lambs number(*,0) not null,
lambs_sets number(*,0) not null,
markings varchar2(4000),
note varchar2(4000),
constraint sheep_pk primary key (sheep_id)
);
-- EVENT table
create table event (
event_id number(*,0) generated by default on null as identity,
sheep_id number not null,
name varchar2(255) not null,
event_date date not null,
description varchar2(4000) not null,
location varchar2(45) not null,
constraint event_pk primary key (event_id),
constraint fk_event_sheep
foreign key (sheep_id)
references sheep(sheep_id)
);
-- SALE table
create table sale (
sale_id number(*,0) generated by default on null as identity,
sheep_id number not null,
price number(*,2) not null,
sale_date date not null,
location varchar2(45) not null,
note varchar2(4000),
constraint sale_pk primary key (sale_id),
constraint fk_sale_sheep
foreign key (sheep_id)
references sheep(sheep_id)
);
This is the way to create a master detail relationship in relational database system and it should allow you to create a Master-Detail form in APEX as well.

Related

Is there a way to make two primary keys, with only one foreign key that references another tables primary key

I'm new to SQL and trying to make it so that there are two primary keys and one foreign key that references other tables primary key.
I've tried adding the attribute that is a primary key that is missing the table that needs referencing and then making that a foreign key but still getting the message that "number of referencing columns must match references columns".
if there is a solution to what I'm trying to achieve it would be much appreciated.
CREATE TABLE Next_of_Kin
(
Employee_No VARCHAR2(8) NOT NULL,
kin_Number VARCHAR2(8) NOT NULL,
Name VARCHAR2(40) NOT NULL,
relationship VARCHAR2(40) NOT NULL,
contact_number VARCHAR2(11) NOT NULL,
PRIMARY KEY (Employee_No, Kin_Number),
FOREIGN KEY (Employee_No, Kin_Number) REFERENCES Employee(Employee_No)
);
CREATE TABLE Employee
(
Employee_No VARCHAR2(8) NOT NULL,
family_Name VARCHAR2(40) NOT NULL,
given_Name VARCHAR2(40) NOT NULL,
address VARCHAR2(80) NOT NULL,
date_of_Birth DATE NOT NULL,
date_Hired DATE NOT NULL,
supervisor VARCHAR2(40) NULL,
PRIMARY KEY (Employee_No, Supervisor),
FOREIGN KEY (Employee_No,Supervisor)
REFERENCES Employee(Employee_No, Supervisor)
);
Presumably supervisor references an employee number (because the supervisor is also an employee), so it should only be one column.
Also, there's no reason why a next of kin entry should refer to another next of kin. All you need to enforce is that the employee number refers to an existing employee.
Single-column keys can be declared as part of the column definition, which simplifies the syntax and also allows the datatype of foreign key columns to be inherited from the parent.
create table employee
( employee_no varchar2(8) primary key
, family_name varchar2(40) not null
, given_name varchar2(40) not null
, address varchar2(80) not null
, date_of_birth date not null
, date_hired date not null
, supervisor references employee(employee_no)
);
create table next_of_kin
( employee_no references employee (employee_no) not null
, kin_number varchar2(8) not null
, name varchar2(40) not null
, relationship varchar2(40) not null
, contact_number varchar2(11) not null
, primary key (employee_no, kin_number)
);
A table can have as many unique constraints as you like, but only one primary key. In your example though, you are trying to define one primary key with two columns, which is allowed (although not needed here). You can also have a foreign key with more than one column, as long as it matches a corresponding primary or unique constraint in the specified table.
I don't know your company, but I'm pretty sure that the employee number is unique. Thus, the primary key of your employee table should be Employee_No, without the supervisor.
The foreign key in your employee table does not make sense. Remove it.
In your next of kin table, leave the primary key as it is, but make only the employee number a foreign key:
FOREIGN KEY (Employee_No) REFERENCES Employee(Employee_No)
It looks like you want to have EMPLOYEE_NO be the primary key on EMPLOYEE, but you also want (EMPLOYEE_NO, SUPERVISOR) to be unique. Note that this unique key is unnecessary - becaue EMPLOYEE_NO is the primary key on EMPLOYEE it's guaranteed to be unique, and thus any combination of (EMPLOYEE_NO, <some other column on EMPLOYEE) will automatically be unique - but sometimes I throw in a unique key like this just as documentation, or to make it referenceable in a foreign key constraint from another table.
So anyways, consider using:
(
Employee_No VARCHAR2(8) NOT NULL,
kin_Number VARCHAR2(8) NOT NULL,
Name VARCHAR2(40) NOT NULL,
relationship VARCHAR2(40) NOT NULL,
contact_number VARCHAR2(11) NOT NULL,
PRIMARY KEY (Employee_No, Kin_Number),
FOREIGN KEY (Employee_No) REFERENCES Employee(Employee_No)
);
CREATE TABLE Employee
(
Employee_No VARCHAR2(8) NOT NULL,
family_Name VARCHAR2(40) NOT NULL,
given_Name VARCHAR2(40) NOT NULL,
address VARCHAR2(80) NOT NULL,
date_of_Birth DATE NOT NULL,
date_Hired DATE NOT NULL,
supervisor VARCHAR2(40) NULL,
PRIMARY KEY (Employee_No),
UNIQUE KEY (Employee_No, Supervisor)
);
Since (EMPLOYEE_NO, KIN_NUMBER) is the primary key on NEXT_OF_KIN it makes no sense to have a self-referencing foreign key utilizing (EMPLOYEE_NO, KIN_NUMBER) as the row would always-and-only refer to itself.

How can I correct the following SQL script?

I am assigned to create a database in SQL based on an ERD that I studied and recreated a week prior. I am using this app called "Oracle SQL Developer" and trying to learn about creating tables, primary keys, foreign keys, sequences, views, etc. I tested my drafts out on the developer and they keep on coming up with the following errors:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vk0cu.png this is some syntax error due to partially recognized rules.
Other errors involve missing right parentheses, tables having more than one primary key, etc. So far, this is my best effort at starting a database:
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER
(CUST_EMAIL VARCHAR(10) PRIMARY KEY,
CUST_NAME VARCHAR(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR(10) NOT NULL,
CUST_CITY VARCHAR(10) NOT NULL,
CUST_STATE VARCHAR(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS
(ORDER_NO NUMERIC(10) PRIMARY KEY,
CUST_EMAIL VARCHAR(10) FOREIGN KEY,
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE(10) NOT NULL,
SHIP_DATE DATE(10) NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO)
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT
(PAY_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
CARD_TYPE VARCHAR(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE(10) NOT NULL,
PAY_CONF INTEGER(10) NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY
(DEL_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
SHIP_DATE DATE(10) FOREIGN KEY,
DEL_DATE DATE(10) NOT NULL,
STATUS VARCHAR(10) NOT NULL,
DEL_MODE VARCHAR(10) NOT NULL,
INVOICE_NO INTEGER(10) NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (SHIP_DATE) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT
(STREET_ADDRESS VARCHAR(10) PRIMARY KEY,
NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10) NOT NULL,
STATE VARCHAR(10) NOT NULL,
ZIP INTEGER(10) NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
Where should I place my parentheses if the app is correct in saying that I am missing some of them? Where do I even have more than one primary key and how can I rephrase my lines to reduce them? How can I take my rules from partially recognized to fully recognized?
This is for a college project on relational database systems. I just need to create some tables, primary keys, and foreign keys so I can be allowed to create sequences.
DATE and INTEGER do not have a precision.
Either declare the PRIMARY KEY inline or out-of-line but you cannot do both.
Same for foreign keys (and inline foreign keys need the REFERENCES keyword and not the FOREIGN KEY keywords).
VARCHAR would be better as VARCHAR2
You cannot have a FOREIGN KEY that refers to a non-primary key, non-unique column (i.e. SHIP_DATE). While you could create a UNIQUE composite key on ORDER_NO and SHIP_DATE and reference that (example below); it is probably better to entirely remove SHIP_DATE from the DELIVERY table (and then you don't need a foreign key) and just keep it in a single table so the tables are in 3rd normal form. If you want the information to display it then JOIN the tables using the ORDER_NO foreign key.
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER(
CUST_EMAIL VARCHAR2(10),
CUST_NAME VARCHAR2(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR2(10) NOT NULL,
CUST_CITY VARCHAR2(10) NOT NULL,
CUST_STATE VARCHAR2(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS(
ORDER_NO NUMERIC(10),
CUST_EMAIL VARCHAR2(10),
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE NOT NULL,
SHIP_DATE DATE NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO),
CONSTRAINT U_PRODUCTS UNIQUE (ORDER_NO, SHIP_DATE),
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER
);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT(
PAY_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
CARD_TYPE VARCHAR2(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE NOT NULL,
PAY_CONF INTEGER NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY(
DEL_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
SHIP_DATE DATE, -- Delete this line
DEL_DATE DATE NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
DEL_MODE VARCHAR2(10) NOT NULL,
INVOICE_NO INTEGER NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS,
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (ORDER_NO, SHIP_DATE) REFERENCES PRODUCTS (ORDER_NO, SHIP_DATE) -- Delete this line.
);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT(
STREET_ADDRESS VARCHAR2(10),
NAME VARCHAR2(10) NOT NULL,
CITY VARCHAR2(10) NOT NULL,
STATE VARCHAR2(10) NOT NULL,
ZIP INTEGER NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
db<>fiddle here

I'm getting "Invalid Identifier Error" for one of my Oracle Apex Queries

I'm trying to run a script on Oracle Apex and so far all the tables and queries work except the last one. It returns the error "ORA-00904: : invalid identifier ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 626 ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658 ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 612 ORA-06512: at "APEX_200200.WWV_FLOW_DYNAMIC_EXEC", line 1749." What should I do to fix this error?
CREATE TABLE customer(
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
sale_date DATE
CONSTRAINT sales__sale_date__fk
REFERENCES sales (sale_date) ON DELETE SET NULL,
c_name VARCHAR2(50)
CONSTRAINT sales__c_name__nn NOT NULL,
address VARCHAR2(50)
CONSTRAINT sales__address__nn NOT NULL,
phone VARCHAR2(11)
CONSTRAINT sales__phone__nn NOT NULL,
gender VARCHAR2(6)
CONSTRAINT sales__gender__nn NOT NULL,
a_income VARCHAR2(30)
CONSTRAINT sales__a_income__nn NOT NULL,
);
I don't know if it helps but VIN and sale_date reference these two working queries:
CREATE TABLE vehicles(
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__pk PRIMARY KEY,
brand VARCHAR2(20)
CONSTRAINT vehicles__brand__nn NOT NULL,
model VARCHAR2(20)
CONSTRAINT vehicles__model__nn NOT NULL,
color VARCHAR2(10)
CONSTRAINT vehicles__color__nn NOT NULL
);
CREATE TABLE sales(
sale_date DATE,
price VARCHAR2(30)
CONSTRAINT sales__price__nn NOT NULL,
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
d_id VARCHAR2(10)
CONSTRAINT dealer__d_id__fk
REFERENCES dealer (d_id) ON DELETE SET NULL,
CONSTRAINT sales__sale_date__pk PRIMARY KEY (sale_date)
);
Remove the last comma.
Also, if your constraints have the naming convention <tablename>__<columnname>__<constrainttype> then don't just copy/paste from another table and update the column name; you need to update the table name as well or you will find you have duplicate constraint names which will raise an exception.
CREATE TABLE customer(
VIN VARCHAR2(17)
CONSTRAINT customer__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
sale_date DATE
CONSTRAINT customer__sale_date__fk
REFERENCES sales (sale_date) ON DELETE SET NULL,
c_name VARCHAR2(50)
CONSTRAINT customer__c_name__nn NOT NULL,
address VARCHAR2(500)
CONSTRAINT customer__address__nn NOT NULL,
phone VARCHAR2(11)
CONSTRAINT customer__phone__nn NOT NULL,
gender CHAR(1)
CONSTRAINT customer__gender__nn NOT NULL
CONSTRAINT customer__gender__chk
CHECK ( gender IN ( 'M', 'F' /*, 'A', 'B', 'C'*/ ) ),
a_income NUMBER(12,2)
CONSTRAINT customer__a_income__nn NOT NULL
);
Then comes the other questions:
Why does a customer have a VIN (vehicle identification number)? A customer is not limited to owning a single car.
Why does a customer have a sale_date? If you are referring to a car sale then why is the customer limited to a single sale? You probably want to fix both these two by moving the data to a separate table called customer_cars (or something similar) so that each customer can own multiple cars and each car can be owned by multiple customers (at different times).
Do you expect all customers' addresses to fit in 50 characters?
Why is gender a VARCHAR(6) and not a CHAR(1) with values M/F (extend with additional character codes as appropriate)?
Why is a_income a string and not a NUMBER?

Simple SQL issues, cannot figure out what I'm doing wrong to get these errors - ORA-00942 / 907 / 922

This is a supposed to be a simple SQL project, but I'm stuck on the first step. I've never worked with SQL before, so I'm pretty lost. Can someone please tell me what I did wrong that's causing these errors?
Here is the file:
SPOOL output.log;
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id));
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE));
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL));
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL));
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL));
SPOOL OFF;
The errors for the DROP statements are to be expected because the tables do not yet exist. You can safely ignore them.
Most of your problems come from the second closing parenthesis, e.g.
CREATE TABLE movie (
...
cat_mov_id CHAR(10) NOT NULL UNIQUE));
^ ---- here
You need to remove them. It is only needed in the first statement because of the column list of the foreign key.
The first (real) error you get is because you misspelled CONSTRAINT (you wrote CONTRAINT. The full clause needs to be
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
^ -- the "s" was missing here
However you can't create a foreign key constraint to a table that does not exist. So you need to change the order of the create statements to first create the customer table, then you can create the rental table.
Putting all that together, your script should look like this:
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL
);
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE
);
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL
);
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL
);
Unrelated, but: you do not want to use the CHAR data type. It's more efficient and will give you less headaches if you use VARCHAR for every character column.
Of course the first time you run the script your DROP TABLEs will fail because you haven't created them yet. That's to be expected and is acceptable in such a script.
Beyond that, if you are going to create FK relationships, you need to create the parent table before you create the child, which you will see after you correct your misspelled key word.
I will leave it as an exercise for the student to take that clue and locate the mis-spelled key word in the first CREATE TABLE statement.

Oracle table create error ORA-00904 invalid identifier

It's very interesting I don't know why I'm getting ORA-00904 invalid identifier when I'm trying to create a table with oracle.
CREATE TABLE animals
(
CONSTRAINT animal_id NUMBER(6) PRIMARY_KEY,
name VARCHAR2(25),
CONSTRAINT license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
When creating tables with CREATE TABLE in Oracle, you have at least four ways to specify constraints.
In-line specification
CREATE TABLE animals
(
animal_id NUMBER(6) PRIMARY KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
In-line specification with explicit constraints' names
CREATE TABLE animals
(
animal_id NUMBER(6) CONSTRAINT animal_id_pk PRIMARY KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) CONSTRAINT animal_tag_no_uq UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
Out-line specification
CREATE TABLE animals
(
animal_id NUMBER(6) ,
name VARCHAR2(25),
license_tag_number NUMBER(10),
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL,
PRIMARY KEY (animal_id),
UNIQUE (license_tag_number)
);
Out-line specification with explicit constraints' names
CREATE TABLE animals
(
animal_id NUMBER(6) ,
name VARCHAR2(25),
license_tag_number NUMBER(10),
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL,
CONSTRAINT animal_id_pk PRIMARY KEY (animal_id),
CONSTRAINT animal_tag_no_uq UNIQUE (license_tag_number)
);
If you don't explicitly specify constraints names, they are generated automatically by the system, and read something like SYS_C0013321. I find the last way the most readable, because you see which constraints are created, and can manage them using user-friendly names (e. g. using view user_constraints).
By the way, there's a typo in your code: you should use PRIMARY KEY instead of PRIMARY_KEY.
This is correct code, you must remove CONSTRAINT word:
CREATE TABLE animals
(
animal_id NUMBER(6) PRIMARY KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
but you can also use constraints for PK and UNIQUE as below:
CREATE TABLE animals
(
animal_id NUMBER(6) not null,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL,
CONSTRAINT animals_PK PRIMARY KEY (animal_id) ,
CONSTRAINT l_tag_number_uq UNIQUE (license_tag_number)
);
It is good practice to use constraints because they give you a friendly name/short description.
SQL Fiddle DEMO
I think there some mistake here :
Catch this example :
CREATE TABLE "name_of_table"
("column_1" "data_type",
"column_2" "data_type",
"column_3" "data_type",
CONSTRAINT column_name PRIMARY KEY (column_1, column_2)
... );
Your code suppose :
CREATE TABLE animals(
CREATE TABLE animals
(
animal_id NUMBER(6) PRIMARY_KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
You may check the example here
I am creating a table of employee
Create table employee ( emp_id number(10), emp_name varchar(10), salary number(10));
I am getting an error of invalid identifier