I keep getting the error when running the code below:
Cannot truncate table 'Entry' because it is being referenced by a FOREIGN KEY constraint.
--ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [PK_Entry_Id]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_HideChrome]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_IsDiscussionEnabled]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_MetaDescription]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_MetaTitle]
Truncate table [Entry]
ALTER TABLE [dbo].[Entry] ADD CONSTRAINT [DF_Entry_HideChrome] DEFAULT ((0)) FOR [HideChrome]
GO
ALTER TABLE [dbo].[Entry] ADD CONSTRAINT [DF_Entry_IsDiscussionEnabled] DEFAULT ((1)) FOR [IsDiscussionEnabled]
GO
ALTER TABLE [dbo].[Entry] ADD CONSTRAINT [DF_Entry_MetaDescription] DEFAULT ('') FOR [MetaDescription]
GO
ALTER TABLE [dbo].[Entry] ADD CONSTRAINT [DF_Entry_MetaTitle] DEFAULT ('') FOR [MetaTitle]
GO
ALTER TABLE [dbo].[Entry] ADD CONSTRAINT [DF_EntryStatus] DEFAULT ('Public-Page') FOR [Status]
GO
Ok so I dropped all other constraints from any tables referencing this table but I still get one more error saying there's still a PK constraint referencing my Entry table.
I go and check out the dependencies on Entry (view dependencies) and see that the comment table is still dependent on it:
Then I see there's a FK as one of the main fields of the comment table but you can't drop that I guess.
So I don't see what other dependencies are referencing this Entry table when View Dependencies says the only table left after I dropped constraints on the other tables is from the Comment table? I don't see it.
In the code of dropping the constraint that you have provide you are not dropping the DF_EntryStatus key. You have only dropped these 4 keys.
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_HideChrome]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_IsDiscussionEnabled]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_MetaDescription]
ALTER TABLE [dbo].[Entry] DROP CONSTRAINT [DF_Entry_MetaTitle]
As this DF_EntryStatus still referencing you are not able to truncate the table.
Hope this solves our problem.
For some odd reason, I had to drop the key FK_Comment_Comment. Don't know how it's related to the Entry table though...weird.
Related
This is what I tried. I simply want to remove the constraint I just did there
ALTER TABLE product
ADD DEFAULT NULL FOR sale_id
ALTER TABLE product
ALTER COLUMN sale_id DROP DEFAULT
ALTER TABLE TABLE_NAME
DROP CONSTRAINT <constraint_name>
Eg:
ALTER TABLE students
ALTER COLUMN ph
DROP NOT NULL;
I have a table with foreign key:
CREATE TABLE [dbo].[MyTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ForeignId] [int] NULL)
ALTER TABLE [dbo].[MyTable] WITH CHECK ADD CONSTRAINT [FK_MyTable_OtherTable] FOREIGN KEY([ForeignId])
REFERENCES [dbo].[OtherTable] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[ExpedíciaVýrobky] CHECK CONSTRAINT [FK_MyTable_OtherTable]
I would like to alter the ForeignId datatype to NOT NULL:
ALTER TABLE [dbo].[MyTable] ALTER COLUMN ForeignId INT NOT NULL;
This requires to drop the FK contraint and recreate it.
Is it possible to do so without manually scripting ALTER TABLE CHECK ADD CONSTRAINT ... FOREIGN KEY bla bla with all the details?
For example, if somebody edited the ON DELETE meanwhile, I don't wont to override this change.
EDIT:
In ideal world, I would like to have stored procedure SpAlterColumnToNotNULL(tablename, columnname), that would do the dropping and recreating indexes automatically.
I created a table with a primary key. I would like to alter a primary key.
I thought using Alter Table command is the solution, but not successful.
ALTER TABLE "tablename" ALTER PRIMARY KEY (col1,col2);
Could you please help me how to alter primary key using SQL statement?
You can add primary key constraint to a column but cannot alter an existing one.
https://docs.snowflake.com/en/sql-reference/sql/create-table-constraint.html#out-of-line-unique-primary-foreign-key
Drop and recreate the primary key. Here's an example:
ALTER TABLE "tablename" DROP CONSTRAINT "pk_name";
ALTER TABLE "tablename" ADD CONSTRAINT "pk_name" PRIMARY KEY (col1,col2);
In case you've multiple primary keys, it's done like this:
ALTER TABLE SCHEMA.TABLE DROP PRIMARY KEY;
ALTER TABLE SCHEMA.TABLE ADD CONSTRAINT CONSTRAINT_NAME PRIMARY KEY (col1, col1, ..., coln);
Possible way to resolve Dynamics Ax Management Reporter issue:
Cannot insert duplicate key row in object '[Reporting].[SecurityPrincipal]' due to trigger '[Reporting].[SecurityPrincipal_Insert_UniqueName]
This could help admins with Dynamics Ax environment integrated with Management Reporter.
After successful integrations we had the error "Cannot insert duplicate key row in object '[Reporting].[SecurityPrincipal]' due to trigger '[Reporting].[SecurityPrincipal_Insert_UniqueName]", probably due to pre-existing user records.
Solution(Maybe not the best)
1. Stop Management Reporter service
2.Go to MSSQL server for ManagementReporter database and execute the following;
ALTER TABLE [Reporting].[SecurityPrincipal] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlColumnMaster] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlReportScheduleTask] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlReportContributorSettings] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlReport] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlReportGroups] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[SecurityCompanyPermissionIntegration] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlRowMaster] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlTreeMaster] NOCHECK CONSTRAINT ALL
ALTER TABLE [Reporting].[ControlReportSchedule] NOCHECK CONSTRAINT ALL
Delete all domain user records only. Do not delete the user "Everyone" and "Management Reporter"
enter code hereDELETE FROM [ManagementReporter].[Reporting].[SecurityPrincipal] WHERE Name like 'domain%'
Re enable all constraints, start the service and hope for the table to be repopulated.
Hope this helps someone in need as I found no solutions online.
What is the syntax to enable/disable the constraint? I tried google
in oracle I write it like this
alter table OPT DISABLE constraint FK_param
how to do that in sybase?
You simply can't disable foreign keys constraints in sybase. But you can drop and recreate
them.
Drop constraint:
alter table table_name drop constraint constraint_name
Add constraint:
alter table table_name add constraint constraint_name
I know that in other DB's, there is something like a CHECK and NOCHECK on these constraints. So you could just NOCHECK the constraint you want to disable.
ALTER TABLE a1 NOCHECK CONSTRAINT a1_TestConstraint
You can't disable constraints in sybase. You can drop/add constraints only.