Altering the table

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



Tpaul_10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gad_flyCommented:
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
LowfatspreadCommented:
please run

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

and post the output
0
Tpaul_10Author Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gad_flyCommented:
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
Tpaul_10Author Commented:
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
Aneesh RetnakaranDatabase AdministratorCommented:
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
gad_flyCommented:
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
imran_fastCommented:
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
imran_fastCommented:
/*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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imran_fastCommented:
use group inside square brackets
[Group]
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.