Right now I\'m still a beginner in databases and still learning all about it. I have a problem with the design of my database and I don\'t really know where to go from here. Bas
Have a look at my database Design
CREATE TABLE Brands
(
BrandID INT IDENTITY CONSTRAINT Pk_Brands Primary Key
,Beandname Varchar(200)
)
CREATE TABLE InstrumentType
(
InstrumentTypeID INT IDENTITY CONSTRAINT Pk_InstrumentType Primary Key
,InstrumentName Varchar(200)
)
CREATE TABLE Products
(
ProductsID INT IDENTITY CONSTRAINT Pk_Products Primary Key
,BrandID INT
,InstrumentTypeID INT
,ProductImage Varchar(100)
)
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products__Brands] FOREIGN KEY([BrandID])
REFERENCES [dbo].[Brands] ([BrandID])
GO
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products_InstrumentTypesID] FOREIGN KEY([InstrumentTypeID])
REFERENCES [dbo].[InstrumentType] ([InstrumentTypeID])
GO
Intial Sample Data
INSERT INTO Brands (Beandname)
SELECT 'Ibanez' UNION ALL
SELECT 'Fender' UNION ALL
SELECT 'PRS' UNION ALL
SELECT 'ESP' UNION ALL
SELECT 'Warwick'
Go
INSERT INTO InstrumentType(InstrumentName)
SELECT 'Guitar' UNION ALL
SELECT 'Bass'
INSERT INTO Products(InstrumentTypeID,BrandID,ProductImage)
SELECT 1 , 1,'xyz.jpg' UNION ALL
SELECT 2 , 1,'hyz.jpg' UNION ALL
SELECT 1 , 2,'abc.jpg' UNION ALL
SELECT 2 , 2,'fgh.jpg' UNION ALL
SELECT 1 , 3,'yui.jpg' UNION ALL
SELECT 1 , 4,'mbm.jpg' UNION ALL
SELECT 2 , 5,'omo.jpg'
SELECT * FROM Brands
SELECT * FROM InstrumentType
SELECT * FRom Products
Get the Result by using below Query
SELECT
p.ProductsID
--,p.BrandID
,B.Beandname
--,p.InstrumentTypeID
,i.InstrumentName
,p.ProductImage
FROM Products P
INNER JOIN Brands B
On B.BrandID=p.BrandID
INNER JOIN InstrumentType I
On I.InstrumentTypeID=P.InstrumentTypeID
Insert Data Into Product Table BY Passing Parameters in to the below Sp
CREATE Procedure Usp_Products_Insert
(
@InstrumentTypeID INT,
@BrandID INT,
@ProductImage VARCHAR(100)
)
AS
BEGIN
IF EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='Products'
)
Begin
INSERT INTO Products(
InstrumentTypeID,
BrandID,
ProductImage)
SELECT @InstrumentTypeID,
@BrandID,
@ProductImage
END
END
EXEC Usp_Products_Insert @InstrumentTypeID=1
,@BrandID=2
,@ProductImage='TTTT.Jpg'
SELECT * FRom Products
If this is all you are storing, you can add an extra two fields in the database One called Guitar and has a 1 when the brand applies to a guitar (so all of them at the moment) and another field called Bass which only has a 1 on the brands which uses a bass.
In your filter you can add "where g.Bass==1"
This will only return the Bass brands.
However, if you intend to expand on the instruments, you should create a separate table which links the type of instrument to the brands.
eg
Type Brand Guitar 1 Guitar 2 Guitar 3 Guitar 4 etc Bass 1 Bass 2 Bass 5
Then link the two tables together in the where.