May 26, 2011 at 3:23 pm
Hi Guys,
I am new to the forum and first post of my sql. I am in my initial learning stage and need all you guys help.
I need a solution for this please...
Created a table and entered values for the columns with nulls and duplicates
create table CUSTOMERS
(
cust_id int,
customer varchar(20)
)
entered values as in the table....
100; ABC
100;SHD
NULL;CDB
My requirement is adding a Primary Key to the column cust_id.
of my learning I came to know the column have to be given a not null constraint before PK is given....
But when I write the code as
ALTER TABLE CUSTOMERS
ALTER COLUMN cust_id int not null
GO
It is throwing an error, .....
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'cust_id', table 'UNIVERSITY1.dbo.CUSTOMERS'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
Please anyone help me with the issue.....Thanks
May 26, 2011 at 5:18 pm
bayalapudi_k (5/26/2011)
Hi Guys,I am new to the forum and first post of my sql. I am in my initial learning stage and need all you guys help.
I need a solution for this please...
Created a table and entered values for the columns with nulls and duplicates
create table CUSTOMERS
(
cust_id int,
customer varchar(20)
)
entered values as in the table....
100; ABC
100;SHD
NULL;CDB
My requirement is adding a Primary Key to the column cust_id.
of my learning I came to know the column have to be given a not null constraint before PK is given....
But when I write the code as
ALTER TABLE CUSTOMERS
ALTER COLUMN cust_id int not null
GO
It is throwing an error, .....
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'cust_id', table 'UNIVERSITY1.dbo.CUSTOMERS'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
Please anyone help me with the issue.....Thanks
Everything up to this point, including the error is pretty obvious. You're trying to make a unique key on non-unique data. So, the question would be, which 100 row do you want to get rid of?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 26, 2011 at 5:39 pm
I want this as a non-unique data.
May 26, 2011 at 6:11 pm
The id has to be unique... the name of customer might repeat, but even then you need a way to differentiate the duplicates.
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy