Drop and recreate primary key - sql

I want to change a primary key/index on a database table to change the index option ignore_dup_key to 'on'.
According to this question " Can I set ignore_dup_key on for a primary key? " I need to drop the index and create a new one.
Before I drop the index, how can I get the command to recreate it? Then I'll just change the ignore_dup_key option.

Right click the primary key in SSMS, and choose Script -> As Create -> To New Window

As indicated by a commenter on another related question, you can enable IGNORE_DUP_KEY for the table:
ALTER TABLE [TableName] REBUILD WITH (IGNORE_DUP_KEY = ON);
and revert:
ALTER TABLE [TableName] REBUILD WITH (IGNORE_DUP_KEY = OFF);

I did not think SQL would allow a PK with IGNORE_DUP_KEY = on so I tested on SQL 2008 R2.
Via script could create a table with PK IGNORE_DUP_KEY = on. But even with drop and create could not change a PK from off to on. What is interesting is the script ran with no error but it did not change the PK from off to on. You may find different results in your environment.

To Drop the Index
You can run
DROP INDEX [IndexName] ON [TableName]
To Create the index with Ignore_Dup_Key option
CREATE UNIQUE INDEX [IndexName] ON [TableName]([ColumnNam])
WITH (IGNORE_DUP_KEY = ON)

Related

EF Update Statement is Violating a Unique Index

I've got a bit of an issue which has exposed my lack of understanding of Uniqueness Constraints. This is the table in question and there is a unique, non-clustered index on the DataManagerId, SurgeonId and HospitalId columns of a table, as shown in this image (apologies, I don't have Visio):
I am using Entity Framework ("EF") and it has generated an UPDATE statement akin to this:
exec sp_executesql N'UPDATE [dbo].[DataManagerSurgeon]
SET [DataManagerId] = #0, [SurgeonId] = #1, [HospitalId] = #2, [DateAdded] = #3, [DateRescinded] = #4
WHERE ([Id] = #5)',N'#0 int,#1 int,#2 int,#3 datetime2(7),#4 datetime2(7),#5 int',#0=24,#1=221,#2=10001,#3='2018-01-02 08:58:29.1061413',#4='2018-01-02 08:58:29.1061413',#5=122
I'll clean that up a bit for the purposes of this question:
UPDATE [dbo].[DataManagerSurgeon]
SET [DataManagerId] = 24, [SurgeonId] = 221, [HospitalId] = 10001, [DateAdded] = '2018-01-02 08:58:29.1061413', [DateRescinded] = '2018-01-02 08:58:29.1061413'
WHERE [Id] = 122
The problem is, this is seeking to alter data (hence the update statement). And there is already a row with the combination (24,221,10001) for (DataManagerId, HospitalId,SurgeonId). As a result, the database is having none of it:
Violation of UNIQUE KEY constraint 'UC_ManagerSurgeonHospital'. Cannot insert duplicate key in object 'dbo.DataManagerSurgeon'. The duplicate key value is (24, 221, 10001).
As further background, I am only trying to update the DateRescinded column and if I were to write the statement manually, it would look like:
UPDATE [dbo].[DataManagerSurgeon]
SET [DateRescinded] = '2018-01-02 08:58:29.1061413'
WHERE [Id] = 122
I guess I learnt that you can violate a uniqueness constraint with an UPDATE statement, even when identifying the row by primary key in such a way that, at the end of the transaction, the conditions of the uniqueness constraint would still be met.
But my question remains, if EF is generating an UPDATE statement which violates a uniqueness constraint (index), how do I get around that? Note: the least optimal solution would be writing manual SQL and sending that to the database. It can be done, but defeats the purpose of using an ORM like EF.
Also note that I added the following code in the Mapping file for that table to make EF "aware" of the index:
this.HasIndex(t => new { t.DataManagerId, t.SurgeonId, t.HospitalId })
.IsUnique(true)
.IsClustered(false)
.HasName("UC_ManagerSurgeonHospital");
Final addendum - I have been referring to this as an Index and I'm not sure how SQL Server distinguishes these from CONSTRAINTS, but the create statement for such an Index looks like:
ALTER TABLE [dbo].[DataManagerSurgeon] ADD CONSTRAINT [UC_ManagerSurgeonHospital] UNIQUE NONCLUSTERED
(
[DataManagerId] ASC,
[SurgeonId] ASC,
[HospitalId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
I only mention that because people might ask why I implemented it as an Index and not a CONSTRAINT. But apparently, this is how you do multi-column constraints in SQL Server.
Thanks

SQL Server ALTER COLUMN NOT NULL has no effect

The designers of table SOME_TABLE did not define a primary key, and worse, they set one of the columns that could define the primary key as NULLable (the others are OK).
The data for SOME_TABLE.PrinterPos does not contain any NULL values.
I am writing an upgrade script to apply to ~50 databases.
The following code is failing:
ALTER TABLE dbo.SOME_TABLE
ALTER COLUMN PrinterPos smallint NOT NULL;
ALTER TABLE dbo.SOME_TABLE
ADD CONSTRAINT PK_SOME_TABLE
PRIMARY KEY CLUSTERED (SOME_TABLE_ID ASC, Store_ID ASC, PrinterPos ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY];
I get the message
Cannot define PRIMARY KEY constraint on nullable column in table 'SOME_TABLE'.
It looks line the first command is being totally ignored. Although there is no message to indicate this.
To put it in it's own batch, I have tried executing the first command using sp_executesql to no effect.
If I execute the first command in SQL Server Management Studio followed by the second then it executes OK.
I need to get this change fully automated. How can I get this to work via script?
Try adding a GO keyword between the two ALTER TABLE commands

create primary key on existing table with data

As part of a migration project, we have imported data from a JDE iSeries DB2 database. An SSIS package was created to create the destination tables and import data. The import went successfully.
Now comes the problem - The customer wants Primary Keys created in the destination DB (SQL 2008 R2). The problem table in this case, would be one table that has 104 columns and 7.5 million rows of data. The PK required for this table is composite and has 7 columns.
We are considering this :
BEGIN TRANSACTION
GO
ALTER TABLE [dbo].[F0911] ADD CONSTRAINT [F0911_PK] PRIMARY KEY CLUSTERED
(
[GLDCT] ASC,
[GLDOC] ASC,
[GLKCO] ASC,
[GLDGJ] ASC,
[GLJELN] ASC,
[GLLT] ASC,
[GLEXTL] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
or this:
-- Rename existing tables
sp_RENAME '[F0911]' , '[F0911_old]'
GO
-- Create new table
SELECT * INTO F0911 FROM F0911_old WHERE 1=0
GO
--Create PK constraints
ALTER TABLE [dbo].[F0911] ADD CONSTRAINT [F0911_PK] PRIMARY KEY CLUSTERED
(
[GLDCT] ASC,
[GLDOC] ASC,
[GLKCO] ASC,
[GLDGJ] ASC,
[GLJELN] ASC,
[GLLT] ASC,
[GLEXTL] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
--Insert data into new tables
INSERT INTO F0911
SELECT * FROM F0911_old
GO
-- Drop old tables
DROP TABLE F0911_old
GO
Which would be a more efficient approach, performance wise? I have a gut feeling that both are the same and even the first approach does the same thing as the second one does, implicitly. Is this understanding correct?
Please note that all these columns already exist in the table and we cannot modify the table definition.
Thanks,
Raj
They're the same. The effect of creating a clustered index is to arrange the pages which will happen in both cases. For non-clustered indexes it will help to disable the index and then turn it back on and rebuilding it.
I think the first approach is right, but I don't understand the reason of BEGIN Transaction and END transaction. I don't think Transaction keyword is necessary because you are not modifying data of the table. Transaction is used where we have to lock the data and we are modifying real time data so the old data is not used.

How to set a unique field in a already existing SQL Server database?

I have a question about unique fields in a SQL Server database.
I have a already existing database with an index and some foreign keys and so on.
Now I need to set one column e.g. name, to be UNIQUE.
How to do this?
ALTER TABLE yourtable
ADD CONSTRAINT
ix_uniquename UNIQUE NONCLUSTERED
(
name
)
WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
or view the design of the table, right click and select Indexes/Keys
Ff you're talking about adding an index, rather than a constraint then the script is something like:
CREATE UNIQUE INDEX ix_uniquename ON dbo.yourtable([name])

How can I change primary key on SQL Azure

I am going to change the primary key on SQL Azure. But it throws an error when using Microsoft SQL Server Management Studio to generate the scripts. Because every tables on SQL Azure must contains a primary key. And I can't drop it before create. What can I do if I must change it?
Script generated
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[mytable]') AND name = N'PK_mytable')
ALTER TABLE [dbo].[mytable] DROP CONSTRAINT [PK_mytable]
GO
ALTER TABLE [dbo].[mytable] ADD CONSTRAINT [PK_mytable] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF)
GO
Error message
Msg 40054, Level 16, State 2, Line 3
Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.
Msg 3727, Level 16, State 0, Line 3
Could not drop constraint. See previous errors.
The statement has been terminated.
Msg 1779, Level 16, State 0, Line 3
Table 't_event_admin' already has a primary key defined on it.
Msg 1750, Level 16, State 0, Line 3
Could not create constraint. See previous errors.
I ran into this exact problem and contacted the Azure team on the forums. Basically it isn't possible. You'll need to create a new table and transfer the data to it.
What I did was create a transaction and within it do the following:
Renamed the old table to OLD_MyTable.
Create the new table with the correct Primary Key and call it MyTable.
Select the contents from OLD_MyTable
into MyTable.
Drop OLD_MyTable.
You may also need to call sp_rename on any constraints so they don't conflict.
See also: http://social.msdn.microsoft.com/Forums/en/ssdsgetstarted/thread/5cc4b302-fa42-4c62-956a-bbf79dbbd040
upgrade SQL V12 and heaps are supported on it. So you can drop the primary key and recreate it.
I appreciate that this may be late in the day for yourself, but it may help others.
I recently came across this issue and found the least painful solution was to download the database from Azure, restore it locally, update the primary key locally (as the key constraint is a SQL Azure specific issue), and then restore the database back into Azure.
This saved any issues in regards to renaming databases or transferring data between them.
You can try the following scripts. Change it to suit for your table def.
EXECUTE sp_rename N'[PK_MyTable]', N'[PK_MyTable_old]', 'OBJECT'
CREATE TABLE [dbo].[Temp_MyTable](
[id] [int] NOT NULL,
[text] [text] NOT NULL CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED (
[id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON))
INSERT INTO dbo.[Temp_MyTable] (Id, Text)
SELECT Id, Text FROM dbo.MyTable
drop table dbo.MyTable
EXECUTE sp_rename N'Temp_MyTable', N'MyTable', 'OBJECT'
This question is outdated because changing PK is already supported in latest version of SQL Azure. And you don't have to create temporary table.