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

365 Data Science - SQL BASICS - SM

Data Science Basic

Uploaded by

ranjeet kumar
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 views226 pages

365 Data Science - SQL BASICS - SM

Data Science Basic

Uploaded by

ranjeet kumar
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
You are on page 1/ 226

TABLE OF CONTENTS

LET’S BREAK THINGS DOWN

1. SQL THEORY
• Data Definition Language (DDL)
• SQL Keywords
• Data Manipulation Language (DML)
• Data Control Language (DCL)
• Transaction Control Language (TCL)
• SQL Syntax
TABLE OF CONTENTS
LET’S BREAK THINGS DOWN

2. THE SQL SELECT STATEMENT


• SELECT…FROM… • IS NOT NULL / IS NULL
• WHERE • Other Comparison Operators
• AND • Select Distinct
• OR • Introduction to Aggregate Functions
• Operator Precedence • GROUP BY
• Wildcard Characters • HAVING
• BETWEEN…AND… • WHERE vs HAVING
• LIMIT
TABLE OF CONTENTS
LET’S BREAK THINGS DOWN

3. THE SQL INSERT STATEMENT


• The INSERT Statement
• Inserting Data INTO a New Table

4. THE SQL UPDATE STATEMENT


• TCL’s COMMIT and ROLLBACK
• The UPDATE Statement

5. THE SQL DELETE STATEMENT


• The DELETE Statement
• DROP vs TURNCATE vs DELETE
DATA DEFINITION
LANGUAGE (DDL)
Data Definition Language
SQL’s syntax
comprises several types of statements that allow you to perform various commands and
operations

Data Definition Language (DDL)


- a syntax
- a set of statements that allow the user to define or modify data structures and objects, such
as tables

the CREATE statement


used for creating entire databases and database objects as tables
Data Definition Language
the CREATE statement
used for creating entire databases and database objects as tables

CREATE object_type object_name;


Data Definition Language
the CREATE statement
used for creating entire databases and database objects as tables

CREATE object_type object_name;

CREATE TABLE object_name (column_name data_type);


Data Definition Language

CREATE TABLE object_name (column_name data_type);


Data Definition Language

CREATE TABLE object_name (column_name data_type);

CREATE TABLE sales (purchase_number INT);


Data Definition Language

CREATE TABLE object_name (column_name data_type);

CREATE TABLE sales (purchase_number INT);

sales
purchase_number
Data Definition Language

CREATE TABLE sales (purchase_number INT);

sales
purchase_number

the table name can coincide with the name assigned to the database
Data Definition Language
the ALTER statement
used when altering existing objects

- ADD
- REMOVE
- RENAME
Data Definition Language

ALTER TABLE sales


ADD COLUMN date_of_purchase DATE;

sales
purchase_number
Data Definition Language

ALTER TABLE sales


ADD COLUMN date_of_purchase DATE;

sales
purchase_number date_of_purchase
Data Definition Language
the DROP statement
used for deleting a database object
Data Definition Language

DROP object_type object_name;

customers
customer_id first_name
Data Definition Language

used for deleting a database object

DROP object_type object_name;

DROP TABLE customers; customers


customer_id first_name
Data Definition Language

used for deleting a database object

DROP object_type object_name;

DROP TABLE customers; customers


customer_id first_name
Data Definition Language
the RENAME statement
allows you to rename an object
Data Definition Language

RENAME object_type object_name TO new_object_name;

customers
customer_id first_name
Data Definition Language

used for deleting a database object

RENAME object_type object_name TO new_object_name;

RENAME TABLE customers TO customer_data;

customers
customer_id first_name
Data Definition Language

used for deleting a database object

RENAME object_type object_name TO new_object_name;

RENAME TABLE customers TO customer_data;

customer_id first_name
Data Definition Language

used for deleting a database object

RENAME object_type object_name TO new_object_name;

RENAME TABLE customers TO customer_data;

customer_data
customer_id first_name
Data Definition Language
the TRUNCATE statement
instead of deleting an entire table through DROP, we can also remove its data and continue to
have the table as an object in the database
Data Definition Language

TRUNCATE object_type object_name;

customers
customer_id first_name
Data Definition Language

used for deleting a database object

TRUNCATE object_type object_name;

TRUNCATE TABLE customers;


customers
customer_id first_name
Data Definition Language

used for deleting a database object

TRUNCATE object_type object_name;

TRUNCATE TABLE customers;


customers
customer_id first_name
Data Definition Language
Data Definition Language (DDL)
- CREATE
- ALTER
- DROP
- RENAME
- TRUNCATE
Keywords
Keywords:
- ADD
- CREATE
- ALTER
- etc.

KEYWORDS IN SQL CANNOT BE VARIABLE NAMES!


objects or databases cannot have names that coincide with SQL keywords
Keywords
CREATE, ALTER:
Keywords
CREATE, ALTER:

CREATE TABLE alter (purchase_number INT);

alter
purchase_number
Data Definition Language
ADD
Data Definition Language
ADD

ALTER TABLE sales


ADD COLUMN date_of_purchase DATE;

sales
purchase_number date_of_purchase
Data Definition Language
ADD, ALTER

ALTER TABLE sales


ADD COLUMN date_of_purchase DATE;

sales
purchase_number date_of_purchase
Keywords

Keywords = reserved words


they cannot be used when naming objects
DATA
MANIPULATION
LANGUAGE (DML)
Data Manipulation Language
Data Manipulation Language (DML)
its statements allow us to manipulate the data in the tables of a database

the SELECT statement


used to retrieve data from database objects, like tables
Data Manipulation Language

SELECT * FROM sales;

sales
purchase_number
Data Manipulation Language

SELECT * FROM sales;

sales
purchase_number
Data Manipulation Language

SELECT… FROM sales;

sales
purchase_number
Data Manipulation Language

SELECT… FROM sales;

sales
purchase_number
Data Manipulation Language

SELECT… FROM sales;

sales
purchase_number
Data Manipulation Language
Why are we going to need just a piece of the table?

- imagine a table with 2 million rows of data


- it can be helpful if you could extract only a portion of the table that satisfies given criteria

- you should know how to use SELECT perfectly well


Data Manipulation Language
the INSERT statement
used to insert data into tables

INSERT INTO… VALUES…;


Data Manipulation Language

INSERT INTO sales (purchase_number, date_of_purchase) VALUES


(1, ‘2017-10-11’);

sales
purchase_number date_of_purchase
Data Manipulation Language

INSERT INTO sales (purchase_number, date_of_purchase) VALUES


(1, ‘2017-10-11’);

sales
purchase_number date_of_purchase
1 2017-10-11
Data Manipulation Language

INSERT INTO sales VALUES


(1, ‘2017-10-11’);

sales
purchase_number date_of_purchase
1 2017-10-11
Data Manipulation Language

INSERT INTO sales (purchase_number, date_of_purchase) VALUES


(1, ‘2017-10-11’);

INSERT INTO sales VALUES


(1, ‘2017-10-11’);
Data Manipulation Language

INSERT INTO sales (purchase_number, date_of_purchase) VALUES


(2, ‘2017-10-27’);

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
the UPDATE statement
allows you to renew existing data of your tables
Data Manipulation Language

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language

UPDATE sales
SET date_of_purchase = ‘2017-12-12’
WHERE purchase_number = 1;

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language

UPDATE sales
SET date_of_purchase = ‘2017-12-12’
WHERE purchase_number = 1;

sales
purchase_number date_of_purchase
1 2017-12-12
2 2017-10-27
Data Manipulation Language
the DELETE statement
- functions similarly to the TRUNCATE statement

TRUNCATE vs. DELETE


TRUNCATE allows us to remove all the records contained in a table

vs.

with DELETE, you can specify precisely what you would like to be removed
Data Manipulation Language

DELETE FROM sales;

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language

DELETE FROM sales; TRUNCATE TABLE


sales;

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language

DELETE FROM sales; TRUNCATE TABLE


sales;

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language

DELETE FROM sales


WHERE
purchase_number = 1;

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language

DELETE FROM sales


WHERE
purchase_number = 1;

sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
Data Manipulation Language (DML)
- SELECT… FROM…
- INSERT INTO… VALUES…
- UPDATE… SET… WHERE…
- DELETE FROM… WHERE…
DATA CONTROL
LANGUAGE (DCL)
Data Control Language
Data Control Language (DCL)

the GRANT and REVOKE statements


allow us to manage the rights users have in a database
Data Control Language
Data Control Language
Data Control Language

users
Data Control Language
The GRANT statement
gives (or grants) certain permissions to users
Data Control Language
The GRANT statement
gives (or grants) certain permissions to users
Data Control Language
The GRANT statement
gives (or grants) certain permissions to users

GRANT type_of_permission ON database_name.table_name TO


‘username’@’localhost’
Data Control Language
The GRANT statement
gives (or grants) certain permissions to users

one can grant a specific type of permission, like complete or partial access

GRANT type_of_permission ON database_name.table_name TO


‘username’@’localhost’
Data Control Language
these rights will be assigned to a person who has a username registered at the local server
(‘localhost’: IP 127.0.0.1)

big companies and corporations don’t use this type of server, and their databases lay on
external, more powerful servers

GRANT type_of_permission ON database_name.table_name TO


‘username’@’localhost’
Data Control Language
Database administrators
people who have complete rights to a database
- they can grant access to users and can revoke it

the REVOKE clause


used to revoke permissions and privileges of database users
- the exact opposite of GRANT
Data Control Language
the REVOKE clause
used to revoke permissions and privileges of database users
Data Control Language
the REVOKE clause
used to revoke permissions and privileges of database users

REVOKE type_of_permission ON database_name.table_name FROM


‘username’@’localhost’
TRANSACTION
CONTROL
LANGUAGE (TCL)
Transaction Control Language
Transaction Control Language (TCL)
- not every change you make to a database is saved automatically

the COMMIT statement


- related to INSERT, DELETE, UPDATE
- will save the changes you’ve made
- will let other users have access to the modified version of the database
Transaction Control Language
DB administrator
Transaction Control Language
DB administrator
- Change the last name of the 4th customer from ‘Winnfield’ to ‘Johnson’
Transaction Control Language
DB administrator
- Change the last name of the 4th customer from ‘Winnfield’ to ‘Johnson’
Transaction Control Language
DB administrator
- Change the last name of the 4th customer from ‘Winnfield’ to ‘Johnson’
Transaction Control Language
DB administrator
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator

Problem: users
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;
Transaction Control Language
DB administrator

users
Transaction Control Language
the COMMIT statement
committed states can accrue

the ROLLBACK clause


the clause that will let you make a step back
- allows you to undo any changes you have made but don’t want to be saved permanently
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;

ROLLBACK;
Transaction Control Language
DB administrator

UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;

ROLLBACK;
Transaction Control Language
the COMMIT statement
- saves the transaction in the database
- changes cannot be undone

the ROLLBACK clause


- allows you to take a step back
- the last change(s) made will not count
- reverts to the last non-committed state
SQL Syntax
DDL – Data Definition Language
creation of data

DML – Data Manipulation Language


manipulation of data

DCL – Data Control Language


assignment and removal of permissions to use this data

TCL – Transaction Control Language


saving and restoring changes to a database
SELECT…FROM…
SELECT… FROM…
the SELECT statement
allows you to extract a fraction of the entire data set

- used to retrieve data from database objects, like tables


- used to “query data from a database”
SELECT… FROM…

SELECT column_1, column_2,… column_n


FROM table_name;
SELECT… FROM…

SELECT column_1, column_2,… column_n


FROM table_name;

- when extracting information, SELECT goes with FROM


SELECT… FROM…

SELECT column_1, column_2,… column_n


FROM table_name;
SELECT… FROM…

SELECT column_1, column_2,… column_n


FROM table_name;

SELECT first_name, last_name


FROM employees;
SELECT… FROM…

SELECT * FROM employees;

* - a wildcard character, means “all” and “everything”


WHERE
WHERE

SELECT * FROM employees;


WHERE

SELECT column_1, column_2,… column_n


FROM table_name;
WHERE
the WHERE clause
it will allow us to set a condition upon which we will specify what part of the data we want to
retrieve from the database
WHERE
the WHERE clause
it will allow us to set a condition upon which we will specify what part of the data we want to
retrieve from the database

SELECT column_1, column_2,… column_n


FROM table_name;
WHERE
the WHERE clause
it will allow us to set a condition upon which we will specify what part of the data we want to
retrieve from the database

SELECT column_1, column_2,… column_n


FROM table_name
WHERE condition;
AND
AND
= equal operator
in SQL, there are many other linking keywords and symbols, called operators, that you can use
with the WHERE clause

- AND - EXISTS - NOT EXISTS


- OR - IS NULL - IS NOT NULL
- IN - NOT IN - comparison operators
- LIKE - NOT LIKE - etc.
- BETWEEN… AND…
AND
AND
allows you to logically combine two statements in the condition code block
AND
AND
allows you to logically combine two statements in the condition code block

SELECT column_1, column_2,… column_n


FROM table_name
WHERE condition_1 AND condition_2;

- allows us to narrow the output we would like to extract from our data
OR
OR
AND
AND binds SQL to meet both conditions enlisted in the WHERE clause simultaneously

SELECT column_1, column_2,… column_n


FROM table_name
WHERE condition_1 AND condition_2;
OR
AND
conditions set on different columns

OR
conditions set on the same column
OPERATOR
PRECEDENCE
Operator Precedence
logical operator precedence
an SQL rule stating that in the execution of the query, the operator AND is applied first, while
the operator OR is applied second

AND > OR
regardless of the order in which you use these operators, SQL will always start by reading the
conditions around the AND operator
WILDCARD
CHARACTERS
Wildcard Characters
wildcard characters

% _ *

you would need a wildcard character whenever you wished to put “anything” on its place
Wildcard Characters
% - a substitute for a sequence of characters

LIKE (‘Mar%’) Mark, Martin, Margaret

_ - helps you match a single character

LIKE (‘Mar_’) Mark, Marv, Marl


Wildcard Characters
* will deliver a list of all columns in a table

SELECT * FROM employees;

- it can be used to count all rows of a table


BETWEEN…AND…
BETWEEN… AND…
BETWEEN… AND…
helps us designate the interval to which a given value belongs
BETWEEN… AND…

SELECT
*
FROM
employees
WHERE
hire_date BETWEEN '1990-01-01' AND '2000-01-01';
BETWEEN… AND…

SELECT
*
FROM
employees
WHERE
hire_date BETWEEN '1990-01-01' AND '2000-01-01';

'1990-01-01' AND '2000-01-01' will be included in the retrieved list of records


BETWEEN… AND…
NOT BETWEEN… AND…
will refer to an interval composed of two parts:
- an interval below the first value indicated
- a second interval above the second value
BETWEEN… AND…

SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';
BETWEEN… AND…

SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';

- the hire_date is before '1990-01-01'


or
- the hire_date is after ‘2000-01-01'
BETWEEN… AND…

SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';

'1990-01-01' AND '2000-01-01’ are not included in the intervals


BETWEEN… AND…
BETWEEN… AND…

- not used only for date values


- could also be applied to strings and numbers
IS NOT NULL
/ IS NULL
IS NOT NULL / IS NULL
IS NOT NULL
used to extract values that are not null
IS NOT NULL / IS NULL
IS NOT NULL
used to extract values that are not null

SELECT column_1, column_2,… column_n


FROM table_name
WHERE column_name IS NOT NULL;
IS NOT NULL / IS NULL
IS NULL
used to extract values that are null

SELECT column_1, column_2,… column_n


FROM table_name
WHERE column_name IS NULL;
OTHER
COMPARISON
OPERATORS
Other Comparison Operators

SQL

= equal to

> greater than

>= greater than or equal to

< less than

<= less than or equal to


Other Comparison Operators

SQL “Not Equal” operators

<>, != not equal,≠

different from
SELECT DISTINCT
SELECT DISTINCT
the SELECT statement
can retrieve rows from a designated column, given some criteria
SELECT DISTINCT
SELECT DISTINCT
selects all distinct, different data values
SELECT DISTINCT
SELECT DISTINCT
selects all distinct, different data values

SELECT DISTINCT column_1, column_2,… column_n


FROM table_name;
INTRODUCTION
TO AGGREGATE
FUNCTIONS
Introduction to Aggregate Functions

aggregate functions

they are applied on multiple rows of a single column of a table and


return an output of a single value
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field

SUM()
sums all the non-null values in a column

MIN()
returns the minimum value from the entire list

MAX()
returns the maximum value from the entire list

AVG()
calculates the average of all non-null values belonging to a certain column of a table
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field

- it is frequently used in combination with the reserved word “DISTINCT”


Introduction to Aggregate Functions
COUNT()

SELECT COUNT(column_name)
FROM table_name;

the parentheses after COUNT() must start right after the keyword, not after a whitespace
Introduction to Aggregate Functions
COUNT(DISTINCT )

SELECT COUNT(DISTINCT column_name)


FROM table_name;
Introduction to Aggregate Functions

aggregate functions

they are applied on multiple rows of a single column of a table and


return an output of a single value

- they ignore NULL values unless told not to


GROUP BY
GROUP BY
GROUP BY
When working in SQL, results can be grouped according to a specific field or fields

- GROUP BY must be placed immediately after the WHERE conditions, if any, and just before the
ORDER BY clause

- GROUP BY is one of the most powerful and useful tools in SQL


GROUP BY
GROUP BY

SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
ORDER BY column_name(s);
GROUP BY
GROUP BY
in most cases, when you need an aggregate function, you must add a GROUP BY clause in
your query, too

Always include the field you have grouped your results by in the SELECT statement!
HAVING
HAVING
HAVING
refines the output from records that do not satisfy a certain condition

- frequently implemented with GROUP BY


HAVING

SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s);

- HAVING is like WHERE but applied to the GROUP BY block


HAVING
WHERE vs. HAVING

after HAVING, you can have a condition with an aggregate function, while WHERE
cannot use aggregate functions within its conditions
WHERE VS HAVING
WHERE vs HAVING
WHERE
allows us to set conditions that refer to subsets of individual rows
WHERE vs HAVING
WHERE vs HAVING

WHERE
WHERE vs HAVING

WHERE
re-organizing the output
into groups
(GROUP BY)
WHERE vs HAVING

WHERE
re-organizing the output
into groups
(GROUP BY)

the output can be further improved, or filtered


WHERE vs HAVING

WHERE
re-organizing the output
into groups
(GROUP BY)

HAVING
WHERE vs HAVING

WHERE
re-organizing the output
into groups
(GROUP BY)

HAVING
WHERE vs HAVING

WHERE
re-organizing the output
into groups
(GROUP BY)

HAVING

ORDER BY…
WHERE vs HAVING
HAVING
- you cannot have both an aggregated and a non-aggregated condition in the HAVING clause
WHERE vs HAVING

Aggregate functions – GROUP BY and HAVING

General conditions - WHERE


WHERE vs HAVING

SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s);
LIMIT
LIMIT

SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s)
LIMIT number ;
THE SQL INSERT
STATEMENT
The INSERT Statement

Sales
The INSERT Statement
The INSERT Statement

INSERT INTO table_name (column_1, column_2, …, column_n)


VALUES (value_1, value_2, …, value_n);
INSERTING DATA
INTO A NEW TABLE
Inserting Data INTO a New Table
INSERT INTO SELECT

INSERT INTO table_2 (column_1, column_2, …, column_n)


SELECT column_1, column_2, …, column_n
FROM table_1
WHERE condition;
THE UPDATE
STATEMENT
TCL’S COMMIT
AND ROLLBACK
TCL’s COMMIT and ROLLBACK
the COMMIT statement
- saves the transaction in the database
- changes cannot be undone
used to save the state of the data in the database at the moment of its execution

the ROLLBACK clause


- allows you to take a step back
- the last change(s) made will not count
- reverts to the last non-committed state
it will refer to the state corresponding to the last time you executed COMMIT
TCL’s COMMIT and ROLLBACK
TCL’s COMMIT and ROLLBACK

COMMIT;

1
TCL’s COMMIT and ROLLBACK

COMMIT; COMMIT;

1 2
TCL’s COMMIT and ROLLBACK

COMMIT; COMMIT;

1 2 …
TCL’s COMMIT and ROLLBACK

COMMIT; COMMIT; COMMIT;

1 2 … 10
TCL’s COMMIT and ROLLBACK

COMMIT; COMMIT; COMMIT; ROLLBACK;

1 2 … 10 1
TCL’s COMMIT and ROLLBACK

- ROLLBACK will have an effect on the last execution you have performed

COMMIT; COMMIT; COMMIT; ROLLBACK;

1 2 … 10 1
TCL’s COMMIT and ROLLBACK

- ROLLBACK will have an effect on the last execution you have performed

COMMIT; COMMIT; COMMIT; ROLLBACK; ROLLBACK;

1 2 … 10 1 2
TCL’s COMMIT and ROLLBACK

- ROLLBACK will have an effect on the last execution you have performed

COMMIT; COMMIT; COMMIT; ROLLBACK; ROLLBACK;

1 2 … 10 1 2 …
TCL’s COMMIT and ROLLBACK

- ROLLBACK will have an effect on the last execution you have performed

COMMIT; COMMIT; COMMIT; ROLLBACK; ROLLBACK; ROLLBACK;

1 2 … 10 1 2 … 20
TCL’s COMMIT and ROLLBACK

- ROLLBACK will have an effect on the last execution you have performed

- you cannot restore data to a state corresponding to an earlier COMMIT

COMMIT; COMMIT; COMMIT; ROLLBACK; ROLLBACK; ROLLBACK;

1 2 … 10 1 2 … 20
THE SQL UPDATE
STATEMENT
The UPDATE Statement
the UPDATE Statement
used to update the values of existing records in a table
The UPDATE Statement
the UPDATE Statement
used to update the values of existing records in a table

UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;

- we do not have to update each value of the record of interest


- we can still say we have updated the specific record
The UPDATE Statement
the UPDATE Statement
used to update the values of existing records in a table

UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;

- if you don’t provide a WHERE condition, all rows of the table will be updated
THE SQL DELETE
STATEMENT
The DELETE Statement
the DELETE statement
removes records from a database

DELETE FROM table_name


WHERE conditions;
FOREIGN KEY Constraint
ON DELETE CASCADE
if a specific value from the parent table’s primary key has been deleted, all the records from the
child table referring to this value will be removed as well
DROP VS
TURNCATE VS
DELETE
DROP vs TRUNCATE vs DELETE
DROP

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
DROP

column_1
1
indexes
2
3
4
+ + constraints


10
DROP vs TRUNCATE vs DELETE
DROP

column_1
1
indexes
2
3
4
+ + constraints


10
DROP vs TRUNCATE vs DELETE
DROP
- you won’t be able to roll back to its initial state, or to the last COMMIT statement

use DROP TABLE only when you are sure you aren’t going to use the table in question anymore
DROP vs TRUNCATE vs DELETE
TRUNCATE

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE ~ DELETE without WHERE

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE ~ DELETE without WHERE

column_1
1
2
3
4
+

10
DROP vs TRUNCATE vs DELETE
TRUNCATE ~ DELETE without WHERE

column_1
1
2
3
4
+

10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1
1
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11 1
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11 1
2 12
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11 1
2 12 2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 1
2 2
3 TRUNCATE 3
4 4

… …
10 10
DROP vs TRUNCATE vs DELETE
DELETE
removes records row by row

DELETE FROM table_name


WHERE conditions;

TRUNCATE ~ DELETE without WHERE


DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- the SQL optimizer will implement different programmatic approaches when we are using
TRUNCATE or DELETE

TRUNCATE delivers the output much quicker than DELETE


row by row row by row
DROP vs TRUNCATE vs DELETE

TRUNCATE vs DELETE without WHERE


- the SQL optimizer will implement different programmatic approaches when we are using
TRUNCATE or DELETE

TRUNCATE delivers the output much quicker than DELETE


row by row row by row
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE
DROP vs TRUNCATE vs DELETE

TRUNCATE vs DELETE without WHERE


- auto-increment values are not reset with DELETE

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE

column_1
1
2
3 DELETE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE

column_1 column_1
1
2
3 DELETE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE

column_1 column_1
1 11
2 12
3 DELETE 13
4 14

… …
10 20

You might also like