Bird
Raised Fist0
SQLquery~20 mins

Constraint naming conventions in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Constraint Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Naming Conventions for Primary Key Constraints

Which of the following is the best practice for naming a primary key constraint on a table named Employees?

APrimaryKey1
BEmployees_PK_Constraint
CPK_Employees
DPK1
Attempts:
2 left
💡 Hint

Think about clarity and consistency in naming constraints.

query_result
intermediate
2:00remaining
Identify the Constraint Name from a Query Result

Given the following SQL query to list constraints on the Orders table, what is the name of the foreign key constraint?

SELECT constraint_name, constraint_type FROM information_schema.table_constraints WHERE table_name = 'Orders';

Assume the output is:

constraint_name       | constraint_type
---------------------|----------------
PK_Orders            | PRIMARY KEY
FK_Orders_Customers  | FOREIGN KEY
CHK_OrderAmount      | CHECK
AFK_Orders_Customers
BCHK_OrderAmount
CPK_Orders
DOrders_FK
Attempts:
2 left
💡 Hint

Look for the constraint type FOREIGN KEY.

📝 Syntax
advanced
2:00remaining
Identify the Correct Syntax for Naming a Unique Constraint

Which SQL statement correctly creates a unique constraint named UQ_Products_SKU on the SKU column of the Products table?

AALTER TABLE Products ADD CONSTRAINT UQ_Products_SKU UNIQUE (SKU);
BALTER TABLE Products ADD UNIQUE CONSTRAINT UQ_Products_SKU (SKU);
CALTER TABLE Products ADD CONSTRAINT UNIQUE UQ_Products_SKU (SKU);
DALTER TABLE Products ADD CONSTRAINT UQ_Products_SKU UNIQUE SKU;
Attempts:
2 left
💡 Hint

Remember the order: ADD CONSTRAINT <name> UNIQUE (columns).

optimization
advanced
2:00remaining
Choosing Efficient Constraint Names for Large Schemas

In a database with hundreds of tables, which naming convention for foreign key constraints helps optimize readability and maintenance?

AForeignKeyConstraint
BFK_[RandomNumber]
CFK_[ParentTable]_[ChildTable]
DFK_[ChildTable]_[ParentTable]
Attempts:
2 left
💡 Hint

Think about how names help identify relationships quickly.

🔧 Debug
expert
2:00remaining
Diagnose the Error in Constraint Naming

Consider this SQL statement:

ALTER TABLE Customers ADD CONSTRAINT PK_Customers PRIMARY KEY (CustomerID);

Later, this statement fails:

ALTER TABLE Customers ADD CONSTRAINT PK_Customers PRIMARY KEY (CustomerID);

What is the most likely cause of the error?

APrimary keys cannot be named with the prefix PK_.
BThe constraint name PK_Customers already exists on the Customers table.
CCustomerID column does not exist in Customers table.
DYou cannot add a primary key constraint after table creation.
Attempts:
2 left
💡 Hint

Think about what happens if you try to add a constraint with a duplicate name.

Practice

(1/5)
1. What is the main reason to use clear and consistent names for SQL constraints?
easy
A. To make databases easier to understand and maintain
B. To speed up query execution
C. To reduce storage space
D. To avoid using indexes

Solution

  1. Step 1: Understand the purpose of constraint names

    Constraint names help identify rules applied to database tables.
  2. Step 2: Recognize the benefit of clear naming

    Clear and consistent names make it easier for developers and DBAs to understand and maintain the database structure.
  3. Final Answer:

    To make databases easier to understand and maintain -> Option A
  4. Quick Check:

    Clear naming = easier maintenance [OK]
Hint: Clear names help everyone understand constraints fast [OK]
Common Mistakes:
  • Thinking constraint names affect query speed
  • Confusing constraint names with indexes
  • Ignoring naming conventions
2. Which of the following is the correct way to name a primary key constraint on a table named Employees?
easy
A. PrimaryKey_Employees
B. PK_Employees
C. Employees_PK
D. KeyPrimary_Employees

Solution

  1. Step 1: Identify the common prefix for primary key constraints

    Primary key constraints commonly start with PK_.
  2. Step 2: Combine prefix with table name

    The convention is prefix + underscore + table name, so PK_Employees is correct.
  3. Final Answer:

    PK_Employees -> Option B
  4. Quick Check:

    Primary key prefix = PK_ [OK]
Hint: Use PK_ prefix plus table name for primary keys [OK]
Common Mistakes:
  • Using full words like PrimaryKey_ instead of PK_
  • Placing prefix after table name
  • Mixing words in wrong order
3. Given the following constraint name on a table Orders: FK_Orders_Customers, what type of constraint is this?
medium
A. Foreign key constraint
B. Check constraint
C. Unique constraint
D. Primary key constraint

Solution

  1. Step 1: Analyze the prefix in the constraint name

    The prefix FK_ stands for Foreign Key.
  2. Step 2: Confirm the constraint type

    Since the name is FK_Orders_Customers, it indicates a foreign key from Orders to Customers table.
  3. Final Answer:

    Foreign key constraint -> Option A
  4. Quick Check:

    FK_ prefix = Foreign Key [OK]
Hint: FK_ prefix means foreign key constraint [OK]
Common Mistakes:
  • Confusing FK_ with primary key PK_
  • Assuming FK_ means unique constraint
  • Ignoring prefix meaning
4. You wrote this constraint name for a unique constraint on the Products table: UQProducts. What is the issue with this name?
medium
A. It should include the column name
B. It uses the wrong prefix for unique constraints
C. It is missing an underscore after the prefix
D. It is too long

Solution

  1. Step 1: Identify the correct prefix and format for unique constraints

    Unique constraints use the prefix UQ_ with an underscore.
  2. Step 2: Check the given name format

    The name UQProducts misses the underscore after UQ, so it should be UQ_Products.
  3. Final Answer:

    It is missing an underscore after the prefix -> Option C
  4. Quick Check:

    Unique prefix = UQ_ with underscore [OK]
Hint: Always put underscore after prefix like UQ_ [OK]
Common Mistakes:
  • Skipping underscore after prefix
  • Using wrong prefix like UK_
  • Adding column name unnecessarily
5. You want to name a check constraint on the Employees table that ensures salary is positive. Which of these names follows best practice for constraint naming conventions?
hard
A. SalaryPositiveCheck
B. CheckSalaryPositive
C. EmployeesCKSalary
D. CK_Employees_SalaryPositive

Solution

  1. Step 1: Identify the prefix for check constraints

    Check constraints use the prefix CK_.
  2. Step 2: Combine prefix with table name and descriptive suffix

    Best practice is prefix + table name + description, so CK_Employees_SalaryPositive is clear and consistent.
  3. Final Answer:

    CK_Employees_SalaryPositive -> Option D
  4. Quick Check:

    Check prefix = CK_ plus table name [OK]
Hint: Use CK_ + table name + description for check constraints [OK]
Common Mistakes:
  • Using no prefix or wrong prefix
  • Not including table name
  • Using unclear or inconsistent names