Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Best way to enforce Duplicate checks

Author  Topic 

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-19 : 12:43:03
Hello,

Within the CRM database that I manage (that uses a SQL 2005 back end) - we have a business rule that needs to now be enforced. That is - only one named organization can exist at any given real world address.

Historically - I have not enforced this rule at either app level of the SQL Level as I continued to use the "benefit of the doubt" clause with our users - hoping that they could understand that only one org with a given name can exist at a single real address - cabut now a situation has occurred where the rules need to be applied.

So - I am writing some code for the application with compares the Orgname and Address entered to see if there are other records that match - but wanted a quick primer on how to create a constraint on SQL Server that would enforce this rule at the SQL table level.

Appreciate any tips that can point me in the right direction.

Cheers,

Bruce

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-19 : 12:54:39
1. You need to Group by the name and the address and check where HAVING COUNT(*) > 1

Then clean that up

In the mean time, prepare a unique Index on those Columns

Once you've completed cleanup, apply the index


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 14:14:31
do you already have another primary key identified in your org table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-19 : 16:00:13
quote:
Originally posted by visakh16

do you already have another primary key identified in your org table?

Yes. Org_ID is the primary key for my Org table.

Bruce

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 16:05:44
ok...then unique index should suffice

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-19 : 21:55:25
quote:
Originally posted by visakh16

ok...then unique index should suffice

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Water Closet

The Unique Index/ Constraint should be a list of all the columns that NEED to be unique

Or, Should it ONLY be the Org Name



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-20 : 15:01:19
only one named organization can exist at any given real world address

sound like indewx should be on OrgName,Address

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-21 : 13:12:02
quote:
Originally posted by visakh16

only one named organization can exist at any given real world address

sound like indewx should be on OrgName,Address




Correct. Any tips on how to apply this in SQL Management Studio?

Cheers!

B
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-21 : 13:13:59
aren't you writing code?

CREATE INDEX....

http://msdn.microsoft.com/en-us/library/ms188783.aspx



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-22 : 10:55:17
quote:
Originally posted by X002548

aren't you writing code?

CREATE INDEX....




If I were that proficient at T-SQL code - I wouldn't need to stop by here - I am hoping that folks that choose to do some things via the SQL Tools/GUI method are not totally kicked to the curb :)

B


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-22 : 11:13:08
ummm...I guess there are quite a few..then they all change over to writing code

How do you plane to Promote changes in different environments if you don't write code?

If you have several environments (dev/qa/ust/prod/DR) do you plan to manually make the changes in every environment?

It will become a HUGE Mess

If you are doing this directly in Production, then what about your dev environments?

If you really want to use the GUI

In SSMS, Click on the database folder and expand it

The Expand the tables folder

The open the folder for the table you want

Right Click on the index folder and select New Index

Off you go

God Luck, and may God Bless...




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-22 : 11:20:17
quote:
Originally posted by X002548

ummm...I guess there are quite a few..then they all change over to writing code. How do you plane to Promote changes in different environments if you don't write code?

If you have several environments (dev/qa/ust/prod/DR) do you plan to manually make the changes in every environment?




You must have me confused with some hot-shot SQL developer at some huge company with multiple servers/environments etc:)

No - I have a single production box and that's it. It's solid, works well and if our prima-donna sales people would get with the program and let me manage their database for them - we wouldn't have this issue:)

Thank you for the pointers. Will give it a shot.

B
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-03-22 : 12:31:38
quote:
Originally posted by visakh16

only one named organization can exist at any given real world address

sound like indewx should be on OrgName,Address

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I think that Addrss needs to be the unique index alone.

Adding orgname to it allows many orgnames at the same address.
Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-22 : 13:11:45
quote:
[i]Originally posted by russellI think that Address needs to be the unique index alone. Adding orgname to it allows many org-names at the same address.


Correction. In the real world - there can be may orgs at the same address. Think of the address as a building downtown with 5 floors. Could have 20 orgs in the building.

This index needs to be a combination of Org and Address.

B
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-03-22 : 13:29:23
quote:
Originally posted by BMcDonald

only one named organization can exist at any given real world address.


According to this, my statement is correct. Also, multi-busineess buildings, including high-rises, have suite numbers, which make the address unique.

If the above quoted text is not the requirement, fine. But if it is...

How are you handling the fact that businesses move?
Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-22 : 13:45:19
quote:
Originally posted by russell

quote:
Originally posted by BMcDonald

only one named organization can exist at any given real world address.


According to this, my statement is correct. Also, multi-busineess buildings, including high-rises, have suite numbers, which make the address unique.

If the above quoted text is not the requirement, fine. But if it is...

How are you handling the fact that businesses move?



1. Probably should have phrased this better(since it only really matters with orgs with multiple locations) - should be more like: For any org in the system with multiple locations - each org record must have it's own unique address. :)

2. If a business moves completely - we deactivate the master org record (keeping just a stub for historical purposes with comments) and then transfer all verified assets (people, calls, orders etc) over the a new org record that is setup for the new location. The system handles all the addressing changes etc.

Cheers,

B
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-22 : 13:56:42
quote:
Originally posted by BMcDonald
You must have me confused with some hot-shot SQL developer at some huge company with multiple servers/environments etc:)



Trust me my friend..there's the right way, and then there's everything else.

Just dump the DB and create another one for development



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

BMcDonald
Starting Member

8 Posts

Posted - 2012-03-22 : 15:23:03
quote:
[i]Originally posted by X002548Just dump the DB and create another one for development

Brett




There's also the fast way - and then there's everything else :)

If it were only that easy....and I actually enjoyed 8 meetings, 14 phone calls and 46 emails with the IT governance committee trying to explain why I needed that dev database....

B
Go to Top of Page
   

- Advertisement -