0% found this document useful (0 votes)
11 views20 pages

Mysql

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

Mysql

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

El ULTIMATE SQL / MySQL

COMPLETE GUIDE

From Beginner to Professional - with


Detailed Explanations for Every Term

1. Introduction to SQL

What is SQL?

SOL (Structured Query Language)


is a language for communicating
with databases.
A database= a digital system for storing &
managing information.

• Structured ➔ Data has a specific


format (tables, columns, rows).

• Query ➔ You can ask the database


for data.

• Language ➔ You use SOL keywords to


communicate.
2. SQL Command Types

1. DDL ➔ Data Definition Language


(Make & modify tables/databases)

2. DML ➔ Data Manipulation Language


(Add, change, delete records)

3. DCL ➔ Data Control Language


(Manage permissions)

4. TCL ➔ Transaction Control Language


(Save or undo changes)

5. DQL ➔ Data Query Language (Retrieve


or search data)

3. DDL - Data Definition Language


3.1 CREATE

Example:

CREATE TABLE Students (


id INT PRIMARY KEY ,
name VARCHAR (100 ),
age INT ,
class VARCHAR (50 )

Term-by-term Explanation:

• CREATE TABLE ➔ Command to make a


new table.

• Students ➔ Name of the table (choose


meaningful names).

• id ➔ Column for unique


student numbers.

• INT ➔ Integer type (whole numbers).

• PRIMARY KEY ➔ Identifies each


row uniquely.

• name ➔ Column for student's name.


• VARCHAR (100) ➔ Text up to
100 characters.

• age ➔ Integer column for age.

• class ➔ Text up to 50 characters for


class name.

3.2 ALTER

ALTER TABLE Students ADD COLUMN


email VARCHAR ( 100 );

• ALTER TABLE Students ➔ Change


existing table Students.

• ADD COLUMN email ➔ New column


called email.

• VARCHAR(100) ➔ Textupto

100 characters.
3.3 DROP

• DROP TABLE ➔ Delete the table


structure and all data permanently.

3.4 TRUNCATE

• TRUNCATE TABLE ➔ Remove all rows


but keep table definition intact.

4. DML - Data Manipulation Language


4.1 SELECT

SELECT name, age FROM Students


WHERE age> 15 ;

• SELECT name, age ➔ Choose columns


name and age .

• FROM Students ➔ From the


Students table.

• WHERE age > 15 ➔ Condition: only


rows where age is more than 15.

4.2 INSERT

INSERT INTO Students (id, name,


age, class)
VALUES ( 1 , 'John' , 16 , '10A' );
• INSERT INTO Students ➔ Add new
record to Students .

• (id, name, age, class) ➔ Columns


to insert into.

• VALUES ( ... ) ➔ Actual data for


those columns.

• 'John' ➔ Strings use quotes,


numbers don't.

4.3 UPDATE

UPDATE Students SET age - 1t


WHERE name= 'John' ;

• UPDATE Students ➔ Change data


in Students.

• SET age = 1 r ➔ Column age


becomes 17.

• WHERE name = 'John' ➔ Only for John.


4.4 DELETE

DELETE FROM Students WHERE id


= 1;

• DELETE FROM Students ➔ Remove


rows from Students .

• WHERE id = 1 ➔ Only the row with id


= 1.

5. DCL - Data Control Language

5.1 GRANT

GRANT SELECT ON Students TO


'user1' @'localhost' ;
• GRANT SELECT ➔ Give permission
to read.

• ON Students ➔ On the
Students table.

• 'use r1 '@' localhost' ➔ Specific


database user and host.

5.2 REVOKE

REVOKE SELECT ON Students FROM


'user1' @'localhost' ;

• REVOKE SELECT ➔ Remove read


permission from the user.

6. TCL - Transaction Control Language


6.1 START TRANSACTION + COMMIT

START TRANSACTION ;
UPDATE Students SET age - 18
WHERE name= 'John' ;
COMMIT ;

• START TRANSACTION; ➔ Begin a set of


operations to be treated as one unit.

• UPDATE ➔ Change John's age.

• COMMIT; ➔ Save permanently.

6.2 ROLLBACK

START TRANSACTION ;
DELETE FROM Students;
ROLLBACK ;
• ROLLBACK; ➔ Undo all changes since
START TRANSACTION.

6.3 SAVEPOINT

SAVEPOINT before_update;
UPDATE Students SET class -
'11A' WHERE name= 'John' ;
ROLLBACK TO before_update;

• SAVEPOINT ➔ Mark point in transaction


to return to later.

• ROLLBACK TO ➔ Undo changes since


that savepoint only.
7. DQL - Data Query Language

sql

SELECT * FROM Students WHERE


class - '10A' ORDER BY name ASC
LIMIT 5;

• * ➔ All columns.

• ORDER BY name ASC ➔ Sort names


alphabetically.

• LIMIT 5 ➔ Show only first 5 rows.


8. Joins

Setup Tables:

CREATE TABLE Teachers (


t_id INT PRIMARY KEY,
t_name VARCHAR( 50 ),
subject VARCHAR( 50 )

CREATE TABLE Classes (


c_id INT PRIMARY KEY,
t_id INT,
class_name VARCHAR( 50 ),
FOREIGN KEY (t id)
REFERENCES Teachers(t id)
);
INNER JOIN

SELECT Teachers.t_name,
[Link] name
FROM Teachers
INNER JOIN Classes ON
Teachers.t id= Classes.t_id;

• INNER JOIN ➔ Only where data exists


in both tables.
LEFT JOIN

SELECT Teachers.t_name,
Classes.class_name
FROM Teachers
LEFT JOIN Classes ON
Teachers.t id= Classes.t id;

• LEFT JOIN ➔ All rows from left table,


matches from right if any.
9. Constraints

Constraint Meaning Example

PRIMARY Unique ID id INT PRIMARY


KEY for table

FOREIGN Links FOREIGN KEY


KEY another (. ..)
table REFERENCES

UNIQUE email
VARCHAR(100)
UNIQUE

CHECK Condition CHECK (age>=


must be true 5)

DEFAULT Auto value if DEFAULT


'active'
10. Aggregations & Functions

SELECT COUNT(*) FROM Students; -


- total rows
SELECT AVG(age) FROM Students; -
- average age
SELECT MIN(age), MAX(age) FROM
Students; -- youngest & oldest
SELECT CON CAT (name, ' - ',
class) FROM Students; --
text join
SELECT UPPER(name) FROM
Students; -- uppercase
11. Advanced SQL

• Subquery

SELECT* FROM Students


WHERE id IN (SELECT id FROM
Students WHERE age> 15 );

• View

CREATE VIEW TeenStudents AS


SELECT* FROM Students WHERE age
BETWEEN 13 AND 19 ;
• Index

CREATE INDEX idx_age ON


Students(age);

• Stored Procedure

DELIMITER//
CREATE PROCEDURE
GetAllStudents()
BEGIN
SELECT* FROM Students;
END//
DELIMITER;
12. MySQL-Specific Commands

• SHOW DATABASES; - List all databases

• USE db name; - Select a database

• DESCRIBE tablename; - Show


table structure

• SHOW TABLES; - Show tables in


current DB

You might also like