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

  • Thread starter SlurrerOfSpeech
  • Start date
In summary: Your database setup seems logical and there may be other ways to achieve the same result without using a trigger, such as setting up a foreign key with ON DELETE CASCADE option.
  • #1
SlurrerOfSpeech
141
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
  • #2
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

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

1. What does the error message "The multi-part identifier ... could not be found" mean?

The error message "The multi-part identifier ... could not be found" means that the identifier used in the query cannot be found or is not recognized. This could be due to a misspelled identifier, a missing table or column, or an incorrect syntax in the query.

2. How can I solve the "multi-part identifier" error?

To solve the "multi-part identifier" error, you can check for any spelling errors in the identifier, make sure all tables and columns referenced in the query exist and are spelled correctly, and ensure that the query syntax is correct. You may also need to add aliases or fully qualify the identifier if it is ambiguous.

3. Why am I getting the "multi-part identifier" error?

The "multi-part identifier" error can occur due to various reasons, such as a misspelled identifier, a missing table or column, an incorrect syntax in the query, or an ambiguous identifier. It could also be caused by a missing or incorrect join condition in a JOIN statement.

4. Can a "multi-part identifier" error be caused by a database connection issue?

No, a "multi-part identifier" error is not caused by a database connection issue. It is solely related to the query and the identifiers used in it. If there is a database connection issue, a different error would be displayed.

5. Is there a way to prevent the "multi-part identifier" error from occurring?

Yes, there are ways to prevent the "multi-part identifier" error from occurring, such as double-checking all identifiers for spelling and ensuring they exist and are spelled correctly, using aliases or fully qualifying the identifiers, and using correct syntax in the query. It is also important to regularly maintain and update the database to avoid any missing or incorrect identifiers.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Beyond the Standard Models
Replies
28
Views
4K
Back
Top