Reputation: 16239
CREATE TABLE [M].[SocialInfo]
(
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT newid(),
[MemberId] UNIQUEIDENTIFIER DEFAULT newid(),
[GroupId] UNIQUEIDENTIFIER DEFAULT newid(),
[NewsURL] VARCHAR(200) NULL,
CONSTRAINT [FK_SocialInfo_Member] FOREIGN KEY ([MemberId]) REFERENCES [M].[Member]([Id]),
CONSTRAINT [FK_SocialInfo_Group] FOREIGN KEY ([GroupId]) REFERENCES [M].[Group]([Id])
)
How can I make both FK nullable?
I'm unable to get the syntax?
Upvotes: 2
Views: 6459
Reputation: 21
I think you need to put NULL keyword as it is shown in below link.
Also this sqlfiddle may be helpful
http://sqlfiddle.com/#!3/39a0b/10
Upvotes: 0
Reputation: 152596
A default value for a foreign key makes no sense - use NULL instead
CREATE TABLE [M].[SocialInfo]
(
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT newid(),
[MemberId] UNIQUEIDENTIFIER NULL,
[GroupId] UNIQUEIDENTIFIER NULL,
[NewsURL] VARCHAR(200) NULL,
CONSTRAINT [FK_SocialInfo_Member] FOREIGN KEY ([MemberId]) REFERENCES [M].[Member]([Id]),
CONSTRAINT [FK_SocialInfo_Group] FOREIGN KEY ([GroupId]) REFERENCES [M].[Group]([Id])
)
Upvotes: 0
Reputation: 4934
I think you just left out the column creation piece.
CREATE TABLE [M].[SocialInfo]
(
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT newid(),
[NewsURL] VARCHAR(200) NULL,
MemberId INT NULL,
GroupId INT NULL,
CONSTRAINT [FK_SocialInfo_Member] FOREIGN KEY ([MemberId]) REFERENCES [M].[Member]([Id]),
CONSTRAINT [FK_SocialInfo_Group] FOREIGN KEY ([GroupId]) REFERENCES [M].[Group]([Id])
)
Upvotes: 3