标签:primary pad clu 数据库 ignore page ons cluster com
在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 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 ALTER TABLE [dbo].[Car] WITH CHECK ADD CONSTRAINT [FK_Car_People] FOREIGN KEY([PeopleID]) REFERENCES [dbo].[People] ([ID]) GO ALTER TABLE [dbo].[Car] CHECK CONSTRAINT [FK_Car_People] GO
其中列ID是Car表的主键,此外列PeopleID是Car表的外键,其关联People表的主键ID。我们还为People表和Car表之间的外键关系[FK_Car_People]设置了强制约束(CHECK)。
下面我们为People表插入两条数据:
INSERT INTO [dbo].[People](ID,Name,Age,Sex) VALUES (1,N‘张三‘,25,N‘男‘), (2,N‘李四‘,26,N‘女‘);
查询结果如下:
然后我们为Car表插入五条数据:
INSERT INTO [dbo].[Car](ID,Brand,PeopleID) VALUES (1,N‘奔驰‘,1), (2,N‘宝马‘,1), (3,N‘大众‘,2), (4,N‘别克‘,NULL), (5,N‘丰田‘,NULL);
注意我们在最后两条数据"别克"和"丰田"中,插入了空值(NULL)到Car表的外键列PeopleID。但是语句并没有报错,五条数据都被成功插入了,查询结果如下:
所以Car表的外键列PeopleID能不能为空(NULL),和其外键关系[FK_Car_People]是不是强制约束(CHECK)无关,它只和Car表允不允许外键列PeopleID为空(NULL)相关。
现在我们把Car表的外键列PeopleID改为不允许为空(NULL),然后再次插入前面的五条数据:
DELETE FROM [dbo].[Car] ALTER TABLE [dbo].[Car] ALTER COLUMN [PeopleID] INT NOT NULL INSERT INTO [dbo].[Car](ID,Brand,PeopleID) VALUES (1,N‘奔驰‘,1), (2,N‘宝马‘,1), (3,N‘大众‘,2), (4,N‘别克‘,NULL), (5,N‘丰田‘,NULL);
这时插入语句就会报错了,因为此时Car表的外键列PeopleID不能为空(NULL):
所以SQL Server中的外键关系强制约束,约束的实际上是外键的非空值,而外键的空值(NULL)并不受外键强制约束。
SQL Server外键关系是强制约束,外键值也可以是空(NULL)
标签:primary pad clu 数据库 ignore page ons cluster com
原文地址:https://www.cnblogs.com/OpenCoder/p/11140645.html