SQL Database Constraint Rules Quiz

SQL Database
0 Passed
0% acceptance

A 50-question quiz covering NOT NULL, UNIQUE, CHECK, DEFAULT, primary keys, foreign keys, and constraint logic.

50 Questions
~100 minutes
1

Question 1

A signup page requires every user to provide an email. The database column storing emails must never allow empty or missing entries. Which constraint ensures this rule?

A
NOT NULL
B
UNIQUE
C
CHECK
D
DEFAULT
2

Question 2

A company ensures that no two employees share the same work email. Which constraint enforces this uniqueness across all rows?

A
UNIQUE
B
CHECK
C
DEFAULT
D
FOREIGN KEY
3

Question 3

A salary column must always hold a positive number. Which constraint ensures only valid values are stored?

A
CHECK (salary > 0)
B
NOT NULL
C
DEFAULT
D
UNIQUE
4

Question 4

A new row should automatically receive today’s date when no creation date is provided. Which constraint supplies this automatic value?

A
DEFAULT
B
CHECK
C
UNIQUE
D
NOT NULL
5

Question 5

A product table uses an ID column that uniquely identifies each product. It may not be NULL or duplicated. Which concept describes this requirement?

A
Primary key
B
Unique index only
C
CHECK constraint only
D
DEFAULT constraint
6

Question 6

A foreign key prevents an order from referencing a non-existing customer. What concept describes this protection?

A
Referential integrity
B
Computed integrity
C
Numeric constraint
D
Derived constraint
7

Question 7

A system demands that age values be at least 18 for certain accounts. What type of rule enforces this?

A
CHECK (age >= 18)
B
UNIQUE (age)
C
DEFAULT 0
D
NOT NULL only
8

Question 8

A table definition requires every product code to be unique so that scanning devices identify items correctly. What constraint supports this?

A
UNIQUE
B
CHECK
C
DEFAULT
D
NOT NULL only
9

Question 9

A system wants to prevent negative stock values. Which constraint is appropriate?

A
CHECK (stock >= 0)
B
DEFAULT 0
C
UNIQUE
D
FOREIGN KEY
10

Question 10

A reminders app must always store a timestamp for each reminder. Which constraint ensures the timestamp cannot be omitted?

A
NOT NULL
B
CHECK
C
UNIQUE
D
DEFAULT
11

Question 11

A price value should always be non-negative and not null. Which combination correctly applies both rules?

A
NOT NULL + CHECK (price >= 0)
B
DEFAULT + UNIQUE
C
UNIQUE + FOREIGN KEY
D
CHECK (price >= 0) only
12

Question 12

A DEFAULT value for a status field sets new entries to 'pending' unless overridden. What advantage does this bring?

A
Reduces repetitive manual input
B
Enforces uniqueness
C
Forces numeric values only
D
Creates automatic indexes
13

Question 13

A foreign key constraint is added to ensure product reviews always refer to a valid product. This prevents what?

A
Orphan review entries that point to missing products
B
Duplicate product names
C
Negative review counts
D
Invalid text input
14

Question 14

A UNIQUE constraint on a username field ensures what important behavior?

A
No two users can share the same username
B
Usernames cannot be empty
C
The username is numeric
D
The username becomes a primary key automatically
15

Question 15

A CHECK constraint validates that a temperature reading stays within realistic limits. What role does this play?

A
It maintains meaningful domain restrictions
B
It prevents missing values
C
It enforces referential links
D
It forces automatic rounding
16

Question 16

A NOT NULL column is added to an existing table. What challenge can arise when applying this constraint to existing data?

A
Existing NULL values violate the new rule
B
Indexes become invalid
C
All rows get deleted
D
Data types must change entirely
17

Question 17

CHECK constraints help enforce rules like 'quantity must be positive.' What kind of data issue does this prevent?

A
Invalid logical states such as negative quantities
B
Duplicate identifiers
C
Inconsistent relationships
D
Automatic null insertion
18

Question 18

A user table includes a UNIQUE constraint on phone numbers. What does this allow the system to do reliably?

A
Identify users by phone number without ambiguity
B
Check numeric comparisons only
C
Force phone numbers to be optional
D
Auto-generate area codes
19

Question 19

A DEFAULT of true is added to an 'is_active' flag so newly created accounts begin active. What advantage does this provide?

A
Ensures consistent initial states
B
Enforces referential integrity
C
Prevents duplicate values
D
Restricts values to non-negative numbers
20

Question 20

A uniqueness rule on (first_name, last_name, birthday) is applied to ID verification. What type of key does this combination form?

A
Composite unique key
B
Foreign key
C
Default key
D
Check key
21

Question 21

If a column has a DEFAULT value but also allows NULL, what happens when a user explicitly inserts NULL?

A
NULL is stored, overriding the default
B
The default is applied instead
C
A UNIQUE error occurs
D
The row becomes invalid
22

Question 22

A database uses a CHECK constraint requiring delivery dates to be on or after order dates. What does this prevent?

A
Logically impossible timelines
B
Duplicate orders
C
Missing customer IDs
D
Invalid usernames
23

Question 23

A NOT NULL column paired with DEFAULT ensures what behavior for new rows?

A
A value is always present even without explicit input
B
Only NULL values are allowed
C
New rows must be entered manually
D
Foreign keys become optional
24

Question 24

A UNIQUE constraint still allows NULL for a nullable column. Why?

A
NULL is not treated as a duplicate of another NULL
B
NULL is auto-replaced by default
C
UNIQUE forces one NULL only
D
UNIQUE forces NOT NULL
25

Question 25

A CHECK constraint on a role-level column ensures only allowed values such as 'admin', 'viewer', or 'editor' appear. What does this enforce?

A
Domain restrictions on accepted values
B
Uniqueness of roles
C
Foreign references
D
Primary key range
26

Question 26

This definition enforces that names cannot be empty. Which rule applies?

sql
CREATE TABLE people (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL
);
A
name must be provided for every row
B
name must be unique
C
name becomes a foreign key
D
name must be numeric
27

Question 27

This rule ensures no two users share the same email. What does it enforce?

sql
CREATE TABLE users (
  email TEXT UNIQUE
);
A
No duplicate emails allowed
B
Emails must be numeric
C
Emails become foreign keys
D
Emails default to NULL
28

Question 28

This constraint enforces that an item's price cannot be zero. What rule is applied?

sql
CREATE TABLE items (
  price DECIMAL CHECK (price > 0)
);
A
Only positive prices are allowed
B
Prices default to zero
C
Prices become unique
D
Price is always NULL
29

Question 29

This column receives a fallback value. What effect does this DEFAULT create?

sql
CREATE TABLE tasks (
  status TEXT DEFAULT 'pending'
);
A
New rows get 'pending' when no status is supplied
B
Status becomes unique
C
Status cannot be changed
D
Status auto-references another table
30

Question 30

This definition adds a NOT NULL rule to a birthdate column. What must inserts do?

sql
CREATE TABLE employees (
  birthdate DATE NOT NULL
);
A
Always supply a birthdate
B
Supply only NULL birthdates
C
Skip the column
D
Use a UNIQUE birthdate
31

Question 31

This constraint prevents invalid rating values. What does the CHECK enforce?

sql
CREATE TABLE ratings (
  score INT CHECK (score BETWEEN 1 AND 5)
);
A
Ratings must be between 1 and 5
B
Ratings must be unique
C
Ratings must be null
D
Ratings must be text
32

Question 32

This column enforces uniqueness on user names. What happens if a duplicate is inserted?

sql
CREATE TABLE accounts (
  username TEXT UNIQUE
);
A
The insert fails due to a UNIQUE violation
B
The username becomes NULL
C
The row gets duplicated
D
It inserts but triggers are disabled
33

Question 33

This rule ensures stock values cannot become negative. What logic is used?

sql
CREATE TABLE inventory (
  stock INT CHECK (stock >= 0)
);
A
stock must be zero or positive
B
stock becomes unique
C
stock cannot be updated
D
stock is always null
34

Question 34

This DEFAULT assigns a current timestamp to new rows. What outcome results?

sql
CREATE TABLE logs (
  created_at TIMESTAMP DEFAULT NOW()
);
A
created_at is auto-filled unless provided
B
created_at must be NULL
C
created_at must be unique
D
created_at must reference another table
35

Question 35

This line defines a foreign key. What requirement does it enforce?

sql
project_id INT REFERENCES projects(id)
A
project_id must match an existing project
B
project_id must be unique
C
project_id becomes a default
D
project_id must be 0 always
36

Question 36

This check ensures email addresses contain an '@'. What logic ensures this?

sql
CHECK (email LIKE '%@%')
A
email must include '@'
B
email becomes unique
C
email defaults to admin
D
email is numeric only
37

Question 37

This constraint requires start_date to be before end_date. What effect does it produce?

sql
CHECK (start_date < end_date)
A
Prevents reversed date ranges
B
Makes both dates unique
C
Nullifies both values
D
Creates automatic ordering
38

Question 38

This rule ensures that two values together must remain unique. What constraint is used?

sql
UNIQUE (student_id, course_id)
A
Prevents duplicate student–course pairs
B
Limits both IDs to NULL
C
Creates cascading deletes
D
Allows duplicates freely
39

Question 39

This rule sets a default quantity. What happens on new inserts?

sql
quantity INT DEFAULT 1
A
New rows use quantity = 1 unless overridden
B
Quantity must be NULL
C
Quantity must be unique
D
Quantity must reference another table
40

Question 40

This definition blocks deleting referenced customers. What behavior occurs?

sql
customer_id INT REFERENCES customers(id) ON DELETE RESTRICT
A
Customers with orders cannot be deleted
B
Customers are deleted automatically
C
customer_id defaults to NULL
D
Orders delete all customers
41

Question 41

This command inserts a NULL into a NOT NULL field. What occurs?

sql
INSERT INTO people (name) VALUES (NULL);
A
The insert fails due to NOT NULL violation
B
The name defaults to blank
C
The row inserts normally
D
The name becomes unique
42

Question 42

This CHECK rule ensures numeric values stay below a limit. What does it guarantee?

sql
CHECK (score <= 100)
A
Scores cannot exceed 100
B
Scores must be unique
C
Scores become NULL
D
Scores reference another table
43

Question 43

This DEFAULT uses NOW(). What behavior does it introduce?

sql
created_at TIMESTAMP DEFAULT NOW()
A
Automatically fills current timestamp
B
Forces null timestamps
C
Makes timestamps unique
D
Requires manual entry
44

Question 44

This constraint ties a contact to a valid user. What rule does it define?

sql
user_id INT REFERENCES users(id)
A
Ensures user_id matches a valid user
B
Makes user_id unique
C
Gives user_id a default value
D
Prevents creating users
45

Question 45

This CHECK ensures discount percentages stay within 0–50. What rule is applied?

sql
CHECK (discount >= 0 AND discount <= 50)
A
Discounts must be between 0 and 50
B
Discounts must be unique
C
Discounts are always null
D
Discounts auto-cascade
46

Question 46

This rule stops duplicate category names. What happens if two inserts attempt the same name?

sql
category_name TEXT UNIQUE
A
The second insert fails due to UNIQUE violation
B
Both names are accepted normally
C
Names become automatically trimmed
D
Names are merged together
47

Question 47

This rule blocks invalid email formats. What does the CHECK accomplish?

sql
CHECK (email LIKE '%@%')
A
Requires '@' in email patterns
B
Makes emails unique
C
Defaults email to blank
D
Sets email to null if wrong
48

Question 48

This default assigns the value 'draft' to new posts. What result does this have?

sql
status TEXT DEFAULT 'draft'
A
Posts begin with status 'draft' unless overridden
B
Posts become unique
C
Posts require no data
D
Posts reference another table automatically
49

Question 49

This CHECK ensures ages are realistic. What is being enforced?

sql
CHECK (age BETWEEN 0 AND 120)
A
Ages must remain within realistic bounds
B
Ages become unique
C
Ages always default to NULL
D
Ages reference another table
50

Question 50

This rule allows a NULL foreign key when a project is removed. Which behavior applies?

sql
project_id INT REFERENCES projects(id) ON DELETE SET NULL
A
project_id becomes NULL if parent is deleted
B
All child rows delete automatically
C
Nulls are disallowed
D
project_id becomes unique

QUIZZES IN SQL Database