SQL Srver foreign key error - sql

I have this assignment for beginning sql server to add foreign keys to these tables. The last ALTER TABLE command always throws an error
There are no primary or candidate keys in the referenced table 'TCases' that match the referencing column list...
and I really can't figure out why it's giving this error. Any insight is appreciated.
CREATE TABLE TCourtRooms
( intCourtRoomID INTEGER NOT NULL
,strCourtRoomNumber VARCHAR(50) NOT NULL
,strJudgeLastName VARCHAR(50) NOT NULL
,strJudgeFirstName VARCHAR(50) NOT NULL
,CONSTRAINT TCourtDockets_PK PRIMARY KEY (intCourtRoomID)
)
CREATE TABLE TCases
( intCourtRoomID INTEGER NOT NULL
,intCaseIndex INTEGER NOT NULL
,strCaseNumber VARCHAR(50) NOT NULL
,strDescription VARCHAR(50) NOT NULL
,CONSTRAINT TCases_PK PRIMARY KEY (intCourtRoomID, intCaseIndex)
)
CREATE TABLE TPersons
( intCourtRoomID INTEGER NOT NULL
,intCaseIndex INTEGER NOT NULL
,intPersonIndex INTEGER NOT NULL
,strLastName VARCHAR(50) NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strPersonRole VARCHAR(50) NOT NULL --Options are plaintiff or defendant
,CONSTRAINT TPlaintiffs_PK PRIMARY KEY (intCourtRoomID, intCaseIndex, intPersonIndex)
)
CREATE TABLE TLawyers
( intLawyerID INTEGER NOT NULL
,intCaseIndex INTEGER NOT NULL
,intPersonIndex INTEGER NOT NULL
,strLastName VARCHAR(50) NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strLawyerRole VARCHAR(50) NOT NULL --plaintiff or defendant
,CONSTRAINT TLawyers_PK PRIMARY KEY (intLawyerID, intCaseIndex, intPersonIndex)
,CONSTRAINT TLawyers_intLawyerID_strLawyerRole_UN UNIQUE (intLawyerID, strLawyerRole)
)
Problem 3.2 Identify and create foreign keys
-- Child Parent Column(s)
-- ----- ------ ---------
-- TCases TCourtRooms intCourtRoomID
-- TPersons TCases intCourtRoomID, intCaseIndex
-- TLawyers TCourtRooms
ALTER TABLE TCases
ADD CONSTRAINT TCases_TCourtRooms_FK
FOREIGN KEY (intCourtRoomID) REFERENCES TCourtRooms (intCourtRoomID)
ALTER TABLE TPersons
ADD CONSTRAINT TPersons_TCases_FK
FOREIGN KEY (intCourtRoomID, intCaseIndex) REFERENCES TCases (intCourtRoomID, intCaseIndex)
ALTER TABLE TLawyers
ADD CONSTRAINT TLawyers_TCases_FK
FOREIGN KEY (intCaseIndex) REFERENCES TCases (intCaseIndex)</code>

This is the primary key for TCases:
CONSTRAINT TCases_PK PRIMARY KEY (intCourtRoomID, intCaseIndex)
It is a composite primary key, with two parts. You need to include both keys in the declaration:
ALTER TABLE TLawyers
ADD CONSTRAINT TLawyers_TCases_FK
FOREIGN KEY (intCaseIndex) REFERENCES TCases (intCourtRoomId, intCaseIndex)
But, alas, you cannot because the field TLawyers.intCourtRoomId does not exist. I'm not sure what you should do:
Add the field to TLawyers.
Fix the primary key definition to reference only one column.
Do something else.
But that is your problem.

Related

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

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

Error There are no primary or candidate keys in the referenced table

I'm getting this error when trying to create a table with foreign key:
There are no primary or candidate keys in the referenced table 'TeamToPlayers' that match the referencing column list in the foreign key 'FKey2'.
I don't understand why, there is a primary key in the table TeamToPlayers.
Here are the queries:
create table TeamToPlayers
(TeamName varchar(50) NOT NULL,
PlayerName varchar(50) NOT NULL,
primary key(TeamName,PlayerName),
CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName)
)
create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES TeamToPlayers(PlayerName)
);
Table TeamToPlayers primary key consists of two fields - you must reference both as otherwise it's not a key. I think you may have your key the wrong way round - it should be on TeamToPlayers and referencing Players like so:
create table TeamToPlayers
(
TeamName varchar(50) NOT NULL,
PlayerName varchar(50) NOT NULL,
primary key(TeamName,PlayerName),
CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES Players(PlayerName)
)
create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
);

Table in Derby with complex primary key

Is it possible to have a primary key composed of foreign keys fk1, fk2, fk3
Where:
fk1 should never be null
fk2 and fk3 can be null
I have the following SQL:
CREATE TABLE role_mappings (
ROLE_NAME VARCHAR(64) NOT NULL,
NESTED_ROLE_NAME VARCHAR(64) DEFAULT NULL,
PRIVILEGE_NAME VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (ROLE_NAME, NESTED_ROLE_NAME, PRIVILEGE_NAME),
CONSTRAINT fk_access_roles_rm_1 FOREIGN KEY (ROLE_NAME) REFERENCES access_roles (ROLE_NAME),
CONSTRAINT fk_access_privileges FOREIGN KEY (PRIVILEGE_NAME) REFERENCES access_privileges(PRIVILEGE_NAME),
CONSTRAINT fk_access_roles_rm_2 FOREIGN KEY (NESTED_ROLE_NAME) REFERENCES access_roles(ROLE_NAME)
);
When I insert NULL into NESTED_ROLE_NAME or PRIVILEGE_NAME, I get:
Column 'NESTED_ROLE_NAME' cannot accept a NULL value.
What's the proper way of doing this?
Primary key values must be unique. If you allow null values into these fields that makeup the primary key you will quickly run into trouble with non-uniqueness. AVOId using null values would be my advice.

SQL - Missing right parenthesis

I am trying to execute this script in Oracle 11g and getting the following error, I dont know where I am missing the paranthesis or what is the mistake kindly help me figure this out.
Script:
CREATE TABLE User_Role (
user_role_id INT NOT NULL ,
Users_user_id INT FOREIGN KEY REFERENCES Users(user_id),
User_Types_user_type VARCHAR(20) FOREIGN KEY REFERENCES User_Types(user_type),
PRIMARY KEY(user_role_id)
)
Error:
ORA-00907: missing right parenthesi
Delete FOREIGN KEY clause. Rewrite your CREATE TABLE statement as follows:
CREATE TABLE User_Role (
user_role_id INT NOT NULL ,
Users_user_id INT REFERENCES Users(user_id),
User_Types_user_type VARCHAR(20) REFERENCES User_Types(user_type),
PRIMARY KEY(user_role_id)
)
In this case constraint names will be generated by Oracle. If you want to give them more meaningful names you could write your create table statement as follows:
CREATE TABLE User_Role1 (
user_role_id INT NOT NULL ,
Users_user_id INT ,
User_Types_user_type VARCHAR(20) ,
constraint PK_YourTable PRIMARY KEY(user_role_id),
constraint FK_Table_1 foreign key(Users_user_id) REFERENCES Users(user_id),
constraint FK_Table_2 foreign key(User_Types_user_type) REFERENCES User_Types(user_type)
)
I think you need to define the columns first and then add the FOREIGN KEY constraint in the very same manner as you are adding PRIMARY KEY constraint as below:
CREATE TABLE User_Role (
user_role_id INT NOT NULL ,
Users_user_id INT,
User_Types_user_type VARCHAR(20),
FOREIGN KEY (Users_user_id) REFERENCES Users(user_id),
FOREIGN KEY (User_Types_user_type) REFERENCES User_Types(user_type),
PRIMARY KEY(user_role_id)
)
You can specify foreign keys inline, you just need to remove the foreign key keyword:
CREATE TABLE User_Role
(
user_role_id INT NOT NULL ,
Users_user_id INT REFERENCES Users,
User_Types_user_type VARCHAR(20) REFERENCES User_Types,
PRIMARY KEY(user_role_id)
);
SQLFiddle example: http://sqlfiddle.com/#!4/4ca9f/1
Listing the primary key columns in the "references" part is optional. If you prefer, you can also write REFERENCES Users(user_id)
This format also has the disadvantage that the constraint names will be generated by Oracle (with a meaningless name). In order to be able to properly specify a constraint name for the foreign key, you need to use the syntax in the accepted answer.
remove the size of int and recompile
Example :-
integer (20) not null
integer not null
User_Types_user_type VARCHAR(20),
User_Types_user_type VARCHAR,

H2 database CREATE TABLE with constraint

I have two SQL statements:
CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
playerid1 INT NOT NULL REFERENCES players(playerid),
playerid2 INT NOT NULL REFERENCES players(playerid),
added TIMESTAMP AS CURRENT_TIMESTAMP NOT NULL);
ALTER TABLE legs ADD CONSTRAINT distinct_players CHECK(playerid1 <> playerid2);
I am 99% sure I should be able to condense them into one, i.e:
CREATE TABLE table(...
playerid2 INT NOT NULL REFERENCES players(playerid) CHECK(playerid1 <> playerid2),
...);
However, I am consistently getting a syntax error. AFAIK, this is where the constraint should be.
CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
playerid1 INT NOT NULL REFERENCES players(playerid),
playerid2 INT NOT NULL REFERENCES players(playerid),
added TIMESTAMP AS CURRENT_TIMESTAMP NOT NULL,
CHECK (playerid1 <> playerid2));