Introduction
NOT NULL makes sure a column always has a value. DEFAULT sets a value automatically if none is given.
CREATE TABLE table_name ( column_name datatype NOT NULL DEFAULT default_value );
CREATE TABLE users ( id INT NOT NULL, name VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL );
CREATE TABLE products ( id INT NOT NULL, name VARCHAR(50) NOT NULL, stock INT NOT NULL DEFAULT 0 );
CREATE TABLE accounts ( id INT NOT NULL, username VARCHAR(50) NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'active' );
CREATE TABLE employees ( id INT NOT NULL, name VARCHAR(50) NOT NULL, department VARCHAR(50) NOT NULL DEFAULT 'General' ); INSERT INTO employees (id, name) VALUES (1, 'Alice'); INSERT INTO employees (id, name, department) VALUES (2, 'Bob', 'Sales'); SELECT * FROM employees;
NOT NULL constraint do in a MySQL table column?age of type INT in MySQL?employees with column status VARCHAR(10) NOT NULL DEFAULT 'active', what will be the value of status after this insert?INSERT INTO employees (name) VALUES ('John');CREATE TABLE products (id INT NOT NULL, price DECIMAL(5,2) DEFAULT 0.00);
price column cannot have a default value. is wrong because price can have a default. The id column allows NULL values. is wrong because id is NOT NULL. There is no PRIMARY KEY defined, so the table is invalid. is not mandatory for table creation.orders where the status column must never be empty and should default to 'pending'. Which of these statements correctly creates this table?Pass the test and this pattern moves unknown