Solved

Creating a TABLE with many columns

Posted on 2008-10-09
3
218 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

13 Experts available now in Live!

Get 1:1 Help Now