Drop Column With Constraint

Sep 25, 2007



Hello !

I am using Microsoft SQL Server 2000

I am trying to drop a column that has a constraint, executing the script inside a transaction:

BEGIN TRANSACTION

ALTER TABLE MOPTeste DROP CONSTRAINT FK_IDMOPPais


ALTER TABLE MOPTeste DROP COLUMN IDMOPPais

COMMIT

If i dont commit the drop constraint, it wont let me drop the column

Solutions?



View 3 Replies


ADVERTISEMENT

How To Drop UNIQUE CONSTRAINT On Only One Column

May 5, 2014

I am very new to sql and I have got this doubt.

CREATE TABLE employee
{ EMP-ID VARCHAR UNIQUE,
DESIG VARCHAR UNIQUE,
SALARY INT };

Probably this is worst table ever created :).Now How to drop UNIQUE constraint on only column lets DESIG.

if i write

ALTER TABLE employee
DROP CONSTRAINT myuniqueconstraint;

will drop constraints on both columns which I dont want.

Any query that remove UNIQUE CONSTRAINT only on DESIGN column.

and one more clarification,

ALTER TABLE employee
MODIFY SALARY INT NOT NULL;

ALTER TABLE employee
ALTER COLUMN SALARY INT NOT NULL;

which of the above two is right query to add NOT NULL constraint to the above employee TABLE?

View 1 Replies View Related

Can't Drop Constraint...?

Aug 30, 2006

Hello, I have hit the wall here...can't make sense of this one.

I have a script that creates a PRIMARY KEY constraint called PK_tblDRG
CODE:

ALTER TABLE [dbo].[tblDRG]
ALTER COLUMN [record_id] Uniqueidentifier NOT NULL
Go
ALTER TABLE [dbo].[tblDRG]
WITH NOCHECK ADD PK_tblDRG PRIMARY KEY CLUSTERED
(
[record_id]
) WITH FILLFACTOR = 90 ON [PRIMARY]

All is fine with that. I run this to verify:

EXEC sp_pkeys @table_name = 'tblDRG'
,@table_owner = 'dbo' ,@table_qualifier = 'Relational_05Q3'

which returns this:

TABLE_QUALIFIERTABLE_OWNERTABLE_NAMECOLUMN_NAMEKEY_SEQPK_NAME
Relational_05Q3dbotblDRGrecord_id1PK_tblDRG

Now I want to drop the constriant if it exists:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PK__tblDRG]') and OBJECTPROPERTY(id, N'IsPrimaryKey') = 1)
ALTER TABLE [dbo].[tblDRG] DROP CONSTRAINT PK__tblDRG

AND I get this in return:

The command(s) completed successfully.

So, lets double check:

EXEC sp_pkeys @table_name = 'tblDRG'
,@table_owner = 'dbo' ,@table_qualifier = 'Relational_05Q3'

AND I STILL GET THIS:

TABLE_QUALIFIERTABLE_OWNERTABLE_NAMECOLUMN_NAMEKEY_SEQPK_NAME
Relational_05Q3dbotblDRGrecord_id1PK_tblDRG

Hmmmmm. Looks like the IF statement didn't do it's job. Ok fine. I'll just kill it myself:

ALTER TABLE [dbo].[tblDRG] DROP CONSTRAINT PK__tblDRG

AND I GET THIS?!?!?!?!

Server: Msg 3728, Level 16, State 1, Line 1
'PK__tblDRG' is not a constraint.

What am I not getting here? Is it me...I can take If I am a bone head here.

Any help would be appreciated. Thanks!

View 1 Replies View Related

Drop And Reacreate CONSTRAINT

Oct 9, 2001

ALTER TABLE doc_exe ADD

/* Add a PRIMARY KEY identity column. */
column_b INT IDENTITY
CONSTRAINT column_b_pk PRIMARY KEY,

/* Add a column referencing another column in the same table. */
column_c INT NULL
CONSTRAINT column_c_fk
REFERENCES doc_exe(column_a)

go

ALTER TABLE doc_exe
DROP CONSTRAINT column_c_fk

How to recreate CONSTRAINT column_c_fk without droping and reacreating column column_c or add new CONSTRAINT on column column_c ?

Thanks a lot .

View 3 Replies View Related

Drop Constraint Never Finishes

Oct 13, 2002

I need to make some changes to tables in a database. To do this requires that I "alter table" and "drop constraint" All constraints drop properly, except one. The last one will churn away forever in query analyzer and never drop. I am unable to make the necessary changes until this completes. I am guessing that there is a data issue that is keeping the constraint from being dropped. Any ideas on how to proceed? Thanks!

View 2 Replies View Related

Urgent!drop Foreign Key Constraint

Mar 1, 2001

I have to drop foreign key constraint from one table. How can I do that? Is it possible?
Also,

Is it possible to add a foreign key constraint on the table once it has a data?

View 1 Replies View Related

Drop Primary Key Constraint Of (#) Hash

Jul 20, 2005

Hi there,I have created a hash table. After using it, somehow the primary keyconstraint of this hash table still exist in database. Which causeerror.When I delete this constraint with Alter table Drop con....It gives no table exist error.Can anybody give any idea.Thanks in Adv.,T.S.Negi

View 2 Replies View Related

UNABLE To ADD Or DROP A Constraint - SQL Complains!

Apr 21, 2008

Hi all, I am trying to create a CONSTRAINT but for some reason T-SQL does not allow me to do that.

When I try to DROP a CONSTRAINT it says:

Msg 3728, Level 16, State 1, Line 13'DF_TBL_SyncTable_DEVUK' is not a constraint.Msg 3727, Level 16, State 0, Line 13Could not drop constraint. See previous errors.

When I try to ADD a CONSTRAINT it says:
Msg 1781, Level 16, State 1, Line 14Column already has a DEFAULT bound to it.Msg 1750, Level 16, State 0, Line 14Could not create constraint. See previous errors.


For some reason I can't win here. Can't drop it nor can I create one. Any solution?




Code Snippet
ALTER TABLE TBL_SyncTable DROP CONSTRAINT DF_TBL_SyncTable_DEVUK
ALTER TABLE TBL_SyncTable ADD CONSTRAINT GOD_TBL_SyncTable_DEVUK DEFAULT 2 FOR DEVUK


Thanks for comments + suggestions.

*UPDATE*

I am trying to use the following code to check if the SCHEMA exists but still no luck. For some reason when I create it and wrap an IF statement around it, it doesn't detect the SCHEMA. Is something wrong with my code?






Code Snippet

IF EXISTS(

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = 'dbo'


AND CONSTRAINT_NAME = 'DF_TBL_SyncTable_DEVUK'


AND TABLE_NAME = 'TBL_SyncTable'

)
SELECT * FROM TABLE_1
ELSE
ALTER TABLE TBL_SyncTable ADD CONSTRAINT DF_TBL_SyncTable_DEVUK DEFAULT 2 FOR DEVUK

View 4 Replies View Related

Whats Wrong Here, Drop Constraint Primary Key ? Help Pls...

Jun 14, 2000

select * from sysobjects where type = 'K'

name
------
pk_dtproperties
1 row(s) affected)

When I say - DROP CONSTRAINT pk_dtproperties

i get this error
-------------------
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CONSTRAINT'.

NOTE:

In tables, I don't have any tables all tables i deleted but wonder from where this creature comes from,.........

View 2 Replies View Related

How To Alter Table Drop Constraint @variable?

Jun 12, 2003

I need to run the alter table to drop a default. However, the default name is kind of 'dynamic' from around 1000 databases, thus I need to run the following sql to get the name to a variable. Now, it looks the alter table statement does not like to drop a vaiable, is there a solution about it?

declare @radius_default varchar(40)
select @radius_default = (select sobj.name from sysobjects sobj
inner join syscolumns scolumn
on sobj.ID = scolumn.cdefault
where scolumn.name = 'radius' and sobj.name like '%LandMarks%')
print 'Need To Drop @radius_default: ' + @radius_default
--====================================
alter table LandMarks drop constraint @radius_default

thanks
David

View 1 Replies View Related

Database Diagrams: FOREIGN KEY Constraint Fell Off; Can't Drop/recreate It...

Mar 25, 2006

Hello,

I have two tables Person & Location where Location has a primary key LocationId and Person has a foreign key LocationId.

Sometime ago I used the Database Diagrams visual tool of SQL Server Management Studio Express to create the foreign key relationship between the two tables - i.e. "visually" (drawing a line between the PK & FK LocationId elements of both tables).

Time has passed and I recently noticed that, upon retrieving my saved diagram, the foreign key relationship had "fallen off" (i.e. the many-to-one line was no longer showing in the diagram).

After recreating the relationship (redrawing the line) I find that I get an error message when I try to save the diagram:

Post-Save Notifications
[!] Errors were encountered during the save process. Some database objects were not saved.

'Location' table saved successfully
'Person' table
- Unable to create relationship 'FK_Person_Location'. 
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Person_Location". The conflict occurred in database "mydb", table "dbo.Location", column 'LocationId'.

When I go back to the object explorer and view the dependencies for the two tables, there is no dependency (between these two tables) revealed.  When I try to create the foreign key constraint manually (T-SQL) it again says can't add the constraint.  It comes up with an error as follows:

ALTER TABLE Person
ADD FOREIGN KEY (LocationId) REFERENCES Location (LocationId)

Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Person__LocationId__793DFFAF". The conflict occurred in database "mydb", table "dbo.Location", column 'LocationId'.

(Note:  Each time I do this, the 8 hexadecimal character suffix changes.)

When I try to drop the foreign key:

alter table Person
drop constraint FK__Person__LocationId

it comes back with the error:

Msg 3728, Level 16, State 1, Line 2
'FK__Person__LocationId' is not a constraint.
Msg 3727, Level 16, State 0, Line 2
Could not drop constraint. See previous errors.

So it seems that there's some kind of goof up here.  Can anybody shed light on this / tell me how to fix it?

View 6 Replies View Related

T-SQL (SS2K8) :: Cannot Define Primary Key Constraint On Nullable Column But Column Not Null

Sep 30, 2014

We have a database where many tables have a field that has to be lengthened. In some cases this is a primary key or part of a primary key. The table in question is:-

/****** Object: Table [dbo].[DTb_HWSQueueMonthEnd] Script Date: 09/25/2014 14:05:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[DTb_HWSQueueMonthEnd](

[Code] ....

The script I am using is

DECLARE@Column varchar(100)--The name of the column to change
DECLARE@size varchar(5)--The new size of the column
DECLARE @TSQL varchar(255)--Contains the code to be executed
DECLARE @Object varchar(50)--Holds the name of the table
DECLARE @dropc varchar(255)-- Drop constraint script

[Code] ....

When I the the script I get the error message Could not create constraint. See previous errors.

Looking at the strings I build

ALTER TABLE [dbo].[DTb_HWSQueueMonthEnd] DROP CONSTRAINT PK_DTb_HWSQueueMonthEnd
ALTER TABLE [dbo].[DTb_HWSQueueMonthEnd] Alter Column [Patient System Number] varchar(10)
ALTER TABLE [dbo].[DTb_HWSQueueMonthEnd] ADD CONSTRAINT PK_DTb_HWSQueueMonthEnd PRIMARY KEY NONCLUSTERED ([Patient System Number] ASC,[Episode Number] ASC,[CensusDate] 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]

They all seem fine except the last one which returns the error

Msg 8111, Level 16, State 1, Line 1
Cannot define PRIMARY KEY constraint on nullable column in table 'DTb_HWSQueueMonthEnd'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

None of the fields I try to create the key on are nullable.

View 2 Replies View Related

ALTER TABLE DROP COLUMN LastUpdated Failed Because One Or More Objects Access This Column.

Mar 7, 2008

Hi I’m trying to alter a table and delete a column I get the following error. The object 'DF__Morningst__LastU__19EB91BA' is dependent on column 'LastUpdated'.
ALTER TABLE DROP COLUMN LastUpdated failed because one or more objects access this column. I tried deleting the concerned constraint. But the next time I get the same error with a different constraint name. I want to find out if I can dynamically check the constraint name and delete it and then drop the column. Can anyone help.IF EXISTS(SELECT 1FROM sysobjects,syscolumnsWHERE sysobjects.id = syscolumns.idAND sysobjects.name = TablenameAND syscolumns.name = column name)BEGIN EXECUTE ('ALTER TABLE tablename DROP CONSTRAINT DF__SecurityM__DsegL__08C105B8')EXECUTE ('ALTER TABLE tablenameDrop column columnname)ENDGO
 

View 1 Replies View Related

More Than One Column FOREIGN KEY Constraint Specified For Column

Jul 23, 2005

How can you indicate that a FOREIGN KEY constraint references twocolumns in two different tables?"SQL Server Books Online" show an example of how to reference twocolumns in the SAME table:REFERENCES ref_table [ ( ref_column [ ,...n ] )Here is the error and the 'bad' SQL code:Server: Msg 8148, Level 16, State 1, Line 4More than one column FOREIGN KEY constraint specified for column'UserOrGroupId', table 'salesforce3.dbo.AccountShare'.CREATE TABLE salesforce3.dbo."AccountShare" ("Id" varchar(18) PRIMARYKEY , ... , "UserOrGroupId" varchar(18) CONSTRAINTFK_UserOrGroupId6349 FOREIGN KEY REFERENCES "User"(Id) REFERENCES"Group"(Id) , ... )

View 2 Replies View Related

Constraint On Column

Dec 13, 2007

Hmm,

I'm creating a table that contains information from two other tables. I'd like to have a constraint on some columns so that thay can only contain information that exists in the other two tables. This is what I got:

create table dbo.KUSE_Bills
(
SERVICE_ID int IDENTITY(1,1) NOT NULL primary key,
BILLDATE datetime NOT NULL,
CNAME varchar(100) NOT NULL,
SNAME varchar(100) NOT NULL,
PRICE money NOT NULL,
UNIT varchar(50) NOT NULL,
NUMBER integer NOT NULL,
BILLSUM money NOT NULL
)
GO

I tried something like this:

create table dbo.KUSE_Bills
(
SERVICE_ID int IDENTITY(1,1) NOT NULL primary key,
BILLDATE datetime NOT NULL,
CNAME varchar(100) NOT NULL,
SNAME varchar(100) NOT NULL,
PRICE money NOT NULL,
UNIT varchar(50) NOT NULL,
NUMBER integer NOT NULL,
BILLSUM money NOT NULL

CONSTRAINT chk_cname CHECK (CNAME NOT IN (
SELECT CNAME FROM KUSE_Customer))
)
GO

But subqueries are not allowed...

So how can I do it?

View 3 Replies View Related

DROP COLUMN

Feb 14, 2000

Does anyone know why this does not work?

ALTER TABLE deal DROP COLUMN TestField

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'TestField'.

Thanks,
Paul

View 4 Replies View Related

How To Drop A Column?

Aug 30, 2006

Hi:

'Unfortunately', there is still a SQL6.5 sp4 server that I need to handle.
I need to drop a coulmn xyz from an important table tblABC.
But neither Enterprise manager could do it nor
'alter table tblABC drop column xyz' could run.

Without the hastle of makin a tblABCcopy and copy all data from tblABC except column xyz and re-do all fks; is there another way to drop the column?

thanks
-D

View 1 Replies View Related

Drop A Column

Apr 6, 2015

I have a question, Every month I get a request from cutomer service to go into a SQL VIEW and DELETE a column to reflect the past Month. How to write a query to create a SQL job to performed this task automatically. For example today I deleted the column [2015/03] which represents the month of March. The columns left are [2015/04] representing the current month, [2015/05], [2015/06],[2015/07]....[2016/08].Next month I will have to delete column [2015/04]and the current month will be [2015/05]....etc.

So I'm thinking I will have to Drop the column and also Maybe using a date funtion to look at the current date and format it to reflect [YYYY/MM] and IF the Column Month is less than current date month then drop the column.

View 3 Replies View Related

Drop Column

Mar 15, 2007

How to drop single column in a table.........

View 3 Replies View Related

FK Constraint From One Column To Two Different Tables

Apr 13, 2012

I have a requirement where one column of a table is supposed to have two foreign key constraints i.e. it can be referring to two different columns in two tables.

View 1 Replies View Related

Unique Column Constraint

Jun 6, 2008

I like to use the management console to design tables, but have
been unable to figure out how to add a unique column constraint.

I have a primary key defined in col1 and want to make col2, char(10),
unique, but keep col1 as the primary key.

I have tried the Check constraint menu path, but do not know how
to write the expression.

If I go along the Indexes/Key path, I am forced to change the primary
key which I do not want to do.

Can anyone help?

TIA, Joe

View 5 Replies View Related

Column Name In A Primary Key Constraint

Jul 20, 2005

Hi allWould there be a easy way to find the column name(s) which constitutea Primary constraint for a table through navigating the systemcatalogs.I found that the PK Constraint object in syscontraints is showing thecolid = 0.TIANorman

View 2 Replies View Related

How To Drop Primary Key By Specifying Column Only?

Apr 24, 2007

I checked column from xsd file which created from DataBase.
And before installing my application I will check any column in destination base whether have complete column or not.
 
And if there is any column which is not wanted column(not same as xsd structure)
I will delete it by creating sql command as follow "ALTER TABLE tableName DROP COLUMN column1, column2, ......"
and Execute it by program initialization.
 
So
I need delete it by run-time
 
 
However some column may be Primary Key with any reason
 
That's why I can't delete them by simple command
 
I expect to delete them by specific column instead of specific constraint name which is not sure name.
 
 
Please advise me....

View 2 Replies View Related

Drop A Column In A Table!!

Aug 2, 2001

Hi all,
how can I drop a column in a table.

View 1 Replies View Related

Can't Drop Column That Has A Default

Jun 13, 2008

I did this...

ALTER TABLE tm_spn_equip_def ADD Customer_No int NOT NULL DEFAULT dbo.f_spn_GetCustomerNo() WITH VALUES

Now when I attempt to do this...

ALTER TABLE tm_spn_equip_def DROP COLUMN Customer_No

I get an error:

Msg 5074, Level 16, State 1, Line 1
The object 'DF__tm_spn_eq__Custo__019E3B86' is dependent on column 'Customer_No'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN Customer_No failed because one or more objects access this column.


How can I get around this?

TIA!

View 4 Replies View Related

How Can I Drop A Column From A Table?

Dec 13, 2007

Hi all!

One more question: how can i drop a column from a table?

Greetings

landau

View 2 Replies View Related

Drop Column Problem

Jul 20, 2005

Hello,I'm just returning to MS SQL Server after two years of dealing withSybase ASE. I need to drop a column, using the alter table command.I keep getting an error indicating that a constraint is using thecolumn. Here is the create script for the tablecreate table mytable(col1 char(1) not null,col2 char(1) default 'A' not null)Here is the alter table command:alter table mytable drop column col2When I run it I get the following error:Server: Msg 5074, Level 16, State 1, Line 1The object 'DF__mytable__col2__114A936A' is dependent on column'col2'.Server: Msg 4922, Level 16, State 1, Line 1ALTER TABLE DROP COLUMN col2 failed because one or more objects accessthis column.Is there anyway to tell the database to drop all column constraintswhen the column is deleted?Thanks,James K.

View 2 Replies View Related

How Do I Drop A Column From A Table

Oct 6, 2007



I have a database that I have made using sql server 2005 express edition. I want to run a query to drop one of the columns from one of the tables. I have tried the following but had no luck.

USE database


DROP column FROM table



This is the error that I got when I tried this.


Msg 156, Level 15, State 1, Line 3

Incorrect syntax near the keyword 'FROM'.

Of course, my query had the real database, table and column names. I have generic ones that I have provided here.

Do I have to alter the table instead of dropping the column that I don't want?
Do I have to remake the table without the column that I don't want?

View 1 Replies View Related

Drop Indentity From Column

Dec 6, 2007

I am trying to remove the Indentity property from a column so I can do some clean up. Then will need to add it back. Adding Indentity property back is not a problem, but cant figure out how to remove it in the first place.

Any help would be greatly apprecaited.

Thanks
Dave

View 4 Replies View Related

CHECK Constraint - Referencing Another Column

Aug 31, 2000

I receive the following error when creating a CHECK constraint that references another column. According to the good old Wrox SQL Server book, I'm using the correct syntax. Anyone have any ideas???

Thanks in advance!

Server: Msg 8141, Level 16, State 1, Line 1
Column CHECK constraint for column 'end_date' references another column, table 'Session'.

Here's an example of the script that I'm using:
CREATE TABLE Session (
session_key char(18) NOT NULL,
course_key char(18) NOT NULL,
site_key char(18) NOT NULL,
instructor_key char(18) NOT NULL,
start_date smalldatetime NULL,
end_date smalldatetime NULL
CHECK (end_date >= start_date)
)


.

View 1 Replies View Related

ALTERing A Column CHECK Constraint

Aug 18, 1999

How do I alter a column check constraint?

I have the table:

CREATE TABLE Mytable(
mykey integer,
mycol integer
CHECK(mycol BETWEEN 1 AND 2)
PRIMARY KEY(mykey))

How do I change the constraint to
CHECK(mycol BETWEEN 0 AND 2)

...without losing any data?

Thanks!
Jim

View 1 Replies View Related

Trigger? Constraint? Computed Column?

Feb 23, 2007

Hi.

I was wondering how I should go about doing this thing. I need to put a value in a column that is based on values on other columns in the same row.

When this statement executes I need to put a value in Col3.

insert into myTable(Col1, Col2)
values(25, -14)

Something like so:

if(Col1 >0 AND Col2 <0)
set Col3 = Col1 - Col2
else
set Col3 = Col1;

I don't now quite how to solve this. I am really going to need this value in a column. Calculating the value at retrieval is not on option...

I appreciate all help. I'm using SQL Server 2005.

Thanks!

View 2 Replies View Related

Alter Column With Default Constraint

Dec 7, 2001

I can't seem to get the syntax correct for ALTERing an existing column with a default constraint. I've been to Help and BOL. There are examples that show how to use the ALTER command to add a column with a default constraint but not how to do it on an existing column.

Any help would be appreciated.

Sidney Ives

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved