Showing posts with label unique. Show all posts
Showing posts with label unique. Show all posts

Wednesday, March 28, 2012

How to force unique entries in a linking table?

I have a table 'Group2Operation' that stores many to many relations
between the 'Group' table and the 'Operation' table (each group is has
permission to perform one or more of the available operations)

PROBLEM
=======
I need to prevent duplicate entries being created. e.g. lets say that
in the 'Group2Operation' table a record links the 'editor' group to
the 'publish' operation. Should I prevent an administrator creating a
duplicate of that record? (Otherwise deleting that permission will
have to be done twice or more for it to be effective)

SOLUTION?
=========
So far I've done this with a trigger:

CREATE TRIGGER Group2OperationDuplicates ON dbo.Group2Operation
FOR INSERT, UPDATE
AS UPDATE Group2Operation
SET NoDuplicate = CONVERT(nvarchar(10),GroupID) + OperationTag

The 'NoDuplicate' unique index column in the Group2Operation table
stores a concatenation of the unique group and operation identifiers.
So when an attempt is made to create a record, the trigger is fired.
If there is a duplicate, this will mean a duplicate entry in the
'NoDuplicate' column. As a result, the INSERT or UPDATE will fail and
the duplication will be prevented.

WHAT DO YOU THINK?
==================
What do you think? Am I going about this in the right way? Is a
trigger a good way to do this or should I rely on application logic to
prevent duplicates?

Any help appreciated by this db novice.
John GristConsider creating a PRIMARY KEY or UNIQUE constraint on the Group2Operation
table. This will ensure duplicate entries cannot be inserted. There is no
need to concatenate values or use a separate table since you can specify a
composite key:

ALTER TABLE Group2Operation
ADD CONSTRAINT PK_Group2Operation
PRIMARY KEY (GroupID, OperationTag)

BTW, it's a good practice to post DDL (create table) when posting questions
to this forum.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"grist2mill" <grist2mill@.excite.com> wrote in message
news:46e240e.0409030059.79f2f36d@.posting.google.co m...
>I have a table 'Group2Operation' that stores many to many relations
> between the 'Group' table and the 'Operation' table (each group is has
> permission to perform one or more of the available operations)
> PROBLEM
> =======
> I need to prevent duplicate entries being created. e.g. lets say that
> in the 'Group2Operation' table a record links the 'editor' group to
> the 'publish' operation. Should I prevent an administrator creating a
> duplicate of that record? (Otherwise deleting that permission will
> have to be done twice or more for it to be effective)
> SOLUTION?
> =========
> So far I've done this with a trigger:
> CREATE TRIGGER Group2OperationDuplicates ON dbo.Group2Operation
> FOR INSERT, UPDATE
> AS UPDATE Group2Operation
> SET NoDuplicate = CONVERT(nvarchar(10),GroupID) + OperationTag
> The 'NoDuplicate' unique index column in the Group2Operation table
> stores a concatenation of the unique group and operation identifiers.
> So when an attempt is made to create a record, the trigger is fired.
> If there is a duplicate, this will mean a duplicate entry in the
> 'NoDuplicate' column. As a result, the INSERT or UPDATE will fail and
> the duplication will be prevented.
> WHAT DO YOU THINK?
> ==================
> What do you think? Am I going about this in the right way? Is a
> trigger a good way to do this or should I rely on application logic to
> prevent duplicates?
> Any help appreciated by this db novice.
> John Grist|||That looks much better. I though there ought to be a better way.
Many thanks for your help
John

"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message news:<uh_Zc.14996$lu3.12768@.newssvr24.news.prodigy.com>...
> Consider creating a PRIMARY KEY or UNIQUE constraint on the Group2Operation
> table. This will ensure duplicate entries cannot be inserted. There is no
> need to concatenate values or use a separate table since you can specify a
> composite key:
> ALTER TABLE Group2Operation
> ADD CONSTRAINT PK_Group2Operation
> PRIMARY KEY (GroupID, OperationTag)
> BTW, it's a good practice to post DDL (create table) when posting questions
> to this forum.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "grist2mill" <grist2mill@.excite.com> wrote in message
> news:46e240e.0409030059.79f2f36d@.posting.google.co m...
> >I have a table 'Group2Operation' that stores many to many relations
> > between the 'Group' table and the 'Operation' table (each group is has
> > permission to perform one or more of the available operations)
> > PROBLEM
> > =======
> > I need to prevent duplicate entries being created. e.g. lets say that
> > in the 'Group2Operation' table a record links the 'editor' group to
> > the 'publish' operation. Should I prevent an administrator creating a
> > duplicate of that record? (Otherwise deleting that permission will
> > have to be done twice or more for it to be effective)
> > SOLUTION?
> > =========
> > So far I've done this with a trigger:
> > CREATE TRIGGER Group2OperationDuplicates ON dbo.Group2Operation
> > FOR INSERT, UPDATE
> > AS UPDATE Group2Operation
> > SET NoDuplicate = CONVERT(nvarchar(10),GroupID) + OperationTag
> > The 'NoDuplicate' unique index column in the Group2Operation table
> > stores a concatenation of the unique group and operation identifiers.
> > So when an attempt is made to create a record, the trigger is fired.
> > If there is a duplicate, this will mean a duplicate entry in the
> > 'NoDuplicate' column. As a result, the INSERT or UPDATE will fail and
> > the duplication will be prevented.
> > WHAT DO YOU THINK?
> > ==================
> > What do you think? Am I going about this in the right way? Is a
> > trigger a good way to do this or should I rely on application logic to
> > prevent duplicates?
> > Any help appreciated by this db novice.
> > John Grist

Friday, March 23, 2012

How to findout if the column is Unique or Key?


I use sp_columns to retrieve colum information of a table.
But, it doesn't return the columns' index information such as Key or Unique..

How can I get these info along with those from sp_columns?

You want to take a look at sp_helpindex, sp_pkeys, sp_foreignkey stored procedures. Also, you want to take a look at info views (i.e. information_schema.*).

How to find unique indexes in SQLserver 2005

How do I find unique indexes in SQLserver 2005.
Is there a website/page which does describe the information_schema and gives
tips etc.
Now I want to find all unique constraints and unique indexes in a given
database.
Thanks for your time and attention.
ben brugmanBen,
this should do for you:
select * from sys.indexes
where is_unique = 1 or is_unique_constraint = 1
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .|||select sysobjects.name from sysobjects join
sysindexes on sysobjects.id=sysindexes.id
where sysindexes.status=2 and type='u'
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"ben brugman" <ben@.niethier.nl> wrote in message
news:uhpOyAg5GHA.2208@.TK2MSFTNGP04.phx.gbl...
> How do I find unique indexes in SQLserver 2005.
> Is there a website/page which does describe the information_schema and
> gives tips etc.
> Now I want to find all unique constraints and unique indexes in a given
> database.
> Thanks for your time and attention.
> ben brugman
>|||Sorry, bad answer. Try this.
--unique indexes
select sysobjects.name,sysindexes.name,sysindexes.status from sysobjects
join
sysindexes on sysobjects.id=sysindexes.id
where (sysindexes.status =2 )
and type='u'
--unique constrains
select name, object_name(parent_object_id) from sys.key_constraints
where type='uq'
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
news:uMQ8aVg5GHA.1252@.TK2MSFTNGP04.phx.gbl...
> select sysobjects.name from sysobjects join
> sysindexes on sysobjects.id=sysindexes.id
> where sysindexes.status=2 and type='u'
>
> --
> Hilary Cotter
> Director of Text Mining and Database Strategy
> RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
> This posting is my own and doesn't necessarily represent RelevantNoise's
> positions, strategies or opinions.
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
>
> "ben brugman" <ben@.niethier.nl> wrote in message
> news:uhpOyAg5GHA.2208@.TK2MSFTNGP04.phx.gbl...
>> How do I find unique indexes in SQLserver 2005.
>> Is there a website/page which does describe the information_schema and
>> gives tips etc.
>> Now I want to find all unique constraints and unique indexes in a given
>> database.
>> Thanks for your time and attention.
>> ben brugman
>

Wednesday, March 21, 2012

How to find unique indexes in SQLserver 2005

How do I find unique indexes in SQLserver 2005.
Is there a website/page which does describe the information_schema and gives
tips etc.
Now I want to find all unique constraints and unique indexes in a given
database.
Thanks for your time and attention.
ben brugman
Ben,
this should do for you:
select * from sys.indexes
where is_unique = 1 or is_unique_constraint = 1
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .
|||select sysobjects.name from sysobjects join
sysindexes on sysobjects.id=sysindexes.id
where sysindexes.status=2 and type='u'
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"ben brugman" <ben@.niethier.nl> wrote in message
news:uhpOyAg5GHA.2208@.TK2MSFTNGP04.phx.gbl...
> How do I find unique indexes in SQLserver 2005.
> Is there a website/page which does describe the information_schema and
> gives tips etc.
> Now I want to find all unique constraints and unique indexes in a given
> database.
> Thanks for your time and attention.
> ben brugman
>
|||Sorry, bad answer. Try this.
--unique indexes
select sysobjects.name,sysindexes.name,sysindexes.status from sysobjects
join
sysindexes on sysobjects.id=sysindexes.id
where (sysindexes.status =2 )
and type='u'
--unique constrains
select name, object_name(parent_object_id) from sys.key_constraints
where type='uq'
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
news:uMQ8aVg5GHA.1252@.TK2MSFTNGP04.phx.gbl...
> select sysobjects.name from sysobjects join
> sysindexes on sysobjects.id=sysindexes.id
> where sysindexes.status=2 and type='u'
>
> --
> Hilary Cotter
> Director of Text Mining and Database Strategy
> RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
> This posting is my own and doesn't necessarily represent RelevantNoise's
> positions, strategies or opinions.
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
>
> "ben brugman" <ben@.niethier.nl> wrote in message
> news:uhpOyAg5GHA.2208@.TK2MSFTNGP04.phx.gbl...
>

How to find unique indexes in SQLserver 2005

How do I find unique indexes in SQLserver 2005.
Is there a website/page which does describe the information_schema and gives
tips etc.
Now I want to find all unique constraints and unique indexes in a given
database.
Thanks for your time and attention.
ben brugmanBen,
this should do for you:
select * from sys.indexes
where is_unique = 1 or is_unique_constraint = 1
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .|||select sysobjects.name from sysobjects join
sysindexes on sysobjects.id=sysindexes.id
where sysindexes.status=2 and type='u'
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"ben brugman" <ben@.niethier.nl> wrote in message
news:uhpOyAg5GHA.2208@.TK2MSFTNGP04.phx.gbl...
> How do I find unique indexes in SQLserver 2005.
> Is there a website/page which does describe the information_schema and
> gives tips etc.
> Now I want to find all unique constraints and unique indexes in a given
> database.
> Thanks for your time and attention.
> ben brugman
>|||Sorry, bad answer. Try this.
--unique indexes
select sysobjects.name,sysindexes.name,sysindexes.status from sysobjects
join
sysindexes on sysobjects.id=sysindexes.id
where (sysindexes.status =2 )
and type='u'
--unique constrains
select name, object_name(parent_object_id) from sys.key_constraints
where type='uq'
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
news:uMQ8aVg5GHA.1252@.TK2MSFTNGP04.phx.gbl...
> select sysobjects.name from sysobjects join
> sysindexes on sysobjects.id=sysindexes.id
> where sysindexes.status=2 and type='u'
>
> --
> Hilary Cotter
> Director of Text Mining and Database Strategy
> RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
> This posting is my own and doesn't necessarily represent RelevantNoise's
> positions, strategies or opinions.
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
>
> "ben brugman" <ben@.niethier.nl> wrote in message
> news:uhpOyAg5GHA.2208@.TK2MSFTNGP04.phx.gbl...
>

how to find the unique key and foreign key reference given the table name in MS SQL

hii...
i m trying to find the Unique key and the foreign key reference in MS-SQL.
plz any one advice me how to do this.
thank u.
radhamohan

Quote:

Originally Posted by radhamohan

hii...
i m trying to find the Unique key and the foreign key reference in MS-SQL.
plz any one advice me how to do this.
thank u.
radhamohan


Hi. You will need to explain this in much more detail. Not sure what you mean by 'reference'sql

Monday, March 12, 2012

How to find out tables which cant be replicated

Is there any query to find out all the tables without a Primary key or without a Unique index ?select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME not in(
select TABLE_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE in('PRIMARY KEY', 'UNIQUE')
)|||I am assuming for Transactional Replication, minimum requirement is a unique index !

Thanks for above query|||BOL:

Microsoft SQL Server 2000 automatically creates unique indexes to enforce the uniqueness requirements of PRIMARY KEY and UNIQUE constraints.

My query returns list of tables without PRIMARY KEY or UNIQUE constraints. Try it on your database.

Former Kentuckian.