Beginner here and trying to solve a problem from which I cannot find any clear solutions. I am supposed to alter a table. I am supposed to add two constraints, these being the addition of a primary key and the addition of a foreign key (Some_ID). However, Some_ID is NULL and I am supposed to modify/alter the column into NOT NULL. I cannot find the solution to that. I am getting a Msg 102, Level 15, State 1 syntax error near the 'int'. Sorry if this has been discussed to no end but I do not get what is the issue here. Perhaps a recommendation or a link would be appreciated. Thanks.
ALTER TABLE KnownTable
ADD CONSTRAINT PK_KnownTable PRIMARY KEY (Known_iD),
CONSTRAINT FK_KnownTable FOREIGN KEY (Some_ID) REFERENCES Other_Table(Some_ID),
MODIFY Some_ID int NOT NULL;
September 4, 2022 at 10:42 pm
SomeID doesn't have to have a value to have the FK placed on it. FK's are "if something, other than a NULL, is present, it has to be in that other table".
If the powers that be insist on SomeID not being NULL, you need to decide what value all those NULLs should be AND that something needs to be in the other table.
A better thing would be to tell the powers that be of this problem and ask them what value they want to be assigned for the NULLs.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 4, 2022 at 11:09 pm
Thank you for your reply. This is all I have to work with for now but the MODIFY or ALTER COLUMN is not doing it for some reason.
You simply can't constrain a column to NOT NULL if it contains NULLs. There has to be a value in every row of a NOT NULL column or one that you wish to make NOT NULL.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 6, 2022 at 3:46 pm
To put it another way, you have to first update the existing table to replace all NULL values in that column with non-NULL values before you can alter the table to make that column NOT NULL.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
September 6, 2022 at 6:13 pm
Or, setup a Default and use the WITH VALUES option,
The end result is the same though. There cannot be any NULLs in a NOT NULL column.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 7, 2022 at 6:48 am
Or, setup a Default and use the WITH VALUES option,
The end result is the same though. There cannot be any NULLs in a NOT NULL column.
Why did it take me 10 years to learn about this "WITH VALUES" option ???
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
September 7, 2022 at 4:21 pm
Jeff Moden wrote:Or, setup a Default and use the WITH VALUES option,
The end result is the same though. There cannot be any NULLs in a NOT NULL column.
Why did it take me 10 years to learn about this "WITH VALUES" option ???
😀 For the same reason that it took me a while to learn that TRIM is much more than just a combo of LTRIM/RTRIM. 😀 No one has the time to keep up with everything.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply