Sample Exam Questions

From the objective of OSS-DB Exam Silver
S3.1 SQL commands (UPDATE statement)

Sample Question

3.52

You created the following tables, and created a row in each table:


create table dept(id integer, name varchar);
create table emp(id integer, name varchar, dept_id integer, dept_name text);

Now, to copy the 'name' column of the 'dept' table to the 'dept_name' column of the 'emp' table, you execute the following SQL:

update emp set dept_name = (select name from dept where id = emp.dept_id);
 

Choose the most appropriate statement. 


  1. The id column of the dept table must have a primary key (PRIMARY KEY) or unique key (UNIQUE) constraint.

  2. The dept_id column of the emp table must have a primary key (PRIMARY KEY) or unique key (UNIQUE) constraint.

  3. If there are duplicates in the id column of the dept table, an error may occur.

  4. If there are duplicates in the id column of the dept table, the result of update is unpredictable.

  5. Unable to execute due to syntax error.

※This sample exam is different from those that appear in the actual OSS-DB Exam.
2024/05/10

Answer and Explanation

The most basic form of the UPDATE statement is
UPDATE table_name SET column_name = value WHERE condition
However, you can perform an update while referencing other tables by placing a parenthesized SELECT statement in the "value" area.

It is important to note that the SELECT statement must not return more than one row (resulting in an error). For example, when the dept table is as shown below, if the emp table has a row with a value 2 in dept_id, an error that says "ERROR: more than one row returned by a subquery used as an expression" occurs.

=> select * from dept;
id |      name
----+----------------
  1 | Mariners
  2 | Yankees
  2 | Red Sox
(3 lines)

Of course, this would not happen if the id column of the dept table had a unique constraint, but it is not required to have the constraint. In addition, even if there were duplicates, the SQL would still succeed if there were no rows with dept_id=2 in the emp table. In this table structure, a foreign key (FOREIGN KEY) constraint is often used to reference the id column of the dept table from the dept_id column of the emp table. However, when using a foreign key constraint, the referenced side, that is the id column of the dept table, must have a primary key or unique constraint.

The presence of a unique constraint on dept_id in the emp table is irrelevant to the execution of this UPDATE statement, but in practice, you wouldn't want to place a unique constraint on a column referencing another table.

Note that PostgreSQL's own extension allows UPDATE to be joined with a FROM clause. You can  specify the almost equivalent operation as the sample UPDATE statement as follows:.

update emp set dept_name = dept.name from dept where emp.dept_id = dept.id

Note that, in this case an error does not occur even if more than one row with id=2 exist in the dept table as shown above; however, the result of updating rows with dept_id=2 in the emp table is unpredictable.

Therefore, the correct answer is C.