Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have never "hand-coded" object creation code for SQL Server and foreign key decleration is seemingly different between SQL Server and Postgres. Here is my sql so far:

drop table exams;

drop table question_bank;

drop table anwser_bank;

create table exams

(

    exam_id uniqueidentifier primary key,

    exam_name varchar(50),

);

create table question_bank

(

    question_id uniqueidentifier primary key,

    question_exam_id uniqueidentifier not null,

    question_text varchar(1024) not null,

    question_point_value decimal,

    constraint question_exam_id foreign key references exams(exam_id)

);

create table anwser_bank

(

    anwser_id           uniqueidentifier primary key,

    anwser_question_id  uniqueidentifier,

    anwser_text         varchar(1024),

    anwser_is_correct   bit

);

When I run the query I get this error:

Msg 8139, Level 16, State 0, Line 9 Number of referencing columns in foreign key differs from number of referenced columns, table 'question_bank'.

Can you spot the error?

1 Answer

0 votes
by (40.7k points)

 You can use ALTER TABLE, to create the constraint on its own

alter table MyTable

add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) references MyOtherTable(PKColumn)

Related questions

0 votes
3 answers
0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...