SQL Server foreign key relationships are mandatory constraints, foreign key can also be empty (NULL)

In SQL Server, in fact, foreign key values ​​Can be empty (NULL), and foreign key relationships are not mandatory irrespective of the constraints.

 

We first establish the SQL Server database and two tables People Car, People can have more than a Car, so the two tables is one to many relationship.

 

People build table statement:

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

Wherein the ID column is the primary key table People

 

Car built table statement:

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

Wherein the ID column is the primary key table Car, furthermore column PeopleID Car table is a foreign key, its associated ID People table's primary key. We also set up a mandatory constraints (as foreign key relationships between tables and Car People Table [FK_Car_People] CHECK ).

 

Here we insert two data tables for the People:

INSERT INTO [dbo].[People](ID,Name,Age,Sex)
VALUES
(1,N'张三',25,N''),
( 2 , N ' John Doe ' , 26 is , N ' F ' );

Query results are as follows:

 

Then we insert five data table Car:

INSERT INTO [dbo].[Car](ID,Brand,PeopleID)
VALUES
(1,N'奔驰',1),
( 2 , N ' BMW ' , 1 ),
( . 3 , N ' public ' , 2 ),
( . 4 , N ' Buick ' , NULL ),
( . 5 , N ' Toyota ' , NULL );

We note that the last two data "Buick" and "Toyota", insert the null value (NULL) to Car table foreign key column PeopleID. But the statement was not an error, five data has been successfully inserted, the query results are as follows:

 

Therefore Car PeopleID table foreign key column can be empty (NULL), and a foreign key relationships [FK_Car_People] is not mandatory constraints ( the CHECK ) irrelevant, and only the table you allow Car PeopleID foreign key column is empty (NULL) related.

Now we have the foreign key column of the table PeopleID Car instead allowed to be empty (NULL), then insert the data again in front of the five:

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 ' BMW ' , 1 ),
( . 3 , N ' public ' , 2 ),
( . 4 , N ' Buick ' , NULL ),
( . 5 , N ' Toyota ' , NULL );

Then insert a statement will be given, because the foreign key column table Car PeopleID not be empty (NULL):

 

Therefore, the foreign key relationships in SQL Server to enforce the constraint, the constraint is actually a non-null foreign key, null value (NULL) foreign key are not subject to mandatory foreign key constraint.

 

Guess you like

Origin www.cnblogs.com/OpenCoder/p/11140645.html