Solved

Date field doesn't seem to work in a multi column primary key

Posted on 2014-02-12
5
236 Views
Last Modified: 2014-02-12
In a new table, I setup a multi-column primary key consisting of a CustomerId (int) field, SalesId (int) field and a MailingDate (date) field. However, this is not preventing me from adding duplicate records into the table.

I suspect that a column of data type date cannot be used in a multi-column key or that there is a time component to this field that is preventing uniqueness?

If true, can someone suggest an alternate method of preventing duplicate records in a table with the same Customer Id, Sales Id and Mailing Date.

Thanks.
0
Comment
Question by:dwcummings
5 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
you could break out the date field to three month / day / year int columns.

Why include the mailing date as a primary field at all though?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I concur with above.
You could have one field with DATE datatype instead of DAYTIME for the primary key part. However if the time portion is needed for other processes you will need two field.
0
 
LVL 3

Author Closing Comment

by:dwcummings
Comment Utility
The mailing date is the only unique column in the table, as customers can have multiple mailings from the same salesperson, but not on the same date. I like the idea of breaking out the date into 3 int columns, but have decided to add an additional column to the table to force uniqueness - that being a mailing id field. I don't need a time component.

Thanks for the help.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
There is nothing that prevents a date or datetime column from participating in the primary key or other indexes.  If it's a datetime column, then the entire time would be considered, not just the date.
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
Mail ID would be the way to go, this way oyu have a reference to all other info about that mailing.  Just my 2 cents.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

772 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

10 Experts available now in Live!

Get 1:1 Help Now