SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 making a column nullable using a constraint
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

heze
Posting Yak Master

USA
192 Posts

Posted - 02/28/2007 :  17:37:09  Show Profile  Reply with Quote
Hi
My table is

myTable(year,Id,gender)

I want to add a primary key using
alter table myTable
add constraint hz_Constraint
primary key (year,Id)

But i receive the folowing from SQL Server:
-------------------------
Server: Msg 8111, Level 16, State 1, Line 1
Cannot define PRIMARY KEY constraint on nullable column in table 'CB1'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
-------------------------

So I then try the folloiwng succesfully:
alter table myTable
add constraint hz_erase2
check (not pidm is null)

alter table myTable
add constraint hz_erase1
check (not Term is null)

But this dod not change the actual property;
my question is:
How do you change the nullable property to not nullable in the year,Id fields?

Thank you

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 02/28/2007 :  17:49:08  Show Profile  Reply with Quote
Use ALTER COLUMN with ALTER TABLE, nullability is not a constraint, its a property of the column. The data type is required even though you won't be changing it, just make it whatever it is now, I used int in this example. You'll need two separate ALTER TABLE statements.
alter table myTable
alter column year int not null
alter table myTable
alter column Id int not null


Go to Top of Page

adsingh82
Starting Member

India
20 Posts

Posted - 05/20/2014 :  01:52:49  Show Profile  Reply with Quote
below url would shed some light on how to resolve this error

http://dotnetbites.com/cannot-define-primary-key-constraint-on-nullable-column-in-table
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.38 seconds. Powered By: Snitz Forums 2000