In a table in MYSQL database, an attribute M of datatype varchar(15) has the value “Ravi”. The attribute N of datatype char(15) has value “Priya”. How many characters are stored in attribute M and attribute N?
-
4,4
-
4,15
-
15,4
-
15,15
Answer:
Answer by student
b. 4,15
Detailed answer by teachoo
-lock-
Let’s look at each option and see why they are correct or incorrect:
- a. 4,4: This is incorrect because it assumes that both varchar and char use the same amount of space as the actual value. However, char always uses the fixed space allocated for it, even if the value is shorter.
- b. 4,15: This is correct because varchar uses only the space required by the value, which is 4 bytes for “Ravi”, and char uses the fixed space allocated for it, which is 15 bytes for char(15), regardless of the value.
- c. 15,4: This is incorrect because it assumes that varchar uses the fixed space allocated for it, which is not true. Varchar only uses the space required by the value, which can be less than the fixed space.
- d. 15,15: This is incorrect because it assumes that both varchar and char pad extra spaces to fill up the fixed space allocated for them. However, varchar does not pad any extra spaces, unlike char.
The answer is b. 4,15 because:
- The attribute M of datatype varchar(15) can store up to 15 characters, but it only uses the space required by the actual value. Since the value is “Ravi”, it only uses 4 bytes to store it, and does not pad any extra spaces.
- The attribute N of datatype char(15) can store exactly 15 characters, and it always uses the fixed space allocated for it. If the value is shorter than 15 characters, it pads the remaining space with spaces. Since the value is “Priya”, it uses 5 bytes to store it, and adds 10 spaces to fill up the remaining space.
So, the attribute M stores 4 characters and the attribute N stores 15 characters.
So, the correct answer is b. 4,15.
-endlock-