Need help please for my SQL

Mary Sachet 41 Reputation points
2022-11-08T04:12:00.903+00:00

These two quieries are not working for me idk why

2
INSERT INTO Unit_Building (Unit_Building_Number, Public_Housing_Number, Number_of_bedrooms,Number_of_Kitchens,Number_of_livingrooms, Unit_area
VALUES
(200,109,3,1,1, tinyint(110)),
(201,111,1,2,1,tinyint(125)),
(202,112,2,3,1,tinyint(130)),
(203,113,3,1,1,tinyint(140)),
(204,114,4,1,1,tinyint(150)),
(204,114,4,1,1,tinyint(150)),
(204,114,4,1,1,tinyint(150)),
(205,115,2,3,1,tinyint(120)),
(206,116,1,1,1,tinyint(180)),
(207,117,4,2,1,tinyint(190)),
(208,118,3,1,1,tinyint(175)),
(209,119,3,2,1,tinyint(200));

Failed to execute query. Error: Incorrect syntax near the keyword 'VALUES'.
Query failed

CREATE TABLE TRACK_OF_MOVING_IN_OUT(
Building_ID int,
Unit_ID int,
Member_ID int,
PRIMARY KEY (Building_ID, Unit_ID, Member_ID),
FOREIGN KEY (Building_ID) REFERENCES BUILDING (Building_ID),
FOREIGN KEY (Unit_ID) REFERENCES UNIT (Unit_ID),
FOREIGN KEY (Member_ID) REFERENCES MEMBER (Member_ID)
ON UPDATE CASCADE ON DELETE CASCADE,
Date_of_Moving_In date,
Date_of_Moving_Out date,
);

Failed to execute query. Error: Foreign key 'FK_TRACK_OF__Build__797309D9' references invalid table 'BUILDING'.
Could not create constraint or index. See previous errors.
Query failed

Developer technologies Transact-SQL
SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2022-11-08T05:16:24.113+00:00

    For the first error, try adding a ")" before VALUES.

    For the second error, probably the BUILDING table does not exist, or you should use a different table and column.

    0 comments No comments

  2. Ronen Ariely 15,206 Reputation points
    2022-11-08T05:32:16.747+00:00

    Hi,

    Failed to execute query. Error: Incorrect syntax near the keyword 'VALUES'.

    Your first query missing a closure of the brackets before the word VALUSE

    In any case, next time or if this did not solve all your issue, please remember that we cannot run a query on a table that does not exists, Therefore, you should always first provide the queries to create the relevant table.

    Error: Foreign key 'FK_TRACK_OF_Build_797309D9' references invalid table 'BUILDING'. Could not create constraint or index. See previous errors.

    (1) Did you notice the part it says See previous errors? I would start with check previous errors

    (2) Your query (second one) has no issue by itself. The syntax is correct, but we cannot check the issue without having the relevant tables which you reference to.

    For example, if I create the missing three table as bellow then there is no error when execute you query

    CREATE TABLE BUILDING (  
        Building_ID int NOT NULL,  
        c int NOT NULL,  
        PRIMARY KEY (Building_ID)  
    );  
    CREATE TABLE UNIT (  
        Unit_ID int NOT NULL,  
        c int NOT NULL,  
        PRIMARY KEY (Unit_ID)  
    );  
    CREATE TABLE MEMBER (  
        Member_ID int NOT NULL,  
        c int NOT NULL,  
        PRIMARY KEY (Member_ID)  
    );  
      
    CREATE TABLE TRACK_OF_MOVING_IN_OUT(  
    	Building_ID int,  
    	Unit_ID int,  
    	Member_ID int,  
    	PRIMARY KEY (Building_ID, Unit_ID, Member_ID),  
    	FOREIGN KEY (Building_ID) REFERENCES BUILDING (Building_ID),  
    	FOREIGN KEY (Unit_ID) REFERENCES UNIT (Unit_ID),  
    	FOREIGN KEY (Member_ID) REFERENCES MEMBER (Member_ID)  
    	ON UPDATE CASCADE ON DELETE CASCADE,  
    	Date_of_Moving_In date,  
    	Date_of_Moving_Out date,  
    );  
    
    0 comments No comments

  3. NikoXu-msft 1,916 Reputation points
    2022-11-08T08:42:54.317+00:00

    Hi @Mary Sachet ,

    For the first question, you seem to be missing half of the parentheses ')'

    INSERT INTO Unit_Building (Unit_Building_Number, Public_Housing_Number, Number_of_bedrooms,Number_of_Kitchens,Number_of_livingrooms, Unit_area)  
    VALUES  
    (200,109,3,1,1, tinyint(110)),  
    (201,111,1,2,1,tinyint(125)),  
    (202,112,2,3,1,tinyint(130)),  
    (203,113,3,1,1,tinyint(140)),  
    (204,114,4,1,1,tinyint(150)),  
    (204,114,4,1,1,tinyint(150)),  
    (204,114,4,1,1,tinyint(150)),  
    (205,115,2,3,1,tinyint(120)),  
    (206,116,1,1,1,tinyint(180)),  
    (207,117,4,2,1,tinyint(190)),  
    (208,118,3,1,1,tinyint(175)),  
    (209,119,3,2,1,tinyint(200));  
    

    For the second question,
    A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables.
    That means you have to make sure you have 'BUILDING', 'UNIT', and 'MEMBER' tables in your database.

    Best regards
    Niko

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.