Comprehension

Ms. Zoya is a Production Manager in a factory which packages mineral water. She decides to create a table in a database to keep track of the stock present in the factory. Each record of the table will have the following fields:

W_Code – Code of the item (type – CHAR(5))
W_Description – Description of the item (type – VARCHAR(20))
B_Qty – Balance quantity of the item (type – INTEGER)
U_Price – Unit Price of the item (type – FLOAT)

The name of the table is W_STOCK.

Question: 1

Write an SQL command to create the above table (W_Code should be the primary key).

Show Hint

Use CREATE TABLE W_STOCK with the four columns and add PRIMARY KEY after W_Code CHAR(5).
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Recall the pattern of CREATE TABLE:
The general form is: CREATE TABLE table_name (column1 type constraint, column2 type, ...);
Columns are separated by commas, and the whole list sits in one pair of brackets.

Step 2: Fill the pattern with the given fields:
Table name: W_STOCK.
First column: W_Code CHAR(5) with the constraint PRIMARY KEY.
Next columns: W_Description VARCHAR(20), B_Qty INTEGER, U_Price FLOAT.

Step 3: Put it together:
CREATE TABLE W_STOCK (
    W_Code CHAR(5) PRIMARY KEY,
    W_Description VARCHAR(20),
    B_Qty INTEGER,
    U_Price FLOAT
);
An equal form puts the key at the end as a table constraint: PRIMARY KEY (W_Code). Both forms are correct.

Final Answer:
The CREATE TABLE command above builds W_STOCK with W_Code as its primary key.
Was this answer helpful?
0
Question: 2

Can U_Price be the primary key of the above table? Justify your answer.

Show Hint

A primary key must be unique and not NULL. Think about whether two items can have the same unit price.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Start from the test for a primary key:
A column can be a primary key only if no two rows can ever hold the same value in it. NULL is also not allowed in that column.
So we ask one question: can two rows of W_STOCK ever have the same U_Price?

Step 2: Try a small example:
Suppose the table has these rows:
W1001, 1 litre bottle, 200, 20.0
W1002, 1 litre jar, 150, 20.0
Both rows carry the unit price 20.0. If U_Price were the primary key, the second row would be rejected as a duplicate.
In a real stock table, such rows must be allowed.

Step 3: Check the NULL rule:
A primary key cannot be empty. A new item may come without a fixed price. Such a row could not be stored if U_Price were the key.

Step 4: Name the better choice:
W_Code is a unique code given to each item. It never repeats and is always filled. So W_Code is the right primary key.

Final Answer:
No. U_Price can repeat and can be NULL, so it cannot identify a row. W_Code is the proper primary key.
Was this answer helpful?
0
Question: 3

Assuming that the table W_STOCK is already created, write an SQL command to add an attribute E_Date (of DATE type) to the table.

Show Hint

Use the DDL command ALTER TABLE with the ADD clause, followed by the column name and its type.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Decide between DDL and DML:
INSERT, UPDATE and DELETE work on the data inside a table (DML). Here the table layout must change, so this is a DDL job.
The DDL command that edits an existing table is ALTER TABLE.

Step 2: Pick the right clause:
ALTER TABLE has several clauses. ADD puts in a new column, DROP COLUMN removes one, and MODIFY changes the type of a column.
We need a new column, so we use ADD.

Step 3: Build the command part by part:
Table name: W_STOCK.
New column name: E_Date.
Data type: DATE.
ALTER TABLE W_STOCK
ADD E_Date DATE;

The semicolon ends the statement. The old rows keep their data and show NULL in the new column.

Final Answer:
ALTER TABLE W_STOCK ADD E_Date DATE;
Was this answer helpful?
0
Question: 4

Assuming that the table W_STOCK is already created, write an SQL command to remove the column B_Qty from the table.

Show Hint

Use ALTER TABLE with the DROP COLUMN clause and the name of the column.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Separate columns from rows:
DELETE FROM removes rows of a table. DROP TABLE removes the whole table. Neither of them removes only one column.
A single column is removed with ALTER TABLE.

Step 2: Choose the clause:
Inside ALTER TABLE, the clause ADD puts in a column and the clause DROP COLUMN takes one out. We need DROP COLUMN.

Step 3: Build the command part by part:
Table name: W_STOCK.
Column to remove: B_Qty.
ALTER TABLE W_STOCK
DROP COLUMN B_Qty;

The statement ends with a semicolon. The other columns W_Code, W_Description and U_Price stay as they are.

Final Answer:
ALTER TABLE W_STOCK DROP COLUMN B_Qty;
Was this answer helpful?
0