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

8 SQL

SQL is a language for managing data in relational databases. Some key points: - Data is stored in tables which can be related to each other. SQL allows data to be retrieved and manipulated using commands like SELECT, INSERT, UPDATE and DELETE. - Tables have columns with different data types like text, numbers, dates. Columns together with a primary key make each row unique. - Relations between tables are one-to-one, one-to-many, or many-to-many. Joins are used to combine data from multiple tables. - Normalization reduces redundancy by splitting data across multiple tables in first normal form through third normal form or Boyce-Codd normal form.

Uploaded by

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

8 SQL

SQL is a language for managing data in relational databases. Some key points: - Data is stored in tables which can be related to each other. SQL allows data to be retrieved and manipulated using commands like SELECT, INSERT, UPDATE and DELETE. - Tables have columns with different data types like text, numbers, dates. Columns together with a primary key make each row unique. - Relations between tables are one-to-one, one-to-many, or many-to-many. Joins are used to combine data from multiple tables. - Normalization reduces redundancy by splitting data across multiple tables in first normal form through third normal form or Boyce-Codd normal form.

Uploaded by

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

SQL

SQL
Not case sensitive.

Semicolon is the standard way to separate each SQL statement.

Numeric fields should not be enclosed in quotes.

Single quotes / Double quotes.


COMMANDS
COMMANDS
Data Definition Language (DDL)

Data Manipulation Language (DML)

Data Control Language (DCL)

Transaction Control Language (TCL)


TABLE
(RDBM)
A relational
database stores
data in tables.
SQL & RDBM
SQL is the language that allows
retrieval and manipulation of
table data in a relational
database.
ERD
Entity Relationship Diagram (ERD) which shows
the tables and their relationships.
DATA TYPES
TEXT, INTEGER, VARCHAR, DATE
Each column a name a data type
DDL
(CREATE, DROP)
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

create
DROP TABLE table_name;
remove
DML
(SELECT, INSERT, UPDATE, DELETE)
SELECT column1, column2, ... FROM table_name;

SELECT * FROM table_name;

SELECT * FROM table_name WHERE condition;

retrieves
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

add
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

updates
DELETE FROM table_name WHERE condition;

DELETE FROM table_name;

removes
WHERE
WHERE condition;
true false

SELECT UPDATE DELETE


OPERATORS
=, >, <, >=, <=, !=
= > < >= <= <> !=
SELECT column1, column2, ...
FROM table_name
WHERE column = ‘value’;
/* =, >, <, >=, <=, != */
AND, OR, NOT
AND OR NOT
SELECT * FROM table_name WHERE condition1 AND condition2;

SELECT * FROM table_name WHERE condition1 OR condition2;

SELECT * FROM table_name WHERE NOT condition;


BETWEEN, LIKE, IN
BETWEEN LIKE IN
SELECT * FROM table_name
WHERE column_name BETWEEN value1 AND value2;

SELECT * FROM table_name


WHERE column_name LIKE pattern; /* ‘a%’, ‘%a’ */

SELECT * FROM table_name


WHERE column_name IN (value1, value2, ...);
IS NULL, IS NOT NULL
no value
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
FUNCTIONS
(AGGREGATE)
MIN(), MAX(), COUNT(), AVG(), SUM()

calculation single scalar value


SELECT MIN(column_name) FROM table_name WHERE condition;

SELECT MAX(column_name) FROM table_name WHERE condition;

SELECT COUNT(column_name) FROM table_name WHERE condition;

SELECT AVG(column_name) FROM table_name WHERE condition;

SELECT SUM(column_name) FROM table_name WHERE condition;


GROUP BY
(STATEMENT)
SELECT column_name(s) FROM table_name
GROUP BY column_name(s);

same values rows

aggregate functions COUNT MAX MIN SUM AVG


HAVING
(CLAUSE)
SELECT column_name(s) FROM table_name
GROUP BY column_name(s)
HAVING condition;

WHERE could not be used aggregate functions


ORDER BY
(SORT)
SELECT column1, column2, … FROM table_name
ORDER BY column_name ASC | DESC;

sort ascending descending order


ALIASES
SELECT column_name AS alias_name
FROM table_name AS alias_name;

table column name temporary name


CONSTRAINTS
CONSTRAINTS
NULL

not be repeated

uniquely

contain UNIQUE cannot contain NULL

refers another table

no value
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
KEYS
key uniquely

more than one possible key


CANDIDATE KEYS
candidate key

minimal
PRIMARY KEY
primary key

quick retrieval

STUDENT_ID
RELATIONSHIPS
(DATABASE DESIGN)
RELATIONSHIPS
One-To-One Relationship

One-To-Many Relationship

Many-To-Many Relationship
ONE-TO-ONE
between two tables
only one matching row

Primary Key-Unique Foreign Key


ONE-TO-MANY
between two tables
multiple matching rows another table

Primary Key-Foreign Key


MANY-TO-MANY
multiple records multiple records in another table

don’t allow two tables

two one-to-many relationships join table

includes primary keys

foreign keys
JOINS
SQL JOINS
(INNER) JOIN

LEFT (OUTER) JOIN

RIGHT (OUTER) JOIN

FULL (OUTER) JOIN


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

INNER JOIN matching values both tables.


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

LEFT JOIN first (left-most) matching second (right-most)


SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

RIGHT JOIN second (right-most) matching first (left-most)


SELECT column_name(s)
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;

FULL JOIN all matching records


NORMALIZATION
DATABASE NORMAL FORMS
1NF (First Normal Form)

2NF (Second Normal Form)

3NF (Third Normal Form)

BCNF (Boyce-Codd Normal Form)


reduces data redundancy

stored one table


1NF (First Normal Form) Rules

single value unique


2NF (Second Normal Form) Rules

Rule 1 - Rule 2 -
3NF (Third Normal Form) Rules

Rule 1 - Rule 2 -
BCNF (Boyce-Codd Normal Form)
Candidate Key

3.5 Normal Form

You might also like