我的代码是
CREATE DATABASE JsPracticeDb;
/* Create tables corresponding to the problems, solutions to
problems, and ratings of problems or solutions */
CREATE TABLE Problems
(
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
prompt_code VARCHAR(5000) NOT NULL,
test_func_code VARCHAR(5000) NOT NULL,
test_input_code VARCHAR(5000) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Solutions
(
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
problem_id INT NOT NULL,
solver VARCHAR(50),
code VARCHAR(5000),
FOREIGN KEY (problem_id) REFERENCES Problems(id) ON DELETE CASCADE,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Ratings
(
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
solution_id INT NOT NULL,
stars TINYINT NOT NULL,
FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
CREATE TABLE Comments
(
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
solution_id INT NOT NULL,
commenter VARCHAR(50),
comment VARCHAR(2000) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
/* Closure Table for comment hierarchy */
CREATE TABLE CommentPaths
(
ancestor INT NOT NULL,
descendant INT NOT NULL,
PRIMARY KEY (ancestor, descendant),
FOREIGN KEY (ancestor) REFERENCES Comments(id) ON DELETE CASCADE,
FOREIGN KEY (descendant) REFERENCES Comments(id) ON DELETE CASCADE
);
GO
/* Create trigger for deleting all comment descendants when
the comment is deleted */
CREATE TRIGGER deleteDescendants ON CommentPaths
FOR DELETE
AS
DELETE FROM Comments
WHERE id IN (SELECT deleted.descendant FROM deleted);
我正在
Msg 2714,Level 16,State 6,Line 5
数据库中已经有一个名为“Problems”的对象。Msg 8197,Level 16,State 4,Procedure deleteDescendants,Line 45
对象“CommentPaths”不存在或对此操作无效。
作为错误。还没有一个名为Problems
的对象,所以我不知道为什么会这样认为。另外,为什么说CommentPaths
不存在(解析触发器时不应该存在)?
答案 0 :(得分:4)
创建数据库后,必须使用USE
切换到该数据库,否则命令将发送到当前数据库,而不是新创建的数据库。
答案 1 :(得分:1)
它显示错误,因为在您执行脚本的数据库中存在表Problems
,请尝试以下解决方案
CREATE DATABASE JsPracticeDb;
/* Create tables corresponding to the problems, solutions to
problems, and ratings of problems or solutions */
GO
USE[JsPracticeDb]
Go
CREATE TABLE Problems (
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
prompt_code VARCHAR(5000) NOT NULL,
test_func_code VARCHAR(5000) NOT NULL,
test_input_code VARCHAR(5000) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Solutions (
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
problem_id INT NOT NULL,
solver VARCHAR(50),
code VARCHAR(5000),
FOREIGN KEY (problem_id) REFERENCES Problems(id) ON DELETE CASCADE,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Ratings (
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
solution_id INT NOT NULL,
stars TINYINT NOT NULL,
FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
CREATE TABLE Comments (
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
solution_id INT NOT NULL,
commenter VARCHAR(50),
comment VARCHAR(2000) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
/* Closure Table for comment hierarchy */
CREATE TABLE CommentPaths (
ancestor INT NOT NULL,
descendant INT NOT NULL,
PRIMARY KEY (ancestor, descendant),
FOREIGN KEY (ancestor) REFERENCES Comments(id) ON DELETE CASCADE,
FOREIGN KEY (descendant) REFERENCES Comments(id) ON DELETE CASCADE
);
/* Create trigger for deleting all comment descendants when
the comment is deleted */
GO
CREATE TRIGGER deleteDescendants ON CommentPaths
FOR DELETE
AS
DELETE FROM Comments
WHERE id IN (SELECT deleted.descendant FROM deleted);
这将解决您的问题。