Introduction
Columns store data in tables. Constraints make sure the data is correct and follows rules.
Columns store data in tables. Constraints make sure the data is correct and follows rules.
column_name data_type [constraint1] [constraint2] ...
id that holds integers and is the primary key (unique identifier).id INT PRIMARY KEY
name column that holds text up to 50 characters and cannot be empty.name VARCHAR(50) NOT NULL
email column that holds text and must be unique (no duplicates).email VARCHAR(100) UNIQUE
age column that holds numbers and defaults to 18 if no value is given.age INT DEFAULT 18
This creates a table named Users with four columns. user_id is the unique ID, username and email must be unique and not empty, and age defaults to 18 if not provided.
CREATE TABLE Users ( user_id INT PRIMARY KEY, username VARCHAR(30) NOT NULL UNIQUE, email VARCHAR(50) NOT NULL UNIQUE, age INT DEFAULT 18 );
Always choose the right data type to save space and improve speed.
Use NOT NULL to avoid missing data.
Primary keys uniquely identify each row and help link tables.
Columns define the kind of data stored in a table.
Constraints enforce rules to keep data accurate and consistent.
Common constraints include PRIMARY KEY, NOT NULL, UNIQUE, and DEFAULT.
NOT NULLNOT NULL constraint prevents a column from having NULL values, meaning it must always have data.UNIQUE allows NULLs but no duplicates, PRIMARY KEY implies NOT NULL and uniqueness, and DEFAULT sets a default value but does not prevent NULL.NOT NULL -> Option ANOT NULL [OK]id as an auto-incrementing primary key in MySQL?PRIMARY KEY and AUTO_INCREMENT.id INT PRIMARY KEY AUTO_INCREMENT follows the correct order: id INT PRIMARY KEY AUTO_INCREMENT. Others have incorrect order or missing keywords.id INT PRIMARY KEY AUTO_INCREMENT -> Option CCREATE TABLE users ( user_id INT PRIMARY KEY, email VARCHAR(100) UNIQUE NOT NULL, age INT DEFAULT 18 );
INSERT INTO users (user_id, email) VALUES (1, NULL);
UNIQUE NOT NULL, so it cannot be NULL.NOT NULL constraint, causing an error.CREATE TABLE products ( product_id INT PRIMARY KEY, name VARCHAR(50) UNIQUE, price DECIMAL(5,2) NOT NULL DEFAULT 0.00 );
product_id 1 again, violating the PRIMARY KEY uniqueness constraint, causing an error.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?customer_emailUNIQUE and NOT NULL. Using either UNIQUE NOT NULL or NOT NULL UNIQUE is valid in MySQL.order_dateDEFAULT 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.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.Pass the test and this pattern moves unknown