exam questions

Exam 70-762 All Questions

View all questions & answers for the 70-762 exam

Exam 70-762 topic 1 question 145 discussion

Actual exam question from Microsoft's 70-762
Question #: 145
Topic #: 1
[All 70-762 Questions]

HOTSPOT -
You have tables with the following requirements:
✑ Some records in the table named OrderLines do not exist in the table named Order.
✑ The column named OrderNumber must be a unique value in the Order table.
✑ The OrderNumber column on the OrderLines table allows null values.
✑ The OrderNumber column on the OrderLines table must be validated with the Order table.
✑ Must not allow a new record in the OrderLines table that does not match a record in the Order table.
How should you complete the statements? To answer, select the appropriate transact-SQL segments from the drop-down menus in the answer area.
NOTE: Each correct selection is worth one point.
Hot Area:

Show Suggested Answer Hide Answer
Suggested Answer:
Box 1: PRIMARY KEY -

Box 2: NOCHECK -
Need NOCHECK as some records in the table named OrderLines do not exist in the table named Order.

Box 3: FOREIGN KEY -
FOREIGN KEY allows inserting NULL values if there is no NOT NULL constraint defined on this key, but the PRIMARY KEY does not accept NULLs.
References:
https://www.sqlshack.com/commonly-used-sql-server-constraints-foreign-key-check-default/

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Ziviola
Highly Voted 5 years, 3 months ago
1. UNIQUE. The OrderNumber column on the OrderLines table allows null values. Unique constraint allow NULL. 2. NOCHECK. Because there are records on OrderLine table they don't exist on Order table. 3. CHECK(Ordernumber IN (SELECT OrderNumber FROM Orders). Must not allow a new record in the OrderLines table that does not match a record in the Order table.
upvoted 17 times
Hoglet
4 years, 10 months ago
While an FK can reference either a PK or UC, in this case we don't want to allow NULLs in Order.OrderNumber. We could either specify a UC and NOT NULL, or use a PK. So I'm going with PK in the first option. You do want NULLs in OrderLines.OrderNumber, so that column can remain NULLable
upvoted 3 times
...
Chocho
4 years, 10 months ago
u can't write subqueries in constraint
upvoted 2 times
MML
4 years, 9 months ago
You are right you cannot have subqueries only scalar expressions are allowed
upvoted 2 times
...
...
MML
4 years, 10 months ago
I agree
upvoted 1 times
MML
4 years, 10 months ago
I mean Primary Key, No Check Check constraint
upvoted 1 times
MML
4 years, 9 months ago
Sorry, the right answer is Unique Nocheck Foreign key
upvoted 3 times
...
...
...
iYoung
5 years, 1 month ago
The check constraint defined on a table must refer to only columns in that table. It can not refer to columns in other tables. A check constraint can NOT include a Subquery. https://www.techonthenet.com/sql_server/check.php
upvoted 9 times
delgadillo
4 years, 10 months ago
Acording to this, it can be done https://stackoverflow.com/questions/3880698/can-a-check-constraint-relate-to-another-table
upvoted 1 times
BabyBee
4 years, 3 months ago
In that example they are using a function.
upvoted 2 times
...
...
...
...
othman_ee
Highly Voted 5 years, 3 months ago
1. unique 2. with check because: The OrderNumber column on the OrderLines table must be validated with the Order table. 3. Foreign Key
upvoted 17 times
New_user
5 years, 3 months ago
1. Set primary key to establish foreign key connection to table. Primary key also ensures uniqueness of records 2. With nocheck constraint doesn't allow to remove existing records in Orderlines 3. Foreign key So, answer looks adorable
upvoted 21 times
Nickname17
5 years ago
The OrderNumber column on the OrderLines table allows null values.
upvoted 3 times
delgadillo
4 years, 10 months ago
Unique NoCheck Check, the last one because the foreign key reference is wrong in the code, it reference itself
upvoted 4 times
...
...
Hiken90
4 years, 10 months ago
Doesn't allow the new record, not doesn't allow TO REMOVE the new record
upvoted 2 times
...
...
...
Andy7622
Most Recent 4 years, 3 months ago
NOCHECK provides the requirement for NULL values in OrderNumber column of OrderLines table . And or course Foreign Key in box 3
upvoted 1 times
...
Andy7622
4 years, 3 months ago
Unique constraint would only allow one NULL value in OrderNumber column in Orders Table. Requerments talk about NULL values in OrderLines table.In my opinion It could be a Primary Key.
upvoted 1 times
...
melvin9900
4 years, 9 months ago
ALTER TABLE DBO.ORDERSLINES WITH NOCHECK ADD CONSTRAINT ORDERS_CHECK check(ordernumber in (Select ordernumber from ORDERS)) This cause an error Sub-queries are not allowed in this context. Only scalar expressions are allowed. You can't have sub queries inside check constraints. What you can do is use a UDF that returns a scalar value inside the check constraint.
upvoted 4 times
...
humpp
4 years, 9 months ago
Unlike PRIMARY KEY constraints, UNIQUE constraints allow for the value NULL. However, as with any value participating in a UNIQUE constraint, only one null value is allowed per column. A UNIQUE constraint can be referenced by a FOREIGN KEY constraint. see https://docs.microsoft.com/en-us/sql/relational-databases/tables/unique-constraints-and-check-constraints?view=sql-server-ver15
upvoted 1 times
...
amar111
4 years, 11 months ago
both primary key and unique will work here . having Primary key on order table , will still allow to enter null order number in orderline table .
upvoted 3 times
Barbedx
4 years, 3 months ago
Agree, also we can set foreign key to the unique constraint.
upvoted 1 times
...
...
lukadataowner
5 years ago
foreign key ... reference... have a syntax error, is impossibile to reference itself
upvoted 4 times
MML
4 years, 9 months ago
Yes it is possible, you can have self reference table. Foreign kei is correct answer
upvoted 2 times
databasejamdown
4 years, 7 months ago
While it is true a table can reference itself. That should not be the case here. The requierements state that The ordernumber in the orderlines table must be validated with the order table. Therefore, the foreign key must relate to the orders table
upvoted 2 times
...
...
...
Bartek
5 years, 2 months ago
All of Your answers are wrong. If in last spot OrderLines table will apear to itself then we cant fulfill last requirement in exercise. See and test sample code made by me : DROP TABLE IF EXISTS DBO.ORDERS DROP TABLE IF EXISTS DBO.ORDERSLINES CREATE TABLE DBO.ORDERS (ORDERNUMBER INT NOT NULL) CREATE TABLE DBO.ORDERSLINES (ORDERNUMBER INT NULL) INSERT INTO DBO.ORDERS VALUES (1),(2),(3),(4) INSERT INTO DBO.ORDERSLINES VALUES (1),(2),(3),(4),(5),(NULL) ALTER TABLE DBO.ORDERS ADD CONSTRAINT ORDERS_KEY PRIMARY KEY (ORDERNUMBER) -- PRIMARY KEY ALTER TABLE DBO.ORDERSLINES WITH NOCHECK ADD CONSTRAINT ORDERS_CHECK FOREIGN KEY (ORDERNUMBER) REFERENCES DBO.ORDERS(ORDERNUMBER) --ERROR IN THIS PART, SHOULD REFERENCE TO ORDERS TABLE, NOT TO THEMSELF LIKE IN EXERCISE INSERT INTO DBO.ORDERSLINES VALUES (6) -- THIS INSERT WILL NOT WORKS "Must not allow a new record in the OrderLines table that does not match a record in the Order table."
upvoted 4 times
Anette
4 years, 10 months ago
Actually this works :) 1. PRIMARY KEY 2. NOCHECK 3. FOREIGN KEY
upvoted 6 times
...
...
tesen_tolga
5 years, 3 months ago
1. I think it should be unique. 2. I think this is correct. Because there are records on OrderLine table they don't exist on Order table.
upvoted 2 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago