Comprehensive SQL Commands Guide
1. Setting Up the Database and Table
Create a database named DBMS:
CREATE DATABASE DBMS;
Use the created database:
USE DBMS;
Create a table named student:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
course VARCHAR(50),
admission_date DATE,
city VARCHAR(50)
);
Insert data into the student table:
INSERT INTO student (id, name, age, course, admission_date, city) VALU
(1, 'Amit Sharma', 20, 'Computer Science', '2023-06-15', 'Mumbai'),
(2, 'Priya Singh', 22, 'Electronics', '2023-07-01', 'Delhi'),
(3, 'Rahul Desai', 21, 'Mechanical', '2023-06-20', 'Pune'),
(4, 'Ankita Mehta', 23, 'Civil', '2023-06-18', 'Bangalore');
2. DDL (Data Definition Language)
ALTER TABLE: Add a column for email.
ALTER TABLE student ADD email VARCHAR(100);
DROP COLUMN: Remove the email column.
ALTER TABLE student DROP COLUMN email;
RENAME TABLE:
ALTER TABLE student RENAME TO students;
DROP TABLE: Delete the students table.
DROP TABLE students;
3. DML (Data Manipulation Language)
INSERT: Add a new student.
INSERT INTO student (id, name, age, course, admission_date, city)
VALUES (5, 'Vikram Patel', 24, 'Chemical', '2023-06-25', 'Ahmedabad');
UPDATE: Change city for a student.
UPDATE student SET city = 'Hyderabad' WHERE id = 5;
DELETE: Remove a student by ID.
DELETE FROM student WHERE id = 5;
4. DQL (Data Query Language)
SELECT: Retrieve all students.
SELECT * FROM student;
SELECT Specific Columns:
SELECT name, city FROM student;
WHERE Clause:
SELECT * FROM student WHERE city = 'Delhi';
ORDER BY Clause:
SELECT * FROM student ORDER BY age DESC;
GROUP BY and HAVING:
SELECT city, COUNT(*) AS student_count FROM student GROUP BY city
5. TCL (Transaction Control Language)
BEGIN TRANSACTION:
START TRANSACTION;
Insert data during transaction:
INSERT INTO student (id, name, age, course, admission_date, city)
VALUES (6, 'Ritika Verma', 22, 'IT', '2023-06-30', 'Mumbai');
Rollback changes:
ROLLBACK;
Commit changes:
COMMIT;
6. Constraints
PRIMARY KEY: Ensure unique student IDs.
ALTER TABLE student ADD PRIMARY KEY (id);
UNIQUE Constraint:
ALTER TABLE student ADD UNIQUE (email);
FOREIGN KEY Example:
CREATE TABLE course (
course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);
ALTER TABLE student ADD CONSTRAINT fk_course FOREIGN KEY (cou
7. Joins
INNER JOIN Example:
SELECT student.name, course.course_name
FROM student INNER JOIN course ON student.course = course.course_n
LEFT JOIN Example:
SELECT student.name, course.course_name
FROM student LEFT JOIN course ON student.course = course.course_na
8. Views
Create a View:
CREATE VIEW StudentDetails AS SELECT name, course, city FROM stud
Drop the View:
DROP VIEW StudentDetails;
9. Indexes
Create an Index:
CREATE INDEX idx_city ON student(city);
Drop the Index:
DROP INDEX idx_city;
10. Miscellaneous
Count Students:
SELECT COUNT(*) FROM student;
Find Oldest Student:
SELECT * FROM student ORDER BY age DESC LIMIT 1;
Notes:
- Always back up your database before making structural changes.
- Use WHERE clauses to target specific data and avoid accidental modifi