Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Altering the table

Posted on 2006-04-13
10
Medium Priority
?
944 Views
Last Modified: 2008-01-09
Hi,

Need help with alter table

I am trying to change the data type for one of the table in my table called "GroupDetails"

I have two columns 1) GroupNumber Int 2) Group SamllInt
and Nullable - "NO"

Since it has got constraint here is what I am doing.

1) Altering the table by droping the constraint

ALTER TABLE GroupDetails
DROP CONSTRAINT pk_GroupDetails

2) Altering the table to change the data type of one column (GroupNumber) to varchat(3)

3) I am creating the constraint again which I have dropped by step1 and I am using the following script and I
am getting the error

ALTER TABLE [dbo].["GroupDetails"] WITH NOCHECK ADD
       PRIMARY KEY  CLUSTERED
      (
            [GroupNumber],
            [Group]
      ) WITH  FILLFACTOR = 90  ON [PRIMARY]
      IF @@ERROR <> 0
            BEGIN
                  SELECT 'ERROR ALTERING THE TABLE. ROLLING BACK THE TRANSACTION'
                  ROLLBACK TRANSACTION
                  RETURN
            END


Here is the error message I am getting

Server: Msg 8111, Level 16, State 1, Line 37
Cannot define PRIMARY KEY constraint on nullable column in table 'GroupDetails'.

And I have got confused by this message, so I ran "sp_help GroupDetails"

Here are the details

Index Name : pk_GroupDetails
Index Description : clustered, unique, primary key located on PRIMARY
Index Keys : GroupNumber, Group
--
Constraint_Type : PRIMARY KEY (clustered)
Constraint Name : pk_GroupDetails
ConstraintKeys : GroupNumber, Group


Please help me out by creating the constraint or index with the same name for my table.

Thanks
Have a good day



0
Comment
Question by:Tpaul_10
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:gad_fly
ID: 16450042
hi

i think you must do this before create an index

Alter Table [dbo].["GroupDetails"] alter column [groupNumber]  not null

and

Alter Table [dbo].["GroupDetails"] alter column [group] not null

and do your part

ALTER TABLE [dbo].["GroupDetails"] WITH NOCHECK ADD
      PRIMARY KEY  CLUSTERED
     (
          [GroupNumber],
          [Group]
     ) WITH  FILLFACTOR = 90  ON [PRIMARY]
     IF @@ERROR <> 0
          BEGIN
               SELECT 'ERROR ALTERING THE TABLE. ROLLING BACK THE TRANSACTION'
               ROLLBACK TRANSACTION
               RETURN
          END

Mohammad Pourebtehaj
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16450058
please run

select * from information_schema.columns
where table_schema = 'dbo'
and table-name = 'groupdetails'
order by ordinal_position

and post the output
0
 

Author Comment

by:Tpaul_10
ID: 16450134
Mohammad Pourebtehaj,
I am getting error

Alter Table GroupDetails alter column [GroupNum]  not null

Server: Msg 156, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'not'.


Lowfatspread,

Here are the details

Column_name : GroupNumber
Ordinal_Position : 1
IS_NULLABLE : No
Data_Type: Int
Numeric_precision : 10
Numeric_Precision_Radix : 10

Column_name : GroupNumber
Ordinal_Position : 1
IS_NULLABLE : No
Data_Type: smallint
Numeric_precision : 5
Numeric_Precision_Radix : 10

and all other columns are null except table_catalog(my databasename) and table_schema (db0)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:gad_fly
ID: 16450149
hi

i forgot to type datatype

Alter Table [dbo].["GroupDetails"] alter column [groupNumber]  Int  not null

and

Alter Table [dbo].["GroupDetails"] alter column [group] SmallInt not null

and do your part

ALTER TABLE [dbo].["GroupDetails"] WITH NOCHECK ADD
      PRIMARY KEY  CLUSTERED
     (
          [GroupNumber],
          [Group]
     ) WITH  FILLFACTOR = 90  ON [PRIMARY]
     IF @@ERROR <> 0
          BEGIN
               SELECT 'ERROR ALTERING THE TABLE. ROLLING BACK THE TRANSACTION'
               ROLLBACK TRANSACTION
               RETURN
          END

Mohammad Pourebtehaj
0
 

Author Comment

by:Tpaul_10
ID: 16450225
Still get the error and

Alter Table [dbo].["GroupDetails"] alter column [group] SmallInt not null  is not changing anything.

I checked the nullable column by running sp_help
groupNum - yes
group - no

my old values for the the columns says "No" and  i don't want to change.

error details

(20 row(s) affected) -- For changing the datatype

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

0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 300 total points
ID: 16450576
Tpaul_10,
> Server: Msg 8111, Level 16, State 1, Line 34
> Cannot define PRIMARY KEY constraint on nullable column in table
> 'FactorOccupancyTerritoryGroup'.
the above error suggests that, you are creating primry key on a nullble column 'FactorOccupancyTerritoryGroup'
You can't create a PRIMARY KEy on a nullable column


Alter Table [dbo].[GroupDetails]
ALTER  COLUMN FactorOccupancyTerritoryGroup  urDatatype NOT NULL
0
 
LVL 2

Assisted Solution

by:gad_fly
gad_fly earned 300 total points
ID: 16452356
Hi

as you say :

" checked the nullable column by running sp_help
-------------------
groupNum - yes
-------------------
group - no "

thats your problem ( " groupnum - yes ")

you have group with not null and it's not needed to alter this . just do this line

Alter Table [dbo].["GroupDetails"] alter column [groupNumber]  Int  not null

if your table name is :"FactorOccupancyTerritoryGroup " and your data type is INT use this

Alter Table [dbo].[FactorOccupancyTerritoryGroup] alter column [groupNumber]  Int  not null

I hope it's usefull

remember , before doing this you must drop your existing constraint on those column .

hi - aneeshattingal -  you suggest same thing dear :)

Mohammad Pourebtehaj
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 16459679
ALTER TABLE GroupDetails
DROP CONSTRAINT pk_GroupDetails
go

alter table GroupDetails alter column GroupNumber varchar(3) not null
go
alter table GroupDetails alter column Group smallint not null
go

ALTER TABLE [dbo].["GroupDetails"] WITH NOCHECK ADD
      PRIMARY KEY  CLUSTERED
     (
          [GroupNumber],
          [Group]
     ) WITH  FILLFACTOR = 90  ON [PRIMARY]
     IF @@ERROR <> 0
          BEGIN
               SELECT 'ERROR ALTERING THE TABLE. ROLLING BACK THE TRANSACTION'
               ROLLBACK TRANSACTION
               RETURN
          END
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 1400 total points
ID: 16459685
/*run this */

ALTER TABLE GroupDetails
DROP CONSTRAINT pk_GroupDetails
go
update GroupDetails  set  Group = 0  where group is null
go
alter table GroupDetails alter column GroupNumber varchar(3) not null
go
alter table GroupDetails alter column Group smallint not null
go

ALTER TABLE [dbo].["GroupDetails"] WITH NOCHECK ADD
      PRIMARY KEY  CLUSTERED
     (
          [GroupNumber],
          [Group]
     ) WITH  FILLFACTOR = 90  ON [PRIMARY]
     IF @@ERROR <> 0
          BEGIN
               SELECT 'ERROR ALTERING THE TABLE. ROLLING BACK THE TRANSACTION'
               ROLLBACK TRANSACTION
               RETURN
          END

0
 
LVL 28

Expert Comment

by:imran_fast
ID: 16459688
use group inside square brackets
[Group]
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question