SQL Default Constraint Tutorial
Example Table
We will create a table named employees
to demonstrate the SQL default constraint:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
hire_date DATE DEFAULT CURRENT_DATE,
salary DECIMAL(10, 2) DEFAULT 50000.00
);
Creating the Table
To create the table, use the following SQL command:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
hire_date DATE DEFAULT CURRENT_DATE,
salary DECIMAL(10, 2) DEFAULT 50000.00
);
Result:
Command | CREATE TABLE employees (employee_id INT PRIMARY KEY, employee_name VARCHAR(100), hire_date DATE DEFAULT CURRENT_DATE, salary DECIMAL(10, 2) DEFAULT 50000.00); |
---|---|
Result |
|
Inserting Data into the Table
Let's insert some data into the employees
table:
INSERT INTO employees (employee_id, employee_name) VALUES
(1, 'John Doe'),
(2, 'Jane Smith'),
(3, 'Alice Johnson');
Result:
Command | INSERT INTO employees (employee_id, employee_name) VALUES (1, 'John Doe'), (2, 'Jane Smith'), (3, 'Alice Johnson'); |
---|---|
Result |
|
Querying the Table
We can query the employees
table to see the data:
SELECT * FROM employees;
Result:
Command | SELECT * FROM employees; | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Result |
|