Introduction
DEFAULT values let you set a starting value for a column when no value is given. This helps keep data complete and consistent.
DEFAULT values let you set a starting value for a column when no value is given. This helps keep data complete and consistent.
CREATE TABLE table_name ( column_name data_type DEFAULT default_value );
CREATE TABLE users ( id INT, name VARCHAR(50), status VARCHAR(10) DEFAULT 'active' );
CREATE TABLE orders ( order_id INT, order_date DATE DEFAULT CURRENT_DATE );
CREATE TABLE products ( product_id INT, quantity INT DEFAULT 0 );
This creates an employees table with a default role 'staff'. Alice gets the default role because none was given. Bob's role is set explicitly.
CREATE TABLE employees ( emp_id INT, emp_name VARCHAR(50), role VARCHAR(20) DEFAULT 'staff' ); INSERT INTO employees (emp_id, emp_name) VALUES (1, 'Alice'); INSERT INTO employees (emp_id, emp_name, role) VALUES (2, 'Bob', 'manager'); SELECT * FROM employees;
DEFAULT values only apply when you omit the column in INSERT statements.
If you insert NULL explicitly, DEFAULT does not apply; NULL is stored.
DEFAULT sets a value automatically when none is provided.
It helps keep data consistent and reduces errors.
Use it for common or safe starting values in columns.
DEFAULT keyword do in a SQL table column definition?score in a table?users with column status VARCHAR(10) DEFAULT 'active', what will be the result of this query?INSERT INTO users (id, name) VALUES (1, 'Alice');
SELECT id, name, status FROM users WHERE id = 1;
status has a default of 'active' and no value was provided, SQL inserts 'active' automatically.CREATE TABLE orders (
id INT PRIMARY KEY,
quantity INT DEFAULT 1,
price DECIMAL(5,2) DEFAULT 0.00
);
products with columns id INT, name VARCHAR(50), and stock INT DEFAULT 0. You want to insert multiple rows but only specify id and name. Which SQL statement correctly inserts two products using DEFAULT for stock?Pass the test and this pattern moves unknown