PrepAway - Latest Free Exam Questions & Answers

You need to set up a rule that every STATE.Country_Abbr must match an existing record in the COUNTRY table

DRAG DROP
You have two existing tables, one named COUNTRY and the other named STATES.
The tables are defined as follows:

You need to set up a rule that every STATE.Country_Abbr must match an existing record in
the COUNTRY table.
Develop the solution by selecting and arranging the required code blocks in the correct order.
You may not need all of the code blocks.

PrepAway - Latest Free Exam Questions & Answers

Answer: See the explanation.

Explanation:

Box 1:

Box 2:

Box 3:

Box 4:

Note:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint
on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders

FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

15 Comments on “You need to set up a rule that every STATE.Country_Abbr must match an existing record in the COUNTRY table

  1. SQLCzar says:

    ALTER TABLE states
    ADD CONSTRAINT FK_state_country
    FOREIGN KEY (country_abbr)
    REFERENCES country(country_abbr)

    create table country(
    sno int primary key,
    country_abbr int)

    CREATE UNIQUE NONCLUSTERED INDEX [IX_country] ON [dbo].[country]
    (
    [country_abbr] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO

    create table states(
    sno1 int primary key,
    country_abbr int)

    CREATE UNIQUE NONCLUSTERED INDEX [IX_states] ON [dbo].[states]
    (
    [country_abbr] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO

    ALTER TABLE states
    ADD CONSTRAINT FK_state_country
    FOREIGN KEY (country_abbr)
    REFERENCES country(country_abbr)




    0



    0

Leave a Reply