显示标签为“key”的博文。显示所有博文
显示标签为“key”的博文。显示所有博文

2012年2月25日星期六

Disabling primary key


Hi
i have a table with primary key defined on col1 and col2. now i want to
have col3 also included in primary key. when i alter the table it gives
me error for duplicate rows. there is an option for 'with nocheck' but
it only works with check or foreign key constraint. is there any option
in sql server like in oracle 'no validate' which doesnt validate the
existing data and force the data validation from new records.
thanx
Farid

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:40b49d58$0$209$75868355@.news.frii.net...
>
> Hi
> i have a table with primary key defined on col1 and col2. now i want to
> have col3 also included in primary key. when i alter the table it gives
> me error for duplicate rows. there is an option for 'with nocheck' but
> it only works with check or foreign key constraint. is there any option
> in sql server like in oracle 'no validate' which doesnt validate the
> existing data and force the data validation from new records.
> thanx
> Farid
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!

No - CHECK/NOCHECK is for foreign keys and check constraints only. I'm not
entirely sure I understand your post - are you saying that you want to allow
duplicate values in a primary key? If so, then it's not possible, and
shouldn't be. Perhaps if you can post some more details (the table DDL and
sample data), someone may be able to suggest an alternative approach.

Simon|||"Simon Hayes" <sql@.hayes.ch> wrote in message
news:40b4d73d_1@.news.bluewin.ch...
> "Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
> news:40b49d58$0$209$75868355@.news.frii.net...
> > Hi
> > i have a table with primary key defined on col1 and col2. now i want to
> > have col3 also included in primary key. when i alter the table it gives
> > me error for duplicate rows. there is an option for 'with nocheck' but
> > it only works with check or foreign key constraint. is there any option
> > in sql server like in oracle 'no validate' which doesnt validate the
> > existing data and force the data validation from new records.
> > thanx
> > Farid
> > *** Sent via Developersdex http://www.developersdex.com ***
> > Don't just participate in USENET...get rewarded for it!
> No - CHECK/NOCHECK is for foreign keys and check constraints only. I'm not
> entirely sure I understand your post - are you saying that you want to
allow
> duplicate values in a primary key? If so, then it's not possible, and
> shouldn't be. Perhaps if you can post some more details (the table DDL and
> sample data), someone may be able to suggest an alternative approach.

I think he wants: SET IDENTITY_INSERT.

> Simon|||>> I have a table with primary key defined on col1 and col2. Now I
want to
have col3 also included in primary key. When I alter the table it
gives me error for duplicate rows. <<

That does not make sense to me. Given this

CREATE TABLE Foobar
(col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
col3 INTEGER NOT NULL,
PRIMARY KEY (col1, col2),
..);

Then a superkey is still unique:

CREATE TABLE Foobar
(col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
col3 INTEGER NOT NULL,
PRIMARY KEY (col1, col2, col3),
..);|||i think u people didn't understand the problem.
i have created table
create table test(col1 int, col2 int, col3 int,col4...)
Primary key(col1,col2)
as there is primary key on col1 and col2 no duolicate data can exist in
them now the scenario changed i have to change the primary key on the
table. now when i alter the table
alter table test primary key (col1, col3, col4) it gives me error
duplicate rows exist.
but in my scenario i want the existing duplication to remain in the
table. and the primary key enforcement starts from new data.
in oracle there is an option of 'no validate' which doesnt check the
existing data in the table but enforce the uniqueness of data from new
records. i want to know is there any option available in sql server
which doesnt check the existing data but enforces the uniqueness of
records from new records.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||Ghulam Farid <gfaryd@.yahoo.com> wrote in message news:<40b57e55$0$207$75868355@.news.frii.net>...
> i think u people didn't understand the problem.
> i have created table
> create table test(col1 int, col2 int, col3 int,col4...)
> Primary key(col1,col2)
> as there is primary key on col1 and col2 no duolicate data can exist in
> them now the scenario changed i have to change the primary key on the
> table. now when i alter the table
> alter table test primary key (col1, col3, col4) it gives me error
> duplicate rows exist.
> but in my scenario i want the existing duplication to remain in the
> table. and the primary key enforcement starts from new data.
> in oracle there is an option of 'no validate' which doesnt check the
> existing data in the table but enforce the uniqueness of data from new
> records. i want to know is there any option available in sql server
> which doesnt check the existing data but enforces the uniqueness of
> records from new records.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!

It sounds like you want to place a primary key on columns which
contain duplicates, but you want to ignore those duplicates and only
enforce the primary key for new values? If so, then it's not possible
- all values in a primary key must always be unique, otherwise it
couldn't be a primary key.

Simon

create table dbo.Test (
col1 int not null,
col2 int not null,
col3 int not null,
col4 int not null,
constraint PK_Test primary key (col1, col2)
)
go

insert into dbo.Test
select 1,1,1,1
union all
select 1,2,1,1
union all
select 3,1,1,1
go

alter table dbo.Test
drop constraint PK_Test

-- This will always fail because of duplicate data
alter table dbo.Test
add constraint PK_Test primary key (col1, col3, col4)
go

drop table dbo.Test
go

Disabling Foreign Key Constraint in Trigger

Hey,
Can anybody tell me how to disable a Foreign Key contraint from executing
while a Trigger is executing on DELETE event?
Best regards,
BillDRI are checked berofe the AFTER TRIGGERS, so if it is a problem the trigger
will not be fired.
AMB
"AST" wrote:

> Hey,
> Can anybody tell me how to disable a Foreign Key contraint from executing
> while a Trigger is executing on DELETE event?
> Best regards,
> Bill
>
>|||Hey AMB,
The table [HistoryDetails] has a FK on the column HistoryID that is being
used to enforce integrity with the table[History].
I am not sure if you are stating you can't disable the FK or if I have to
modify the context of the Trigger?
Best regards,
Bill
The Trigger is defined as:
/****** Object: Trigger dbo.TRIGGER_CascDeleteOnHistoryDetails Script
Date: 5/24/2002 12:14:53 AM ******/
CREATE TRIGGER [TRIGGER_CascDeleteOnHistoryDetails] ON dbo.History
FOR DELETE
AS
DELETE HistoryDetails FROM deleted, HistoryDetails WHERE deleted.HistoryID =
HistoryDetails.HistoryID
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:4662B0B5-25C5-4217-84F0-B75E5ECCF604@.microsoft.com...
> DRI are checked berofe the AFTER TRIGGERS, so if it is a problem the
trigger
> will not be fired.
>
> AMB
>
> "AST" wrote:
>
executing|||That will make quite a mess:
1) Disabling a Foreign Key constraint is Data Definition Language, and can
only be executed by the table owner and members of the symin fixed server
roles and the db_owner and db_ddladmin database roles, and is not
transferable. So you have to give your users a large number of unnecessary
permissions.
2) Disabling a foreign key works for all connections, not just the current
one. So while you disable it in your trigger, someone else might need it.
What exactly are you trying to achieve? This is definitely not the right
way.
Jacco Schalkwijk
SQL Server MVP
"AST" <no_reply@.please.com> wrote in message
news:O50z0N6EFHA.3672@.TK2MSFTNGP14.phx.gbl...
> Hey,
> Can anybody tell me how to disable a Foreign Key contraint from executing
> while a Trigger is executing on DELETE event?
> Best regards,
> Bill
>|||Hey Jacco,
What I am trying to achieve is the following:
I have 2 tables [History], [HistoryDetails] where the Primary Table
[History] mantains detailed data in the [HistoryDetails] table. The key
relationship is History.HistoryID=> HistoryDetails.HistoryID.
I have place a constraint on the [HistoryDetails] table such that a record
cannot be added unless the HistoryID is existing in the [History] table.
I had also created a trigger to delete all relational records in the
[HistoryDetails] table when the Primary Key record in the [History] table
was deleted.
This is where I run into problems because the execution of the trigger fails
due to the FK Constraint.
Any suggestions would be very welcome?
Best regards,
Bill
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:uEUZEs6EFHA.2700@.TK2MSFTNGP14.phx.gbl...
> That will make quite a mess:
> 1) Disabling a Foreign Key constraint is Data Definition Language, and can
> only be executed by the table owner and members of the symin fixed
server
> roles and the db_owner and db_ddladmin database roles, and is not
> transferable. So you have to give your users a large number of unnecessary
> permissions.
> 2) Disabling a foreign key works for all connections, not just the current
> one. So while you disable it in your trigger, someone else might need it.
> What exactly are you trying to achieve? This is definitely not the right
> way.
> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "AST" <no_reply@.please.com> wrote in message
> news:O50z0N6EFHA.3672@.TK2MSFTNGP14.phx.gbl...
executing
>|||Drop the trigger and use the ON DELETE CASCADE option on the foreign
key constraint.
David Portas
SQL Server MVP
--|||Hey David,
I don't think this is supported in SQL 7 and I have to support both SQL7 and
SQL2000
I tried running the SQL to add this constraint in SQL7 and it fails and
doesn't seem to recognize anthing to do with cascaded deletes?
Best regards,
Bill
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1108507933.414888.306670@.l41g2000cwc.googlegroups.com...
> Drop the trigger and use the ON DELETE CASCADE option on the foreign
> key constraint.
> --
> David Portas
> SQL Server MVP
> --
>|||AST
Well, you have to drop a FK constraint and to allow deletion within a
trigger.
"AST" <no_reply@.please.com> wrote in message
news:emTg1W8EFHA.624@.TK2MSFTNGP09.phx.gbl...
> Hey David,
> I don't think this is supported in SQL 7 and I have to support both SQL7
and
> SQL2000
> I tried running the SQL to add this constraint in SQL7 and it fails and
> doesn't seem to recognize anthing to do with cascaded deletes?
> Best regards,
> Bill
> "David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
> news:1108507933.414888.306670@.l41g2000cwc.googlegroups.com...
>

Disabling DISTINCT of certain selects please help!

Hi everyone,
Hope you can help me with this. I'm at my wits end. I have a table
with 5 fields in it, one of which is the key. I'm doing a:
SELECT DISTINCT column2, column3
FROM tableName
but the problem is though I need the other fields but do not want the
DISTINCT keyword to wortk on them. Do you know what I mean? In other
words:
SELECT DISTINCT column2, column3, column1, column4
FROM tableName
where column1 is the primary key and both column1 & column4 are not
effect by the distinct keyword. Can anyone out there help me please?
Any comments/suggestions/code-samples much appreciated.
Cheers,
Al.No, I don't know what you mean. Can you post some sample data and sample
output that you would like from your query?
Adam Machanic
SQL Server MVP - http://sqlblog.com
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
<almurph@.altavista.com> wrote in message
news:1183653609.489518.122550@.q75g2000hsh.googlegroups.com...
> Hi everyone,
>
> Hope you can help me with this. I'm at my wits end. I have a table
> with 5 fields in it, one of which is the key. I'm doing a:
> SELECT DISTINCT column2, column3
> FROM tableName
>
> but the problem is though I need the other fields but do not want the
> DISTINCT keyword to wortk on them. Do you know what I mean? In other
> words:
> SELECT DISTINCT column2, column3, column1, column4
> FROM tableName
>
> where column1 is the primary key and both column1 & column4 are not
> effect by the distinct keyword. Can anyone out there help me please?
> Any comments/suggestions/code-samples much appreciated.
> Cheers,
> Al.
>|||On 5 Jul, 17:40, "almu...@.altavista.com" <almu...@.altavista.com>
wrote:
> Hi everyone,
> Hope you can help me with this. I'm at my wits end. I have a tabl
e
> with 5 fields in it, one of which is the key. I'm doing a:
> SELECT DISTINCT column2, column3
> FROM tableName
> but the problem is though I need the other fields but do not want
the
> DISTINCT keyword to wortk on them. Do you know what I mean? In other
> words:
> SELECT DISTINCT column2, column3, column1, column4
> FROM tableName
> where column1 is the primary key and both column1 & column4 are no
t
> effect by the distinct keyword. Can anyone out there help me please?
> Any comments/suggestions/code-samples much appreciated.
> Cheers,
> Al.
You haven't explained which values you want to see for Column1 and
Column4. If you only want one row for each value of Column2 and
Column3 then there has to be some selection criterion for the other
columns. For example you might want the MIN or MAX values:
SELECT col2, col3,
MIN(col1) AS col1, MIN(col4) AS col4
FROM TableName
GROUP BY col2, col3;
Or maybe you would want the row with the first (minimum) primary key
value for each distinct Column2 and Column3:
SELECT col2, col3, col1, col4
FROM TableName AS t
WHERE col1 =
(SELECT MIN(col1)
FROM TableName
WHERE col2 = t.col2
AND col3 = t.col3);
Hope this helps.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||On Jul 5, 5:50 pm, David Portas
<REMOVE_BEFORE_REPLYING_dpor...@.acm.org> wrote:
> On 5 Jul, 17:40, "almu...@.altavista.com" <almu...@.altavista.com>
> wrote:
>
>
>
>
>
>
>
>
>
> You haven't explained which values you want to see for Column1 and
> Column4. If you only want one row for each value of Column2 and
> Column3 then there has to be some selection criterion for the other
> columns. For example you might want the MIN or MAX values:
> SELECT col2, col3,
> MIN(col1) AS col1, MIN(col4) AS col4
> FROM TableName
> GROUP BY col2, col3;
> Or maybe you would want the row with the first (minimum) primary key
> value for each distinct Column2 and Column3:
> SELECT col2, col3, col1, col4
> FROM TableName AS t
> WHERE col1 =
> (SELECT MIN(col1)
> FROM TableName
> WHERE col2 = t.col2
> AND col3 = t.col3);
> Hope this helps.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:http://msdn2.microsoft.com/library/ms130214(en-US,
SQL.90).aspx
> -- Hide quoted text -
> - Show quoted text -
Hi I just want to see the values them seleves nothing else.|||On 5 Jul, 17:52, "almu...@.altavista.com" <almu...@.altavista.com>
wrote:
> Hi I just want to see the values them seleves nothing else.
>
I'm sorry, but that doesn't explain anything. You are saying you want
to select only certain values for columns 1 and 4 - correct? But you
aren't explaining *which* values you want to select.
Please post enough information to reproduce the problem:
1. A CREATE TABLE statement (include the key constraints please).
2. A few INSERT statements to generate some data.
3. Show what end result you want based on that sample data.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

Disabling DISTINCT of certain selects please help!

Hi everyone,
Hope you can help me with this. I'm at my wits end. I have a table
with 5 fields in it, one of which is the key. I'm doing a:
SELECT DISTINCT column2, column3
FROM tableName
but the problem is though I need the other fields but do not want the
DISTINCT keyword to wortk on them. Do you know what I mean? In other
words:
SELECT DISTINCT column2, column3, column1, column4
FROM tableName
where column1 is the primary key and both column1 & column4 are not
effect by the distinct keyword. Can anyone out there help me please?
Any comments/suggestions/code-samples much appreciated.
Cheers,
Al.On 5 Jul, 17:40, "almu...@.altavista.com" <almu...@.altavista.com>
wrote:
> Hi everyone,
> Hope you can help me with this. I'm at my wits end. I have a table
> with 5 fields in it, one of which is the key. I'm doing a:
> SELECT DISTINCT column2, column3
> FROM tableName
> but the problem is though I need the other fields but do not want the
> DISTINCT keyword to wortk on them. Do you know what I mean? In other
> words:
> SELECT DISTINCT column2, column3, column1, column4
> FROM tableName
> where column1 is the primary key and both column1 & column4 are not
> effect by the distinct keyword. Can anyone out there help me please?
> Any comments/suggestions/code-samples much appreciated.
> Cheers,
> Al.
You haven't explained which values you want to see for Column1 and
Column4. If you only want one row for each value of Column2 and
Column3 then there has to be some selection criterion for the other
columns. For example you might want the MIN or MAX values:
SELECT col2, col3,
MIN(col1) AS col1, MIN(col4) AS col4
FROM TableName
GROUP BY col2, col3;
Or maybe you would want the row with the first (minimum) primary key
value for each distinct Column2 and Column3:
SELECT col2, col3, col1, col4
FROM TableName AS t
WHERE col1 = (SELECT MIN(col1)
FROM TableName
WHERE col2 = t.col2
AND col3 = t.col3);
Hope this helps.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||On Jul 5, 5:50 pm, David Portas
<REMOVE_BEFORE_REPLYING_dpor...@.acm.org> wrote:
> On 5 Jul, 17:40, "almu...@.altavista.com" <almu...@.altavista.com>
> wrote:
>
>
> > Hi everyone,
> > Hope you can help me with this. I'm at my wits end. I have a table
> > with 5 fields in it, one of which is the key. I'm doing a:
> > SELECT DISTINCT column2, column3
> > FROM tableName
> > but the problem is though I need the other fields but do not want the
> > DISTINCT keyword to wortk on them. Do you know what I mean? In other
> > words:
> > SELECT DISTINCT column2, column3, column1, column4
> > FROM tableName
> > where column1 is the primary key and both column1 & column4 are not
> > effect by the distinct keyword. Can anyone out there help me please?
> > Any comments/suggestions/code-samples much appreciated.
> > Cheers,
> > Al.
> You haven't explained which values you want to see for Column1 and
> Column4. If you only want one row for each value of Column2 and
> Column3 then there has to be some selection criterion for the other
> columns. For example you might want the MIN or MAX values:
> SELECT col2, col3,
> MIN(col1) AS col1, MIN(col4) AS col4
> FROM TableName
> GROUP BY col2, col3;
> Or maybe you would want the row with the first (minimum) primary key
> value for each distinct Column2 and Column3:
> SELECT col2, col3, col1, col4
> FROM TableName AS t
> WHERE col1 => (SELECT MIN(col1)
> FROM TableName
> WHERE col2 = t.col2
> AND col3 = t.col3);
> Hope this helps.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> -- Hide quoted text -
> - Show quoted text -
Hi I just want to see the values them seleves nothing else.|||No, I don't know what you mean. Can you post some sample data and sample
output that you would like from your query?
Adam Machanic
SQL Server MVP - http://sqlblog.com
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
<almurph@.altavista.com> wrote in message
news:1183653609.489518.122550@.q75g2000hsh.googlegroups.com...
> Hi everyone,
>
> Hope you can help me with this. I'm at my wits end. I have a table
> with 5 fields in it, one of which is the key. I'm doing a:
> SELECT DISTINCT column2, column3
> FROM tableName
>
> but the problem is though I need the other fields but do not want the
> DISTINCT keyword to wortk on them. Do you know what I mean? In other
> words:
> SELECT DISTINCT column2, column3, column1, column4
> FROM tableName
>
> where column1 is the primary key and both column1 & column4 are not
> effect by the distinct keyword. Can anyone out there help me please?
> Any comments/suggestions/code-samples much appreciated.
> Cheers,
> Al.
>|||On 5 Jul, 17:52, "almu...@.altavista.com" <almu...@.altavista.com>
wrote:
> Hi I just want to see the values them seleves nothing else.
>
I'm sorry, but that doesn't explain anything. You are saying you want
to select only certain values for columns 1 and 4 - correct? But you
aren't explaining *which* values you want to select.
Please post enough information to reproduce the problem:
1. A CREATE TABLE statement (include the key constraints please).
2. A few INSERT statements to generate some data.
3. Show what end result you want based on that sample data.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

2012年2月24日星期五

Disabling DISTINCT of certain selects please help!

Hi everyone,
Hope you can help me with this. I'm at my wits end. I have a table
with 5 fields in it, one of which is the key. I'm doing a:
SELECT DISTINCT column2, column3
FROM tableName
but the problem is though I need the other fields but do not want the
DISTINCT keyword to wortk on them. Do you know what I mean? In other
words:
SELECT DISTINCT column2, column3, column1, column4
FROM tableName
where column1 is the primary key and both column1 & column4 are not
effect by the distinct keyword. Can anyone out there help me please?
Any comments/suggestions/code-samples much appreciated.
Cheers,
Al.
No, I don't know what you mean. Can you post some sample data and sample
output that you would like from your query?

Adam Machanic
SQL Server MVP - http://sqlblog.com
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
<almurph@.altavista.com> wrote in message
news:1183653609.489518.122550@.q75g2000hsh.googlegr oups.com...
> Hi everyone,
>
> Hope you can help me with this. I'm at my wits end. I have a table
> with 5 fields in it, one of which is the key. I'm doing a:
> SELECT DISTINCT column2, column3
> FROM tableName
>
> but the problem is though I need the other fields but do not want the
> DISTINCT keyword to wortk on them. Do you know what I mean? In other
> words:
> SELECT DISTINCT column2, column3, column1, column4
> FROM tableName
>
> where column1 is the primary key and both column1 & column4 are not
> effect by the distinct keyword. Can anyone out there help me please?
> Any comments/suggestions/code-samples much appreciated.
> Cheers,
> Al.
>
|||On 5 Jul, 17:40, "almu...@.altavista.com" <almu...@.altavista.com>
wrote:
> Hi everyone,
> Hope you can help me with this. I'm at my wits end. I have a table
> with 5 fields in it, one of which is the key. I'm doing a:
> SELECT DISTINCT column2, column3
> FROM tableName
> but the problem is though I need the other fields but do not want the
> DISTINCT keyword to wortk on them. Do you know what I mean? In other
> words:
> SELECT DISTINCT column2, column3, column1, column4
> FROM tableName
> where column1 is the primary key and both column1 & column4 are not
> effect by the distinct keyword. Can anyone out there help me please?
> Any comments/suggestions/code-samples much appreciated.
> Cheers,
> Al.
You haven't explained which values you want to see for Column1 and
Column4. If you only want one row for each value of Column2 and
Column3 then there has to be some selection criterion for the other
columns. For example you might want the MIN or MAX values:
SELECT col2, col3,
MIN(col1) AS col1, MIN(col4) AS col4
FROM TableName
GROUP BY col2, col3;
Or maybe you would want the row with the first (minimum) primary key
value for each distinct Column2 and Column3:
SELECT col2, col3, col1, col4
FROM TableName AS t
WHERE col1 =
(SELECT MIN(col1)
FROM TableName
WHERE col2 = t.col2
AND col3 = t.col3);
Hope this helps.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
|||On Jul 5, 5:50 pm, David Portas
<REMOVE_BEFORE_REPLYING_dpor...@.acm.org> wrote:
> On 5 Jul, 17:40, "almu...@.altavista.com" <almu...@.altavista.com>
> wrote:
>
>
>
>
>
>
> You haven't explained which values you want to see for Column1 and
> Column4. If you only want one row for each value of Column2 and
> Column3 then there has to be some selection criterion for the other
> columns. For example you might want the MIN or MAX values:
> SELECT col2, col3,
> MIN(col1) AS col1, MIN(col4) AS col4
> FROM TableName
> GROUP BY col2, col3;
> Or maybe you would want the row with the first (minimum) primary key
> value for each distinct Column2 and Column3:
> SELECT col2, col3, col1, col4
> FROM TableName AS t
> WHERE col1 =
> (SELECT MIN(col1)
> FROM TableName
> WHERE col2 = t.col2
> AND col3 = t.col3);
> Hope this helps.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> -- Hide quoted text -
> - Show quoted text -
Hi I just want to see the values them seleves nothing else.
|||On 5 Jul, 17:52, "almu...@.altavista.com" <almu...@.altavista.com>
wrote:
> Hi I just want to see the values them seleves nothing else.
>
I'm sorry, but that doesn't explain anything. You are saying you want
to select only certain values for columns 1 and 4 - correct? But you
aren't explaining *which* values you want to select.
Please post enough information to reproduce the problem:
1. A CREATE TABLE statement (include the key constraints please).
2. A few INSERT statements to generate some data.
3. Show what end result you want based on that sample data.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx

Disabling constraints

Hi,
I need to disable all the foreign key and primary key constraints in a table
and reenable them at the end of my commands. I can use ALTER TABLE to do
this but I was wondering if it is a good practise. It is not maintenance
code but production code. Everything is done in a transaction but can I be
sure there is no incidence with concurent accesses ? What about the rights
for the db user ?
Thanks.
FredFrdric Mayot wrote:
> Hi,
> I need to disable all the foreign key and primary key constraints in
> a table and reenable them at the end of my commands. I can use ALTER
> TABLE to do this but I was wondering if it is a good practise. It is
> not maintenance code but production code. Everything is done in a
> transaction but can I be sure there is no incidence with concurent
> accesses ? What about the rights for the db user ?
> Thanks.
> Fred
Why would you need to disable constraints in production code?
David Gugick
Quest Software
www.imceda.com
www.quest.com|||The reason is quite simple.
Assume we have the tables
A(pkA <PK> )
B(pkfkA <PK,FK>, pkfkC <PK,FK> )
C(pkC <PK> )
with the data
A = {1}
C = {11, 12}
B = {(1, 11), (1, 12)}
Now, I want to update the two rows in B in the following manner :
(1, 11) -> (1, 12) and (1, 12) -> (1, 11).
This operation is supposed to be atomic (that's to say in a transaction)
"David Gugick" <david.gugick-nospam@.quest.com> a crit dans le message de
news: %23$J$Y0pbFHA.3040@.TK2MSFTNGP14.phx.gbl...
> Frdric Mayot wrote:
> Why would you need to disable constraints in production code?
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com|||Have you tried this?
UPDATE B
SET pkfkC = CASE pkfkC
WHEN 11 THEN 12
WHEN 12 THEN 11
END
WHERE pkfkA = 1 AND pkfkC IN (11, 12)
This update is atomic and as such doesn't cause a constraint violation error
.
"Frédéric Mayot" wrote:

> The reason is quite simple.
> Assume we have the tables
> A(pkA <PK> )
> B(pkfkA <PK,FK>, pkfkC <PK,FK> )
> C(pkC <PK> )
> with the data
> A = {1}
> C = {11, 12}
> B = {(1, 11), (1, 12)}
> Now, I want to update the two rows in B in the following manner :
> (1, 11) -> (1, 12) and (1, 12) -> (1, 11).
> This operation is supposed to be atomic (that's to say in a transaction)
>
> "David Gugick" <david.gugick-nospam@.quest.com> a écrit dans le message de
> news: %23$J$Y0pbFHA.3040@.TK2MSFTNGP14.phx.gbl...
>
>