Consider a table users with a username column that has a UNIQUE constraint. What happens when you try to insert a duplicate username?
CREATE TABLE users (id INT PRIMARY KEY, username VARCHAR(50) UNIQUE); INSERT INTO users VALUES (1, 'alice'); INSERT INTO users VALUES (2, 'alice');
Think about what UNIQUE means for a column.
A UNIQUE constraint prevents duplicate values in the column. Trying to insert a duplicate causes an error.
Why do we use the NOT NULL constraint on a database column?
Think about what NULL means in a database.
NOT NULL means the column must always have a value; it cannot be left empty (NULL).
Which option correctly adds a FOREIGN KEY constraint to the orders table referencing customers(id)?
ALTER TABLE orders ADD CONSTRAINT fk_customer_id ... ;
Check the correct syntax for FOREIGN KEY in SQL.
The correct syntax requires parentheses around the column name and the keyword REFERENCES.
Given a table products with a CHECK constraint price > 0, what happens if you try to insert a product with price = -5?
CREATE TABLE products (id INT PRIMARY KEY, price DECIMAL CHECK (price > 0)); INSERT INTO products VALUES (1, -5);
What does a CHECK constraint do?
CHECK constraints enforce conditions on column values. Violating them causes an error.
Consider this table creation:
CREATE TABLE employees ( id INT, name VARCHAR(100), PRIMARY KEY (name) );
Why might this PRIMARY KEY constraint cause problems?
Think about what PRIMARY KEY means for data uniqueness.
PRIMARY KEY requires unique values. Names may repeat, so using name as PRIMARY KEY can cause duplicate key errors.