SQL Table Datastructure, is this wrong? Using CASCADING Delete - sql

Here are the 3 tables I am having problems with:
Table: Opportunities - Holds various opportunity(job) descriptions
Table: Opportunities_Applicants - Holds various applicants applying for opportunities. 1 Applicant can only apply for 1 opportunity, however 1 opportunity can have many applicants
Table: Opportunities_Category - Holds category name and type. 1 Category can relate to many Opportunities.
I am trying to perform a CASCADING Delete when a Opportunity Category is deleted, it will delete corresponding Opportunities and Applicants for those Opportunities.
Is this structure appropriate or should I setup database differently? How should my table relationships be setup in order for the CASCADING Delete to work when a Opportunity Category is deleted?
Should I even be using CASCADING Delete?
create table Opportunities_Category
(
CategoryID int identity(1,1) not null
constraint PK_CategoryID primary key clustered,
[Name] varchar(150) not null,
[Type] varchar(100) not null --Pay, Volunteer, Volunteer Yearly
)
create table Opportunities
(
OpportunityID int identity(1,1) not null
constraint PK_OpportunityID primary key clustered,
CategoryID int not null
constraint FK_CategoryID foreign key references Opportunities_Category(CategoryID) ON DELETE CASCADE,
Title varchar(300) not null,
PostingDate datetime not null,
ClosingDate datetime not null,
Duration varchar(150) not null, --Part Time, Full Time, Seasonal, Contract
Compensation varchar(150) not null, --Hourly, Volunteer, Salary
[Description] varchar(5000) not null,
Qualifications varchar(5000) not null,
Show int not null
)
create table Opportunities_Applicant
(
ApplicantID int identity(1,1) not null
constraint PK_ApplicantID primary key clustered,
OpportunityID int not null
constraint FK_OpportunityID foreign key references Opportunities(OpportunityID) ON DELETE CASCADE,
[First] varchar(150) not null,
[Last] varchar(150) not null,
Phone varchar(20) not null,
Cell varchar(20) not null,
EMail varchar(200) not null,
CoverLetterResume varchar(300) null,
[Timestamp] datetime not null
)

It turns out that my tables are setup properly:
Yesterday, i had been trying to do: DELETE FROM Opportunities WHERE CategoryID = #CategoryID. This was only deleting the records from Opportunities and Opportunities_Applicants.
Today, i changed to: DELETE FROM Opportunities_Categoies WHERE CategoryID = #CategoryID and all 3 tables are deleting their corresponding records!

ALTER TABLE [dbo].[Opportunities] WITH CHECK ADD CONSTRAINT [FK_OpportunitiesCategory_Opportunities] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Opportunities_Category] ([CategoryID])
ON DELETE CASCADE
GO
Good Luck...

Related

SQL, Delete Records Based On Related Fields In Other Table

I have a SQL table based on hotel data. I have two tables and a bridge table to relate them. I'm still learning so I'm sure some of this is not ideal or has potential risks.
Guest Table
CREATE TABLE Guest
(
Guest_ID INT PRIMARY KEY IDENTITY (1, 1),
GuestName NVARCHAR(60) NOT NULL,
Street NVARCHAR(50) NOT NULL,
City NCHAR(30) NOT NULL,
[State] CHAR(2) NOT NULL,
CONSTRAINT [State.State]
FOREIGN KEY ([State]) REFERENCES [State]([State]),
Zip CHAR(5) NOT NULL,
Phone VARCHAR(15) NOT NULL
);
Room Table
CREATE TABLE Room
(
Room_ID SMALLINT PRIMARY KEY,
Room_Type_ID SMALLINT NOT NULL,
CONSTRAINT Room_Type_ID
FOREIGN KEY (Room_Type_ID) REFERENCES Room_Type([Type_ID]),
Amenity_Type_ID SMALLINT NOT NULL,
CONSTRAINT Amenity_Type_ID
FOREIGN KEY (Amenity_Type_ID) REFERENCES Amenity_Type([Type_ID])
);
Bridge Table (Reservations)
CREATE TABLE Guest_Bridge_Rooms
(
Guest_ID INT NOT NULL,
CONSTRAINT Guest_ID
FOREIGN KEY (Guest_ID) REFERENCES Guest(Guest_ID),
Room_ID SMALLINT NOT NULL,
CONSTRAINT Room_ID
FOREIGN KEY (Room_ID) REFERENCES Room(Room_ID),
Date_Start DATE NOT NULL,
Date_End DATE NOT NULL,
Occ_Adults SMALLINT NOT NULL,
Occ_Children SMALLINT NOT NULL,
Price_Total DECIMAL(13,2) NOT NULL
);
Now with these tables, I would like to write a script to DELETE all rows where a reservation (bridged table) has a specific guest NAME by somehow relating the given Guest_ID to its GuestName in the related table. I could simply use Guest_ID but that is not the goal here.
For example something like
DELETE FROM Guest_Bridge_Rooms
WHERE Guest[ID].GuestName = 'John Doe';
Is there a simple way to do this?
You can use a subquery:
DELETE FROM Guest_Bridge_Rooms
WHERE Guest_ID = (SELECT g.Guest_Id FROM Guests g WHERE g.GuestName = 'John Doe');
Note: The exact syntax might vary, depending on the database. This also assumes that GuestName is unique in Guests.

LinqPad Syntax for adding foreign key constraint

This is the SQL I used in LinqPad:
CREATE TABLE Category
(
catID int NOT NULL,
catName NVARCHAR(150) NOT NULL,
catDesc NVARCHAR(150) NULL,
catCount int NOT NULL ,
CONSTRAINT category_pk PRIMARY KEY (catID)
);
CREATE TABLE InventoryTBLC
(
itemID int NOT NULL IDENTITY(1,1) CONSTRAINT PK_items PRIMARY KEY,
itemName NVARCHAR(150) NOT NULL,
itemDesc NVARCHAR(150) NULL,
itemQuantity int,
itemPrice int,
imagePath NVARCHAR(300),
correctInsert NVARCHAR(300),
realImage image,
CONSTRAINT FK_category FOREIGN KEY (catID) REFERENCES Category(catID)
);
But I get the following error:
Invalid column ID. [ catID ]
Can someone help me with the LinqPad syntax?
If you want to use a column as the foreign key, you have to list that column in your table definition first! Adding the foreign key constraint does NOT add that column to your table!
And this is in no way Linqpad specific - this is standard SQL behavior.
So either you need to add catId to your second table like this:
CREATE TABLE InventoryTBLC
(
itemID int NOT NULL IDENTITY(1,1) CONSTRAINT PK_items PRIMARY KEY,
itemName NVARCHAR(150) NOT NULL,
itemDesc NVARCHAR(150) NULL,
itemQuantity int,
itemPrice int,
imagePath NVARCHAR(300),
correctInsert NVARCHAR(300),
realImage image,
catID int NOT NULL, -- this column *MUST* exist to be used as FK
CONSTRAINT FK_category
FOREIGN KEY (catID) REFERENCES Category(catID)
);
or then maybe you want to use one of the other pre-existing columns in InventoryTBLC as your FK column?

How to Relate 2 table with each other with 2 foreign keys

Here is my code it generates Foreign key conflict error
CREATE TABLE tblProducts
(
ProductID int NOT NULL IDENTITY PRIMARY KEY,
ProductName nvarchar(30) NOT NULL,
BatchID int NOT NULL FOREIGN KEY REFERENCES tblBatches (BatchID)
)
CREATE TABLE tblBatches
(
BatchID INT NOT NULL IDENTITY PRIMARY KEY,
BatchCode nvarchar(20) NOT NULL,
Quantity int NOT NULL,
BatchMnf Date NOT NULL,
BatchExp Date NOT NULL,
PurchaseRate int NOT NULL,
SalesRate int NOT NULL,
ProductID int NOT NULL FOREIGN KEY REFERENCES tblProducts (ProductID)
)
You cannot do that. This is a circular reference.
This is a bad design but if you want to do that, you need to make foreign key columns Nullable.
CREATE TABLE tblProducts
(
ProductID int NOT NULL IDENTITY PRIMARY KEY,
ProductName nvarchar(30) NOT NULL,
BatchID int NULL FOREIGN KEY REFERENCES tblBatches (BatchID)
)
CREATE TABLE tblBatches
(
BatchID INT NOT NULL IDENTITY PRIMARY KEY,
BatchCode nvarchar(20) NOT NULL,
Quantity int NOT NULL,
BatchMnf Date NOT NULL,
BatchExp Date NOT NULL,
PurchaseRate int NOT NULL,
SalesRate int NOT NULL,
ProductID int NULL FOREIGN KEY REFERENCES tblProducts (ProductID)
)
Then you need to update reference fields after inserting records in tblBatches and tblProducts.
The good design says you need to create a bridge table like this:
CREATE TABLE tblProductsBatch
(
ID int NOT NULL IDENTITY PRIMARY KEY,
ProductID int NOT NULL,
BatchID int NOT NULL
)
And after inserting product and batch, you need to insert a record in this table to link rows to each other.

Error relate SQL Server tables

I have the following problem: when we run the code, it displays the following error:
There are primary keys or candidates in the reference Ticket table
that match the list of referencing columns in foreign key '
FK__Payment__PkTicke__1A14E395
Code:
create table SystemUser
(
PkUser int identity(1,1),
UserLogin nvarchar(20) not null unique,
UserPassword nvarchar(50) not null,
UserName nvarchar(50) not null,
UserCpf nvarchar(50) not null,
UserBirth datetime not null,
UserGender nvarchar(15) not null,
AddressCep int not null,
AddressStreet nvarchar(50) not null,
AddressNumber nvarchar(20) not null,
AddressComplement nvarchar(50) not null,
AddressCity nvarchar(50) not null,
AddressState nvarchar(50) not null,
primary key(PkUser)
)
create table Attractions
(
PkAttraction integer identity(1,1) ,
AttractionName nvarchar(50) not null unique,
AttractionDate datetime not null,
AttractionDescription nvarchar(150) not null
primary key(PkAttraction)
)
create table Ticket
(
PkTicket int identity(1,1),
PkUser int not null,
PkAttraction int not null,
TicketPrice decimal not null,
primary key(PkTicket, PkUser, PkAttraction),
foreign key(PkUser) references SystemUser(PkUser),
foreign key(PkAttraction) references Attractions(PkAttraction)
)
create table Payment
(
PkPayment int identity(1,1),
PkTicket int not null,
Portion int not null,
IdTransaction nvarchar(100) not null,
Payday datetime not null,
primary key(PkPayment, PkTicket),
foreign key(PkTicket) references Ticket(PkTicket),
)
create table FormPayment
(
PkFromPayment int identity(1,1),
PkPayment int not null,
ShareValue decimal not null,
ExpirationDate datetime not null
primary key(PkFromPayment, PkPayment),
foreign key(PkPayment) references Payment(PkPayment),
)
Your Ticket table as a primary key made up from 3 columns:
create table Ticket
(
.....
primary key(PkTicket, PkUser, PkAttraction),
....
)
Any table that wants to reference that table Ticket must also provide all 3 columns for the foreign key.
You cannot reference only part of a primary key - if you want to reference it, you must have all columns that it contains - otherwise you cannot establish a FK relationship.
So you must add the PkUser and PkAttraction columns to your Payment table so that you can establish this FK relationship:
create table Payment
(
PkPayment int identity(1,1),
PkTicket int not null,
PkUser int not null, // add this
PkAttraction int not null, // add this
Portion int not null,
IdTransaction nvarchar(100) not null,
Payday datetime not null,
primary key(PkPayment, PkTicket),
// change to this
foreign key(PkTicket, PkUser, PkAttraction) references Ticket(PkTicket, PkUser, PkAttraction)
.....
)
When you not specify a name for FK and PK, SQL server generates a name. in this case, looks like SQL server generates duplicate name.
If you specify name for FK and PK, it will work.

T-SQL: CHECK constraint not working

I have the following T-SQL schema. The problem I am having is that the check constraint on the Download table is not working. I am still able to insert records into that table that contain NULL values for both ProductId and CategoryId. Why is this so?
I want both the ProductId and CategoryId columns to allow NULL values, but for any given record only one of these are allowed to be set to NULL, the other needs to be a corresponding Id of the Category or Product table.
CREATE TABLE Category (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Description nvarchar(100) NULL,
ParentCategoryId int NULL
CONSTRAINT fk_CategoryId_CategoryId FOREIGN KEY (Id) REFERENCES Category(Id)
)
GO
CREATE TABLE Product (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Title nvarchar(100) NOT NULL,
TagLine nvarchar(MAX) NOT NULL,
Description nvarchar(MAX)NULL,
CategoryId int NOT NULL,
ImageUrl nvarchar(255) NULL,
Keywords nvarchar(200) NOT NULL
CONSTRAINT fk_ProductCategoryId_CategoryId FOREIGN KEY (CategoryId) REFERENCES Category(Id)
)
GO
CREATE TABLE Download (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Title nvarchar(100) NOT NULL,
Description nvarchar(MAX) NULL,
CategoryId int NULL,
ProductId int NULL,
DownloadUrl nvarchar(255) NOT NULL,
CONSTRAINT fk_DownloadCategoryId FOREIGN KEY (CategoryId) REFERENCES Category(Id),
CONSTRAINT fk_DownloadProductId FOREIGN KEY (ProductId) REFERENCES Product(Id),
CONSTRAINT chk_ReferencesCategoryOrProduct CHECK (ProductID != NULL AND CategoryId != NULL)
)
GO
Use:
CONSTRAINT chk_ReferencesCategoryOrProduct CHECK (ProductID IS NOT NULL
OR CategoryId IS NOT NULL)
NULL isn't a value -- it's a placeholder for the lack of a value. Which is why you need to use specific syntax to check for it.