在SQL Server中,实际上外键值可不可以为空(NULL),和外键关系是不是强制约束无关。
我们先在SQL Server数据库中建立两张表People和Car,一个People可以有多个Car,所以这两张表是一对多关系。
建立表
People建表语句:
CREATE TABLE [dbo].[People]( [ID] [int] NOT NULL, [Name] [nvarchar](50) NULL, [Age] [int] NULL, [Sex] [nvarchar](50) NULL, CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
其中列ID是People表的主键
Car建表语句:
CREATE TABLE [dbo].[Car]( [ID] [int] NOT NULL, [Brand] [nvarchar](50) NULL, [PeopleID] [int] NULL, CONSTRAINT [PK_Car] PRIMARY KEY