Foreign key constraint error during SQL Server table creation - sql

I am trying to create a table on SQL Server which has foreign key constraints.I checked all my parent tables and their primary keys match with my new foreign key constraints. Can someone help me out in resolving this issue. My DDL and error message are as below.
DDL
CREATE TABLE I_IPV_LOB_PROG_PROV_MO_METRIC_TRNS(
AARP_ORG_ID int NOT NULL,
LOB_ID int NOT NULL,
PGM_CAT_ID int NOT NULL,
PGM_ID int NOT NULL,
PROV_ID int NOT NULL,
CAT_OF_MEAS_ID int NOT NULL,
SUBCAT_OF_MEAS_ID int NOT NULL,
MEAS_ID int NOT NULL,
TYPE_OF_METRIC_ID int NOT NULL,
METRIC_VAL_ROLE_ID int NOT NULL,
MO_ID int NOT NULL,
OPER_TXT varchar(5) NULL,
METRIC_VAL decimal(19, 2) NOT NULL,
LST_UPD_DT datetime NOT NULL,
LOAD_DT datetime NULL,
LST_UPD_USERID char(20) NOT NULL,
CONSTRAINT PK79 PRIMARY KEY NONCLUSTERED (TYPE_OF_METRIC_ID, METRIC_VAL_ROLE_ID, AARP_ORG_ID, LOB_ID, PGM_CAT_ID, PGM_ID, PROV_ID, MEAS_ID, MO_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID),
CONSTRAINT RefI_IPV_TYPE_OF_METRIC_TRNS73 FOREIGN KEY (TYPE_OF_METRIC_ID)
REFERENCES I_IPV_TYPE_OF_METRIC_TRNS(TYPE_OF_METRIC_ID),
CONSTRAINT RefI_IPV_METRIC_VAL_ROLE_TRNS75 FOREIGN KEY (METRIC_VAL_ROLE_ID)
REFERENCES I_IPV_METRIC_VAL_ROLE_TRNS(METRIC_VAL_ROLE_ID),
CONSTRAINT RefI_IPV_LOB_PROG_PROV_MEAS_TRNS345 FOREIGN KEY (AARP_ORG_ID,LOB_ID,PGM_CAT_ID,PGM_ID,PROV_ID,MEAS_ID,CAT_OF_MEAS_ID,SUBCAT_OF_MEAS_ID)
REFERENCES I_IPV_LOB_PROG_PROV_MEAS_TRNS(AARP_ORG_ID,LOB_ID,PGM_CAT_ID,PGM_ID,PROV_ID,MEAS_ID,CAT_OF_MEAS_ID,SUB_CAT_OF_MEAS_ID)
)
go
Parent Table DDL
CREATE TABLE I_IPV_LOB_PROG_PROV_MEAS_TRNS(
AARP_ORG_ID int NOT NULL,
LOB_ID int NOT NULL,
PGM_CAT_ID int NOT NULL,
PGM_ID int NOT NULL,
PROV_ID int NOT NULL,
CAT_OF_MEAS_ID int NOT NULL,
SUBCAT_OF_MEAS_ID int NOT NULL,
MEAS_ID int NOT NULL,
LOAD_DT datetime NULL,
LST_UPD_USERID char(20) NOT NULL,
LST_UPD_DT datetime NOT NULL,
CONSTRAINT PK115 PRIMARY KEY NONCLUSTERED (MEAS_ID, PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID),
CONSTRAINT RefI_IPV_LOB_PROG_CAT_PROV_TRNS322 FOREIGN KEY (PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID)
REFERENCES I_IPV_LOB_PROG_CAT_PROV_TRNS(PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID),
CONSTRAINT RefI_IPV_MEAS_CAT_TRNS342 FOREIGN KEY (MEAS_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID)
REFERENCES I_IPV_MEAS_CAT_TRNS(MEAS_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID)
)
go
Error Msg
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'I_IPV_LOB_PROG_PROV_MEAS_TRNS' that match the referencing column list in the foreign key 'RefI_IPV_LOB_PROG_PROV_MEAS_TRNS345'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

One of the fields in the target table must be its primary key (best) or be very quickly findable via some index using the at least one of nthe fields you've listed. If there are no keys in your list it will have to read the whole table to find the specific row and this is not a good thing to do, so it won't.
Create a unique index on the target table containing some/all of the fields you're constraining and it should be OK.

Related

Error during foreign key creation: Invalid references

I have 2 tables and I want to create a foreign key constraint in the second table. This is what I tried:
Table 1:
CREATE TABLE REMINDER_RULE_M
(
REMINDER_RULE_M_D int IDENTITY(1,1) NOT NULL,
COMMUNICATION_MODE nvarchar(255) NOT NULL,
REMINDER_TO nvarchar(255) NOT NULL,
REMINDER_VALUE varchar(255) NOT NULL,
REMINDER_CONDITION varchar(255) NOT NULL,
REMINDER_TO_CUSTOM varchar(255)
)
Table 2:
CREATE TABLE REMINDER_AUDIT
(
REMINDER_AUDIT_D int IDENTITY(1,1) NOT NULL,
ACTION varchar(255) NOT NULL,
CONSTRAINT FK_b892318b20e5bbe162722ea5946
FOREIGN KEY (REMINDER_RULE_M_D)
REFERENCES REMINDER_RULE_M(REMINDER_RULE_M_D),
OLD_VALUE nvarchar(1024) NOT NULL,
NEW_VALUE nvarchar(1024) NOT NULL,
)
I get an error running the second SQL query:
Reason:
SQL Error [1769] [S0001]: Foreign key 'FK_b892318b20e5bbe162722ea5946' references invalid column 'REMINDER_RULE_M_D' in referencing table 'REMINDER_AUDIT'.
As the error clearly tells you - you don't have a column in your second table.
You must have a column in order to create a FK constraint - the FK constraint does NOT create a column in your table - it just establishes a constraint between existing tables and columns.
So try this for your second table:
CREATE TABLE REMINDER_AUDIT
(
REMINDER_AUDIT_D int IDENTITY(1,1) NOT NULL,
ACTION varchar(255) NOT NULL,
-- define the column!
REMINDER_RULE_M_D int NOT NULL,
-- I'd strongly recommend trying to come up with a more
-- intuitive and useful naming convention for your FK constraints!
CONSTRAINT FK_b892318b20e5bbe162722ea5946
FOREIGN KEY (REMINDER_RULE_M_D)
REFERENCES REMINDER_RULE_M(REMINDER_RULE_M_D),
OLD_VALUE nvarchar(1024) NOT NULL,
NEW_VALUE nvarchar(1024) NOT NULL,
)
I just guessed that REMINDER_RULE_M_D is NOT NULL - you might need to adapt this (if it's an optional key).
You do not need to write Foreign Key
CREATE TABLE REMINDER_AUDIT (
REMINDER_AUDIT_D int IDENTITY(1,1) NOT NULL,
ACTION varchar(255) NOT NULL,
CONSTRAINT FK_b892318b20e5bbe162722ea5946 REFERENCES REMINDER_RULE_M(REMINDER_RULE_M_D),
OLD_VALUE nvarchar(1024) NOT NULL,
NEW_VALUE nvarchar(1024) NOT NULL,
)

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

FOREIGN KEY references invalid

CREATE TABLE MEDICO (
DNIMedico CHAR(9) NOT NULL,
NumeroColegiado VARCHAR(200) NOT NULL,
Nombre VARCHAR(200) NOT NULL,
FechaNacimiento DATE NOT NULL,
CONSTRAINT pk_MEDICO PRIMARY KEY (DNIMedico)
)
CREATE TABLE PACIENTE (
DNIPaciente CHAR(9) NOT NULL,
Nombre VARCHAR(200) NOT NULL,
Direccion VARCHAR (200) NOT NULL,
Edad INT NOT NULL,
Peso FLOAT NOT NULL,
Altura FLOAT NOT NULL,
CONSTRAINT pk_PACIENTE PRIMARY KEY (DNIPaciente)
)
CREATE TABLE MEDICO_PACIENTE (
CONSTRAINT fkMP_MEDICO FOREIGN KEY (DNIMedico) REFERENCES
MEDICO (DNIMedico),
CONSTRAINT fkMP_PACIENTE FOREIGN KEY (DNIPaciente) REFERENCES
PACIENTE (DNIPaciente),
Especialidad VARCHAR(200) NOT NULL
)
Msg 1769, Level 16, State 1, Line 18
Foreign key 'fkMP_MEDICO' references invalid column 'DNIMedico' in referencing table 'MEDICO_PACIENTE'.
Msg 1750, Level 16, State 0, Line 18
Could not create constraint or index. See previous errors.
Declaring a foreign key does not automagically create a column. You need to declare the column first, then the foreign key.
Hence:
CREATE TABLE MEDICO_PACIENTE (
DNIMedico CHAR(9) NOT NULL,
DNIPaciente CHAR(9) NOT NULL,
Especialidad VARCHAR(200) NOT NULL,
CONSTRAINT fkMP_MEDICO FOREIGN KEY (DNIMedico) REFERENCES MEDICO (DNIMedico),
CONSTRAINT fkMP_PACIENTE FOREIGN KEY (DNIPaciente) REFERENCES PACIENTE (DNIPaciente)
);
Note that each refering columns should have the same datatype and length as the column they reference.
It would also be a good idea to declare a primary key for the MEDICO_PACIENTE table. Possibly, you want a compound primary key on (DNIMedico, DNIPaciente ):
CREATE TABLE MEDICO_PACIENTE (
DNIMedico CHAR(9) NOT NULL,
DNIPaciente CHAR(9) NOT NULL,
Especialidad VARCHAR(200) NOT NULL,
CONSTRAINT fkMP_MEDICO FOREIGN KEY (DNIMedico) REFERENCES MEDICO (DNIMedico),
CONSTRAINT fkMP_PACIENTE FOREIGN KEY (DNIPaciente) REFERENCES PACIENTE (DNIPaciente),
CONSTRAINT pk_MEDICO_PACIENTE PRIMARY KEY (DNIMedico, DNIPaciente)
);
Demo on DB Fiddle

There are no primary or candidate keys in the referenced table ... that match the referencing column list in the foreign key

I am getting this error while creating the table [dbo].[WeibullSummaryDetails].
These are my two tables
CREATE TABLE [dbo].[WeibullFilterDetails]
(
[WeibullFilterDetailsId] [int] IDENTITY(1,1) NOT NULL,
[ProjectTeamId] int not null,
[WeekStartDate] date not NULL,
[WeekEndDate] date not null ,
[IsRefreshed] bit NULL,
CONSTRAINT FK_WeibullFilterDetails_WeibullFilterDetails
FOREIGN KEY ([ProjectTeamId])
REFERENCES [dbo].[ProjectTeams]([Id]),
PRIMARY KEY ([ProjectTeamId], [WeibullFilterDetailsId])
)
CREATE TABLE [dbo].[WeibullSummaryDetails]
(
[WeibullSummaryDetailsId] [int] IDENTITY(1,1) NOT NULL,
[WeibullFilterDetailsId] int not null,
[ProjectTeamId] int not null,
[ActualEstimatedBugCount] int NULL,
[CurrentBugCount] int NULL,
[PercentageBugFound] float NULL,
[PercentageBugResolved] float NULL,
[BugsToFind] int NULL,
BugsToResolve int NULL,
LinearEquation nvarchar(100) null,
RSquare float NULL,
Shape float NULL,
Scale float NULL
PRIMARY KEY ([WeibullSummaryDetailsId], [WeibullFilterDetailsId],[ProjectTeamId]),
CONSTRAINT FK_WeibullSummaryDetails_WeibullFilterDetails
FOREIGN KEY ([WeibullFilterDetailsId],[ProjectTeamId])
REFERENCES [dbo].[WeibullFilterDetails]([WeibullFilterDetailsId],[ProjectTeamId])
)
Detailed error message
Msg 1776, Level 16, State 0, Line 14
There are no primary or candidate keys in the referenced table 'dbo.WeibullFilterDetails' that match the referencing column list in the foreign key 'FK_WeibullSummaryDetails_WeibullFilterDetails'.
Msg 1750, Level 16, State 0, Line 14
Could not create constraint. See previous errors.
I have seen other posts on this error, usually the solution given is that if parent table has a composite key, then both the columns should be present in the child table too and should be used for the foreign key constraint.
That is exactly what I am doing here, but still getting this error.
Help is greatly appreciated!
For [dbo].[WeibullFilterDetails] you defined the primary key as ([ProjectTeamId],[WeibullFilterDetailsId]), yet in your REFERENCES clause you wrote ([WeibullFilterDetailsId],[ProjectTeamId]) -- the order doesn't match. Try:
CREATE TABLE [dbo].[WeibullSummaryDetails](
...
FOREIGN KEY ([ProjectTeamId],[WeibullFilterDetailsId])
REFERENCES [dbo].[WeibullFilterDetails]([ProjectTeamId],[WeibullFilterDetailsId])
);

Run sql command line, issue. Creating tables

I'm trying to create a table in run sql command line of oracle database 11 xe.
My problem is when I finish typing my code:
create table vigilantes(
idVigilantes integer(3) not null,
nombre varchar(100) not null,
paterno varchar(100) not null,
materno varchar(100) not null,
id_caseta integer(3) null,
id_turno integer(3) null,
edad integer(3) not null,
id_genero integer(1) not null,
idEmpresa integer(3) not null,
constraint pk_idVigilantes PRIMARY KEY (idVigilantes)
constraint fk_id_caseta FOREIGN KEY (id_caseta)
references Caseta(id_caseta)
constraint fk_id_turno FOREIGN KEY(id_turno)
references Turno(id_turno)
constraint fk_id_genero FOREIGN KEY(id_genero)
references Generos(id_genero)
constraint fk_idEmpresa FOREIGN KEY(idEmpresa)
references Empresa(idEmpresa)
);
I get "ORA-00907: missing right parenthesis" issue.
I read that this is often caused by not defining a value.
e.g:
create table vigilantes(
idVigilantes integer not null,
.......
But still no solution here.
Any help or clue will be a lot of help.
You are missing comma after constraints. Also, integer has no precision.
create table vigilantes (
idVigilantes integer not null,
nombre varchar(100) not null,
paterno varchar(100) not null,
materno varchar(100) not null,
id_caseta integer null,
id_turno integer null,
edad integer not null,
id_genero integer not null,
idEmpresa integer not null,
constraint pk_idVigilantes primary key (idVigilantes),
constraint fk_id_caseta foreign key (id_caseta) references Caseta(id_caseta),
constraint fk_id_turno foreign key (id_turno) references Turno(id_turno),
constraint fk_id_genero foreign key (id_genero) references Generos(id_genero),
constraint fk_idEmpresa foreign key (idEmpresa) references Empresa(idEmpresa)
);
If you must define precision, use number datatype:
create table vigilantes (
idVigilantes number(3, 0) not null,
nombre varchar(100) not null,
paterno varchar(100) not null,
materno varchar(100) not null,
id_caseta number(3, 0) null,
id_turno number(3, 0) null,
edad number(3, 0) not null,
id_genero number(1, 0) not null,
idEmpresa number(3, 0) not null,
constraint pk_idVigilantes primary key (idVigilantes),
constraint fk_id_caseta foreign key (id_caseta) references Caseta(id_caseta),
constraint fk_id_turno foreign key (id_turno) references Turno(id_turno),
constraint fk_id_genero foreign key (id_genero) references Generos(id_genero),
constraint fk_idEmpresa foreign key (idEmpresa) references Empresa(idEmpresa)
);