0% found this document useful (0 votes)
13 views12 pages

SQL Basics: Commands & MySQL Guide

Database Presentation, providing valuable insights of Databases

Uploaded by

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

SQL Basics: Commands & MySQL Guide

Database Presentation, providing valuable insights of Databases

Uploaded by

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

Introduction to SQL

and Basic Commands


(DDL, DML, DQL, DCL, TCL)
What is SQL?
 SQL is defined as Structured Query Language.
 It is used to communicate with databases.
 SQL helps to store, retrieve, update, and delete

data.
What is MySQL?
 MySQL is a tool/database management system.
 It uses SQL to manage and organize data.
 MySQL stores data in tables in a structured

format.
Basic MySQL Vocabulary
 Schema: A database (collection of tables).
 Script: A file containing SQL commands.
 Query: A request to fetch or modify data.
 Table: A structure to store data in rows and

columns.
How to Create Your First Table
in MySQL
 1. Open MySQL Workbench.
 2. Connect to your server.
 3. Create a new schema (database).
 4. Open a new SQL tab.
 5. Write a CREATE TABLE query.
 6. Click on the lightning bolt (Execute) to run the

query.
SQL Commands Overview
 DDL (Data Definition Language) - CREATE,
ALTER, DROP, TRUNCATE
 DML (Data Manipulation Language) -

INSERT, UPDATE, DELETE


 DQL (Data Query Language) - SELECT
 DCL (Data Control Language) - GRANT,

REVOKE
 TCL (Transaction Control Language) -

COMMIT, ROLLBACK
DDL
Comman
Purpose Syntax Example
d
CREATE TABLE table_name CREATE TABLE
Make a new table or
CREATE (column1 datatype, students (id INT, name
database.
column2 datatype); VARCHAR(50));
ALTER TABLE table_name
ADD column_name ALTER TABLE students
Change an existing table
datatype; ADD age INT;
ALTER (add, delete, or modify
ALTER TABLE table_name ALTER TABLE students
columns).
DROP COLUMN DROP COLUMN age;
column_name;
Delete a table or
DROP DROP TABLE table_name; DROP TABLE students;
database permanently.
Delete all data inside a
TRUNCA TRUNCATE TABLE TRUNCATE TABLE
table but keep the
TE table_name; students;
structure.
DML
Comman
Purpose Syntax Example
d

Add new INSERT INTO table_name INSERT INTO students


INSERT data into a (col1, col2) VALUES (val1, (name, age) VALUES
table. val2); ('Sara', 20);

Change UPDATE table_name SET UPDATE students SET


UPDATE existing col1 = val1 WHERE age = 21 WHERE name
data. condition; = 'Sara';

Remove DELETE FROM


DELETE FROM students
DELETE data from table_name WHERE
WHERE age < 18;
a table. condition;

To off the safe update mode:


To on the safe update mode:
SET SQL_SAFE_UPDATES =
SET SQL_SAFE_UPDATES = 1;
0;
DQL
Command Purpose Syntax Example

SELECT column1, SELECT name,


Get data from a
SELECT column2 FROM age FROM
table.
table_name; students;

Get all data (all SELECT * FROM SELECT * FROM


columns). table_name; students;

SELECT * FROM SELECT * FROM


Use condition
table_name students WHERE
(filter rows).
WHERE condition; age > 18;

SELECT * FROM
SELECT * FROM
table_name
Sort the result. students ORDER
ORDER BY
BY age;
column_name;
How to use DCL?
 Create new User as
◦ CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
 Grant Privileges/Access
◦ GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
◦ GRANT SELECT, INSERT ON database_name.table_name TO 'username'@'localhost';
 To apply changes
◦ FLUSH PRIVILEGES;
 To view new user’s privileges
◦ SHOW GRANTS FOR 'username'@'localhost';
 To view all active users and connections
◦ SHOW PROCESSLIST;
 Login as new user from cmd with the ‘username’ and password set earlier.
◦ mysql -u username –p >>> enter password
 Select Database
◦ USE databasename;
 Revoke Privileges/Access by
◦ REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'localhost';
◦ REVOKE SELECT, INSERT ON database_name.table_name FROM 'username'@'localhost';
 You can also drop/delete user by using drop/delete commands.
DCL

Command Purpose Syntax Example


GRANT privilege ON GRANT SELECT ON
Give permission to a
GRANT [Link] TO [Link] TO
user
'username'@'host'; 'user1'@'localhost';
REVOKE privilege REVOKE SELECT ON
Take back
ON [Link] [Link]
REVOKE permission from a
FROM FROM
user
'username'@'host'; 'user1'@'localhost';
TCL
1. ROLLBACK only works before you use COMMIT.
2. MySQL must be in a transactional mode (like with InnoDB engine).
3. You can use START TRANSACTION or BEGIN, to use the TCL commands

Command Purpose Syntax Example


After inserting or
COMMIT Save all changes COMMIT;
updating data
Undo changes since If something goes
ROLLBACK ROLLBACK;
last COMMIT wrong
Set a point to SAVEPOINT Create a mini
SAVEPOINT
rollback to savepoint_name; checkpoint

You might also like