Table 'tableName' contains a constraint definition with column 'columnName' which is not in the table Java Derby - sql

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

Related

How to fix error "there is no unique constraint matching given keys for referenced table"

I am trying to create some tables in pgadmim.
Although in both tables tb_discipline and tb_round the discipline_id is the primary key I get the error:
there is no unique constraint matching given keys for the referenced table
"tb_round"
Adding the full code:
CREATE TABLE tb_discipline (
discipline_id INT NOT NULL,
name CHARACTER VARYING(50) NOT NULL,
inventor CHARACTER VARYING(50) NOT NULL,
type CHARACTER VARYING(10) NOT NULL,
object_type CHARACTER(20) DEFAULT NULL,
CONSTRAINT PK_tb_discipline PRIMARY KEY(discipline_id)
);
------------------------------------------------------------------------------------------------
--
-- Create table tb_athlete
--
------------------------------------------------------------------------------------------------
CREATE TABLE tb_athlete (
athlete_id CHARACTER(7) NOT NULL,
name CHARACTER VARYING(50) NOT NULL,
country CHARACTER(3) NOT NULL,
substitute_id CHARACTER (7),
CONSTRAINT PK_tb_athlete PRIMARY KEY(athlete_id),
CONSTRAINT FK_athlete_substitute FOREIGN KEY (substitute_id) REFERENCES tb_athlete(athlete_id)
);
------------------------------------------------------------------------------------------------
--
-- Create table tb_play
--
------------------------------------------------------------------------------------------------
CREATE TABLE tb_play (
athlete_id CHARACTER(7) NOT NULL,
discipline_id INT NOT NULL,
CONSTRAINT FK_play_athlete FOREIGN KEY (athlete_id) REFERENCES tb_athlete(athlete_id),
CONSTRAINT FK_play_discipline FOREIGN KEY (discipline_id) REFERENCES tb_discipline(discipline_id)
);
------------------------------------------------------------------------------------------------
--
-- Create table tb_round
--
------------------------------------------------------------------------------------------------
CREATE TABLE tb_round (
round_number INT NOT NULL,
discipline_id INT NOT NULL,
CONSTRAINT PK_tb_round PRIMARY KEY(round_number, discipline_id),
CONSTRAINT FK_round_discipline FOREIGN KEY (discipline_id) REFERENCES tb_discipline(discipline_id)
);
------------------------------------------------------------------------------------------------
--
-- Create table tb_register
--
------------------------------------------------------------------------------------------------
CREATE TABLE tb_register (
athlete_id CHARACTER(7) NOT NULL,
round_number INT NOT NULL,
discipline_id INT NOT NULL UNIQUE,
register_date DATE NOT NULL DEFAULT CURRENT_DATE,
register_position INT,
register_time TIME,
register_measure REAL,
CONSTRAINT PK_tb_register PRIMARY KEY(athlete_id,round_number,discipline_id),
CONSTRAINT FK_register_athlete FOREIGN KEY (athlete_id) REFERENCES tb_athlete(athlete_id),
CONSTRAINT FK_register_round_discipline FOREIGN KEY (discipline_id) REFERENCES tb_round(discipline_id),
CONSTRAINT FK_register_round_number FOREIGN KEY (round_number) REFERENCES tb_round(round_number)
);
Any idea how can I solve this?
You have two foreign keys in tb_register referencing round but only part of its key. Make that one referencing the complete key.
CREATE TABLE tb_register
(...
CONSTRAINT fk_register_round_number_discipline_id
FOREIGN KEY (round_number,
discipline_id)
REFERENCES tb_round
(round_number,
discipline_id)
...);

How to reference to primary key?

create database [PostaShpejte]
use PostaShpejte
create table Posta
(
ID_Posta int not null Primary Key,
Emri varchar(50)not null,
Qyteti varchar(15) not null,
)
create table Dergesa
(
ID_Dergesa int IDENTITY(1,1) not null Primary Key,
Emri_Dergeses varchar(30) not null,
Pershkrimi varchar(100),
Qmimi int not null,
Statusi varchar(30) not null,
CONSTRAINT CHK_Statusi CHECK (Statusi='E regjistruar' or Statusi='E nisur' or Statusi='Ne depo' or Statusi='E refuzuar' or Statusi='E derguar'),
)
create table Menaxhon
(
ID_Dergesa int not null references Dergesa (ID_Dergesa),
ID_Posta int not null references Posta(ID_Posta),
Primary Key(ID_Dergesa,ID_Posta),
)
--drop table TelBleresi
create table TelBleresi
(
ID_Tel_Bleresi int not null,
--ID_Bleresi int not null,
NumriTel int not null Unique,
Primary Key(ID_Tel_Bleresi),
)
--drop table Bleresi
create table Bleresi
(
ID_Bleresi int not null,
ID_Tel_Bleresi int not null,
Emri varchar(20) not null,
Mbiemri varchar(20) not null,
Shteti varchar(20) not null,
Qyteti varchar(20) not null,
Rruga varchar(50) not null,
ZIPKodi int not null,
FOREIGN KEY(ID_Tel_Bleresi) references TelBleresi(ID_Tel_Bleresi),
Primary Key (ID_Bleresi , ID_Tel_Bleresi),
)
create table Dergohet
(
ID_Dergesa int not null,
ID_Bleresi int not null,
Data_e_regj date not null,
Data_e_mbrritjes date not null,
----------------PROBLEM HERE---------------------------
FOREIGN KEY (ID_Dergesa) references Dergesa(ID_Dergesa),
FOREIGN KEY (ID_Bleresi) references **Bleresi**(ID_Bleresi),
*Error: There are no primary or candidate key to table Bleresi ....*
---------------------------------------------------------
PRIMARY KEY (ID_Dergesa,ID_Bleresi),
)
Bleresi has a compound primary key (ID_Bleresi, ID_Tel_Bleresi), so you need to reference all columns. That means adding ID_Tel_Bleresi to Dergohet.
create table Dergohet(
ID_Dergesa int not null,
ID_Bleresi int not null,
ID_Tel_Bleresi int not null, -- add this column
Data_e_regj date not null,
Data_e_mbrritjes date not null,
FOREIGN KEY (ID_Dergesa) references Dergesa(ID_Dergesa),
-- Reference the full compound key
FOREIGN KEY (ID_Bleresi, ID_Tel_Bleresi) references Bleresi(ID_Bleresi, ID_Tel_Bleresi),
PRIMARY KEY (ID_Dergesa,ID_Bleresi),
)
While they have some uses, compound primary keys are annoying as they create a proliferation of foreign key columns and complicate indexing. Some of yours seem unnecessary: Bleresi already has a ID_Bleresi, is that not unique?
In general, I'd recommend using simple big integer (2 billion creeps up on you surprisingly fast) auto incrementing primary keys. If you need to guarantee other uniquenesses, make a unique index.
The error say that ID_Bleresi is not the primary key on Bleresi table and for that can't be a foreign key. The primary key is:
Primary Key (ID_Bleresi , ID_Tel_Bleresi)
If ID_Bleresi is not a unique column, I recommend that in the table, you create a new unique column that is the primary key. In case it is, it would be best to set ID_Bleresi as the unique primary key

Add constraint to compare two attributes of different tables?

Here are my tables. I need to check that the 'program' attribute referenced in StudentsBranch with the table Students and the table Branches is the same. How can I do it?
CREATE TABLE Programmes (
name VARCHAR(200) UNIQUE NOT NULL,
CONSTRAINT pk_Programmes PRIMARY KEY (name)
);
CREATE TABLE Students (
id NUMERIC(10,0) UNIQUE NOT NULL,
program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (idnr),
FOREIGN KEY (program) REFERENCES Programmes(name)
);
CREATE TABLE Branches (
name VARCHAR(200) UNIQUE NOT NULL,
program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Branches PRIMARY KEY (name, program),
FOREIGN KEY (program) REFERENCES Programmes(name)
);
CREATE TABLE StudentsBranch (
student NUMERIC(10,0) NOT NULL,
program VARCHAR(200) NOT NULL,
branch VARCHAR(200) NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (student),
/* Below how the foreign keys I think should be */
FOREIGN KEY (student, program) REFERENCES Students(idnr, program),
FOREIGN KEY (branch, program) REFERENCES Branches(name, program)
/* I need to add a constraint to verify that the 'program' in Students
* and the 'program' in Branches are equivalent. How?
*/
);
You can't write that constraint with that existing database model you have now.
The only way I see you could do it, is by changing the primary key of Students to (id, program):
CREATE TABLE Students (
id NUMERIC(10,0) UNIQUE NOT NULL,
program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (id, program),
FOREIGN KEY (program) REFERENCES Programmes(name)
);
Then the table StudentsBranch could naturally enforce both FKs with using the single column program, as in:
CREATE TABLE StudentsBranch (
student NUMERIC(10,0) NOT NULL,
program VARCHAR(200) NOT NULL,
branch VARCHAR(200) NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (student),
FOREIGN KEY (student, program) REFERENCES Students (id, program),
FOREIGN KEY (branch, program) REFERENCES Branches (name, program)
);
It is always a good idea to have an numeric column of primary key and have primary key for every table.
Once you have primary key for every table you can referece primary key of a specific table and refere it as foerign key.
CREATE TABLE Programmes (
ID INT,
name VARCHAR(200) UNIQUE NOT NULL,
CONSTRAINT pk_Programmes PRIMARY KEY (ID)
);
CREATE TABLE Students(
id INT,
ProgrammID INT NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (ID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(ID)
);
CREATE TABLE Branches (
BranchID INT,
ProgrammID INT NOT NULL,
name VARCHAR(200) UNIQUE NOT NULL,
--program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Branches PRIMARY KEY (BranchID, ProgrammID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(ID)
);
CREATE TABLE StudentsBranch (
StudentsBranchID INT,
studentID INT NOT NULL,
ProgrammID INT NOT NULL,
BranchID INT NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (StudentsBranchID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(id),
FOREIGN KEY (studentID) REFERENCES Students(id),
FOREIGN KEY (BranchID, ProgrammID) REFERENCES Branches(BranchID, ProgrammID)
);

SQL Server : error creating two foreign keys on one table

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

SQL: create table with composite referencing primary key, and foreign key

So I have the following ER diagram:
So Subthing is a generalization. Stuff is either made out of Metal or Wood, it can't be just a Subthing. Here's the create code:
CREATE TABLE Thing (
ThingID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE Subthing (
consists_of INTEGER REFERENCES Thing(ThingID),
SubthingID INTEGER NOT NULL,
PRIMARY KEY (SubthingID, consists_of)
);
CREATE TABLE Wood (
Wstuff VARCHAR(40) NOT NULL,
consists_of INTEGER NOT NULL,
SubthingID INTEGER NOT NULL,
FOREIGN KEY (SubthingID, consists_of) REFERENCES Subthing(SubthingID, consists_of),
PRIMARY KEY (SubthingID, consists_of)
);
CREATE TABLE Dust (
DustID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE Metal (
Mstuff VARCHAR(40) NOT NULL,
consists_of INTEGER NOT NULL,
SubthingID INTEGER NOT NULL,
requires INTEGER NOT NULL REFERENCES Dust(DustID),
FOREIGN KEY (SubthingID, consists_of) REFERENCES Subthing(SubthingID, consists_of),
PRIMARY KEY (SubthingID, consists_of)
);
What I want is for Metal to, at the same time, have a primary key which references (SubthingID, consists_of) and a foreign key with requires, referencing DustID. I can't declare it as a foreign key after the fact since (SubthingID, consists_of) is already a foreign key.
How could you solve this, other than making requires into a table?
Never mind, I was too stupid.
CREATE TABLE Metal (
Mstuff VARCHAR(40) NOT NULL,
consists_of INTEGER NOT NULL,
SubthingID INTEGER NOT NULL,
requires INTEGER NOT NULL,
FOREIGN KEY (requires) REFERENCES Dust(DustID),
FOREIGN KEY (SubthingID, consists_of) REFERENCES Subthing(SubthingID, consists_of),
PRIMARY KEY (SubthingID, consists_of)
);
Works just fine.