Solved

defining tables relationship

Posted on 2012-04-04
2
196 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 200 total points
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now