Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-02-12
5
Medium Priority
?
256 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 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 39854132
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39854155
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
ID: 39854215
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 70

Expert Comment

by:Scott Pletcher
ID: 39854219
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 41

Expert Comment

by:Kyle Abrahams
ID: 39854291
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

810 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