我从来没有在我的数据库中真正使用过外键,我一直只是编写可以为我执行任务的代码,但我希望将一些逻辑转移到数据库中并更多地了解外键。
我目前有一个包含表的数据库Categories
:
CREATE TABLE [dbo].[Categories](
[CategoryID] [bigint] IDENTITY(1,1) NOT NULL,
[ParentCategoryID] [bigint] NOT NULL,
[CategoryStatus] [bit] NOT NULL,
[CategoryTitle] [varchar](64) NOT NULL,
[CategorySlug] [varchar](64) NOT NULL,
[CategoryThumbnail] [varchar](128) NULL,
[CategoryHeaderImage] [varchar](128) NULL,
[CategoryDescription] [text] NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UI_Categories_Slug] UNIQUE NONCLUSTERED
(
[CategorySlug] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
我想创建一个与ParentCategoryID
备份相关的外键CategoryID
。在创建 FK 时,哪个是主列,在这种情况下哪个是外部列?