0
0
SQLquery~20 mins

Constraint naming conventions in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.