Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

SQL for Begginers - Full Course

Module-1: SQL Session - 1


 What is DBMS & RDBMS?
 What is Database?
 What is SQL?
 Database components - Client & Server
 My SQL Installing
 My SQL Workbench
 Connecting to database & Querying Tables
 Database vs. Schema
 SQL Commands
 SQL Data Types
 My SQL Functions
 My SQL Clauses and Results
 My SQL Set Operators
 My SQL Joins
 Sub Queries
 Integrity Constrains
 Auto Increment
 Limiting Rows
 SQL Views & Index
 What is DBMS & RDBMS?

DBMS -> Database Management System


Ex: Dbase, Foxpro, Ms Access, etc

RBDSM -> Relational Database Management System


Ex: Oracle, MS-SQL Server, DB2, MySQL, MS Access

 What is Database?
- database in a data storage area

 What is SQL?
SQL - is used to communicate to the database to perform different kind of tasks

 Database components - Client & Server


- data is stored in Database Server and we can send SQL commands using client
software
- there are 2 types of client:
1) GUI - Graphical mode
2) CLI - Command line interface

Ex:
Oracle - GUI - SQL Developer
- CLI - SQL Plus

MySQL - GUI - MySQL Workbench


- CLI - MySQL Command Line Tool

 Database vs. Schema

Database - includes all the objects, includes the schemas

Schema - a subfolder of the database, including tables (it is also a database)

 SQL Commands

1. DDL - data definition language (about the structure of the table)

a) CREATE:
CREATE DATABASE name; - creates database
CREATE DATABASE IF NOT EXISTS name; - checks if database name exists
CREAT SCHEMA name; - creates schema
CREATE SCHEMA IF NOT EXISTS name; - checks if schema name exists
Ex: CREATE TABLE name(col1 datatype,col2 datatype,col3 datatype,…) - creates
table
Ex: CREATE TABLE gdpr(BadgeNo int(4),FirstName varchar(20), DepId int(3));

b) ALTER:
- adding a new column
- dropping the existing column
- modifying the existing column (increase/decrease size of the column & change the
data type of the column
- renaming a column

1) Adding a new column:


ALTER TABLE tablenameADD(columnname varchar(integer));
ALTER TABLE gdpr ADD(Age varchar(3));
2) Dropping a column from table:
ALTER TABLE tablename DROP COLUMN columnname;
ALTER TABLE gdpr DROP COLUMN Age;

3) Modify existing column:


- increase/decrease the column size
- decrease the column size ONLY when existing column values can fit in the new size
- column SHOULD be EMPTY to change its data type
ALTER TABLE tablename MODIFY COLUMN columnname INT(15); - modifies size
from 11 to 15
ALTER TABLE gdpr MODIFY COLUMN salary INT(15); - modifies size from 11 to 15

4) Renaming a column:
ALTER TABLE tablename RENAME COLUMN oldname TO newname;
ALTER TABLE tablename RENAME COLUMN Age TO Years;

c) DROP:
- will completely drop the table (including the structure) NOT WORKING with
ROLLBACK command
EX: DROP TABLE tamblename
Ex: DROP TABLE gdpr

d) TRUNCATE:
TRUNCATE DATABASE name;
TRUNCATE SCHEMA name;
- will delete only the data from the table, not the structure
- WILL NOT WORK with ROLLBACK command to rollback the data

e) RENAME -
Ex: RENAME TABLE oldtablename TO newtablename
Ex: RENAME TABLE gdpr TO gdpr1;

2. DML - data manipulation language

1) INSERT -
INSERT INTO name VALUES(Val1,Val2,Val3,…);

Ex: INSERT INTO student VALUES(101,’KIRAN’80);


Ex: INSERT INTO student(SNAM,SNO,MARKS) VALUES(‘RAM’,102,60);
Ex: INSERT INTO student VALUES(103,’KRISHNA’,NULL);
2) UPDATE
- is used to modify the existing records in a table
Ex: UPDATE table SET column1=value WHERE column2=value;
Ex: UPDATE gdpr SET LastName=’Pipps’ WHERE Country=’Germany’;

3) DELETE:
DELETE DATABASE name;
DELETE SCHEMA name;
- will delete only the data from the table, not the structure, but can be rollback
- WILL WORK with ROLLBACK command to rollback the data
- you need to use these commands first:

1) SET autocommit=0; when 0 we have to manually commit with COMMIT command


When 1 it automatically commits the commands
2) SET SQL_SAFE_UPDATES=0; - to allow us to update the table

Ex: DELETE FROM tablename; - will delete all data from the table
Ex: DELETE FROM gdpr;

3. DRL/DQL - data retrieval language / data query language

1) SELECT
- it shows the content of the table (selected)

Ex: SELECT SNAME,SNO,MARKS FROM student;


Ex: SELECT * FROM gdpr WHERE salary>3000;
Ex: SELECT * FROM gdpr WHERE salary<=3000;
Ex: SELECT * FROM gdpr WHERE Country=Germany;
Ex: SELECT * FROM gdpr WHERE FirstName=’Carlos’;
Ex: SELECT * FROM gdpr WHERE city is null;
Ex: SELECT City FROM gdpr;
Ex: SELECT DISTINCT * FROM gdpr;
Ex: SELECT DISTINCT Country FROM gdpr;
Conclusion: WHERE and DISTINCT are clauses for the SELECT command
WHERE - is filtering records based on conditions
DISTINCT - is displaying unique records from a table

Ex: SELECT * FROM gdpr WHERE Salary>3000 AND Country=’Spain’;


Ex: SELECT * FROM gdpr WHERE Salary>3000 OR Country=’Mexico’;
Ex: SELECT * FROM gdpr WHERE NOT FirstName=’Carlos’;
Ex: SELECT * FROM gdpr WHERE Salary=3000 OR Salary=3550;
Conclusion: AND, OR and NOT are logical operators for commands and clauses
Ex: SELECT * FROM gdpr WHERE Salary BETWEEN 2500 AND 3000;
or
Ex: SELECT * FROM gdpr WHERE Salary>=2500 AND Salary=>3000;

Ex: SELECT * FROM gdpr WHERE Salary NOT BETWEEN 2500 AND 3000;
or
Ex: SELECT * FROM gdpr WHERE NOT Salary>=2500 AND Salary=>3000;
Ex: SELECT * FROM gdpr WHERE Salary IN(3000, 3550,4150);
Ex: SELECT * FROM gdpr WHERE Salary NOT IN(3000, 3550,4150);
Conclusion: BETWEEN/NOT BETWEEN, IN/NOT IN are logical operators for
commands and clauses

Ex: SELECT * FROM gdpr WHERE FirstName LIKE ‘S%’;


Ex: SELECT * FROM gdpr WHERE FirstName LIKE ‘%r’;
Ex: SELECT * FROM gdpr WHERE FirstName LIKE ‘S%r’;
Ex: SELECT * FROM gdpr WHERE FirstName LIKE ‘%m%’;
Ex: SELECT * FROM gdpr WHERE FirstName LIKE ‘%e_’;
Ex: SELECT * FROM gdpr WHERE FirstName LIKE ‘__’;
Ex: SELECT * FROM gdpr WHERE FirstName NOT LIKE ‘S&’;
Conclusion: % (many characters), ‘_’ (singe characters).- whiled card carachters

4. TCL - transaction control language

a) COMMIT - is used to save all changes made to the database since last COMMIT or
ROLLBACK
b) ROLLBACK - is used for reverting changes performed by a quarry since last
COMMIT or ROLLBACK
c) SAVE POINT - a point in the transaction to which you can later rollback

5. DCL - data control language

a) GRANT - used for granting privileges on tables and views


b) REVOKE - used for removing all granted permissions
 SQL Data Types
 MySQL Functions

A) STRING FUNCTIONS - operates on string data type


B) NUMERIC FUNCTIONS - operates on numeric data types
C) DATE FUNCTIONS - operates on date/time data types
D) AGGREGATE FUNCTIONS - operates on all of the data types and produces
summarizes results sets

A) STRING FUNCTIONS:

1) UPPER() - converts into upper case letters


Ex: SELECT UPPER(columnname) FROM tablename;
Ex: SELECT UPPER(FirstName) FROM gdpr;

2) LOWER() - converts into lower case letters


Ex: SELECT LOWER(columnname) FROM tablename;
Ex: SELECT LOWER(FirstName) FROM gdpr;

3) LENGTH() - returns the length of the string


Ex: SELECT LENGTH(columnname);
Ex: SELECT LENGTH(FirstName);
Ex: SELECT * FROM tablename WHERE LENGTH(columnname)=X;
Ex: SELECT * FROM gdpr WHERE LENGTH(City)=4;

4) TRIM() - removes the specified characters from both sides


Ex: SELECT TRIMM(‘ORACLE’) FROM DUAL;
Ex: SELECT TRIMM(‘ORACLE’) FROM DUAL;
Ex: SELECT TRIMM(“x” FROM ‘xOraclex’)FROM DUAL;
Ex: SELECT TRIMM(“Z” FROM ‘ZOracleZ’)FROM DUAL;

5) NSTR() - returns the position of the character within a string


Ex: SELECT INSTR(‘Oracle’,’e’) - 6 (shows the character position in the string row)

6) SUBSTR()/SUBSTRING() - returns the sub string of the string


Ex: SELECT SUBSTR(‘Oracle’,2,3); - rac
Ex: SELECT SUBSTR(‘Oracle’,3,3); - acl
Ex: SELECT SUBSTR(‘Oracle’, 4,3); - cle
Ex: SELECT SUBSTRING(‘Oracle’,2,3); -rac
Ex: SELECT SUBSTRING(‘Oracle’,3,3); - acl
Ex: SELECT SUBSTRING(‘Oracle’,4,3); - cle
- first digit shows where you start the sub string and second digit shows the string
length
Ex: SELECT SUBSTR(columnname,1,3) FROM tablename;
Ex: SELECT SUBSTR(FirstName,1,3) FROM gdpr;
7) CONCAT() - used to join two strings
Ex: SELECT CONCAT(‘string1’,’string2’);
Ex: SELECT CONCAT(‘Oracle’,’Training’);
Ex: SELECT CONCAT(columnname1,columnname2) FROM tablename;
Ex: SELECT CONCAT(FirstName,LastName) FROM gdpr;

B) NUMERIC FUNCTIONS:

1) ABS, SQRT, MOD, POWER


Ex: SELECT ABS(-40); - shows the absurd value of the value = 40 (valoarea absoluta)
Ex: SELECT ABS(40); - shows the absurd value of the value = 40 (valoarea absoluta)
Ex: SELECT SQRT(25); - square root = 5 (radacina patrata)
Ex: SELECT MOD(10,3); - returns the reminder of a number divided by another
number (10:3=3(rest 1) = 1
Ex: SELECT POWER(2,5); - returns the value of 2 powered 5 times = 32

2) TRUNCATE() - truncates a number to the specified number o decimals places


Ex: SELECT TRUNCATE(40.1234,3); - 40.123
Ex: SELECT TRUNCATE(40.1234,2); - 40.12
Ex: SELECT TRUNCATE(6876,-1); - 6870
Ex: SELECT TRUNCATE(6876,-2); - 6800

3) GREATEST()/LEAST() - returns greatest , least values from the values provided


Ex: SELECT GREATEST(100,200,300); - 300
Ex: SELECT LEAST(100,200,300); - 100

C) DATE FUNCTIONS

1) CURDATE()/CURRENT_DATE() - returns the current date


Ex: SELECT CURDATE();
Ex: SELECT CURRENT_DATE();

2) CURTIME()/CURRENT_TIME() - returms the current time


Ex: SELECT CURTIME();
Ex: SELECT CURRENT_TIME();

3) NOW() - returns the current date and time


Ex: SELECT NOW();

4) SYSDATE() - returns the system current date and time


Ex: SELECT SYSDATE();
5) MONTH() - returns the month part from a given date(number from 1-12)
Ex: SELECT MONTH(“2019-05-19”); 5

6) YEAR() - returns the year part from a given date(number from 1000-9999)
Ex: SELECT YEAR(“2019-05-19”); 2019

7) DAY() - returns the day part from a given date(number from 1-31)
Ex: SELECT DAY(“2019-05-19”); 19

Ex: Display employees who are born in 2017


SELECT * FROM tablename WHERE YEAR(datecolumn)=”2017”

Ex: Display employees who are born in June


SELECT * FROM tablename WHERE MONTH(datecolumn)=”June”
Or
SELECT * FROM tablename WHERE MONTH(datecolumn)=”6”

D) AGGREGATE FUNCTIONS

1) AVG - returns the average


Ex: SELECT AVG(columnname) FROM tablename;
Ex: SELECT AVG(Salary) FROM gdpr;

2) SUM - calculates the total sum


Ex: SELECT SUM(columnname) FROM tablename;
Ex: SELECT SUM(Salary) FROM gdpr;

3) MIN - returns the minimum


Ex: SELECT MIN(columnname) FROM tablename;
Ex: SELECT MIN(Salary) FROM gdpr;

4) MAX - returns the maximum


Ex: SELECT MAX(columnname) FROM tablename;
Ex: SELECT MAX(Salary) FROM gdpr;

5) COUNT -
Ex: SELECT COUNT(*) FROM tablename;
Ex: SELECT COUNT(*) FROM gdpr;
 MySQL Clauses and Results

A) Group By Class
- records into summery rows
- returns one record for each group
- typically also involves aggregates: COUNT, MAX, SUM, AVG, etc.
- can group by one ore more columns

Ex: SELECT columnname1,SUM(columname2) FROM tablename GROUP BY


columname1;
Ex: SELECT Country,SUM(Salary) FROM gdpr GROUP BY Country;

Ex: SELECT columnname1,MIN(columname2) FROM tablename GROUP BY


columname1;
Ex: SELECT Country,MIN(Salary) FROM gdpr GROUP BY Country;

Ex: SELECT columnname1,MAX(columname2) FROM tablename GROUP BY


columname1;
Ex: SELECT Country,MAX(Salary) FROM gdpr GROUP BY Country;

Ex: SELECT columnname1,AVG(columname2) FROM tablename GROUP BY


columname1;
Ex: SELECT Country,AVG(Salary) FROM gdpr GROUP BY Country;

Ex: SELECT column, COUNT(*) FROM tablename GROUP BY columnname;


Ex: SELECT Country, COUNT(*) FROM gdpr GROUP BY Country;

Multiple conditions:

Ex: SELECT column1,column2,COUNT(*) FROM tablename GROUP BY


column1,column2
Ex: SELECT Country,City,COUNT(*) FROM gdpr GROUP BY Country,City

Ex: SELECT columnname1,columnname2,SUM(columname2) FROM tablename


GROUP BY columname1,columnname2;
Ex: SELECT Country,City,SUM(Salary) FROM gdpr GROUP BY Country, City;

B) HAVING Clause:
Ex: SELECT column1, SUM(column2) FROM tablename GROUP BY column1 HAVING
SUM(column2)>4000;
Ex: SELECT Country, SUM(Salary) FROM gdpr GROUP BY Country HAVING
SUM(Salary)>10000;
C) WHERE Clause:
Ex: SELECT column1, SUM(column2) FROM tablename WHERE column1<>Germany
GROUP BY column1;
Ex: SELECT Country, SUM(Salary) FROM gdpr WHERE Country<>Germany GROUP BY
Country;

B) +C)
Ex: SELECT Country, SUM(Salary) FROM gdpr WHERE Country<>Germany GROUP BY
Country HAVING SUM(Salary)>5000;

C) Order By Clause
- used to sort results

Ex: SELECT * FROM tablename ORDER BY columnname DESC/ASC;


Ex: SELECT * FROM gdpr ORDER BY Salary DESC/ASC;

Conclusion: WHERE ---> GROUP BY ---> HAVING ---> ORDER BY


Ex: SELECT columnname --- FROM tablename --- WHERE condition --- GROUP BY
columnname --- HAVING condition --- ORDER BY columnname
Ex: SELECT Country,SUM(Salary) FROM gdpr WHERE Country<>’Germany’ GROUP BY
Country HAVING SUM(Salary)>10000 ORDER BY SUM(Salary)DESC;

 My SQL Set Operators


- used to join and extract data from multiple tables

1) Union - it extracts the data from the multiple tables (without duplicate data)
2) Union All - it extracts the data from the multiple tables (with duplicate data)
3) Intersect - it extract only the common data from the multiple tables
4) Minus - it extract only the different data from the multiple tables

Union & Union All Conditions:


- each SELECT statement within UNION must have similar data type
- each SELECT statement should have the same number of columns
- the same order of the SELECT statements

Ex: SELECT column1 FROM table1 UNION SELECT column1 FROM table2
Ex: SELECT column1,column2 FROM table1 UNION SELECT column1,column2 FROM
table2
Ex: SELECT column1 FROM table1 UNION ALL SELECT column1 FROM table2
Ex: SELECT column1,column2 FROM table1 UNION ALL SELECT column1,column2
FROM table2
 My SQL Joins
- joins help retrieving data from two or more data tables
- the tables are mutually related using primary and foreign keys
- the tables should have a common column
- types of joins:
1. Inner Join - what is common in tables (match records)
2. Right Join - match records and unmatched record from left table
3. Left Join - match records and unmatched records from right table
4. Full Join - match and unmatched records from the two tables
5. Self Join - if you want to joint a table with the same table

1) Inner Join
Ex: SELECT * FROM tabel1 INNER JOIN table2 ON table1.column=table2.column;
Ex: SELECT * FROM tab1 INNER JOIN tab2 ON tab1.numid=tab2.numid;

2) Right Join
Ex: SELECT * FROM tabel1 RIGHT JOIN table2 ON table1.column=table2.column;
Ex: SELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.numid=tab2.numid;

3) Left Join
Ex: SELECT * FROM tabel1 LEFT JOIN table2 ON table1.column=table2.column;
Ex: SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.numid=tab2.numid;

4) Full Join
Ex: SELECT * FROM tabel1 FULL JOIN table2 ON table1.column=table2.column;
Ex: SELECT * FROM tab1 FULL JOIN tab2 ON tab1.numid=tab2.numid;

5) Self Join
Ex: SELECT * FROM tabel1 SELF JOIN table2 WHERE table1.column=table2.column;
Ex: SELECT * FROM tab1 SELF JOIN tab2 WHERE tab1.numid=tab2.numid;
 Sub Queries
- queries within queries
- contains 2 parts:
- INNER QUERY
- OUTER QUERY
- the output of the INNER QUERY is the input of the OUTER QUERY
- there are 2 types of sub queries:
- singe row sub query: <=, >=, !=
- multi row sub query: IN, ANY, ALL

 Integrity Constrains
- used to specify rules for table data
- can be specified when the table is created with the CREAT TABLE statement. Or
after creating the table with ALTER TABLE statement.

SQL Constrains:

1) NOT NULL - Ensures that a column cannot have a NULL value


Ex: CREATE TABLE tablename(col1 int(3) NOT NULL,col2 varchar(20),col3 int(3));
Ex: CREATE TABLE gdpr(EmpID int(3) NOT NULL,FirstName varchar(20), BadgeNo
int(3));

2) UNIQUE
- ensures that all values in a column are different
- will not allow duplicate values
- can apply on both column and table level
- can accept multiple NULLS

Column level:
Ex: CREATE TABLE tablename(col1 int(3) UNIQUE,col2 varchar(20),col3 int(3));
Ex: CREATE TABLE gdpr(EmpID int(3) UNIQUE,FirstName varchar(20), BadgeNo
int(3));

Table level:
Ex: CREATE TABLE tablename(col1 int(3),col2 varchar(20),col3 int(3),UNIQUE(col1));
Ex: CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20), BadgeNo
int(3),UNIQUE(EmpID));
3) PRIMARY KEY
- NOT NULL + UNIQUE - uniquely identifies each row in a table
- will not allow duplicate values and also null values
- can apply on both column and table level

Column level:
Ex: CREATE TABLE tablename(col1 int(3) PRIMARY KEY,col2 varchar(20), col3 int(3));
Ex: CREATE TABLE gdpr(EmpID int(3) PRIMARY KEY,FirstName varchar(20), BadgeNo
int(3));

Table level:
Ex: CREATE TABLE tablename(col1 int(3),col2 varchar(20),col3 int(3),PRIMARY
KEY(col1));
Ex: CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20), BadgeNo
int(3),PRIMARY KEY(EmpID));

Composite key(only at Table Level):


- when PRIMARY KEY aplies to more columns of the same table
Ex: CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20), BadgeNo
int(3),PRIMARY KEY(EmpID,BadgeNo));

4) FOREIGN KEY
- uniquely identifies a row/record in another table
- is a field (or collection of fields) in one table that refers to the PRIMARY KEY from
another table
- the table containing the FOREIGN KEY is called the child table, and the table
containing the candidate key is called parent table

Ex:
//parent table:
CREATE TABLE table1(col1 int(3),col2 varchar(20),col3 int(3), PRIMARY KEY(col1));

// child table:
CREATE TABLE table2(col4 int(3), FOREIGN KEY(col1) REFERENCES table1(col1),col5
varchar(20));

Ex:
//parent table:
CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20),BadgeNO int(3), PRIMARY
KEY(EmpID));

// child table:
CREATE TABLE gdpr2(EmpID int(3), FOREIGN KEY(EmpID) REFERENCES
gdpr(EmpID),Address varchar(20));
On delete cascade:
- we cannot delete rows from parent table unless we delete corresponding row from
child table
- we can delete the rows from the parent table & corresponding child table row as
well (at the same time) by using the ON DELETE CASCADE option

Ex:
//parent table:
CREATE TABLE table1(col1 int(3),col2 varchar(20),col3 int(3), PRIMARY KEY(col1));

// child table:
CREATE TABLE table2(col4 int(3), FOREIGN KEY(col1) REFERENCES table1(col1) ON
DELETE CASCADE,col5 varchar(20));

Ex:
//parent table:
CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20),BadgeNO int(3), PRIMARY
KEY(EmpID));

// child table:
CREATE TABLE gdpr2(EmpID int(3), FOREIGN KEY(EmpID) REFERENCES gdpr(EmpID)
ON DELETE CASCADE,Address varchar(20));

5) CHECK - ensures that all values in a column satisfies a specific condition

Ex: CREATE TABLE tablename(col1 int(3),col2 varchar(20),col3 int(3) CHECK(col3


BETWEEN val1 AND val2));
Ex: CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20), BadgeNo int(3)
CHECK(BadgeNo BETWEEN 50 AND 100));

Ex: CREATE TABLE tablename(col1 int(3),col2 varchar(20) CHECK(col2


IN(Val1,Val2,Val3),col3 int(3));
Ex: CREATE TABLE gdpr(EmpID int(3),FirstName varchar(20) CHECK(FirstName
IN(SCHIOPU,PETRU,LECA), BadgeNo int(3));

6) DEFAULT
- sets a default value for a column when no value is specified
- the default value will be added to all new records if no other value is specified

Ex: CREATE TABLE tablename(col1 int(3),col2 varchar(20),col3 datetime DEFAULT


now());
Ex: CREATE TABLE gdpr(EmpID int(3) NOT NULL,FirstName varchar(20), OrderDate
datetime DEFAULT now());
 Auto Increment
- AUTO_INCREMENT is a function that operates on numeric data types
- it automatically generates sequential values every time that a record is inserted
into a table for a field defined as auto increment

Ex:
CREATE TABLE tablename
(col1 int(5)PRIMARI KEY AUTO_INCREMENT,
col2 varchar(20),
col3 int(3));
ALTER TABLE tablename AUTO_INCREMENT=value;

Ex:
CREATE TABLE gdpr
(BadgeNo int(5)PRIMARI KEY AUTO_INCREMENT,
Name varchar(20),
DepID int(3));
ALTER TABLE gdpr AUTO_INCREMENT=100;

INSERT INTO gdpr(Name, DepID) values (‘X’,60);


INSERT INTO gdpr(Name, DepID) values (‘X’,45);
INSERT INTO gdpr(Name, DepID) values (‘X’,150);

SELECT * FROM gdpr;


 Limiting Rows
- is used to display limited rows from a table

Ex: SELECT * FROM tablename LIMIT value;


Ex: SELECT * FROM gdpr LIMIT 10; - shows row number 5
Ex: SELECT * FROM gdpr LIMIT 5,10; - shows from ro5 to 10

 SQL Views & Index


- a view is a virtual table based on the result-set of the result-set of an SQL statement
- it contains rows and columns, just like a real table
- the fields in the view are the fields from one ore more real tables in the database
- you can add SQL functions + WHERE and JOIN statements and present data as if it
was from a single table

Ex:
SELECT * FROM tablename;
CREATE VIEW tablename_v1 AS SELECT col1,col2,col3 FROM tablename;
SELECT * FROM tablename_v1;

DROP VIEW tablename_v1;

Ex:
SELECT * FROM gdpr;
CREATE VIEW gdpr_v1 AS SELECT FirstName,LastName,City FROM gdpr;
SELECT * FROM gdpr_v1;

DROP VIEW gdpr_v1;


- indexes are used to retrieve data from the databases very fast
- the users cannot see the indexes, they are just used to speed up searches/queries

Creating Index:

CREATE INDEX idx_tablename ON tablename(col);

Dropping Index:

DROP INDEX idx_tablename ON tablename;

You might also like