0% found this document useful (0 votes)
3 views

SQL commands

Uploaded by

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

SQL commands

Uploaded by

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

SQL Basics

Tuesday, 23 March 2021 7:08 PM

A language used in relational databases. DBMS and SQL supports CRUD - Create, Read,
Update, Delete commands.

Notes:
• SQL keywords are case insensitive
• Mathematics can be done in SQL

Language
SQL provides the following capabilities:
Data Definition Language Define and setup database CREATE, ALTER, DROP
(DDL)
Data Manipulation Maintain and use database SELECT, INSERT, DELETE,
Language (DML) UPDATE
Data Control Language Control access to database GRANT, REVOKE
(DCL)
Other commands Administer database and N/A
transaction control

DDL Commands
CREATE TABLE entity_name (
Attribute type,
...
) ENGINE=InnoDB;

auto_increment Lets the database maintain the value - used as an automatic


unique identifier
NOT NULL Ensures value is non-empty
PRIMARY KEY (keys) Declares primary keys
FOREIGN KEY (keys) Declares foreign keys
REFERENCES table(identifier) • Disallows deleting a parent row if child row exists that
ON DELETE RESTRICT references value for that parent row
ON UPDATE CASCADE,
• If parent primary key changed, child value will also
change

e.g.
CREATE TABLE Account (
AccountID smallint auto_increment,
AccountName varchar(100) NOT NULL,
OutstandingBalance DECIMAL(10,2) NOT NULL,
CustomerID smallint NOT NULL,
PRIMARY KEY (AccountID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
ON DELETE RESTRICT
ON UPDATE CASCADE
);

DML Commands - INSERT


INSERT INTO entity_name VALUES

Relational Algebra and SQL Page 14


INSERT INTO entity_name VALUES
(values),
(values),
(values);

Records can also be inserted from an existing table (see next section)
INSERT INTO entity_name
SELECT * FROM entity_name;

(columns) Specifies which columns will be entered


NULL Leave as empty or unknown
DEFAULT Leave as default

e.g.
INSERT INTO Customer
(CustFirstName, CustLastName, CustType)
VALUES ("Peter", "Smith", 'Personal');

INSERT INTO Customer


VALUES (DEFAULT, "James", NULL, "Jones", "JJ Enterprises",
'Company');

CustID CustomerFirstNam CustMiddleNam CustLastNam BusinessNam CustType


e e e e
1 Peter NULL Smith NULL Personal
2 James NULL Jones JJ Enterprises Compan
y

Relational Algebra and SQL Page 15


DML Basics
Tuesday, 23 March 2021 9:13 PM

All DML statements should follow an order:


1. SELECT
2. FROM
3. WHERE
4. GROUP BY
5. HAVING
6. ORDER BY
7. LIMIT
SELECT [all/attribute] select_expr [select_expr]

➢ Columns can be renamed using the AS clause


e.g. to rename a column from Count(CustomerID) to Count)
SELECT CustType, Count(CustomerID) AS Count
FROM Customer
GROUP BY CustType;
➢ Remove duplicates using SELECT DISTINCT

FROM entity

➢ SELECT and FROM denotes projection.


○ Use * to denote 'all'

e.g. :
SELECT CustLastName FROM Customer;

WHERE where_condition

➢ WHERE denotes selection.


e.g. :
WHERE cond1 AND cond2 OR cond3;

e.g. :
SELECT CustLastName FROM Customer
WHERE CustLastName = "Smith";

➢ LIKE can be used as a condition to pattern match with a regular expression


○ % denotes zero, one, or multiple characters
○ _ denotes a single character
e.g.
WHERE CustomerName Finds any values that start with "a"
LIKE 'a%'
WHERE CustomerName Finds any values that have "or" in any position
LIKE '%or%'
WHERE CustomerName Finds any values that start with "a" and are at least
LIKE 'a_%_%'
three characters in length

Aggregate functions
Aggregate functions operate on the set of values in a column of a relation and return a
single value e.g. CONCAT(), AVG(), COUNT(), MIN(), SUM(), MAX() etc.

Relational Algebra and SQL Page 16


single value e.g. CONCAT(), AVG(), COUNT(), MIN(), SUM(), MAX() etc.

e.g. Finding how many customers are in the database:


SELECT COUNT(CustomerID) FROM Customer;

GROUP BY attribute [ASX/DESC]

➢ Often used with aggregate functions


e.g.
SELECT AVG(OutstandingBalance) Calculates average balance of all
FROM Account;
accounts
SELECT AVG(OutstandingBalance) Calculates average balance of accounts
FROM Account
WHERE CustomerID=1; of customer 1.
SELECT AVG(OutstandingBalance) Calculates average balance per
FROM Account
GROUP BY CustomerID; customer.

➢ Groups all records together over a set of attributes

HAVING where_condition

➢ Indicates condition - added as the 'WHERE' keyword cannot be used with aggregate
functions
e.g. List the number of customers of each country only including countries with
more than 5 customers.
SELECT COUNT(CustomerID), CountryName
FROM Customers
GROUP BY CountryName
HAVING COUNT(CustomerID)>5;

ORDER BY attribute [ASC/DESC]

➢ Orders records by particular columns


○ ASC is the default close
e.g. Order a database with CustLastName in descending order.
SELECT CustLastName, CustType
FROM Customer
ORDER BY CustLastName DESC;

LIMIT row_count OFFSET offset

➢ LIMIT limits the output size and OFFSET skips a number of records

Relational Algebra and SQL Page 17


Joins and Set Operations
Tuesday, 23 March 2021 10:08 PM

Cross product SELECT * FROM Rel1, Rel2;

Inner/Equi join SELECT * FROM Rel1 INNER JOIN Rel2


ON condition
Natural join SELECT * FROM Rel1 NATURAL JOIN Rel2;

Left outer join SELECT * FROM Rel1 LEFT OUTER JOIN Rel2
ON condition
Right outer join SELECT * FROM Rel1 RIGHT OUTER JOIN Rel2
ON condition
Full outer join SELECT * FROM Rel1 FULL OUTER JOIN Rel2
ON condition

UNION Shows all rows returned from queries.


INTERSECT Shows only rows that are common in queries (unsupported by
MySQL).
[UNION/INTERSECT] If duplicate rows are wanted.
ALL

Relational Algebra and SQL Page 18


Query Nesting and Subqueries
Tuesday, 30 March 2021 4:54 PM

SQL provides abilities to nest subqueries, where another select query is commonly used to
perform set tests.

Sub-query comparison operators


IN/NOT Used to test whether the List the BuyerID, Name and Phone number for all
IN attribute is in/not in the bidders on artefact 1:
subquery list. SELECT * FROM Buyer
WHERE BuyerID IN
(SELECT BuyerID FROM Offer WHERE
ArtefactID = 1)
BuyerID Name Phone
1 Maggie 0333333333
2 Nicole 0444444444

Which Artefacts don’t have offers made on them?


SELECT * FROM Artefact
WHERE ID NOT IN
(SELECT ArtefactID FROM Offer);
ID Name Description
3 Pot Old Pot

ANY Must satisfy at least one WHERE sal > ANY(200, 300, 400);
of the inner conditions. =
WHERE sal>200 OR sal>300 OR sal>400;
ALL Must satisfy all inner WHERE sal > ALL(200, 300, 400);
conditions. =
WHERE sal>200 AND sal>300 AND sal>400;
EXISTS Inner query returns at SELECT * FROM Buyer
WHERE EXISTS (SELECT * FROM Offer WHERE
least one record. Buyer.BuyerID = Offer.BuyerID AND
ArtefactID = 1)

BuyerID Name Phone


1 Maggie 0333333333
2 Nicole 0444444444

IN alternatives
There is often a more efficient alternate method for IN operations.

e.g. List the BuyerID, Name and Phone number for all bidders on artefact 1.
SELECT * FROM Buyer
WHERE BuyerID IN
(SELECT BuyerID FROM Offer WHERE ArtefactID = 1)

SELECT BuyerID, Name, Phone


FROM Buyer NATURAL JOIN Offer
WHERE ArtefactID = 1

Relational Algebra and SQL Page 19


Using examples:

Offer
SellerID ArtefactID BuyerID Date
1 1 1 2012-06-20
1 1 2 2012-06-20
2 2 1 2012-06-20
2 2 2 2012-06-20

Buyer
BuyerID Name Phone
1 Maggie 0333333333
2 Nicole 0444444444
3 Oleg 0555555555

Artefact
ID Name Description
1 Vase Old Vase
2 Knife Old Knife
3 Pot Old Pot

Relational Algebra and SQL Page 20


Update, Delete, Replace and Views
Tuesday, 30 March 2021 5:33 PM

UPDATE entity
SET attribute = new_attribute
WHERE condition;

➢ Changes existing data in tables


○ Order of statements is important
○ Specifying a WHERE clause is used to operate on specific sections of the whole
table

e.g. Increase salaries less than $100,000 by 10%.


UPDATE Salared
SET AnnualSalary = AnnualSalary * 1.10
WHERE AnnualSalary < 100000;

➢ The CASE command is helpful for multiple conditions


○ Akin to if/else

e.g. If salary is lower than $100,000 increase by 5% and otherwise increase by


10%.
UPDATE Salaried
SET AnnualSalary =
CASE
WHEN AnnualSalary <= 100000
THEN AnnualSalary * 1.05
ELSE AnnualSalary * 1.10
END;

is better than:
UPDATE Salared
SET AnnualSalary = AnnualSalary * 1.10
WHERE AnnualSalary > 100000;
UPDATE Salared
SET AnnualSalary = AnnualSalary * 1.05
WHERE AnnualSalary <= 100000;

REPLACE

➢ Works identically as INSERT except if an old row in a table has a key value the same as
the new row, then it is overwritten

DELETE FROM entity WHERE condition;

➢ Deletes all records unless specified with a condition

e.g. Delete Grace's records.


DELETE FROM Employee
WHERE Name = "Grace";

CREATE VIEW view_name AS statement

➢ Any relation not in physical models but made available to the user as a virtual relation
○ Helps hide query complexity and data from users
○ Once a view is defined its definition is stored in the database and can be used
like any other table

Relational Algebra and SQL Page 21


DDL and DCL Commands
Tuesday, 30 March 2021 6:00 PM

More DDL commands


ALTER Allows to add or remove attributes to/from ALTER TABLE table ADD
attribute_name type
a relation.
ALTER TABLE table DROP
attribute_name
RENAME Allows renaming of tables. RENAME TABLE old_name TO
new_name
TRUNCAT Deletes all data from a table, faster than TRUNCATE TABLE table
E DELETE command but cannot be rolled back.
DROP Removes all data and relation tied to a DROP TABLE table
table.

DCL and other commands


• Users and permissions
○ CREATE USER, DROP USER
○ GRANT, REVOKE
○ SET PASSWORD
• Database administration
○ BACKUP TABLE, RESTORE TABLE
○ ANALYZE TABLE
• Miscellaneous
○ DESCRIBE table
○ USE database

Relational Algebra and SQL Page 22


File Organisation
Tuesday, 30 March 2021 7:05 PM

DBMS stores information of disks (normally hard disks) and involves many read and write
operations when data is accessed - high cost. So storage and indexing is needed.

Terminology
Conceptual modelling Entity Attribute Instance of an entity
Logical modelling Relation Attribute Tuple
Physical modelling/SQL Table Column/field Row
Disk storage File Field Record

A page is an allocation of space on desk or in memory containing a collection of records -


each page is of the same size.

A file is a collection of pages, each containing a collection of records.

File organisation types

Heap (unordered) files


Simplest file structure, contains records in no particular order.
• As file grows and shrinks, disk pages are allocated and de-allocated
• No ordering, sequencing or indexing
• Quick insert and retrieving all records but slow search due to inserting in just empty
space

Sorted files
Similar structure as heap files however pages and records are ordered.
• Sequential order based on the search key (by columns)
• Quick search (especially on a range) but slow insert due to needing to reshuffle
records
• Can be good for range search with less than <

Index file organisations


Any subset of the fields of a table is indexed based on queries that are frequently run
against the database.
• Quick search on subset attributes
• Different index and types of index can be chosen
• Quick search but insert depends on type - slower than heap file organisation due to
need of maintenance

Cost of storage
Data is typically stored in pages on hard disks, to be able to process an analyse it data needs
to be brought to the RAM.
• Access to hard disks are much slower than access to memory
• I/O cost dominates overall cost and accounts for much more than CPU - so
measurements for access to memory is negligible

For all operations, DBMS models the cost - the number of pages (or disk I/O operations - to
bring data from disk to memory).
Storage^J Indexing and Query Processing Page 23
bring data from disk to memory).
• e.g. a table of 100 records with each page storing 10 records, the cost of accessing the
entire file is 10 I/O (or 10 pages)

Storage^J Indexing and Query Processing Page 24


Indexes
Tuesday, 30 March 2021 7:16 PM

Indexes are made up of data entries which refer back to the data in the relation.
• Speeds up selection on the search key fields
• Stored in an index file, in contrast to data file which contains actual records
• Data file pages are not necessarily organised in the same manner as Index Pages

Data entries refer back to the data in the relation:

k: search key, rid: record ID in format (page no., record no.)

A data structure built on over specific fields called search key fields.
• Built on top of data files
• Speeds up selections on the search key fields
• Any subset of the fields of a relation can be the search key for an index

Index classification
Clustering Clustered - data records in data file have
same order as data entries (sorted)

Unclustered - data records in the data file


are not sorted by search key/data entries.

Properties
• There is only one search key
combination of a clustered index
• Clustered indexes more efficient for
range and equality queries but
expensive to maintain
• Clustered indexes can only be applied
to table that uses sorted file
organisation
Primary vs Primary - records are retrieved based on the N/A
secondary value of the primary key.
index
Secondary - any other index that isn't the
primary key, often fields that are frequently
queried.

Properties
• Primary index never contains
duplicates whereas secondary index
may contain duplicates
Composite An index built over a combination of search

Storage^J Indexing and Query Processing Page 25


Composite An index built over a combination of search
search keys keys e.g. index on <age, salary>

Hash-based Represents index as a collection of buckets. e.g. Suppose you are given 5 buckets and
index A hash function maps the search key to the .
corresponding bucket. Bucket Key
• Better for equality selections
• Can't perform range selections 0 200
1
2 22
3 8, 33
4 119

Tree-base Sorting data on search key and maintaining


index a hierarchical search data structure (B+
tree) that will direct the search to the
respective page.
• Maintenance is costly as tree is
updated with every insertion or
deletion
• Better for range selections but still
good with equality

Choosing an index considerations


• Which relations are accessed frequently?
• Which attributes are retrieved?
• Which attribute are involved in selection, join and other conditions?
• If a query involves updating the relation, what attributes are affected?

Storage^J Indexing and Query Processing Page 26

You might also like