Solved

Creating a TABLE with many columns

Posted on 2008-10-09
3
217 Views
Last Modified: 2010-03-20
I am new to SQL and I need a table to store data on the top 50 wines sold in a region. The key would be the regionID and then I need to store the 50 wineIds of the most sold wine for that region. Do I need to create 50 different columns or is there a way to create one column with 50 different values likwe an array?
0
Comment
Question by:acsakany
3 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 22681422
Here's how I'd do it:

CREATE TABLE RegionWineRankings
(RegionID INT NOT NULL REFERENCES Regions (RegionId),
 WineID INT NOT NULL REFERENCES Wines (WineId),
 Ranking SMALLINT NOT NULL CHECK (Ranking BETWEEN 1 AND 50),
 PRIMARY KEY (RegionID, WineID)
);

I am not assuming that (RegionID,Ranking) is unique because I don't know if you want to allow tied rankings.
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 50 total points
ID: 22681431
>>Do I need to create 50 different columns

NO!

Create Table Wine(
  WineName nvarchar(50)
/* any other info about the wine */
)

Create Table Region(
  RegionName nvarchar(100)
 /* any other info on the region */
)

Create Table WineRegion(
 WineName nvarchar(50),
 RegionName nvarchar(100),
 DateSoldAtTop datetime
/* any other info that releate the wine to the region */
)
0
 
LVL 57
ID: 22681440

  When one talks about using SQL, which is Structured Query Language, you are talking about using a relational database.  

  When designing a relational database, you follow some basic rules.  The process of following those rules is called normalization.  If you don't follow the rules, then the database doesn't work to well and neither does SQL queries.

  For example, in a single field, you don't store multiple values "1 hammer, 20 10D nails"  .  What you got is four values; two qty's and 2 descriptions of something.  You also don't repeat fields:

  Phone 1, phone 2, phone 3, phone 4.

  The reason is if you wanted to know how many people had at least 3 phone numbers, you'd need to look at each record and each of the phone fields.  SQL is designed to work with *sets* of records.   Each of those phone fields should be a record in a seperate table.

  So back to your question.  You should have the following:

tblRegion - One record per region
RegionID - Autonumber - A unique number to indentify the region - This is the Pirmary Key
Description - Text - Description of the region

tblWines - One record per wine
WineID - Autonumber - Again the PK
RegionID - Forgein key - This means that this value is a primary key in another table
SalesDollars - Currency - Sales of this wine.

  Now it's easy to look at tblWines group by region, sort descending by SalesDollars, and pick only the top 50 in each group.  Then with that list, use the RegionID to get a record from tblregion and the description for use in a form or report.

HTH,
JimD.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

20 Experts available now in Live!

Get 1:1 Help Now