Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 which constraints to drop in order to truncate

Author  Topic 

jpotucek
Constraint Violating Yak Guru

273 Posts

Posted - 2010-01-06 : 12:41:34


I have one table that I need to get refreshed from a table in another database . MS SQL 2005

I thought it would be as easy as dropping a PK constraint but it is not turning out that way.

Here is my my table DDL

****** Object:  Table [dbo].[em]    Script Date: 01/06/2010 12:34:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[em](
[area_chargable] [numeric](7, 2) NOT NULL DEFAULT (0.0),
[area_comn] [numeric](7, 2) NOT NULL DEFAULT (0.0),
[area_comn_nocup] [numeric](7, 2) NOT NULL DEFAULT (0.0),
[area_comn_ocup] [numeric](7, 2) NOT NULL DEFAULT (0.0),
[area_comn_rm] [numeric](7, 2) NOT NULL DEFAULT (0.0),
[area_comn_serv] [numeric](7, 2) NOT NULL DEFAULT (0.0),
[area_rm] [numeric](8, 2) NOT NULL DEFAULT (0.0),
[bl_id] [char](8) NULL DEFAULT (null),
[calling_card_number] [char](24) NULL DEFAULT (null),
[cellular_number] [char](24) NULL DEFAULT (null),
[comments] [varchar](500) NULL DEFAULT (null),
[contingency_bl_id] [char](8) NULL DEFAULT (null),
[contingency_email] [varchar](50) NULL DEFAULT (null),
[contingency_fac_at] [smallint] NOT NULL DEFAULT (0),
[contingency_fax] [char](20) NULL DEFAULT (null),
[contingency_fl_id] [char](4) NULL DEFAULT (null),
[contingency_phone] [char](20) NULL DEFAULT (null),
[contingency_rm_id] [char](8) NULL DEFAULT (null),
[cost] [numeric](8, 2) NOT NULL DEFAULT (0.0),
[dp_id] [char](16) NULL DEFAULT (null),
[dv_id] [char](16) NULL DEFAULT (null),
[dwgname] [char](64) NULL DEFAULT (null),
[ehandle] [char](16) NULL DEFAULT (null),
[em_number] [char](20) NULL CONSTRAINT [DF_em_em_number_default] DEFAULT (null),
[em_std] [char](16) NULL DEFAULT (null),
[email] [varchar](50) NULL DEFAULT (null),
[emergency_contact] [char](64) NULL DEFAULT (null),
[emergency_phone] [char](20) NULL DEFAULT (null),
[emergency_relation] [char](32) NULL DEFAULT (null),
[extension] [char](20) NULL DEFAULT ('0'),
[fax] [char](20) NULL DEFAULT (null),
[fl_id] [char](4) NULL DEFAULT (null),
[honorific] [char](10) NULL DEFAULT ('Mr'),
[image_file] [char](64) NULL DEFAULT (null),
[layer_name] [char](32) NULL DEFAULT (null),
[mailstop] [char](10) NULL DEFAULT (null),
[name_first] [char](32) NULL DEFAULT (null),
[name_last] [char](32) NULL DEFAULT (null),
[net_id] [char](12) NULL DEFAULT (null),
[net_user_name] [char](16) NULL DEFAULT (null),
[option1] [char](16) NULL DEFAULT (null),
[option2] [char](16) NULL DEFAULT (null),
[pager_number] [char](24) NULL DEFAULT (null),
[pct_rm] [numeric](6, 2) NOT NULL DEFAULT (0.0),
[phone] [char](20) NULL DEFAULT (null),
[phone_home] [char](20) NULL DEFAULT (null),
[recovery_status] [char](12) NOT NULL DEFAULT ('NONE'),
[rm_id] [char](8) NULL DEFAULT (null),
[status] [char](10) NULL DEFAULT (null),
[tc_level] [char](16) NULL DEFAULT (null),
[em_id] [char](35) NOT NULL DEFAULT (null),
[fire_marshal] [char](3) NULL DEFAULT ('NO'),
[hire_date] [datetime] NULL DEFAULT (null),
[home_add1] [varchar](30) NULL DEFAULT (null),
[home_add2] [varchar](30) NULL DEFAULT (null),
[name_mi] [char](1) NULL DEFAULT (null),
[ss_num] [char](11) NULL DEFAULT (null),
[state_id] [char](2) NULL DEFAULT (null),
[zip_home] [char](10) NULL DEFAULT (null),
[badge_num] [char](8) NULL DEFAULT (null),
[city_id] [char](20) NULL DEFAULT (null),
[dob] [datetime] NULL DEFAULT (null),
[on_palm] [smallint] NOT NULL CONSTRAINT [DF_em_on_palm_default] DEFAULT (0),
[time_stamp] [char](40) NULL DEFAULT (null),
[username] [varchar](64) NULL DEFAULT (null),
[em_bar_code] [char](16) NULL DEFAULT (null),
CONSTRAINT [em_PK] PRIMARY KEY CLUSTERED
(
[em_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_bl_id] FOREIGN KEY([bl_id])
REFERENCES [dbo].[bl] ([bl_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_bl_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_contingency_bl_id] FOREIGN KEY([contingency_bl_id])
REFERENCES [dbo].[bl] ([bl_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_contingency_bl_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_contingency_fl_id] FOREIGN KEY([contingency_bl_id], [contingency_fl_id])
REFERENCES [dbo].[fl] ([bl_id], [fl_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_contingency_fl_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_contingency_rm_id] FOREIGN KEY([contingency_bl_id], [contingency_fl_id], [contingency_rm_id])
REFERENCES [dbo].[rm] ([bl_id], [fl_id], [rm_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_contingency_rm_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_dp_id] FOREIGN KEY([dv_id], [dp_id])
REFERENCES [dbo].[dp] ([dv_id], [dp_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_dp_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_dv_id] FOREIGN KEY([dv_id])
REFERENCES [dbo].[dv] ([dv_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_dv_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_em_std] FOREIGN KEY([em_std])
REFERENCES [dbo].[emstd] ([em_std])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_em_std]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_fl_id] FOREIGN KEY([bl_id], [fl_id])
REFERENCES [dbo].[fl] ([bl_id], [fl_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_fl_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_net_id] FOREIGN KEY([net_id])
REFERENCES [dbo].[net] ([net_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_net_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_rm_id] FOREIGN KEY([bl_id], [fl_id], [rm_id])
REFERENCES [dbo].[rm] ([bl_id], [fl_id], [rm_id])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_rm_id]
GO
ALTER TABLE [dbo].[em] WITH CHECK ADD CONSTRAINT [em_tc_level] FOREIGN KEY([tc_level])
REFERENCES [dbo].[afm_tclevel] ([tc_level])
GO
ALTER TABLE [dbo].[em] CHECK CONSTRAINT [em_tc_level]


Can anyone tell me which constraints I would need to drop in order to truncate and reload this table?

shan
Yak Posting Veteran

84 Posts

Posted - 2010-01-06 : 12:48:20
Though this is not a good idea to maintan the data quality, Disable the conistanits and load the data and then reenable it.

Disable all Constraints
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'

Enable all Constraints
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'

Go to Top of Page

shan
Yak Posting Veteran

84 Posts

Posted - 2010-01-06 : 12:51:59
Sorry ignore my above post

use this

Disable all Constraints
ALTER TABLE em NOCHECK CONSTRAINT ALL
Enable all Constraints

ALTER TABLE em CHECK CONSTRAINT ALL'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 12:52:23
quote:
Originally posted by shan

Though this is not a good idea to maintan the data quality, Disable the conistanits and load the data and then reenable it.

Disable all Constraints
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'

Enable all Constraints
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'




then wats of effect of keeping the constraints? how will he ensure integrity of data?
Go to Top of Page

shan
Yak Posting Veteran

84 Posts

Posted - 2010-01-06 : 12:54:37
Yes thats why i told it affects the data quality \ integrity.
Go to Top of Page
   

- Advertisement -