SQL Server 2008 Database Design - sql-server

Background: In SQL Server, my database table only has three columns:
DataID (PK, bigint)
ProductName (Nchar20)
Price (float)
Every day it will automatically insert about 5000+ rows of data into this table. Later on this table may add more columns like AdjustedPrice (float), Discount (float)...
Question: I know I should probably make ProductName as FK to another table (ProductNameTable with ProductID (PK,int) and ProductName (Nchar), but what about the price (float)? Price are depends, could range to 100 - 1000, should I make it FK to another table or just leave it alone in the table?
Any advice from DBA? Thank you sooooo much.....

Yeah. No. Price is an atmic element. But my advice would be to get a copy of the Data Model Ressoure Book volume 1 - it discusses database schemata for pricing engines. A good read.

Related

returning identity value in Visual Studio (SQL Server)

I have a project in Visual Studio where I need to use two tables, Bill and bill detail.
Bill has the primary key billNumber which I made with identity. And bill detail has the billNumber as a foreign key.
I need help. How can I know the last id which was created? Visual Studio doesn't recognize top(1) or other methods.
The tables are like this:
bill bill detail
billNumer idClient amount billNumber product quantity price
You can try the following sql query to find the last id from the table.
SELECT billNumer FROM Bill WHERE billNumer = (SELECT MAX(billNumer) FROM Bill)
Result:

SQL Server : database design - composite primary key

I need your suggestion on how to implement SQL Server table relationships.
I have many customers, each holds a unique id (customerID)
each customer can have many categories associated (categoryID)
each customer can have many sub categories (subCategoryID) for
when a customer logs in I know its CustomerID, CategoryID and SubCategoryID.
customer plans in advance how many hours will work every week for the year to come (on december
2014 plans 52 weeks of 2015)
every day a customer reports if they worked or took a day off.
I thought of having a table called WeeklyPlanning with columns:
CustomerID, CategoryID, SubCategoryID, Year, WeekNumber, WorkingHoursPlan
and another table called DailyWorkingHours with columns:
Dates, WorkingHours
My questions:
I don't know how to combine these two tables together, use a compound key? (CustomerID, CategoryID, SubCategoryID, Year, WeekNumber) or maybe generate a unique PK in WeeklyPlanning that will be used as a FK in DailyWorkingHours?
I'm looking for the best way to implement this.
thanks
Do you really want to specify FIVE column's values for each JOIN between those two tables? That's what you'll have to do if you have a compound primary key made up from five columns - you need all those columns in your child table as well, and you need to always specify all five columns when doing a JOIN ..... ouch ......
If not - use a surrogate column in WeeklyPlanning (a WeeklyPlanningID INT IDENTITY(1,1)) to simplify your life significantly!

SQL Server query coding in master detail table

I have two tables master and detail
Master table has columns:
id primary
mmrec
billno
billdate
billtype
name
address
amount
Details table has columns:
id primary
purchrecno - master>mmrec
itemcode
itemqty
amount
transtype (There will always be 4 types for this )
taxtype
Besides there is a tax table which has
taxcode primary
taxdesc
percent
What I want to achieve is a summary report, which will have four rows as per billtyp
There will be two sets of columns having twice as many columns as taxtype that were used in a particular period. One set will display base amount (this will be sum of amount for a particular type of tax) another set will have sum of tax (tax is calculated using percent from tax table on amount so that base amount + tax amount equals to amount ( in details table)
obviously Master table will provide id to select details table transaction and provide
date for the details table transaction
tax table will provide tax information, and percent for tax calculation.
What I have conceived is there will 4 union for each transtype.
But somehow or other could not work out properly.
And I need your help in coding this query.
I am open to using a stored procedure or view but would prefer a query will get me 4 rows.
Thanks

Copying data from old database to new one with a new structure

I had an old database with a single table containing customer orders and customer details. I went on to create a new database model using seperate tables for customers and details. I managed to migrate the customer details to the new database, but was unable to migrate the the cusomer orders. We thought that this would be ok, and that we would just build the order record from now on ignoring all previous orders in the old database. This was a while ago, and I cannot remember the exact reason why I was unable to import the customer orders. However, now we have discovered that we will need the old orders in the new database. Is there an easy way to do this using Microsoft Access?
This is the reason why:
Split a table in access into two linked tables
Depending on how complex your schema is, a simple approach would be schema-mapping by a INSERT INTO SELECT query.
For example if your old database had a table:
Orders
------
OrdID
CustID
ProductName
Price
oDay
oMonth
oYear
And your new database had fields with different names, extra fields, etc:
OrderDetails
------
Order_ID
Customer_ID
Product
Price
DeliveryAddress
OrderDate
All you would need to do was to create an insert query to append the old records to the new table. In defining the query, you can specify the source and destination field names, and you can even perform functions / expressions on the data. You can even query on the other table directly, without linking or importing it into your new database:
INSERT INTO OrderDetails (Order_ID,Customer_ID,Product,Price,OrderDate)
SELECT OrdID,CustID,ProductName,Price,DateSerial(oYear,oMonth,oDay) AS oDate
FROM Orders IN 'C:\oldDatabasePath.mdb';
If you have to do additional transformations to the data, such as run expressions on column values, I would recommend testing out the SELECT part of the query before adding the INSERT line.

normalization of reservation database

hi im developing a online reservation of a event place my issue is in the module of ticketing if a client booked two functionroom in one ticket im not sure with my database if it is full normalized i just want to be sure please leave comment for database improvement
create table RESERVATIONS (
PK reservation_id
lname,
fname,
contact,
email,
date,
timeStart,
timeEnd
numGuest,
total)
create table FUNCTIONROOMS (
PK function_id,
FK functionroom_id,
FK reservation_id)
create table FUNCTIONROOMDETAILS(
PK functionroom_id,
functionName,
functionPrice,
functionStatus)
im planning for this query is my database normalized? or not
select functionRoom.functionRoom_id
from reservations, functionRoom
where reservation.reservation_id = functionRoom.reservation_id
and reservation_id = 'reservation_id'
Your tables doesn't seem to be normalized, there is no apparent primary key in Reservation.
Normalization is quite a topic, and there are several levels of normal forms, you can use just the ones you need.
And as Jim Garrison said in the comments queries can't be normalized, so your last question doesn't have a clear answer.

Resources