主要和SQL的SQL独特价值外键

时间:2015-12-27 07:59:18

标签: sql-server foreign-keys unique primary-key

我很确定之前已经问过这个问题,但是使用SQL我试图只允许表使用主键和外键来获得唯一值。这可能听起来令人困惑,因此这些是我的SQL语句。

Folder表:

CREATE TABLE [dbo].[Folder] 
(
    [FolderID] VARCHAR(50) NOT NULL,
    [FolderURL] NCHAR(255) NOT NULL,
    [FolderTag] VARCHAR(50) NOT NULL,

    PRIMARY KEY CLUSTERED ([FolderURL] ASC),
    UNIQUE NONCLUSTERED ([FolderID] ASC)
);

Extension表:

CREATE TABLE [dbo].[Extension] 
(
    [ExtensionID] VARCHAR(10) NOT NULL,
    [FolderID] VARCHAR(50) NOT NULL,
    [LocationURL] VARCHAR(MAX) NOT NULL,

    PRIMARY KEY CLUSTERED ([ExtensionID] ASC),

    CONSTRAINT [fk_FolderID] 
        FOREIGN KEY ([FolderID])
        REFERENCES [dbo].[Folder]([FolderID]) ON DELETE CASCADE,
    CONSTRAINT uc_Extension UNIQUE ([ExtensionID], [FolderID]) 
);

Extension表中,我希望它使用[FolderID] & [ExtensionID]具有唯一值。我首先尝试通过使用两个值创建主键来实现这一点,然后我尝试了上面的当前语句。

修改

我没有提供足够详细的信息,下面是我要创建的两个表的布局以及使用上述方法的结果。

Folder表:

----------------------------------------------------------
| Folder ID |        FolderURL        |  FolderTag       |
|-----------|-------------------------|------------------|
|   Home    | C:/users/test/          | Home Folder      |
| Downloads | E:/Donwloads            | Downloads Folder |
|   Music   | C:/users/test/music     | Music Folder     |
| Documents | C:/users/test/documents | Downloads Folder |
----------------------------------------------------------

Extension表:

---------------------------------------------------------
| Extension ID | FolderID  |        LocationURL         |
|--------------|-----------|----------------------------|
|    .jpg      | Downloads | C:/users/test/pictures     | = Pass 
|    .png      | Downloads | C:/users/test/pictures     | = Pass
|    .docx     | Documents | C:/users/test/BusinessDocs | = Pass
|    .mp3      |   Home    | C:/users/test/music        | = Pass
|    .jpg      |   Home    | C:/users/test/pictures     | = Fail
|    .mp3      | Downloads | C:/users/test/music        | = Fail
---------------------------------------------------------

从上面提供的表格布局中,您可以看到由于.mp3 ID而导致的最后两个失败已经输入到表格中。我要问的是创建一个具有唯一主键的表,或其他允许唯一ExtensionIDFolderID的表,以便它允许{{1}中的最后两个值1}}表。

如果有人能帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:0)

CREATE TABLE [dbo].[Extension] 
(
    [ExtensionID] VARCHAR(10) NOT NULL,
    [FolderID] VARCHAR(50) NOT NULL,
    [LocationURL] VARCHAR(MAX) NOT NULL,

    PRIMARY KEY ([ExtensionID], [FolderID]),

    CONSTRAINT [fk_FolderID] 
        FOREIGN KEY ([FolderID])
        REFERENCES [dbo].[Folder]([FolderID]) ON DELETE CASCADE
);

这里有完整的代码和输出。 SQLFiddle.com对我不利;这是PostgreSQL语法。

CREATE TABLE Folder
(
    FolderID VARCHAR(50) NOT NULL,
    FolderURL NCHAR(255) NOT NULL,
    FolderTag VARCHAR(50) NOT NULL,

    PRIMARY KEY (FolderURL),
    UNIQUE (FolderID)
);

CREATE TABLE Extension 
(
    ExtensionID VARCHAR(10) NOT NULL,
    FolderID VARCHAR(50) NOT NULL,
    LocationURL VARCHAR(255) NOT NULL,

    PRIMARY KEY (ExtensionID, FolderID),

    CONSTRAINT fk_FolderID 
        FOREIGN KEY (FolderID)
        REFERENCES Folder(FolderID) ON DELETE CASCADE
);

insert into folder values
('Home', 'C:/users/test/', 'Home Folder'),
('Downloads', 'E:/Donwloads', 'Downloads Folder'),
('Music', 'C:/users/test/music', 'Music Folder'),
('Documents', 'C:/users/test/documents', 'Downloads Folder');

insert into Extension values
('.jpg', 'Downloads', 'C:/users/test/pictures'),
('.png', 'Downloads', 'C:/users/test/pictures'),
('.docx', 'Documents', 'C:/users/test/BusinessDocs'),
('.mp3', 'Home', 'C:/users/test/music'),
('.jpg', 'Home', 'C:/users/test/pictures'),
('.mp3', 'Downloads', 'C:/users/test/music');

select * from folder;
folderid    folderurl                 foldertag
--
Home        C:/users/test/            Home Folder
Downloads   E:/Donwloads              Downloads Folder
Music       C:/users/test/music       Music Folder
Documents   C:/users/test/documents   Downloads Folder
select * from extension;
extensionid  folderid    locationurl
--
.jpg         Downloads   C:/users/test/pictures
.png         Downloads   C:/users/test/pictures
.docx         Documents  C:/users/test/BusinessDocs
.mp3         Home        C:/users/test/music
.jpg         Home        C:/users/test/pictures
.mp3         Downloads   C:/users/test/music