MCQs on Data Types and Constraints | PostgreSQL Database

This comprehensive set of 30 MCQs focuses on PostgreSQL’s data types and constraints, covering common data types, user-defined types, NULL/NOT NULL handling, and essential constraints like primary keys, foreign keys, and more. Perfect for exams and practical learning.


Common Data Types (Text, Numeric, Date/Time, etc.)

  1. Which of the following is a valid data type in PostgreSQL for storing textual data?
    • A) VARCHAR
    • B) TEXT
    • C) CHAR
    • D) All of the above
  2. What is the difference between CHAR and VARCHAR in PostgreSQL?
    • A) CHAR is variable-length, VARCHAR is fixed-length
    • B) CHAR is fixed-length, VARCHAR is variable-length
    • C) Both are fixed-length
    • D) Both are variable-length
  3. Which data type is used for storing whole numbers in PostgreSQL?
    • A) FLOAT
    • B) DECIMAL
    • C) INTEGER
    • D) CHAR
  4. Which data type in PostgreSQL is best suited for storing monetary values?
    • A) NUMERIC
    • B) MONEY
    • C) FLOAT
    • D) DOUBLE PRECISION
  5. Which of the following is a valid Date/Time data type in PostgreSQL?
    • A) DATE
    • B) TIME
    • C) TIMESTAMP
    • D) All of the above
  6. What does the BOOLEAN data type in PostgreSQL store?
    • A) True or False
    • B) Yes or No
    • C) 0 or 1
    • D) All of the above

User-Defined Data Types

  1. How can you create a user-defined data type in PostgreSQL?
    • A) Using CREATE TYPE statement
    • B) Using CREATE TABLE statement
    • C) Using ALTER TABLE statement
    • D) Using CREATE DOMAIN statement
  2. Which of the following is a key advantage of user-defined data types?
    • A) Increased storage efficiency
    • B) Custom validation rules
    • C) Built-in constraints
    • D) Automatic indexing
  3. What are enumerated (ENUM) data types in PostgreSQL?
    • A) Data types for storing numerical ranges
    • B) Data types that map to arrays
    • C) Data types for storing predefined constant values
    • D) Data types for storing JSON objects
  4. Which command is used to create an ENUM data type in PostgreSQL?
  • A) CREATE DOMAIN
  • B) CREATE ENUM
  • C) CREATE TYPE
  • D) CREATE TABLE
  1. Can user-defined data types in PostgreSQL be altered after creation?
  • A) Yes, using the ALTER TYPE command
  • B) Yes, using the ALTER TABLE command
  • C) No, they cannot be changed
  • D) Only by dropping and recreating
  1. Which of the following is NOT a user-defined data type in PostgreSQL?
  • A) ENUM
  • B) DOMAIN
  • C) CUSTOM TABLE
  • D) COMPOSITE

NULL and NOT NULL

  1. What does a NULL value in PostgreSQL signify?
  • A) 0
  • B) Empty string
  • C) Undefined or missing value
  • D) False
  1. Which constraint ensures that a column cannot store NULL values?
  • A) UNIQUE
  • B) PRIMARY KEY
  • C) NOT NULL
  • D) CHECK
  1. If a column is defined as NOT NULL, what happens when you try to insert a NULL value?
  • A) The NULL value is stored
  • B) The row is rejected
  • C) The value is replaced with 0
  • D) The column is left blank
  1. What is the default behavior of a column in PostgreSQL regarding NULL values?
  • A) Allows NULL values
  • B) Disallows NULL values
  • C) Requires a DEFAULT value
  • D) Depends on the data type
  1. Which SQL keyword checks for NULL values in PostgreSQL?
  • A) = NULL
  • B) IS NULL
  • C) NULL CHECK
  • D) IS NOT NULL
  1. How can you prevent NULL values in multiple columns of a table?
  • A) Add a NOT NULL constraint to each column
  • B) Use a CHECK constraint
  • C) Use a UNIQUE constraint
  • D) Use a FOREIGN KEY constraint

Primary Key, Foreign Key, Unique, Check, and Default Constraints

  1. What is the primary purpose of a PRIMARY KEY in PostgreSQL?
  • A) To enforce unique values in a column
  • B) To establish relationships between tables
  • C) To identify rows uniquely in a table
  • D) To provide a default value
  1. Can a table have multiple PRIMARY KEY constraints in PostgreSQL?
  • A) Yes
  • B) No
  • C) Only if combined with FOREIGN KEY
  • D) Depends on the table design
  1. What is the difference between PRIMARY KEY and UNIQUE constraints?
  • A) PRIMARY KEY allows NULL values; UNIQUE does not
  • B) UNIQUE allows NULL values; PRIMARY KEY does not
  • C) Both are identical in PostgreSQL
  • D) UNIQUE applies to rows, PRIMARY KEY to columns
  1. What does a FOREIGN KEY constraint do in PostgreSQL?
  • A) Enforces unique values in a column
  • B) Links two tables based on a reference column
  • C) Prevents NULL values
  • D) Sets a default value for a column
  1. Which of the following constraints is used to enforce a specific range of values?
  • A) PRIMARY KEY
  • B) CHECK
  • C) DEFAULT
  • D) UNIQUE
  1. How does the DEFAULT constraint work in PostgreSQL?
  • A) Ensures the column is not NULL
  • B) Provides a default value if none is specified
  • C) Limits the values in the column to a predefined list
  • D) Enforces relationships between tables
  1. What is the key difference between CHECK and UNIQUE constraints?
  • A) CHECK applies to ranges, UNIQUE applies to duplicates
  • B) UNIQUE applies to ranges, CHECK applies to duplicates
  • C) Both have the same functionality
  • D) CHECK is for relationships, UNIQUE for rows
  1. Which of the following is true about composite PRIMARY KEYs?
  • A) They involve multiple columns
  • B) They apply only to one column
  • C) They are used for user-defined data types
  • D) They cannot be used with FOREIGN KEYs
  1. Can a column have both a DEFAULT and NOT NULL constraint?
  • A) Yes
  • B) No
  • C) Only with PRIMARY KEY
  • D) Only with UNIQUE
  1. What happens if a value violates a CHECK constraint during insertion?
  • A) The value is accepted
  • B) The value is replaced with NULL
  • C) The operation is rejected
  • D) The constraint is ignored
  1. What does the ON DELETE CASCADE option do for a FOREIGN KEY?
  • A) Deletes all rows in the parent table
  • B) Automatically deletes rows in the child table
  • C) Prevents deletion of the referenced row
  • D) Ignores the delete operation
  1. Which statement about UNIQUE constraints is true?
  • A) They allow multiple NULL values
  • B) They allow only one NULL value
  • C) They disallow all NULL values
  • D) They enforce relationships between tables

Answer Table

QnoAnswer
1D) All of the above
2B) CHAR is fixed-length, VARCHAR is variable-length
3C) INTEGER
4B) MONEY
5D) All of the above
6D) All of the above
7A) Using CREATE TYPE statement
8B) Custom validation rules
9C) Data types for storing predefined constant values
10C) CREATE TYPE
11A) Yes, using the ALTER TYPE command
12C) CUSTOM TABLE
13C) Undefined or missing value
14C) NOT NULL
15B) The row is rejected
16A) Allows NULL values
17B) IS NULL
18A) Add a NOT NULL constraint to each column
19C) To identify rows uniquely in a table
20B) No
21B) UNIQUE allows NULL values; PRIMARY KEY does not
22B) Links two tables based on a reference column
23B) CHECK
24B) Provides a default value if none is specified
25A) CHECK applies to ranges, UNIQUE applies to duplicates
26A) They involve multiple columns
27A) Yes
28C) The operation is rejected
29B) Automatically deletes rows in the child table
30A) They allow multiple NULL values

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top