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
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: List the columns and their types:
W_Code: CHAR(5), and it is the primary key.
W_Description: VARCHAR(20).
B_Qty: INTEGER.
U_Price: FLOAT.

Step 2: Choose the command:
A table is made with the DDL command CREATE TABLE.
The primary key is set by writing PRIMARY KEY after the column type.

Step 3: Write the command:
CREATE TABLE W_STOCK (
    W_Code CHAR(5) PRIMARY KEY,
    W_Description VARCHAR(20),
    B_Qty INTEGER,
    U_Price FLOAT
);
With PRIMARY KEY on W_Code, no two rows can have the same item code, and W_Code cannot be NULL.

Final Answer:
CREATE TABLE W_STOCK (
    W_Code CHAR(5) PRIMARY KEY,
    W_Description VARCHAR(20),
    B_Qty INTEGER,
    U_Price FLOAT
);
Was this answer helpful?
0
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
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Recall what a primary key is:
A primary key is a column (or a set of columns) that identifies every row of a table in a unique way.
It follows two rules. Its values must never repeat, and they must never be NULL.

Step 2: Look at the column U_Price:
U_Price stores the unit price of an item, and its type is FLOAT.
Two different items can easily have the same unit price. For example, two different bottle sizes may both cost 20.0.
A price can also be left empty (NULL) when a new item is added before its price is fixed.
So the values of U_Price are not guaranteed to be unique or non-null.

Step 3: Compare with the other columns:
W_Code is the code of the item. Every item has its own code, so it never repeats. This is the column that fits the rules of a primary key.
W_Description and B_Qty can repeat as well, so they are also poor choices.

Final Answer:
No, U_Price cannot be the primary key, because two items can have the same price and a price can be NULL. W_Code should be the primary key.
Was this answer helpful?
0
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
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Find the type of command needed:
The table already exists and we only want to change its structure by adding a column.
Changing the structure of a table is done with the DDL command ALTER TABLE.

Step 2: Recall the syntax:
To add a column, we write the table name after ALTER TABLE and then use the clause ADD with the column name and its data type.
ALTER TABLE table_name ADD column_name datatype;


Step 3: Put in the names from the question:
The table name is W_STOCK. The new column is E_Date and its type is DATE.
ALTER TABLE W_STOCK ADD E_Date DATE;

After this command, every existing row gets a new E_Date value, which is NULL until we update it.

Final Answer:
The required command is ALTER TABLE W_STOCK ADD E_Date DATE;
Was this answer helpful?
0
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
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Find the type of command needed:
We must remove a whole column from an existing table. This changes the structure of the table, so we use the DDL command ALTER TABLE.
DELETE is not suitable, because DELETE removes rows, not columns.

Step 2: Recall the syntax:
To remove a column, we use the DROP COLUMN clause with the name of the column.
ALTER TABLE table_name DROP COLUMN column_name;


Step 3: Put in the names from the question:
The table is W_STOCK and the column to remove is B_Qty.
ALTER TABLE W_STOCK DROP COLUMN B_Qty;

After this command, the column B_Qty and all the values stored in it are gone from the table.

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