I'm trying to create a table with a couple columns having the same FK constraint. What is wrong with my syntax? I'm receiving a msg 102, incorrect syntax error around "fk_fighterID"
"FIGHTERS" is a table that has been set up with "fighterID" as the primarykey
CREATE TABLE FIGHTS
(
FIGHTID INT PRIMARY KEY,
CONSTRAINT fk_fighterID FOREIGN KEY REFERENCES FIGHTERS(fighterID),
FIGHTER_ID_CHALLANGER int fk_fighterID,
FIGHTER_ID_CHALLANGED int fk_fighterID,
CONSTRAINT fk_weightclass FOREIGN KEY (classID) REFERENCES WEIGHT_CLASSES
(classID)
WEIGHTCLASS NOT NULL fk_wightclass,
IS_MAIN BIT NOT NULL,
IS_CO_MAIN BIT NOT NULL,
SCHEDULED ROUNDS TINYINT(1),
CONSTRAINT fk_WINMETH FOREIGN KEY (WINID) REFERENCES WINNING_METHODS (WINID)
WINNING METHOD NOT NULL fk_WINMETH
)
;
Correct Statement:
CONSTRAINT fk_weightclass FOREIGN KEY (classID) REFERENCES WEIGHT_CLASSES (classID)
Incorrect Statement:
CONSTRAINT fk_fighterID FOREIGN KEY REFERENCES FIGHTERS(fighterID),
You forgot to mention something like '(classId)'.
Edit:
I am not sure what you are trying to achieve, but your above query is wrong, try below query if it makes sense.
CREATE TABLE FIGHTS
(
FIGHTID INT PRIMARY KEY,
FIGHTER_ID_CHALLANGER INT,
FIGHTER_ID_CHALLANGED INT,
IS_MAIN BIT NOT NULL,
IS_CO_MAIN BIT NOT NULL,
SCHEDULED_ROUNDS TINYINT(1),
WEIGHTCLASS INT NOT NULL,
CLASS_ID INT NOT NULL,
WINNING_METHOD INT NOT NULL,
CONSTRAINT fk_fighterID FOREIGN KEY (FIGHTER_ID_CHALLANGER) REFERENCES FIGHTERS (fighterID),
CONSTRAINT fk_weightclass FOREIGN KEY (CLASS_ID) REFERENCES WEIGHT_CLASSES (classID),
CONSTRAINT fk_WINMETH FOREIGN KEY (WINNING_METHOD) REFERENCES WINNING_METHODS (WINID)
);
Shouldn`t the constraints be outside of the table creation statement like this:
CREATE TABLE Sales.TempSalesReason (TempID int NOT NULL, Name nvarchar(50),
CONSTRAINT PK_TempSales PRIMARY KEY NONCLUSTERED (TempID),
CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID)
REFERENCES Sales.SalesReason (SalesReasonID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Related
This is my first table :
CREATE TABLE [dbo].[County]
(
[CountyId] INT IDENTITY (1, 1) NOT NULL,
[County] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_County] PRIMARY KEY CLUSTERED ([CountyId] ASC)
);
This is my second table :
CREATE TABLE [dbo].[Theatre]
(
[TheatreId] INT IDENTITY (1, 1) NOT NULL,
[TheatreName] VARCHAR(50) NOT NULL,
[CountyId] INT NOT NULL,
CONSTRAINT [PK_Theatre]
PRIMARY KEY CLUSTERED ([TheatreId] ASC),
CONSTRAINT [FK_Theatre_County]
FOREIGN KEY ([CountyId]) REFERENCES [dbo].[County] ([CountyId])
);
This is my third table :
CREATE TABLE [dbo].[Movies]
(
[CinemaId] INT NOT NULL,
[CategoryId] INT NOT NULL IDENTITY(101, 1),
[CinemaName] VARCHAR(50) NOT NULL,
[TheatreId] INT NOT NULL,
[ShowTimings] TIME (7) NOT NULL,
CONSTRAINT [PK_Movies]
PRIMARY KEY CLUSTERED ([CinemaId], [CategoryId]),
CONSTRAINT [FK_Movies_Theatre]
FOREIGN KEY ([TheatreId]) REFERENCES [dbo].[Theatre] ([TheatreId])
);
This is my last table:
CREATE TABLE [dbo].[Reviews]
(
[MovieId] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR(50) NOT NULL,
[Genres] VARCHAR(50) NOT NULL,
[Image] VARCHAR(50) NOT NULL,
[ShortDescription] TEXT NOT NULL,
[Rating] VARCHAR(50) NOT NULL,
[Grade] VARCHAR(50) NOT NULL,
[CategoryId] INT NOT NULL,
CONSTRAINT [PK_Reviews]
PRIMARY KEY CLUSTERED ([MovieId] ASC),
CONSTRAINT [FK_Reviews_Movies]
FOREIGN KEY ([CategoryId]) REFERENCES [Movies]([CategoryId]),
);
I have created a relationship between the tables yet I am getting an error while I am updating the last table:
SQL71516 :: The referenced table '[dbo].[Movies]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
Can any about please tell me what is my mistake that I am doing?
Just like the error says, Movies.CategoryId is not a key, so you can't reference it in a Foreign Key. You need a Categories table that has CategoryId as its primary key. Then both Reviews and Movies can have a Foreign Key referencing Categories.
THIS IS NOT DUPLICITY
I am NOT setting value to identity column!
I cant add value in my Table. I think all set good but this error keeps comming:
"Cannot insert explicit value for identity column in table 'MainQueue'
when IDENTITY_INSERT is set to OFF."
My table:
CREATE TABLE [dbo].[MainQueue] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Created] DATETIME NOT NULL,
[IdOffice] INT NOT NULL,
[IdCategory] INT NOT NULL,
[StartProcessTime] DATETIME NULL,
[EndProcessTime] DATETIME NULL,
[IdUser] INT NULL,
[Sms] BIT CONSTRAINT [DF__MainQueue__Sms__412EB0B6] DEFAULT ((0)) NOT NULL,
[OrderNumber] INT NOT NULL,
[IdSms] INT NULL,
[UserWindowNumber] INT NULL,
CONSTRAINT [PK__MainQueu__3214EC0783954F32] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_MainQueue_Category] FOREIGN KEY ([IdCategory]) REFERENCES [dbo].[Category] ([Id]),
CONSTRAINT [FK_MainQueue_Office] FOREIGN KEY ([IdOffice]) REFERENCES [dbo].[Office] ([Id]),
CONSTRAINT [FK_MainQueue_User] FOREIGN KEY ([IdUser]) REFERENCES [dbo].[User] ([Id]),
CONSTRAINT [FK_MainQueue_SmsQueue] FOREIGN KEY ([IdSms]) REFERENCES [dbo].[SmsQueue] ([Id])
);
My code for adding:
var queueItem = new MainQueue();
queueItem.IdOffice = officeId;
queueItem.IdCategory = categoryId;
queueItem.Created = DateTime.Now;
queueItem.OrderNumber = orderNum;
dc.MainQueues.InsertOnSubmit(queueItem);
dc.SubmitChanges();
THIS IS NOT DUPLICITY
I am NOT setting value to identity column!
Your described ID column is an autoincrement identity column, that means you cannot pass any explicit value for the ID, after an insert the DBS will do fill the ID. Remove the ID column in your Insert method and do not pass a parameter for the Id when calling your method.
If you really need to insert an explicit value for the ID, either make the column not an IDENTITY, instead primary or turn IDENTITY INSERT OFF before you call the insert and turn in back on afterwards.
I have a table that can be in a 1-* relationship with one of 2 different tables. I currently have it setup where there are 2 nullable columns that reference another table. However, this is causing an issue for cascade deletions.
CREATE TABLE [dbo].[TA] (
[Id] INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_TA] PRIMARY KEY CLUSTERED ([Id] ASC),
);
CREATE TABLE [dbo].[TB] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[TAId] INT NOT NULL,
CONSTRAINT [FK_TB_To_TA] FOREIGN KEY ([TAId]) REFERENCES [dbo].[TA] ([Id]) ON DELETE CASCADE,
CONSTRAINT [PK_TB] PRIMARY KEY CLUSTERED ([Id] ASC),
);
CREATE TABLE [dbo].[TC] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[TAId] INT NULL,
[TBId] INT NULL,
CONSTRAINT [PK_TC] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_TC_To_TA] FOREIGN KEY ([TAId]) REFERENCES [dbo].[TA] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_TC_To_TB] FOREIGN KEY ([TBId]) REFERENCES [dbo].[TB] ([Id]), -- NOTE: DELETE CASCADE CAUSES A CIRCULAR REFERENCE
);
Essentially, this creates a nested structure like:
TA1
+ -- TC1, TC2
+ -- TB1
+ -- TC3, TC4
A TC object should only be a child of either TA or TB (not both). How should I go about developing this so that I can DELETE a TA row and have all TC and TB referenced rows deleted as well?
To resolve this I ended up breaking [TC] into multiple tables as described in a blog for entity framework. This created the following table structure:
CREATE TABLE [dbo].[TA] (
[Id] INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_TA] PRIMARY KEY CLUSTERED ([Id] ASC),
);
CREATE TABLE [dbo].[TB] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[TAId] INT NOT NULL,
CONSTRAINT [FK_TB_To_TA] FOREIGN KEY ([TAId]) REFERENCES [dbo].[TA] ([Id]) ON DELETE CASCADE,
CONSTRAINT [PK_TB] PRIMARY KEY CLUSTERED ([Id] ASC),
);
CREATE TABLE [dbo].[TCA] (
[Id] INT IDENTITY (2, 2) NOT NULL,
[TAId] INT NULL,
CONSTRAINT [PK_TCA] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_TCA_To_TA] FOREIGN KEY ([TAId]) REFERENCES [dbo].[TA] ([Id]) ON DELETE CASCADE,
);
CREATE TABLE [dbo].[TCB] (
[Id] INT IDENTITY (1, 2) NOT NULL,
[TBId] INT NULL,
CONSTRAINT [PK_TCB] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_TCB_To_TB] FOREIGN KEY ([TBId]) REFERENCES [dbo].[TB] ([Id]) ON DELETE CASCADE,
);
I set the identities to even\odd values in order to prevent collision when creating a union of the two tables (see above link).
I have Table:
create table Authorized(
diver_number int not null,
level_name char(30) not null,
constraint PK_Authorized primary key (diver_number, level_name),
Constraint FK_diver_number1 foreign key (diver_number) references Diver(diver_number)
on update cascade on delete cascade,
Constraint FK_level_name foreign key (level_name) references Level(name)on update
cascade on delete cascade,
club_number int Constraint FK_club_number foreign key (club_number) references DivingClub(number)
on update cascade on delete cascade not null,
authorization_date date not null,
picture image)
And atable:
create table Works_for(
diver_number int not null,
club_number int not null,
constraint PK_Works_for primary key (diver_number, club_number),
Constraint FK_diver_number2 foreign key (diver_number) references Diver(diver_number)
on update cascade on delete cascade ,
Constraint FK_club_number2 foreign key (club_number) references DivingClub (number)on update
cascade on delete cascade,
start_working_date date not null,
end_working_date date)
When i add diver_number to table Works_for, i want to check if this diver is a "Guide" (level_name in Authorized table). How can i check it?
I would think your scalar function would be more like this.
CREATE FUNCTION dbo.checkADate(#diver_number int) RETURNS BIT AS
BEGIN
declare #Found bit
select #Found = count(*)
from Authorized
WHERE diver_number = #diver_number
and level_name = 'Guide'
return #Found
END
I have a table Item master, in which i have a primary key as item code
tbl_item_master
Itm_code int PK
Bill of material(tbl_Bom)
child_id int
parent_id int
I have another table of bill of material(tbl_bom) in which there are 2 columns of parent and child which are having their values which is nothin but primary key of item master. I.e parent and child id in BOM are from Item Master's primary key. Follwoing is my script that I am using to apply foreign key on child and parent .
CREATE TABLE dbo.BOM_MASTER
(
BOM_SrNo INT CONSTRAINT DF_BOM_MASTER_BOM_SrNo DEFAULT ((0)) NOT NULL,
BOM_Key INT NOT NULL,
PD_Key INT,
BOM_Level INT,
BOM_Parent_Code VARCHAR (50),
BOM_Child_Code VARCHAR (50),
BOM_UOM INT,
BOM_Qty DECIMAL (14, 2),
BOM_Final_Version INT,
BOM_ItemDimension_Applicable BIT,
BOM_MatType INT CONSTRAINT DF__BOM_MASTE__BOM_M__192BAC54 DEFAULT ((0)) NOT NULL,
CONSTRAINT FK_BOM_MASTER_Project_Details_Master FOREIGN KEY (PD_Key) REFERENCES dbo.Project_Details_Master (PD_Key),
CONSTRAINT FK_BOM_MASTER_tbl_itm_master FOREIGN KEY (BOM_Parent_Code) REFERENCES tbl_itm_master (BSL_COST_ITEM_Item_SAP_Code),
CONSTRAINT FK_BOM_MASTER_tbl_itm_master FOREIGN KEY (BOM_Child_Code) REFERENCES tbl_itm_master (BSL_COST_ITEM_Item_SAP_Code)
)
GO
and I am getting the following error
Cannot create two constraints named 'FK_BOM_MASTER_tbl_itm_master'. Duplicate constraint names are not allowed. Severity 16
If you add newlines, the issue becomes apparent.
CREATE TABLE dbo.BOM_MASTER
(
BOM_SrNo INT CONSTRAINT DF_BOM_MASTER_BOM_SrNo DEFAULT ((0)) NOT NULL,
BOM_Key INT NOT NULL,
PD_Key INT,
BOM_Level INT,
BOM_Parent_Code VARCHAR (50),
BOM_Child_Code VARCHAR (50),
BOM_UOM INT,
BOM_Qty DECIMAL (14, 2),
BOM_Final_Version INT,
BOM_ItemDimension_Applicable BIT,
BOM_MatType INT CONSTRAINT DF__BOM_MASTE__BOM_M__192BAC54 DEFAULT ((0)) NOT NULL,
CONSTRAINT FK_BOM_MASTER_Project_Details_Master FOREIGN KEY (PD_Key) REFERENCES dbo.Project_Details_Master (PD_Key),
CONSTRAINT FK_BOM_MASTER_tbl_itm_master FOREIGN KEY (BOM_Parent_Code) REFERENCES tbl_itm_master (BSL_COST_ITEM_Item_SAP_Code),
CONSTRAINT FK_BOM_MASTER_tbl_itm_master FOREIGN KEY (BOM_Child_Code) REFERENCES tbl_itm_master (BSL_COST_ITEM_Item_SAP_Code)
)
GO
The last 2 are named identically. It is also clear there are other naming issues. What is DF__BOM_MASTE__BOM_M__192BAC54? Will future users of the table be able to gleem anything from this naming?