Bird
Raised Fist0
MySQLquery~10 mins

Column definitions and constraints in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Column definitions and constraints
Start CREATE TABLE
↓
Define Column Name
↓
Specify Data Type
↓
Add Constraints?
No→Next Column or End
Yes↓
Add Constraints (e.g., NOT NULL, UNIQUE, PRIMARY KEY)
↓
Repeat for all columns
↓
End CREATE TABLE
This flow shows how a table is created by defining each column's name, data type, and optional constraints step-by-step.
Execution Sample
MySQL
CREATE TABLE Users (
  id INT PRIMARY KEY,
  username VARCHAR(50) NOT NULL UNIQUE,
  email VARCHAR(100) NOT NULL,
  age INT
);
Creates a Users table with columns having data types and constraints like PRIMARY KEY, NOT NULL, and UNIQUE.
Execution Table
StepActionColumn NameData TypeConstraintsResulting Column Definition
1Define columnidINTPRIMARY KEYid INT PRIMARY KEY
2Define columnusernameVARCHAR(50)NOT NULL, UNIQUEusername VARCHAR(50) NOT NULL UNIQUE
3Define columnemailVARCHAR(100)NOT NULLemail VARCHAR(100) NOT NULL
4Define columnageINTNoneage INT
5End CREATE TABLE---Table Users created with 4 columns
💡 All columns defined with their data types and constraints; table creation completes.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Columnsemptyid INT PRIMARY KEYid INT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUEid INT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, email VARCHAR(100) NOT NULLid INT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, email VARCHAR(100) NOT NULL, age INTFinal table columns defined
Key Moments - 3 Insights
Why do we add constraints like NOT NULL or UNIQUE to columns?
Constraints ensure data rules are followed. For example, NOT NULL means the column must have a value. UNIQUE means no two rows can have the same value in that column. See execution_table rows 2 and 3 where these constraints are added.
Can a column have multiple constraints?
Yes, a column can have more than one constraint. For example, username has both NOT NULL and UNIQUE constraints as shown in execution_table row 2.
What happens if we don't specify constraints?
If no constraints are specified, the column can accept NULL values and duplicate data. See execution_table row 4 for the age column with no constraints.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what constraints does the 'username' column have at step 2?
APRIMARY KEY
BNOT NULL only
CNOT NULL and UNIQUE
DNo constraints
💡 Hint
Check the Constraints column in execution_table row 2.
At which step is the 'email' column defined with its constraints?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Column Name and Step columns in execution_table.
If we remove the UNIQUE constraint from 'username', how would the variable_tracker change after step 2?
Ausername VARCHAR(50) NOT NULL only
Busername VARCHAR(50) NOT NULL UNIQUE remains the same
Cusername VARCHAR(50) only
DNo change in variable_tracker
💡 Hint
Constraints affect the column definition string in variable_tracker after step 2.
Concept Snapshot
CREATE TABLE syntax defines columns with name, data type, and optional constraints.
Constraints include NOT NULL, UNIQUE, PRIMARY KEY, etc.
Constraints enforce rules on data input.
Columns without constraints accept NULL and duplicates.
Multiple constraints can be combined on one column.
Full Transcript
This lesson shows how to define columns and constraints when creating a table in MySQL. Each column needs a name and a data type like INT or VARCHAR. Constraints such as PRIMARY KEY, NOT NULL, and UNIQUE can be added to control the data allowed in that column. The execution table traces each step of defining columns and adding constraints. The variable tracker shows how the table's column definitions build up step-by-step. Key moments clarify why constraints matter and how multiple constraints can be combined. The quiz tests understanding by asking about specific steps and effects of constraints. The snapshot summarizes the syntax and purpose of column definitions and constraints.

Practice

(1/5)
1. Which constraint ensures that a column cannot have empty (NULL) values in MySQL?
easy
A. NOT NULL
B. UNIQUE
C. PRIMARY KEY
D. DEFAULT

Solution

  1. Step 1: Understand the purpose of NOT NULL

    The NOT NULL constraint prevents a column from having NULL values, meaning it must always have data.
  2. Step 2: Compare with other constraints

    UNIQUE allows NULLs but no duplicates, PRIMARY KEY implies NOT NULL and uniqueness, and DEFAULT sets a default value but does not prevent NULL.
  3. Final Answer:

    NOT NULL -> Option A
  4. Quick Check:

    Prevent empty values = NOT NULL [OK]
Hint: NOT NULL means no empty values allowed [OK]
Common Mistakes:
  • Confusing UNIQUE with NOT NULL
  • Thinking DEFAULT prevents NULL
  • Assuming PRIMARY KEY is only for uniqueness
2. Which of the following is the correct way to define a column named id as an auto-incrementing primary key in MySQL?
easy
A. id AUTO_INCREMENT INT PRIMARY KEY
B. id INT AUTO_INCREMENT PRIMARY
C. id INT PRIMARY KEY AUTO_INCREMENT
D. id PRIMARY KEY INT AUTO_INCREMENT

Solution

  1. Step 1: Recall correct column definition order

    In MySQL, the correct syntax places the data type first, then constraints like PRIMARY KEY and AUTO_INCREMENT.
  2. Step 2: Validate each option's syntax

    id INT PRIMARY KEY AUTO_INCREMENT follows the correct order: id INT PRIMARY KEY AUTO_INCREMENT. Others have incorrect order or missing keywords.
  3. Final Answer:

    id INT PRIMARY KEY AUTO_INCREMENT -> Option C
  4. Quick Check:

    Data type first, then constraints [OK]
Hint: Data type first, then PRIMARY KEY and AUTO_INCREMENT [OK]
Common Mistakes:
  • Placing AUTO_INCREMENT before data type
  • Using PRIMARY instead of PRIMARY KEY
  • Mixing order of keywords
3. Given the table definition:
CREATE TABLE users (
  user_id INT PRIMARY KEY,
  email VARCHAR(100) UNIQUE NOT NULL,
  age INT DEFAULT 18
);

What will happen if you try to insert:
INSERT INTO users (user_id, email) VALUES (1, NULL);
medium
A. Insert fails due to NOT NULL constraint on email
B. Insert succeeds with email set to NULL
C. Insert succeeds with email set to default value
D. Insert fails due to UNIQUE constraint violation

Solution

  1. Step 1: Check the email column constraints

    Email is defined as UNIQUE NOT NULL, so it cannot be NULL.
  2. Step 2: Analyze the insert statement

    The insert tries to set email to NULL, violating the NOT NULL constraint, causing an error.
  3. Final Answer:

    Insert fails due to NOT NULL constraint on email -> Option A
  4. Quick Check:

    NOT NULL rejects NULL values [OK]
Hint: NOT NULL columns reject NULL inserts [OK]
Common Mistakes:
  • Assuming DEFAULT applies when value is NULL
  • Confusing UNIQUE with NOT NULL
  • Thinking NULL is allowed if UNIQUE
4. Consider this table creation statement:
CREATE TABLE products (
  product_id INT PRIMARY KEY,
  name VARCHAR(50) UNIQUE,
  price DECIMAL(5,2) NOT NULL DEFAULT 0.00
);

Which of the following statements will cause an error?
medium
A. INSERT INTO products (product_id, name, price) VALUES (1, 'Pen', 1.50);
B. INSERT INTO products (product_id, name) VALUES (2, 'Book');
C. INSERT INTO products (product_id, price) VALUES (3, 2.00);
D. INSERT INTO products (product_id, name, price) VALUES (1, 'Pencil', 0.50);

Solution

  1. Step 1: Check constraints on each insert

    INSERT INTO products (product_id, name, price) VALUES (1, 'Pen', 1.50); inserts all required fields correctly. INSERT INTO products (product_id, name) VALUES (2, 'Book'); omits price but uses default 0.00, allowed. INSERT INTO products (product_id, price) VALUES (3, 2.00); omits name, which is nullable, allowed.
  2. Step 2: Identify duplicate primary key error

    INSERT INTO products (product_id, name, price) VALUES (1, 'Pencil', 0.50); tries to insert product_id 1 again, violating the PRIMARY KEY uniqueness constraint, causing an error.
  3. Final Answer:

    INSERT INTO products (product_id, name, price) VALUES (1, 'Pencil', 0.50); -> Option D
  4. Quick Check:

    PRIMARY KEY must be unique [OK]
Hint: Duplicate PRIMARY KEY values cause errors [OK]
Common Mistakes:
  • Assuming UNIQUE allows duplicates
  • Forgetting DEFAULT applies only if column omitted
  • Ignoring PRIMARY KEY uniqueness
5. You want to create a table orders where order_id is a primary key, customer_email must be unique and not null, and order_date defaults to the current date if not provided. Which of the following column definitions is correct?
hard
A. order_id INT PRIMARY KEY, customer_email VARCHAR(100) NOT NULL UNIQUE, order_date DATE DEFAULT NOW()
B. order_id INT PRIMARY KEY, customer_email VARCHAR(100) UNIQUE NOT NULL, order_date DATE DEFAULT CURRENT_DATE
C. order_id INT PRIMARY KEY, customer_email VARCHAR(100) UNIQUE, order_date DATE DEFAULT CURRENT_TIMESTAMP
D. order_id INT PRIMARY KEY, customer_email VARCHAR(100) NOT NULL UNIQUE, order_date DATE DEFAULT CURDATE()

Solution

  1. Step 1: Check constraints on customer_email

    The column must be both UNIQUE and NOT NULL. Using either UNIQUE NOT NULL or NOT NULL UNIQUE is valid in MySQL.
  2. Step 2: Verify default for order_date

    MySQL supports DEFAULT CURRENT_DATE for DATE columns. NOW() and CURRENT_TIMESTAMP return datetime, not date, causing errors. CURDATE() is a function and cannot be used directly as default.
  3. Step 3: Confirm full correctness

    order_id INT PRIMARY KEY, customer_email VARCHAR(100) UNIQUE NOT NULL, order_date DATE DEFAULT CURRENT_DATE uses correct syntax for all constraints and default value.
  4. Final Answer:

    order_id INT PRIMARY KEY, customer_email VARCHAR(100) UNIQUE NOT NULL, order_date DATE DEFAULT CURRENT_DATE -> Option B
  5. Quick Check:

    Use CURRENT_DATE for DATE default [OK]
Hint: Use CURRENT_DATE for DATE default, UNIQUE NOT NULL order matters [OK]
Common Mistakes:
  • Using NOW() or CURRENT_TIMESTAMP for DATE default
  • Trying to use functions like CURDATE() as default
  • Confusing UNIQUE and NOT NULL order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard