<p>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:</p>

<pre><code>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
);
</code></pre>

<p>When I run the query I get this error:</p>

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

<p>Can you spot the error?</p>

[{'answer_id': 48778, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 9, 'selected': True, 'text': '<pre><code>create table question_bank\n(\n    question_id uniqueidentifier primary key,\n    question_exam_id uniqueidentifier not null,\n    question_text varchar(1024) not null,\n    question_point_value decimal,\n    constraint fk_questionbank_exams foreign key (question_exam_id) references exams (exam_id)\n);\n</code></pre>\n'}, {'answer_id': 48838, 'author': 'Sara Chipps', 'author_id': 4140, 'author_profile': 'https://Stackoverflow.com/users/4140', 'pm_score': 6, 'selected': False, 'text': '<p>You can also name your foreign key constraint by using:</p>\n\n<pre><code>CONSTRAINT your_name_here FOREIGN KEY (question_exam_id) REFERENCES EXAMS (exam_id)\n</code></pre>\n'}, {'answer_id': 48883, 'author': 'AlexCuse', 'author_id': 794, 'author_profile': 'https://Stackoverflow.com/users/794', 'pm_score': 8, 'selected': False, 'text': "<p>And if you just want to create the constraint on its own, you can use ALTER TABLE</p>\n\n<pre><code>alter table MyTable\nadd constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) references MyOtherTable(PKColumn)\n</code></pre>\n\n<p>I wouldn't recommend the syntax mentioned by Sara Chipps for inline creation, just because I would rather name my own constraints.</p>\n"}, {'answer_id': 52341, 'author': 'Vitor Silva', 'author_id': 1842864, 'author_profile': 'https://Stackoverflow.com/users/1842864', 'pm_score': 3, 'selected': False, 'text': "<p>Like you, I don't usually create foreign keys by hand, but if for some reason I need the script to do so I usually create it using ms sql server management studio and before saving then changes,  I select Table Designer | Generate Change Script</p>\n"}, {'answer_id': 6297360, 'author': 'Bijimon', 'author_id': 51038, 'author_profile': 'https://Stackoverflow.com/users/51038', 'pm_score': 4, 'selected': False, 'text': '<pre><code>create table question_bank\n(\n    question_id uniqueidentifier primary key,\n    question_exam_id uniqueidentifier not null constraint fk_exam_id foreign key references exams(exam_id),\n    question_text varchar(1024) not null,\n    question_point_value decimal\n);\n</code></pre>\n\n<p>--That will work too. Pehaps a bit more intuitive construct?</p>\n'}, {'answer_id': 24415106, 'author': 'Shavais', 'author_id': 283102, 'author_profile': 'https://Stackoverflow.com/users/283102', 'pm_score': 5, 'selected': False, 'text': "<p>I like AlexCuse's answer, but something you should pay attention to whenever you add a foreign key constraint is how you want updates to the referenced column in a row of the referenced table to be treated, and especially how you want deletes of rows in the referenced table to be treated. </p>\n\n<p>If a constraint is created like this:</p>\n\n<pre><code>alter table MyTable\nadd constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) \nreferences MyOtherTable(PKColumn)\n</code></pre>\n\n<p>.. then <strong>updates or deletes in the referenced table will blow up with an error if there is a corresponding row in the referencing table.</strong></p>\n\n<p>That might be the behaviour you want, but in my experience, it much more commonly isn't.</p>\n\n<p>If you instead create it like this:</p>\n\n<pre><code>alter table MyTable\nadd constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) \nreferences MyOtherTable(PKColumn)\non update cascade \non delete cascade\n</code></pre>\n\n<p>..then updates and deletes in the parent table will result in updates and deletes of the corresponding rows in the referencing table.</p>\n\n<p>(I'm not suggesting that the default should be changed, the default errs on the side of caution, which is good. I'm just saying it's something that a person who is creating constaints <strong><em>should always pay attention to</em></strong>.)</p>\n\n<p>This can be done, by the way, when creating a table, like this:</p>\n\n<pre><code>create table ProductCategories (\n  Id           int identity primary key,\n  ProductId    int references Products(Id)\n               on update cascade on delete cascade\n  CategoryId   int references Categories(Id) \n               on update cascade on delete cascade\n)\n</code></pre>\n"}, {'answer_id': 33299702, 'author': 'Stefan Steiger', 'author_id': 155077, 'author_profile': 'https://Stackoverflow.com/users/155077', 'pm_score': 2, 'selected': False, 'text': "<p>Necromancing.<br />\nActually, doing this correctly is a little bit trickier. </p>\n\n<p>You first need to check if the primary-key exists for the column you want to set your foreign key to reference to. </p>\n\n<p>In this example, a foreign key on table T_ZO_SYS_Language_Forms is created, referencing dbo.T_SYS_Language_Forms.LANG_UID </p>\n\n<pre><code>-- First, chech if the table exists...\nIF 0 &lt; (\n    SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES \n    WHERE TABLE_TYPE = 'BASE TABLE'\n    AND TABLE_SCHEMA = 'dbo'\n    AND TABLE_NAME = 'T_SYS_Language_Forms'\n)\nBEGIN\n    -- Check for NULL values in the primary-key column\n    IF 0 = (SELECT COUNT(*) FROM T_SYS_Language_Forms WHERE LANG_UID IS NULL)\n    BEGIN\n        ALTER TABLE T_SYS_Language_Forms ALTER COLUMN LANG_UID uniqueidentifier NOT NULL \n\n        -- No, don't drop, FK references might already exist...\n        -- Drop PK if exists \n        -- ALTER TABLE T_SYS_Language_Forms DROP CONSTRAINT pk_constraint_name \n        --DECLARE @pkDropCommand nvarchar(1000) \n        --SET @pkDropCommand = N'ALTER TABLE T_SYS_Language_Forms DROP CONSTRAINT ' + QUOTENAME((SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS \n        --WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' \n        --AND TABLE_SCHEMA = 'dbo' \n        --AND TABLE_NAME = 'T_SYS_Language_Forms' \n        ----AND CONSTRAINT_NAME = 'PK_T_SYS_Language_Forms' \n        --))\n        ---- PRINT @pkDropCommand \n        --EXECUTE(@pkDropCommand) \n\n        -- Instead do\n        -- EXEC sp_rename 'dbo.T_SYS_Language_Forms.PK_T_SYS_Language_Forms1234565', 'PK_T_SYS_Language_Forms';\n\n\n        -- Check if they keys are unique (it is very possible they might not be) \n        IF 1 &gt;= (SELECT TOP 1 COUNT(*) AS cnt FROM T_SYS_Language_Forms GROUP BY LANG_UID ORDER BY cnt DESC)\n        BEGIN\n\n            -- If no Primary key for this table\n            IF 0 =  \n            (\n                SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS \n                WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' \n                AND TABLE_SCHEMA = 'dbo' \n                AND TABLE_NAME = 'T_SYS_Language_Forms' \n                -- AND CONSTRAINT_NAME = 'PK_T_SYS_Language_Forms' \n            )\n                ALTER TABLE T_SYS_Language_Forms ADD CONSTRAINT PK_T_SYS_Language_Forms PRIMARY KEY CLUSTERED (LANG_UID ASC)\n            ;\n\n            -- Adding foreign key\n            IF 0 = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME = 'FK_T_ZO_SYS_Language_Forms_T_SYS_Language_Forms') \n                ALTER TABLE T_ZO_SYS_Language_Forms WITH NOCHECK ADD CONSTRAINT FK_T_ZO_SYS_Language_Forms_T_SYS_Language_Forms FOREIGN KEY(ZOLANG_LANG_UID) REFERENCES T_SYS_Language_Forms(LANG_UID); \n        END -- End uniqueness check\n        ELSE\n            PRINT 'FSCK, this column has duplicate keys, and can thus not be changed to primary key...' \n    END -- End NULL check\n    ELSE\n        PRINT 'FSCK, need to figure out how to update NULL value(s)...' \nEND \n</code></pre>\n"}, {'answer_id': 37452116, 'author': 'Abhishek Jaiswal', 'author_id': 5275530, 'author_profile': 'https://Stackoverflow.com/users/5275530', 'pm_score': 3, 'selected': False, 'text': '<p>To Create a foreign key on any table </p>\n\n<pre><code>ALTER TABLE [SCHEMA].[TABLENAME] ADD FOREIGN KEY (COLUMNNAME) REFERENCES [TABLENAME](COLUMNNAME)\nEXAMPLE\nALTER TABLE [dbo].[UserMaster] ADD FOREIGN KEY (City_Id) REFERENCES [dbo].[CityMaster](City_Id)\n</code></pre>\n'}, {'answer_id': 44222324, 'author': 'elkhayari abderrazzak', 'author_id': 7840801, 'author_profile': 'https://Stackoverflow.com/users/7840801', 'pm_score': 3, 'selected': False, 'text': '<p>This script is about creating tables with foreign key and I added referential integrity constraint <strong>sql-server</strong>.</p>\n\n<pre><code>create table exams\n(  \n    exam_id int primary key,\n    exam_name varchar(50),\n);\n\ncreate table question_bank \n(\n    question_id int primary key,\n    question_exam_id int not null,\n    question_text varchar(1024) not null,\n    question_point_value decimal,\n    constraint question_exam_id_fk\n       foreign key references exams(exam_id)\n               ON DELETE CASCADE\n);\n</code></pre>\n'}, {'answer_id': 45422537, 'author': 'Md Ashikul Islam', 'author_id': 7801470, 'author_profile': 'https://Stackoverflow.com/users/7801470', 'pm_score': 3, 'selected': False, 'text': "<p>If you want to create two table's columns into a relationship by using a query try the following:</p>\n\n<pre><code>Alter table Foreign_Key_Table_name add constraint \nForeign_Key_Table_name_Columnname_FK\nForeign Key (Column_name) references \nAnother_Table_name(Another_Table_Column_name)\n</code></pre>\n"}, {'answer_id': 57623016, 'author': 'Aamir Shaikh', 'author_id': 6043224, 'author_profile': 'https://Stackoverflow.com/users/6043224', 'pm_score': 2, 'selected': False, 'text': '<p>I always use this syntax to create the foreign key constraint between 2 tables</p>\n\n<pre><code>Alter Table ForeignKeyTable\nAdd constraint `ForeignKeyTable_ForeignKeyColumn_FK`\n`Foreign key (ForeignKeyColumn)` references `PrimaryKeyTable (PrimaryKeyColumn)`\n</code></pre>\n\n<p>i.e. </p>\n\n<pre><code>Alter Table tblEmployee\nAdd constraint tblEmployee_DepartmentID_FK\nforeign key (DepartmentID) references tblDepartment (ID)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/48772', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1638/']