Docsity

Basics of SQL: Complete Study Notes, Important Commands, Queries & Database Concepts, Study notes of Technology

Partial preview of the text

Download Basics of SQL: Complete Study Notes, Important Commands, Queries & Database Concepts and more Study notes Technology in PDF only on Docsity!

1. SQL BASICS SQL SQL means Structured Query Language. It is used to create, store, retrieve, update and i manage data in-arelational database. | Database | A-database is an organized collection of |_ related data. A table stores data in rows and columns. |_Importantterms Row / Record = one complete entry. Column / Field = one type of information. Primary Key = uniquely identifies each row. | Foreign Key = connects related tables. Uses of SQL Create databases and tables. | —Insert, search, update and delete data- Filter, sort and group data. Combine data using JOIN. Generate reports and manage access. SOL Notes « 1 2. SQL COMMANDS DDL Data Definition Language changes database structure. |-GREATE, ALTER, DROP, TRUNCATE | DML | Data Manipulation Language changes data. |_ INSERT, UPDATE, DELETE nar DL | _Data Query_Language retrieves data. SELECT I_pcl Data Control Language manages permissions. GRANT, REVOKE TCL Transaction Control Language manages transactions. | COMMIT, ROLLBACK, SAVEPOINT SOL Notes «2 4. INSERT & SELECT INSERT INSERT INTO student (id, name, age, city) | VALUES (1, ‘Riya’, 19, ‘Agartala’); ———— SELECT name, city FROM student; ______|__ SELECT DISTINCT city FROM student; ————- WHERE _____| _SELECT* FROM student WHERE age >= 18; SELECT | SELECT * FROM student; Operators: =, <>, !=, >, <, >=, <= AND, OR and NOT can combine conditions. UPDATE UPDATE student SET city = 'Kolkata’ WHERE id = 1; | DELETE FROM student WHERE id = 1; ————Use WHERE earefully-so- unwanted rows are ____|__not deleted. DELETE SOL Notes « 4 5. FILTERING & SORTING LIKE % means any number of characters. ———-LIKE“A%' > starts-with-A- ____|_LIKE '"%a' > ends with a ————IN-& BETWEEN ___| WHERE city IN (‘Agartala’,‘Delhi’); __ means one character. WHERE age BETWEEN 18 AND 21; | NULL WHERE email IS NULL; WHERE email IS NOT NULL; ORDER BY SELECT * FROM student ORDER BY age ASC; SELECT * FROM student ORDER BY age DESC; LIMIT SELECT * FROM student LIMIT 5; Shows only the required number of rows. SQL Notes «5 7. JOINS JOIN JOIN combines related data from two or more tables using a common column. INNER JOIN SELECT s.name, d.dept_name ——— FROM students ____|__INNER JOIN department d ON s.dept_id = d.dept_id; Returns matching rows only. LEFT JOIN Returns all rows from the left table and matching rows from the right table. RIGHT JOIN Returns all rows from the right table and matching rows from the left table. FULL OUTER JOIN ———dees not directly: support FULL OUTER JOIN. | Returns all rows from both tables. MySQL | SELF JOIN | Atable is joined with itself, usefultor | _employee-manager relationships. SQL Notes «7 8. KEYS & CONSTRAINTS Keys PRIMARY KEY - unique row identifier. | FOREIGN KEY = links two tables. ; CGANBIDATE-KEY— possible unique identifier. |_ COMPOSITE KEY — key made from multiple columns. Constraints NOT NULL -> value required. UNIQUE — no duplicate values. CHECK — validates a condition. | DEFAULT = supplies an automatic value. Example CREATE TABLE marks ( ; —toll_no INT PRIMARY KEY, name VARCHAR(50) NOT NULL. marks INT CHECK (marks BETWEEN 0 AND 100), city VARCHAR(30) DEFAULT ‘Agartala’ ) SQL Notes + 8 10. TRANSACTION & NORMALIZATION Transaction COMMIT > permanently save changes. | ROLLBACK — undo changes. | SAVEPOINT — create-a rollback point. | ACID | —Atomicity = alt or nothing: = | _Gonsistency = valid data. Isolation = transactions do not interfere incorrectly. Durability = committed data stays saved. Normalization 1NF — atomic values, no repeating groups. | 2NF -> TNF + no partial dependency. | 3NF-—2NF-+no transitive dependency. Exam revision | _DELETE-vs FRUNGATE: DELETE -can-use WHERE: TRUNCATE removes all rows. WHERE vs HAVING: rows vs groups. UNION vs UNION ALL: duplicates removed vs retained. SQL Notes + 10