简体   繁体   中英

Constraint violation - SQL Server

I have instructions to the creation of two tables:

CREATE TABLE S
(
    c INT PRIMARY KEY,
    d INT
);

CREATE TABLE R
(
    a INT PRIMARY KEY,
    b INT REFERENCES S(c)
);

R(a,b) has 4 rows: (0,4),(1,5),(2,4),(3,5)

S(c,d) also has 4 rows: (2,10),(3,11),(4,12),(5,13)

Considering this information I need to choose which of the following modifications will NOT be rejected due to a constraint violation (syntax is not considered here):

Query (a)

INSERT INTO S
VALUES (3, 3)

Query (b)

INSERT INTO S
VALUES (4, 4)

Query (c)

DELETE (5, 13) FROM S

Query (d)

DELETE (4, 12) FROM S

I am having some difficulty answering this question as I believe all of the option will be rejected.

I can't insert (3, 3) or (4, 4) into S because c is a primary key so I can't insert values that will be repeated in c.

And I can't delete (5, 13) nor (4, 12) from S because column b in table R is pointing to column c if those rows are deleted column b will be pointing to something that doesn't exist which will cause an error.

Am I missing something?

Statements (a) and (b) will fail by all means for the reason you mentioned "they are trying to create duplicate primary keys"

However, for statements (c) and (d) they may succeed or fail depending on your referential integrity configurations as follows:

Referential Integrity has 4 options (No Action, Cascade, Set Null, Set Default)

Check how to configure referential integrity

  1. "No Action" means both statements (b) and (c) will fail as you expected.
  2. "Cascade" means cascading deletion operation to the referenced table and delete the relevant records. In your case, deleting S(5,13) will delete R(1,5) and deleting S(4,12) will delete R(0,4) .
  3. "Set Null" means setting the referenced value to Null . In your case deleting S(5,13) will update R(1,5) to R(1,Null) and deleting S(4,12) will update R(0,4) to R(0,Null)
  4. "Set Default" means setting the the referenced value to its default value if there is one, or setting it to Null if no default value defined. In your case it will fail as you didn't set a default value, but assuming you set default value of R(b) to 0 for example deleting S(5,13) will update R(1,5) to R(1,0) and deleting S(4,12) will update R(0,4) to R(0,0)

I'm guessing this is an exam question of some sort, so I'm going to focus more on how to come to the answer rather than just giving you the answer.

You have two constraints to be concerned about.

Table S has a primary key constraint on column C . Table R has a foreign key constraint on column B .

Given the primary key constraint, you will receive an error if you try to INSERT into S with a value of C that already exists.

Examine your INSERT statements to see if any of the values attempting to be inserted into C already exist. If the do, they will fail.

Given the foreign key constraint, you will receive an error if you try to delete from S any value of C that is already referenced by the foreign key constraint on table R .

Examine your DELETE statements. If any of the values being deleted are already referenced in R , then those statements will fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM