"The multi-part identifier .... could not be found"

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
AI Thread Summary
The discussion revolves around a SQL Server 2014 database setup involving tables for problems, solutions, ratings, comments, and a closure table for comment hierarchy. The user encounters an error with a trigger designed to delete all comment descendants when a comment is deleted, specifically the error stating that "the multi-part identifier deleted.descendant could not be bound." This issue arises because the "deleted" virtual table can contain multiple records in a delete trigger. The suggested solution is to modify the delete statement to use an IN clause, allowing for the deletion of multiple comments based on the IDs in the "deleted" table. Additionally, there is a request for feedback on the overall database logic and alternatives to using a trigger for this functionality.
SlurrerOfSpeech
Messages
141
Reaction score
11
Microsoft SQL Server 2014 (T-SQL)

My codez are

Code:
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(3000) NOT NULL,
    test_func_code VARCHAR(3000) NOT NULL,
    test_input_code VARCHAR(3000) 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(3000),
   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=deleted.descendant;

and the very last part deleted.descendant is getting hit with the error

"The multi-part identifier deleted.descendant could not be bound."

Any help?

Also, is there anything wrong with the logic of my database setup, and there is any way of doing what I'm trying to do without using a trigger?
 
Technology news on Phys.org
SlurrerOfSpeech said:
Microsoft SQL Server 2014 (T-SQL)

My codez are

Code:
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(3000) NOT NULL,
    test_func_code VARCHAR(3000) NOT NULL,
    test_input_code VARCHAR(3000) 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(3000),
   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=deleted.descendant;

and the very last part deleted.descendant is getting hit with the error

"The multi-part identifier deleted.descendant could not be bound."

Any help?

Also, is there anything wrong with the logic of my database setup, and there is any way of doing what I'm trying to do without using a trigger?

In a delete trigger, the virtual table "deleted" can contain more than one record, that's why
Code:
DELETE FROM Comments WHERE id=deleted.descendant;
doesn't work.
Try something like
SQL:
DELETE FROM Comments WHERE id in (select deleted.descendant from deleted);
 
  • Like
Likes Silicon Waffle and SlurrerOfSpeech
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top