Recover Foreign keys - sql

CREATE TABLE Infrastructure.OBSModules(
OBSId int FOREIGN KEY REFERENCES Infrastructure.OBS(Id) NOT NULL,
ModuleId int FOREIGN KEY REFERENCES Infrastructure.Module(Id) NOT NULL,
Constraint PK_Infrastructure_OBS_Module Primary Key(OBSId,ModuleId)
)
Go
The above code is my code by which I created Infrastructure.OBSType table,but I deleted foreign keys manually so I cant Insert any rows in my table.how can I recover those foreign keys?

Related

showing error for creating table with foreign key

CREATE TABLE location (
uid int not null auto_increment primary key,
name varchar(255) NOT NULL,
`state_uid` int not null,
FOREIGN KEY location(state_uid)
REFERENCES state(uid)
ON UPDATE CASCADE
ON DELETE RESTRICT,
`city_uid` int not null,
FOREIGN KEY location(city_uid)
REFERENCES city(uid)
ON UPDATE CASCADE
ON DELETE RESTRICT,
`area_uid` int not null,
FOREIGN KEY location(area_uid)
REFERENCES area(uid)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
CREATE TABLE location (
uid int not null auto_increment primary key,
name varchar(255) NOT NULL,
state_uid int not null,
city_uid int not null,
area_uid int not null,
CONSTRAINT fk_state FOREIGN KEY (state_uid) REFERENCES state(uid) ,
CONSTRAINT fk_city FOREIGN KEY (city_uid) REFERENCES city(uid) ,
CONSTRAINT fk_area FOREIGN KEY (area_uid) REFERENCES area(uid)
);
Try this query
make sure parent tables exists
Your version is almost just fine. The problem is the location in the foreign key reference.
You have no issue with the cascading stuff or the ordering of the columns. So, this works:
CREATE TABLE location (
uid int not null auto_increment primary key,
name varchar(255) NOT NULL,
`state_uid` int not null,
FOREIGN KEY (state_uid) REFERENCES state(uid) ON UPDATE CASCADE ON DELETE RESTRICT,
`city_uid` int not null,
FOREIGN KEY (city_uid) REFERENCES city(uid) ON UPDATE CASCADE ON DELETE RESTRICT,
`area_uid` int not null,
FOREIGN KEY (area_uid) REFERENCES area(uid) ON UPDATE CASCADE ON DELETE RESTRICT
);
Here is a SQL Fiddle.
Note that it is often traditional to put explicit foreign key (and other constraints) after the column definitions, there is no rule or standard for this. In fact, most databases support in-line foreign key definitions. MySQL does not, however.

Oracle error building table from 2 foreign keys

I'm in the process of building a database and have already successfully created 2 primary key tables however when I try to bring them in as 2 foreign keys to another table I am running into a
"CLIENTID" invalid identifier
Unsure how to resolve as not the best at this.
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
);
You are missing the columns on which your primary key and foreign keys are created; you need something like the following, to be edited with the right type for your columns:
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
ClientId number, -- missing
CourseID number, -- missing
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
)

Error no primary key in a table with two foreign key

I have two tables Maquinista and Tractorista.
And another called Transportes, this is for relation (and only have 2 columns that are the data I need of each other)
That's my code:
CREATE TABLE [dbo].[Transporte]
(
[MaquinistaId] INT NOT NULL,
[TractoristaId] INT NOT NULL,
CONSTRAINT [FK_Transporte_Maquinista]
FOREIGN KEY ([MaquinistaId]) REFERENCES [Maquinista]([MaquinistaId]),
CONSTRAINT [FK_Transporte_Tractorista]
FOREIGN KEY ([TractoristaId]) REFERENCES [Tractorista]([TractoristaId])
)
The error:
The referenced table [dbo].[Transporte] contains no primary or candidate keys that match the referencing column list in the foreign key.

Error when setting foreign key in SQL Server

I have the following queries that I run to create tables in MS SQL Server:
CREATE TABLE menus
(
menu_id int NOT NULL PRIMARY KEY,
menu_name char,
other_details char
)
CREATE TABLE bookings
(
booking_Id int NOT NULL PRIMARY KEY,
date_booked DATE,
date_of_booking DATE,
other_details char,
staff_id int FOREIGN KEY REFERENCES staff(staff_id),
customer_id int FOREIGN KEY REFERENCES customers(customer_id)
)
CREATE TABLE menus_booked
(
menu_id INT NOT NULL,
booking_id INT NOT NULL,
CONSTRAINT PK_menus_booked PRIMARY KEY(menu_id,booking_id),
FOREIGN KEY (menu_id) REFERENCES menus(menu_id),
FOREIGN KEY (booking_id) REFERENCES bookings(booking_id)
)
CREATE TABLE menu_changes
(
change_id int NOT NULL PRIMARY KEY,
menu_id int NOT NULL,
booking_id int NOT NULL,
change_details char,
FOREIGN KEY (menu_id) REFERENCES menus_booked(menu_id),
FOREIGN KEY (booking_id) REFERENCES menus_booked(booking_id)
)
On running the last query I get the error:
There are no primary or candidate keys in the referenced table 'menus_booked' that match the referencing column list in the foreign key 'FK_menu_chan_menu'
I am unsure if my queries are correct and can't resolve this error.
The primary key of menus_booked is a unique combination of menu_id and booking_id. A foreign must point to that combination, not just one of its fields, which is not necessarily unique. Your query currently tries to define two foreign keys, one on each column, instead of one foreign key on the combination of the columns:
CREATE TABLE menu_changes
(
change_id int NOT NULL PRIMARY KEY,
menu_id int NOT NULL,
booking_id int NOT NULL,
change_details char,
FOREIGN KEY (menu_id, booking_id)
REFERENCES menus_booked(menu_id, booking_id) -- Here!
)
A foreign key has to reference a primary key (or unique key but here the PK is the problem), and it has to reference it in it's entirety.
FOREIGN KEY (menu_id) REFERENCES menus_booked(menu_id),
FOREIGN KEY (booking_id) REFERENCES menus_booked(booking_id)
You have two foreign key's referencing part of the primary key of menus_booked. You'll have to alter it to:
FOREIGN KEY (menu_id, booking_id) REFERENCES menus_booked(menu_id, booking_id)

There are no PK in the referenced table. Why?

I have the following T-SQL to create 3 SQL tables:
create table dbo.Posts
(
Id int identity not null
constraint PK_Posts_Id primary key clustered (Id),
Active bit not null
constraint DF_Posts_Active default (0)
);
create table dbo.PostsLocalized
(
Id int not null,
Culture int not null
constraint CK_PostsLocalized_Culture check ([Culture] in ('1', '2', '3')),
[Text] nvarchar (200) not null,
constraint PK_PostsLocalized_Id_Culture primary key clustered (Id, Culture)
);
create table dbo.Tags
(
Id int identity not null
constraint PK_Tags_Id primary key clustered (Id),
Name nvarchar not null
);
create table dbo.PostsLocalized_Tags
(
PostLocalizedId int not null,
TagId int not null,
constraint PK_PostsLocalized_Tags_Post_PostLocalizedId_TagId primary key clustered (PostLocalizedId, TagId)
);
Then I have added the following constraints:
alter table dbo.PostsLocalized
add constraint FK_PostsLocalized_Id foreign key (Id) references dbo.Posts(Id) on delete cascade on update cascade;
alter table dbo.PostsLocalized_Tags
add constraint FK_PostsLocalized_Tags_PostLocalizedId foreign key (PostLocalizedId) references PostsLocalized(Id) on delete cascade on update cascade,
constraint FK_PostsLocalized_Tags_TagId foreign key (TagId) references Tags(Id) on delete cascade on update cascade;
But I get the following error:
There are no primary or candidate keys in the referenced table 'PostsLocalized' that match the referencing column list in the foreign key 'FK_PostsLocalized_Tags_PostLocalizedId'.
How can I solve this?
Thank You,
Miguel
SQL Server mandates that foreign key references be to a primary key or unique key. The foreign key reference has to be to all the columns that constitute the primary/unique key. The documentation says:
In a foreign key reference, a link is created between two tables when
the column or columns that hold the primary key value for one table
are referenced by the column or columns in another table. This column
becomes a foreign key in the second table.
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY
KEY constraint in another table; it can also be defined to reference
the columns of a UNIQUE constraint in another table. A FOREIGN KEY
constraint can contain null values; however, if any column of a
composite FOREIGN KEY constraint contains null values, verification of
all values that make up the FOREIGN KEY constraint is skipped. To make
sure that all values of a composite FOREIGN KEY constraint are
verified, specify NOT NULL on all the participating columns.
The primary key in PostsLocalized contains the culture column, so you need to add it into the foreign key reference.
Your PK on PostsLocalized table is complex consisting of two columns - id and culture and you are trying to create FK on only one of those columns which is not possible.
You'll have to either add Culture column on PostsLocalized_Tags and use them both in foreign key or remove Culture from your PK on PostLocalized