This repository was archived by the owner on Jul 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask6.sql
More file actions
89 lines (66 loc) · 1.82 KB
/
Copy pathtask6.sql
File metadata and controls
89 lines (66 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
--Create Database & Table
CREATE DATABASE task6;
USE task6;
CREATE TABLE employees (
emp_id INT AUTO_INCREMENT PRIMARY KEY,
emp_name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10,2),
joining_date DATE,
status VARCHAR(20)
);
-- Insert Bulk Data
INSERT INTO employees (emp_name, department, salary, joining_date, status) VALUES
('Rahul Sharma', 'IT', 55000, '2022-01-15', 'Active'),
('Anita Verma', 'HR', 45000, '2021-03-10', 'Active'),
('Suresh Kumar', 'Finance', 60000, '2020-07-25', 'Inactive'),
('Priya Singh', 'IT', 70000, '2019-11-01', 'Active'),
('Amit Patel', 'Sales', 40000, '2023-02-05', 'Active');
-- Read (SELECT) – Filtered Data
-- All employees
SELECT * FROM employees;
-- Employees from IT department
SELECT emp_name, salary
FROM employees
WHERE department = 'IT';
-- Salary greater than 50,000
SELECT * FROM employees
WHERE salary > 50000;
-- Update Records Using Conditions
-- Increase salary for IT department
UPDATE employees
SET salary = salary + 5000
WHERE department = 'IT';
-- Mark employee as inactive
UPDATE employees
SET status = 'Inactive'
WHERE emp_name = 'Amit Patel';
--Validate after update
SELECT * FROM employees;
--Delete Selective Rows
-- Delete inactive employees
DELETE FROM employees
WHERE status = 'Inactive';
-- Practice Safe Deletes
-- Enable safe updates (MySQL specific)
SET SQL_SAFE_UPDATES = 1;
-- Safe delete using primary key
DELETE FROM employees
WHERE emp_id = 5;
-- Never do this
-- DANGEROUS (deletes all rows)
DELETE FROM employees;
-- Use Transactions
START TRANSACTION;
UPDATE employees
SET salary = salary - 2000
WHERE department = 'HR';
-- Check before committing
SELECT * FROM employees;
-- Rollback if mistake
ROLLBACK;
-- Or save permanently
-- COMMIT;
-- Validate Before & After States
SELECT COUNT(*) FROM employees;
SELECT * FROM employees;