[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Creating a TABLE with many columns

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
acsakany
Asked:
acsakany
1 Solution
 
dportasCommented:
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
 
Daniel WilsonCommented:
>>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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:

  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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now