Related
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 have ran this Script File on Oracle Application Express and it does not create my tables and does not state that I have errors. If you can't help that is okay, but I have been at this for hours and cannot figure out why it is not working
Here is my Script File:
CREATE TABLE GUEST_T
(GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR(25),
GUEST_ADDRESS VARCHAR(30),
CONSTRAINT GUEST_PK PRIMARY KEY (GUEST_ID));
CREATE TABLE COMPANY_T
(GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR(25),
COMPANY_NAME VARCHAR(25),
ADDRESS VARCHAR(30),
NO_OF_GUESTS INT,
CONSTRAINT COMPANY_PK PRIMARY KEY (GUEST_ID),
CONSTRAINT COMPANY_FK FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID));
CREATE TABLE ACCOMMODATION_T
(ACCOMMODATION_ID NUMBER NOT NULL,
ACCOMMODATION_TYPE VARCHAR(25),
PRICE DECIMAL (6,2),
CONSTRAINT ACCOMMODATION_PK PRIMARY KEY (ACCOMMODATION_ID));
CREATE TABLE EMPLOYEE_T
(EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR(25),
HOURS_WORKED INT,
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (EMPLOYEE_ID));
CREATE TABLE RECEPTIONIST_T
(EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR(25),
HOURS_WORKED INT,
HOURLY_WAGE DECIMAL (4,2),
CONSTRAINT RECEPTIONIST_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT EMPLOYEE_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE MANAGER_T
(EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR(25),
HOURS_WORKED INT,
SALARY DECIMAL (10,2)
CONSTRAINT MANAGER_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT EMPLOYEE_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE ACCOMMODATION_REQUEST_T
(ACCOMMODATION_ID NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
TIME REQUESTED NUMBER,
DATE_OF_USE DATE DEFAULTSYSDATE,
CONSTRAINT ACCOMMODATION_REQUEST_PK PRIMARY KEY (ACCOMMODATION_ID, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK1 FOREIGN KEY (ACCOMMODATION_ID) REFERENCES ACCOMMODATION_T(ACCOMMODATION_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE ROOM_T
(ROOM_NO NUMBER NOT NULL,
FLOOR_NO NUMBER,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR(20) CHECK (ROOM_STATUS IN (‘Vacant’, ‘Occupied’)),
CONSTRAINT ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT ROOM_FK FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T(FLOOR_NO));
CREATE TABLE DOUBLE_ROOM_T
(ROOM_NO NUMBER NOT NULL,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR(20) CHECK (ROOM_STATUS IN (‘Vacant’, ‘Occupied’)),
ADJOINED VARCHAR(5) CHECK (ADJOINED IN (‘Yes’, ’No’)),
CONSTRAINT DOUBLE_ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT DOUBLE_ROOM_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T (ROOM_NO),
CONSTRAINT DOUBLE_ROOM_FK2 FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T (FLOOR_NO));
CREATE TABLE RESERVATION_T
(ROOM_NO NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
CHECK_IN_DATE DATE DEFAULTSYSDATE,
CHECK_OUT_DATE DATE DEFAULTSYSDATE,
NO_OF_ROOMS INT,
CONSTRAINT RESERVATION_PK PRIMARY KEY (ROOM_NO, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT RESERVATION_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T(ROOM_NO),
CONSTRAINT RESERVATION_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT RESERVATION_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID));
CREATE TABLE FLOOR_T
(FLOOR_NO NUMBER NOT NULL,
NO_OF_ROOMS INT,
CONSTRAINT FLOOR_PK PRIMARY KEY (FLOOR_NO));
CREATE TABLE FACILITY_T
(FACILITY_ID NUMBER NOT NULL,
FACILITY_NAME VARCHAR(25),
CONSTRAINT FACILITY_PK PRIMARY KEY (FACILITY_ID));
I have run this Script File on Oracle Application Express and it does not create my tables and does not state that I have errors. If you can't help that is okay, but I have been at this for hours and cannot figure out why it is not working.
There are a lot of issues in your script (and they do show up when the scripts runs). The most proeminent ones are:
the same constraint name is used in several tables; a constraint name must be unique in a schema, you can prefix them with the table name to avoid clashes
various typos : missing underscore, missing spaces, missing commas
funky quotes ‘’ instead of regular quotes
wrong table creation sequence: a parent table must be created before its dependant tables
side note: you want to use VARCHAR2, that Oracle recommends to replace VARCHAR
Here is a new version of your script that works fine in this DB fiddle. All the changes are commented:
CREATE TABLE GUEST_T (
GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR2(25),
GUEST_ADDRESS VARCHAR2(30),
CONSTRAINT GUEST_PK PRIMARY KEY (GUEST_ID)
);
CREATE TABLE COMPANY_T (
GUEST_ID NUMBER NOT NULL,
GUEST_NAME VARCHAR2(25),
COMPANY_NAME VARCHAR2(25),
ADDRESS VARCHAR2(30),
NO_OF_GUESTS INT,
CONSTRAINT COMPANY_PK PRIMARY KEY (GUEST_ID),
CONSTRAINT COMPANY_FK FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID)
);
CREATE TABLE ACCOMMODATION_T (
ACCOMMODATION_ID NUMBER NOT NULL,
ACCOMMODATION_TYPE VARCHAR2(25),
PRICE DECIMAL (6,2),
CONSTRAINT ACCOMMODATION_PK PRIMARY KEY (ACCOMMODATION_ID)
);
CREATE TABLE EMPLOYEE_T (
EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR2(25),
HOURS_WORKED INT,
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (EMPLOYEE_ID)
);
CREATE TABLE RECEPTIONIST_T (
EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR2(25),
HOURS_WORKED INT,
HOURLY_WAGE DECIMAL (4,2),
CONSTRAINT RECEPTIONIST_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT RECEPTIONIST_T_EMPLOYEE_FK -- constraint name must be unique
FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
CREATE TABLE MANAGER_T (
EMPLOYEE_ID NUMBER NOT NULL,
EMPLOYEE_NAME VARCHAR2(25),
HOURS_WORKED INT,
SALARY DECIMAL (10,2), -- missing comma here
CONSTRAINT MANAGER_PK PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT MANAGER_T_EMPLOYEE_FK -- constraint name must be unique
FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
CREATE TABLE ACCOMMODATION_REQUEST_T (
ACCOMMODATION_ID NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
TIME_REQUESTED NUMBER, -- missing underscore between TIME and REQUESTED
DATE_OF_USE DATE DEFAULT SYSDATE, -- missing space between DEFAULT and SYSDATE
CONSTRAINT ACCOMMODATION_REQUEST_PK PRIMARY KEY (ACCOMMODATION_ID, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK1 FOREIGN KEY (ACCOMMODATION_ID) REFERENCES ACCOMMODATION_T(ACCOMMODATION_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT ACCOMMODATION_REQUEST_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
-- must be created before the dependant tables (ROOM_T, DOUBLE_ROOM_T, ...)
CREATE TABLE FLOOR_T (
FLOOR_NO NUMBER NOT NULL,
NO_OF_ROOMS INT,
CONSTRAINT FLOOR_PK PRIMARY KEY (FLOOR_NO)
);
CREATE TABLE ROOM_T (
ROOM_NO NUMBER NOT NULL,
FLOOR_NO NUMBER,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR2(20) CHECK (ROOM_STATUS IN ('Vacant', 'Occupied')), -- funky quotes ‘’
CONSTRAINT ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT ROOM_FK FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T(FLOOR_NO)
);
CREATE TABLE DOUBLE_ROOM_T (
ROOM_NO NUMBER NOT NULL,
ROOM_PRICE DECIMAL (8,2),
ROOM_STATUS VARCHAR2(20) CHECK (ROOM_STATUS IN ('Vacant', 'Occupied')), -- funky quotes ‘’
ADJOINED VARCHAR2(5) CHECK (ADJOINED IN ('Yes', 'No')), -- funky quote ‘’
CONSTRAINT DOUBLE_ROOM_PK PRIMARY KEY (ROOM_NO),
CONSTRAINT DOUBLE_ROOM_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T (ROOM_NO)
-- CONSTRAINT DOUBLE_ROOM_FK2 FOREIGN KEY (FLOOR_NO) REFERENCES FLOOR_T (FLOOR_NO) -- there is no such column
);
CREATE TABLE RESERVATION_T (
ROOM_NO NUMBER NOT NULL,
GUEST_ID NUMBER NOT NULL,
EMPLOYEE_ID NUMBER NOT NULL,
CHECK_IN_DATE DATE DEFAULT SYSDATE, -- missing space between DEFAULT and SYSDATE
CHECK_OUT_DATE DATE DEFAULT SYSDATE, -- missing space between DEFAULT and SYSDATE
NO_OF_ROOMS INT,
CONSTRAINT RESERVATION_PK PRIMARY KEY (ROOM_NO, GUEST_ID, EMPLOYEE_ID),
CONSTRAINT RESERVATION_FK1 FOREIGN KEY (ROOM_NO) REFERENCES ROOM_T(ROOM_NO),
CONSTRAINT RESERVATION_FK2 FOREIGN KEY (GUEST_ID) REFERENCES GUEST_T(GUEST_ID),
CONSTRAINT RESERVATION_FK3 FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE_T(EMPLOYEE_ID)
);
CREATE TABLE FACILITY_T (
FACILITY_ID NUMBER NOT NULL,
FACILITY_NAME VARCHAR2(25),
CONSTRAINT FACILITY_PK PRIMARY KEY (FACILITY_ID)
);
I am trying to run a SQL script to my database created on Java Derby:
CREATE TABLE USUARIO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
EMAIL VARCHAR(40) NOT NULL UNIQUE,
NOMBRES VARCHAR(20) NOT NULL,
APELLIDOS VARCHAR(20) NOT NULL,
CONTRASEÑA VARCHAR(20) NOT NULL,
CEDULA INTEGER,
TELEFONO INTEGER,
CONSTRAINT ID_USUARIO_PK PRIMARY KEY (ID) -- Primary Key
);
CREATE TABLE ORGANIZACION (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
NOMBRE VARCHAR(20) NOT NULL,
CONSTRAINT ID_ORGANIZACION_PK PRIMARY KEY (ID),
CONSTRAINT ID_DIRECCION_ORG_FK FOREIGN KEY (DIRECCION_ORG) REFERENCES DIRECCION_ORG (ID),
CONSTRAINT ID_TELEFONO_ORG_FK FOREIGN KEY (TELEFONO_ORG) REFERENCES TELEFONO_ORG (ID)
);
CREATE TABLE TELEFONO_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
TELEFONO INTEGER NOT NULL,
CONSTRAINT ID_TELEFONO_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE DIRECCION_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
DIRECCION VARCHAR(300) NOT NULL,
CONSTRAINT ID_DIRECCION_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE PRODUCTO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
RUTA_IMAGEN VARCHAR(400) NOT NULL,
NOMBRE VARCHAR(20) NOT NULL,
CANTIDAD INTEGER,
PRECIO INTEGER,
COSTO INTEGER,
CONSTRAINT ID_PRODUCTO_PK PRIMARY KEY (ID), -- Primary Key
CONSTRAINT ID_ORGANIZACION_FK FOREIGN KEY (ORGANIZACION) REFERENCES ORGANIZACION (ID)
);
But I am getting this error:
Table ORGANIZACION contains a constraint definition with column
DIRECCION_ORG which is not in the table.
What can be wrong here?
You are making constraints to foreign keys but haven't actually created the columns to contain those keys yet. Make those columns and make the constraints point to them (instead of just using the table's name again). Shown below with -- comments indicating where to add it.
CREATE TABLE USUARIO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
EMAIL VARCHAR(40) NOT NULL UNIQUE,
NOMBRES VARCHAR(20) NOT NULL,
APELLIDOS VARCHAR(20) NOT NULL,
CONTRASEÑA VARCHAR(20) NOT NULL,
CEDULA INTEGER,
TELEFONO INTEGER,
CONSTRAINT ID_USUARIO_PK PRIMARY KEY (ID) -- Primary Key
);
CREATE TABLE ORGANIZACION (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
NOMBRE VARCHAR(20) NOT NULL,
DIRECCION_ORG_ID INTEGER NOT NULL, -- ADD THIS and change constraint FK name
TELEFONO_ORG_ID INTEGER NOT NULL, -- ADD THIS and change constraint FK name
CONSTRAINT ID_ORGANIZACION_PK PRIMARY KEY (ID),
CONSTRAINT ID_DIRECCION_ORG_FK FOREIGN KEY (DIRECCION_ORG_ID) REFERENCES DIRECCION_ORG (ID),
CONSTRAINT ID_TELEFONO_ORG_FK FOREIGN KEY (TELEFONO_ORG_ID) REFERENCES TELEFONO_ORG (ID)
);
CREATE TABLE TELEFONO_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
TELEFONO INTEGER NOT NULL,
CONSTRAINT ID_TELEFONO_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE DIRECCION_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
DIRECCION VARCHAR(300) NOT NULL,
CONSTRAINT ID_DIRECCION_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE PRODUCTO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
RUTA_IMAGEN VARCHAR(400) NOT NULL,
NOMBRE VARCHAR(20) NOT NULL,
CANTIDAD INTEGER,
PRECIO INTEGER,
COSTO INTEGER,
ORGANIZACION_ID INTEGER NOT NULL, --ADD THIS and change constraint FK name
CONSTRAINT ID_PRODUCTO_PK PRIMARY KEY (ID), -- Primary Key
CONSTRAINT ID_ORGANIZACION_FK FOREIGN KEY (ORGANIZACION_ID) REFERENCES ORGANIZACION (ID)
);
Both the columns DIRECCION_ORG and TELEFONO_ORG are not defined in the table ORGANIZACION
The same problem occurs in the table PRODUCTO where the column ORGANIZACION is not defined
You have an error in your CONSTRAINT clause.
The format of the FOREIGN KEY constraint is:
CONSTRAINT {constraint name} FOREIGN KEY ([This_table_column],...) REFERENCES [Other_table] ([Other_table_column], ...).
Read this: https://db.apache.org/derby/docs/10.1/ref/rrefsqlj13590.html#rrefsqlj13590
create table TiposPeliculas
(
id_tipoPelicula int identity (1, 1),
descipcion varchar (30)
constraint pk_tipoPelicula primary key(id_tipoPelicula)
)
create table Generos
(
id_genero int identity (1,1),
descripcion varchar(20)
constraint pk_generos primary key(id_genero)
)
create table Peliculas
(
id_pelicula int not null,
id_tipoPelicula int,
id_genero int,
titulo varchar (30),
duracion time,
idioma varchar (30),
sub bit
constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
constraint fk_tipoPeliculasDePeli foreign key (id_tipoPelicula) references TipoPeliculas (id_tipoPelicula)
)
The FOREIGN KEY for id_tipoPelicula throws an error:
foreign key "fk_tipoPeliculasDePeli" references invalid table "TipoPeliculas"
create table Peliculas
(
id_pelicula int not null,
id_tipoPelicula int,
id_genero int,
titulo varchar (30),
duracion time,
idioma varchar (30),
sub bit
constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
constraint fk_tiposPeliculasDePeli foreign key (id_tiposPelicula) references TiposPeliculas (id_tiposPelicula)
)
This will work the error in spelling mistake in relation name TiposPeliculas.
The error:
foreign key "fk_tipoPeliculasDePeli" references invalid table "TipoPeliculas"
It says, the foreign key is reference to invalid Table "TipoPeliculas", Which means table "TipoPeliculas" doesn't exists.
In your question the table name is "TiposPeliculas". Hence it seems you need to correct table name in reference.
Also the column name should be "id_tipoPelicula" instead of "id_tiposPelicula" in reference.
Run the below code
create table TiposPeliculas
(
id_tipoPelicula int identity (1, 1),
descipcion varchar (30)
constraint pk_tipoPelicula primary key(id_tipoPelicula)
)
create table Generos
(
id_genero int identity (1,1),
descripcion varchar(20)
constraint pk_generos primary key(id_genero)
)
create table Peliculas
(
id_pelicula int not null,
id_tipoPelicula int,
id_genero int,
titulo varchar (30),
duracion time,
idioma varchar (30),
sub bit
constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
constraint fk_tipoPeliculasDePeli foreign key (id_tipoPelicula) references TiposPeliculas (id_tipoPelicula)
)
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID)
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
Here is my code and I can not figure out why comes the error.
CREATE TABLE Customer_T
(CustomerID NUMBER(11,0) NOT NULL,
CustomerFname VARCHAR2(25),
CustomerMname VARCHAR2(25),
CustomerLname VARCHAR2(25),
CustomerAddress1 VARCHAR2(50),
CustomerAddress2 VARCHAR2(50),
CustomerCity VARCHAR2(25),
CustomerState CHAR(2),
CustomerZipCode NUMBER(5,0),
CustomerPhoneNumber NUMBER(11,0),
CONSTRAINT Customer_PK PRIMARY KEY (CustomerID)
);
CREATE TABLE Pool_T
(PoolID NUMBER(11,0) NOT NULL,
PoolAddress1 VARCHAR2(25),
PoolAddress2 VARCHAR2(25),
PoolCity VARCHAR2(25),
PoolState VARCHAR2(25),
PoolZipCode NUMBER(5,0),
TypeSurface VARCHAR2(9),
Gallon INTEGER,
Filters VARCHAR2(25),
VisitPerMonth INTEGER,
Dates DATE DEFAULT SYSDATE,
Fee NUMBER(11,0),
CONSTRAINT Pool_PK PRIMARY KEY (PoolID)
);
CREATE TABLE PoolVisit_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
LengthStay INTEGER,
CONSTRAINT Poolvisit_PK PRIMARY KEY (PoolID,ServiceDate)
CONSTRAINT PoolVisit_FK FOREIGN KEY (PoolID)
REFERENCES Pool_T (PoolID)
);
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID)
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
CREATE TABLE PoolChemical_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ChemicalName VARCHAR2(25) NOT NULL,
Quantity INTEGER,
Cost NUMBER(11,2),
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName)
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID)
CONSTRAINT PoolChemical_FK FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
);
CREATE TABLE Activity_T
(ActivityID NUMBER(11,0) NOT NULL,
ActivityDesc VARCHAR2(500),
CONSTRAINT Activity_PK PRIMARY KEY (ActivityID));
CREATE TABLE Chemical_T
(ChemicalName VARCHAR2(25) NOT NULL,
CONSTRAINT Chemical_PK PRIMARY KEY (ChemicalName));
The error isn't just in the table you highlighted, it's also in the one before, PoolVisit_T. In both, you're just missing a comma after the primary key constraint:
CREATE TABLE PoolVisit_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
LengthStay INTEGER,
CONSTRAINT Poolvisit_PK PRIMARY KEY (PoolID,ServiceDate), -- was missing comma
CONSTRAINT PoolVisit_FK FOREIGN KEY (PoolID)
REFERENCES Pool_T (PoolID)
);
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID), -- was missing
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
And you're missing two in the one after that:
CREATE TABLE PoolChemical_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ChemicalName VARCHAR2(25) NOT NULL,
Quantity INTEGER,
Cost NUMBER(11,2),
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName), -- was missing
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID), -- was missing
CONSTRAINT PoolChemical_FK FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
);
If you use an IDE like SQL Developer it will highlight syntax problems like this as you go.
But you still have issues; your PoolActivity_T foreign key:
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
is referring only to the PoolID, but the primary key on PoolVisit_T and the primary key has two columns, so that needs to be:
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID,ServiceDate)
REFERENCES PoolVisit_T(PoolID,ServiceDate),
And you've used the same FK name for two constraints, so the next bit is:
CONSTRAINT PoolActivity_FK2 FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
(but pick more descriptive FK names, perhaps), and you have to create the Activity_T table before this one.
And similar issues with PoolChemical_T:
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName),
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID,ServiceDate)
REFERENCES PoolVisit_T(PoolID,ServiceDate),
CONSTRAINT PoolChemical_FK2 FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
and create Chemical_T first.
SQL Fiddle with all tables created.