如何创建联结 table 并添加索引?
How to create a junction table and add index?
下面是我创建两个 table 的代码,由于播放列表和歌曲是多对多关系,我继续创建一个连接点 table...
CREATE TABLE Playlist (
Id VARCHAR(20),
[Name] VARCHAR(50),
Create_Date DATE,
Username VARCHAR(20),
PRIMARY KEY (Id),
FOREIGN KEY (username) REFERENCES Users (username)
);
CREATE TABLE Song (
Id VARCHAR(20),
Title VARCHAR(50),
Genre VARCHAR(20),
[Length] TIME,
[Language] VARCHAR(20),
Album_Id VARCHAR(20),
Company_Id VARCHAR(20),
PRIMARY KEY (Id),
FOREIGN KEY (Company_Id) REFERENCES Company (Id),
FOREIGN KEY (Album_Id) REFERENCES Album (Id)
);
CREATE TABLE Song_Playlist (
Song_Id VARCHAR(20) NOT NULL,
Playlist_Id VARCHAR(20) NOT NULL,
PRIMARY KEY (Song_Id, Playlist_Id),
FOREIGN KEY (Song_id) REFERENCES Song (Id) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (Playlist_Id) REFERENCES Playlist (Id) ON DELETE RESTRICT ON UPDATE CASCADE
);
但是,当我尝试创建联结点 table 时,它不断向我发送此错误消息:
Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'RESTRICT'.
我想知道如何解决这个问题,如果我想为歌曲 table 中的流派创建索引,我该怎么做?
您似乎正在使用 SQL 服务器(根据代码和错误消息),但来自 docs online
语法:
| FOREIGN KEY
( column [ ,...n ] )
REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ NOT FOR REPLICATION ]
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT }
Specifies what action happens to rows in the table that is altered, if those rows have a referential relationship and the referenced row is deleted from the parent table. The default is NO ACTION.
这就是您收到错误 Incorrect syntax near the keyword 'RESTRICT'
的原因,因为 SQL 服务器没有 ON DELETE RESTRICT
下面是我创建两个 table 的代码,由于播放列表和歌曲是多对多关系,我继续创建一个连接点 table...
CREATE TABLE Playlist (
Id VARCHAR(20),
[Name] VARCHAR(50),
Create_Date DATE,
Username VARCHAR(20),
PRIMARY KEY (Id),
FOREIGN KEY (username) REFERENCES Users (username)
);
CREATE TABLE Song (
Id VARCHAR(20),
Title VARCHAR(50),
Genre VARCHAR(20),
[Length] TIME,
[Language] VARCHAR(20),
Album_Id VARCHAR(20),
Company_Id VARCHAR(20),
PRIMARY KEY (Id),
FOREIGN KEY (Company_Id) REFERENCES Company (Id),
FOREIGN KEY (Album_Id) REFERENCES Album (Id)
);
CREATE TABLE Song_Playlist (
Song_Id VARCHAR(20) NOT NULL,
Playlist_Id VARCHAR(20) NOT NULL,
PRIMARY KEY (Song_Id, Playlist_Id),
FOREIGN KEY (Song_id) REFERENCES Song (Id) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (Playlist_Id) REFERENCES Playlist (Id) ON DELETE RESTRICT ON UPDATE CASCADE
);
但是,当我尝试创建联结点 table 时,它不断向我发送此错误消息:
Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'RESTRICT'.
我想知道如何解决这个问题,如果我想为歌曲 table 中的流派创建索引,我该怎么做?
您似乎正在使用 SQL 服务器(根据代码和错误消息),但来自 docs online
语法:
| FOREIGN KEY
( column [ ,...n ] )
REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ NOT FOR REPLICATION ]
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } Specifies what action happens to rows in the table that is altered, if those rows have a referential relationship and the referenced row is deleted from the parent table. The default is NO ACTION.
这就是您收到错误 Incorrect syntax near the keyword 'RESTRICT'
的原因,因为 SQL 服务器没有 ON DELETE RESTRICT