Re: Best Data storage practice?
From: Joe Celko (jcelko212_at_earthlink.net)
Date: 11/01/04
- Next message: Hari Prasad: "RE: Encrypting passwords in a SQL database"
- Previous message: Hugo Kornelis: "Re: simple stored procedure question for noobie"
- In reply to: Leon: "Best Data storage practice?"
- Next in thread: Leon: "Re: Best Data storage practice?"
- Reply: Leon: "Re: Best Data storage practice?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 01 Nov 2004 13:54:01 -0800
>> I have six textbox controls on my web-form that allows the user to
enter the numbers 1-47 non-duplicates in each box <<
We don't care about that stuff; this is the database, not the GUI.
>> Is it best to store the user set of six numbers in the database as..
NumID-PK, Num1, Num2, Num3, Num4, Num5, Num6 <<
Almost. A column is not a field; it can have constraints. You need to
enforce your business rules here and captrue the important data, more
like this:
CREATE TABLE LotteryDrawings
(drawing_date DATETIME DEFAULT CURRRENT_TIMESTAMP
NOT NULL,
user_id CHAR(15) NOT NULL,
PRIMARY KEY (user_id, drawing_date),
num_1 INTEGER NOT NULL
CHECK(num_1 BETWEEN 1 AND 47),
num_2 INTEGER NOT NULL
CHECK(num_2 BETWEEN 1 AND 47),
num_3 INTEGER NOT NULL
CHECK(num_3 BETWEEN 1 AND 47),
num_4 INTEGER NOT NULL
CHECK(num_4 BETWEEN 1 AND 47),
num_5 INTEGER NOT NULL
CHECK(num_5 BETWEEN 1 AND 47),
num_6 INTEGER NOT NULL
CHECK(num_6 BETWEEN 1 AND 47),
CONSTRAINT sort_numbers
CHECK ((num_1 < num_2)
AND (num_2 < num_3)
AND (num_3 < num_4)
AND (num_4 < num_5)
AND (num_5 < num_6))
);
Notice that I am making you sort the numbers to make them easier to
handle later. This is an easy job the front end, but you can do it in
one INSERT INTO statement if you wish.
>> NumID-PK, NumSet {1-PK, 1, 35, 23, 5, 4, 17 respectfully stored
separated by commas in the column in the database}? <<
NEVER, never do this! Look up First Normal Form (1NF) in the beginning
of any basic RDBMS book.
--CELKO--
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
- Next message: Hari Prasad: "RE: Encrypting passwords in a SQL database"
- Previous message: Hugo Kornelis: "Re: simple stored procedure question for noobie"
- In reply to: Leon: "Best Data storage practice?"
- Next in thread: Leon: "Re: Best Data storage practice?"
- Reply: Leon: "Re: Best Data storage practice?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|