Alter Primary key constraint - sql

In my table I have primary key on 3 columns (name, dept, MobNo).
Now I want to change it to be on two columns (Name, MobNo).
Is there any way I can alter the primary key constraint without dropping it?
I know I can drop old constraint and can create new but without dropping old constraint it is possible to alter it?

The only and one way would be to drop the constraint with an Alter table then recreate it.
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)

If you (probably) have dependencies on that PK, you will also have to drop them and recreate them. To have all this done automagically, it's easier from SSMS to right-click on the table, choose Design, and from there you click in the top left toolbar the button called Manage indexes and Keys. From there you make your changes, and at the end, you have 2 options:
you close and save your changes (and it works fine but you don't learn anything)
instead you click on the Generate change script so you can examine and execute the script later
(at least it works like this with my version 2014 of SSMS)

Related

How to delete with no check foreign key constraints

I have a table with some bad data in SQL. this table has a lot of relationship with other tables and other tables have a lot of data so when I want to delete bad data it's very slowly and take lots of time to do. I think the cause of this problem is foreign key constraints. the main problem is how can disable all of the foreign key constraints from one table.
You have to disable check constraint:
SET FOREIGN_KEY_CHECKS=0;
Make sure to turn it back after your commands:
SET FOREIGN_KEY_CHECKS=1;
Disable constraint for one table:
alter table
table_name
DISABLE constraint
constraint_name;
Here is an example:
select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints;

How to add a foreign key for a table that is already created?

I have added a table to a database called settings. This table has a column called id (integer) which is the pirmary key. I have also added a column called settingsID to a table called sessions. What I want to do is add a foreign key to settingsID which references the primary key.
I don't want to create a new table as it is already created. All I want to do is to references the id from the settings table in settingsID which is in sessions table.
ALTER TABLE Sessions ADD FOREIGN KEY (_SettingsID) REFERENCES settings (id)
Here is my error:
near "FOREIGN": syntax error
Can someone tell me the right way to approach this?
Short answer: you can't.
This is documented as part of the SQL Features That SQLite Does Not Implement:
Only the RENAME TABLE, ADD COLUMN, and RENAME COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Bold emphasis is mine.
So basically you would need to create the constraint at the time when the table is created. For your use case, one solution would be to:
create a new table (with the foreign key constraint)
copy the data from the old table
drop the old table (or better yet, rename it, so you have a backup)
rename the new table

Generating Create script for existing foreign key through SSMS

While generating the Create DB script for the existing Foreign Key constraint through SSMS, we could able to see two set of alter statements. one with NOCHECK ADD and another with CHECK.
ALTER TABLE [dbo].[claim] WITH NOCHECK ADD CONSTRAINT [FK_CLAIM_COPCID]
FOREIGN KEY([copcid])
REFERENCES [dbo].[copc] ([CopcId])
GO
ALTER TABLE [dbo].[claim] CHECK CONSTRAINT [FK_CLAIM_COPCID]
GO
Why we are getting two set of scripts to create a new Constraint?.
The first alter statement is creating a constraint and instructing that constraint to be added without checking whether the existing rows obey the new constraint.
When you add a constraint without first checking the rows, it will not be fully active until you enable it. That's what the second statement does, it enables the new constraint. If there are rows that break the constraint, you won't be able to enable it.

Can we drop and recreate primary key in SQL Server table # Production environment? Do we have to down the server for it or we can do it live?

Can we drop and recreate a composite primary key in a SQL Server table in production environment? Do we have take down the server for it, or can we do it live?
Because we have to add more columns to the primary key. If we done it in live what are the problems we have to face?
You can remove primary key from table.This will also remove clustered index of that table if you have't mentioned explicitly on other column.
To remove primary key run below query
-- Drop CHECK CONSTRAINT from the table
ALTER TABLE /*schema*/./*table*/
DROP CONSTRAINT /*constraint_name*/
GO
and to add primary key run below code
-- Add a new CHECK CONSTRAINT to the table
ALTER TABLE /*schema*/./*table*/
ADD CONSTRAINT /*contraint_name*/ /*constraint_type*/ (/*constraint_column_name*/ /*logical_expression*/)
GO

Index should be dropped but is preventing new primary key from being added

I have a strange situation that I don't understand. I am running a block of SQL in a merge module to update a oracle schema. I am trying to change the primary key of several tables so I am performing the following steps:
Drop FK constraints,
Drop PK,
Drop PK Index (if the index persists after the PK is dropped),
Add new PK,
Add FK's
Here's my problem. All is well until we get to the part where indexes are dropped. The primary is dropped and (to the best of my knowledge) any index that Oracle created based on that key will drop instantly as well. I was having issues with the index persisting so I added the DROP INDEX script to be certain it is removed, however, this is what happens:
Alter Table TABLE1 Drop Constraint TABLE1_PK
The command ran successfully
DROP INDEX TABLE1_PK
ORA-01418: specified index does not exist (This is exactly what I expected)
ALTER TABLE TABLE1 ADD (CONSTRAINT TABLE1_PK PRIMARY KEY (TABLE_KEY) ENABLE VALIDATE)
ORA-00955: name is already used by an existing object (The object being, in each case, the old index)
This of course prevents any of the FK's from linking because they are now based on the new key. When I run this in TOAD, the SQL works, but I can't figure out why it doesn't through the merge module. Can anyone help?