All Exams Test series for 1 year @ ₹349 only
Question

Given table 'StudAtt' with structure as Rno, Attdate, Attendance.
Identify the suitable command to add a primary key to the table after table creation.
Note: In the given case, we want to make both Rno and Attdate columns as primary key.

The correct answer is
ALTER TABLE StudAtt ADD PRIMARY KEY(Rno, Attdate);

SQL Command to Add Composite Primary Key to StudAtt Table

The task is to identify the correct SQL command for adding a primary key to an existing table named $StudAtt$. Specifically, the primary key needs to be formed by combining two columns: $Rno$ and $Attdate$. This type of primary key, which consists of multiple columns, is called a composite primary key.

Understanding SQL `ALTER TABLE` Statement

In SQL, the $ALTER TABLE$ statement is used to modify the structure of an existing database table. This includes adding, dropping, or modifying columns, as well as adding or dropping constraints like primary keys, foreign keys, and unique constraints.

To add a primary key constraint to an existing table, the basic syntax is:

$ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ...);$

Note: It's often possible to omit the constraint name, and the database system will assign a default one. Also, multiple columns can be specified within the parentheses to create a composite primary key.

Evaluating the Provided Options

Let's analyze each option to find the suitable command:

  • Option 1: $ALTER TABLE StudAtt ADD PRIMARY KEY(Rno, Attdate);$

    This statement uses $ALTER TABLE$ to target the $StudAtt$ table. It correctly uses the $ADD PRIMARY KEY$ clause and specifies both $Rno$ and $Attdate$ within the parentheses. This syntax is appropriate for defining a composite primary key on the existing table.

  • Option 2: $CREATE TABLE StudAtt ADD PRIMARY KEY(Rno);$

    This option is incorrect. The $CREATE TABLE$ command is used to create a new table, not modify an existing one. Additionally, it only specifies $Rno$ as the primary key, not the required composite key involving $Attdate$.

  • Option 3: $ALTER TABLE StudAtt ADD PRIMARY KEY;$

    This command is syntactically incomplete. While it correctly identifies the table using $ALTER TABLE$, it fails to specify which column or columns should be designated as the primary key. The database needs to know the column(s) involved.

  • Option 4: $ALTER TABLE StudAtt ADD PRIMARY KEY(Rno) AND PRIMARY KEY(Attdate);$

    This is incorrect SQL syntax. A table can only have one primary key. This command attempts to define two separate primary keys, which is not permissible. The correct way to define a composite key is by listing all participating columns within a single $PRIMARY KEY$ constraint definition.

  • Option 5: No SQL command provided.

    This option is invalid as it does not present a SQL command.

Final Determination

Option 1 provides the correct SQL syntax for modifying the $StudAtt$ table to include a composite primary key composed of the $Rno$ and $Attdate$ columns.

Was this answer helpful?

Important Questions from Miscellaneous

  1. A stone is thrown horizontally from the top of a 20 m high building with a speed of 12 m/s. It hits the ground at a distance R from the building. Taking g = 10 m/s2 and neglecting air resistance will give :

  2. A sphere of volume V is made of a material with lower density than water. While on Earth, it floats on water with its volume f1V (f1 < 1) submerged. On the other hand, on a spaceship accelerating with acceleration a < g (g is the acceleration due to gravity on Earth) in outer space, its submerged volume in water is f2V. Then:

  3. A railway wagon (open at the top) of mass M1 is moving with speed v1 along a straight track. As a result of rain, after some time it gets partially filled with water so that the mass of the wagon becomes M2 and speed becomes v2. Taking the rain to be falling vertically and the water stationery inside the wagon, the relation between the two speeds v1 and v2 is :

  4. Consider the following statements:

    1. Distance between the longitudes becomes zero on North Pole and South Pole.

    2. Distance between the longitudes is maximum on the Equator.

    3. Number of longitudes is more than number of latitudes.

    Which of the statements given above is/are correct?

  5. One block of 2⋅0 kg mass is placed on top of another block of 3⋅0 kg mass. The coefficient of static friction between the two blocks is 0⋅2. The bottom block is pulled with a horizontal force F such that both the blocks move together without slipping. Taking acceleration due to gravity as 10 m/s2, the maximum value of the frictional force is :

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App