?
Solved

defining tables relationship

Posted on 2012-04-04
2
Medium Priority
?
208 Views
Last Modified: 2012-06-21
Hello,

I need to relate 3 tables e.g. prod, series, sub-series and noticed that when I am using composite primary key it cannot be incremental. This is my plan and I will appreciate it if you give me some help:

table 1: prod     has prod_id (PK) and desc
table 2: series   has prod_id(FK and PK) and series_id (PK)
tables 3: sub-series  series_id (FK and PK) and sub_series(PK)

if this is a right approach, how can I make prod_id and series_id incremental ? and same thing about sub_series table?

if not, then what you suggest?

Thanks in advance,
ak
0
Comment
Question by:akohan
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 800 total points
ID: 37807347
CREATE TABLE Prod (ProdKey INT IDENTITY(1,1) --Other fields)
CREATE TABLE Series (SeriesID INT IDENTITY(1,1), ProdKey int) --ProdKey is a FK to Prod Table
CREATE TABLE SubSeries(SubSeriesID int IDENTITY(1,1), SeriesID int) SeriesID is FK to Series table

Each Prod can have multiple series, but each series can only have 1 prod
Each Series can have multiple subSeries, but each sub-series can only belong to a single series.

If you want to allow multiple series to have multiple products (a many-to-many relationship), then you do it this way:
CREATE TABLE Prod (ProdKey INT IDENTITY(1,1) --Other fields)
CREATE TABLE Series (SeriesID INT IDENTITY(1,1) --Other fields)
CREATE TABLE ProductSeries( ProdKey int, SeriesID int) --Both Prodkey and seriesID make a composite PK

and to do the same with subseries
CREATE TABLE SubSeries(SubSeriesID int IDENTITY(1,1) --Other fields)
CREATE TABLE SeriesSubSeries(SeriesID int, SubSeriesID int) --Both seriesID and SubseriesID make a composite PK
0
 

Author Closing Comment

by:akohan
ID: 37825698
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question