I am am trying to construct a database in SQL Server 2008 R2 that will allow users to place their own sub-types into categories. I have a parent table that holds the preset category names (defined by me).
The question I face is what is what is the best way to deal with the PRIMARY KEY and UNIQUE constraint, and foreign key REFERENCES. Indexing is at the center of this as I anticipate that the sub table (we will call it CategoryTypes) will grow quite large over time and will need to be able to efficiently allow reads from the data based on the parent table (Categories). Is there any problem I would need to anticipate if the tables were laid out as follows?
My concern is that the IDENTITY column in the CategoryTypes table will need to maintain a unique count. The reason I have included this field is to allow a simpler reference when passing data between tiers in the application. By passing an Integer versus an Integer / String pair. The data in these tables will persist at each layer of the database to save on bandwidth. From a database perspective, does the layout below pose any major challenges once deployed?
To simplify, is there a problem with using a unique ID field (IDENTITY) that is not included in the primary key when a composite key is present? See table layout below:
Parent Table:
CREATE TABLE schema.Categories
(
Id TINYINT PRIMARY KEY NOT NULL,
Name VARCHAR(100) NOT NULL,
)
Sub Table (User inserted data over time):
CREATE TABLE schema.CategoryTypes
(
Id INT IDENTITY(1,1) NOT NULL,
CategoryId TINYINT REFERENCES schema.Categories(Id) NOT NULL,
Name VARCHAR(100) NOT NULL,
CONSTRAINT PRIMARY KEY CLUSTERED(CategoryId, Name)
CONSTRAINT UC_CategoryTypesId UNIQUE NONCLUSTERED(Id)
)