locked
How to create theses tables in SQL Server ? RRS feed

  • Question

  • User-1392235324 posted

    Now I created two tables as following :


    CREATE TABLE StatsTable
    (
       ID int IDENTITY(1,1),
       Title varchar(100),
       NumberOfQuestions int,
       HighestScore int,
       LowestScore int
    )

    CREATE TABLE Questions
    (
       QuestionID int primary key IDENTITY(1,1),
       Answer varchar(100),
       AnswerID int,
       QuestionNumber int,
       QuestionText varchar(400)
    )

    But, the beneath table has problem :

    CREATE TABLE Choices
    (
       ChoiceID int primary key IDENTITY(1,1),
       ChoiceLetter varchar(100),
       ChoiceText varchar(200),
       CONSTRAINT QuestionID foreign key(QuestionID)
       REFERENCES Questions (QuestionID)
       on delete cascade
       on update cascade
    )

    Msg 1769, Level 16, State 1, Line 1
    Foreign key 'QuestionID' references invalid column 'QuestionID' in referencing table 'Choices'.
    Msg 1750, Level 16, State 0, Line 1
    Could not create constraint. See previous errors.

    Thursday, July 28, 2016 8:52 AM

Answers

  • User-1404113929 posted

    hi,

    try this one

    CREATE TABLE Choices
    (
    ChoiceID int primary key IDENTITY(1,1),
    ChoiceLetter varchar(100),
    ChoiceText varchar(200),
    QuestionID int,
    CONSTRAINT QuestionID foreign key(QuestionID)
    REFERENCES Questions (QuestionID)
    on delete cascade
    on update cascade
    )

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 9:13 AM
  • User-1392235324 posted

    Thank you balu.devara

    Your solution solved my problem

    and this is another :

    CREATE TABLE Choices
    (
       ChoiceID int primary key IDENTITY(1,1),
       ChoiceLetter varchar(100),
       ChoiceText varchar(200),
       QuestionID int
    )

    alter table Choices add foreign key(QuestionID)
    REFERENCES Questions (QuestionID)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 9:54 AM
  • User-1404113929 posted

    hi,

    can you try this bellow code.

    CREATE TABLE Choices
    (
    ChoiceID int primary key IDENTITY(1,1),
    ChoiceLetter varchar(100),
    ChoiceText varchar(200),
    QuestionID int
    )

    alter table Choices add CONSTRAINT fk_questionid foreign key (QuestionID)
    REFERENCES Questions (QuestionID)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 11:50 AM

All replies

  • User-1404113929 posted

    hi,

    try this one

    CREATE TABLE Choices
    (
    ChoiceID int primary key IDENTITY(1,1),
    ChoiceLetter varchar(100),
    ChoiceText varchar(200),
    QuestionID int,
    CONSTRAINT QuestionID foreign key(QuestionID)
    REFERENCES Questions (QuestionID)
    on delete cascade
    on update cascade
    )

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 9:13 AM
  • User-1392235324 posted

    Thank you balu.devara

    Your solution solved my problem

    and this is another :

    CREATE TABLE Choices
    (
       ChoiceID int primary key IDENTITY(1,1),
       ChoiceLetter varchar(100),
       ChoiceText varchar(200),
       QuestionID int
    )

    alter table Choices add foreign key(QuestionID)
    REFERENCES Questions (QuestionID)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 9:54 AM
  • User-1404113929 posted

    hi,

    can you try this bellow code.

    CREATE TABLE Choices
    (
    ChoiceID int primary key IDENTITY(1,1),
    ChoiceLetter varchar(100),
    ChoiceText varchar(200),
    QuestionID int
    )

    alter table Choices add CONSTRAINT fk_questionid foreign key (QuestionID)
    REFERENCES Questions (QuestionID)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 11:50 AM