Creation, altering and dropping of tables and inserting rows into a table (use constraints while creating tables) examples using SELECT command.
Create a table named employees
with the following columns:
employee_id
(Primary Key)name
age
department
Here’s how you can achieve this in SQL:
-- Create the employees table with constraints
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age > 24),
department VARCHAR(50) CHECK (department IN ('IT', 'HR', 'Finance', 'Marketing')),
UNIQUE (employee_id)
);
SQL> DESC employees;
Now, let’s alter the table to add a new column for salary
:
-- Alter table to add a new column for salary
ALTER TABLE employees
ADD salary DECIMAL(10, 2) NOT NULL;
SQL> DESC employees;
After creating and altering the table, let’s insert some rows into it with constraints:
-- Inserting rows into the employees table
INSERT INTO employees (employee_id, name, age, department, salary) VALUES
(1, 'Sujata Sain', 30, 'IT', 50000.00),
(2, 'Raghuvaran Rao', 35, 'HR', 55000.00),
(3, 'Preeti Singh', 40, 'Finance', 60000.00);
-- Inserting a row with constraint violations & duplicate employee_id (age < 24, department not in specified list)
INSERT INTO employees (employee_id, name, age, department, salary) VALUES
(1, 'Rohit Sinha', 20, 'Marketing', 45000.00),
(4, 'Rohit Sharma', 20, 'Marketing', 45000.00),
(5, 'Rakesh Kumar', 22, 'Marketing', 48000.00);
SQL> SELECT * FROM employees;
In the above example, we’ve inserted rows into the employees
table. However, the last insert operation violates a constraint where the age of the employee is less than 18. This will result in an error.
Now, let’s drop the employees
table:
-- Drop the employees table
DROP TABLE employees;