Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

SQL With MySQL - 1

The document contains lecture notes on SQL with MySQL over 18 pages. It discusses creating and altering databases and tables, creating relationships between tables, and executing SQL queries. Mastering SQL skills can boost one's career prospects across different industries.

Uploaded by

Mushaim Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL With MySQL - 1

The document contains lecture notes on SQL with MySQL over 18 pages. It discusses creating and altering databases and tables, creating relationships between tables, and executing SQL queries. Mastering SQL skills can boost one's career prospects across different industries.

Uploaded by

Mushaim Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Intro. To Database Systems Instructor: Haider S.

Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Lecture - 1:

Page 1
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 2
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 3
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 4
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 5
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 6
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

SQL Queries executed in MySQL during Lecture-1:

CREATE DATABASE sql_store2;


CREATE DATABASE IF NOT EXISTS sql_store2;
DROP DATABASE IF EXISTS sql_store2;
DROP DATABASE sql_store2;

-- tables ------------------------------------------------------------
--------------------------------
CREATE DATABASE IF NOT EXISTS sql_store2;
USE sql_store2;
CREATE TABLE customers
(
customer_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
points INT NOT NULL DEFAULT 0,
email VARCHAR (255) NOT NULL UNIQUE
);

-- ALTER tables --try not to alter DB of ongoing project, first try on


dummy DB.
-- forgot to add last_name , or want to change data type or remove
existing column

ALTER TABLE customers


ADD last_name VARCHAR(50) NOT NULL DEFAULT '' AFTER first_name;

ALTER TABLE customers


ADD city VARCHAR(50) NOT NULL,
MODIFY first_name VARCHAR(55) DEFAULT '',
DROP points
;

Page 7
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

-- How to create a relationship


-- create a new orders table & relate it to existing customers table
in sql_store2

CREATE TABLE IF NOT EXISTS orders


(
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
FOREIGN KEY fk_orders_customers (customer_id)
REFERENCES customers (customer_id)
ON UPDATE CASCADE
ON DELETE NO ACTION
);

DROP TABLE IF EXISTS orders;

-- ALTER FOREIGN or PRIMARY KEY

ALTER TABLE orders


ADD customer_id INT NOT NULL,
ADD FOREIGN KEY fk_orders_customers (customer_id)
REFERENCES customers (customer_id)
ON UPDATE CASCADE
ON DELETE NO ACTION;

Page 8
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Lecture - 2:

Page 9
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 10
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 11
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 12
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 13
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 14
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 15
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 16
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 17
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 18
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

Page 19
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

SQL Queries executed in MySQL during Lecture-2:


-- Deleting a tuple
SELECT *
DELETE FROM invoices FROM sql_store.customers
WHERE invoice_id=1; WHERE birth_date >= "1990-01-
01";
-- Select Statement
-- Practice: Get the orders
SELECT 1,2 placed in 2019.
SELECT city , points
FROM sql_store.customers -- AND OR NOT operators
WHERE points>=1000 SELECT *
ORDER BY points DESC FROM sql_store.customers
-- reward customer by 10% WHERE birth_date >= "1990-01-
increment in points 01" AND points>=1000 AND
-- you can order by column , alias state='FL';
or expression also
SELECT customer_id , points, SELECT *
points*1.1 AS reward_points FROM sql_store.customers
FROM sql_store.customers WHERE NOT (points>=1000
-- WHERE points>=1000 AND birth_date>'1990-01-01');
ORDER BY 1 DESC -- points<1000

SELECT DISTINCT state seLECT DISTINCT state


FROM sql_store.customers FROM sql_store.customers
WHERE state = 'vA' OR
SELECT * , unit_price*3 state='FL' OR state= 'GA';
FROM sql_store.products; -- Better approach

SELECT state
-- WHERE >,>=,<,<=,=,!=, <> FROM sql_store.customers ;
Page 20
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.
Intro. To Database Systems Instructor: Haider S. Ahad
Lecture Notes: SQL with MySQL haiderahad@gmail.com

SELECT * -- REGEXP (Self Learning)


FROM sql_store.customers
WHERE points>=1000 AND SELECT * FROM
points<=3000 sql_store.customers
ORDER BY points DESC; WHERE phone IS NOT NULL

-- Better Approach -- Get the orders that are yet


to be shipped / delivered
SELECT *
FROM sql_store.customers Select *
WHERE points BETWEEN 1000 From orders
AND 3000 WHERE shipped_date IS NULL
ORDER BY points DESC , AND shipper_id IS NULL
first_name;

SELECT * -- Show your top 3 loyal


FROM sql_store.customers customers !
WHERE birth_date >= "1990-01- SELECT *
01" AND birth_date < "2000- FROM sql_store.customers
01-01" ORDER BY points DESC
LIMIT 3
SELECT *
FROM sql_store.customers -- Show your top 3 loyal
-- WHERE last_name LIKE 'B%' customers !
-- WHERE last_name LIKE '%y' SELECT *
-- WHERE last_name LIKE FROM sql_store.customers
'%e%' ORDER BY points
WHERE last_name LIKE LIMIT 6, 4 -- offset
'____e_'

Page 21
SQL skills remain highly sought-after across industries. Mastering SQL can significantly boost your career prospects.

You might also like