4. Database
• A collection of systematic data.
• Data
• Name, Age, Height, Weight
• Record
• a table row corresponds to a unit of data
• https://www.youtube.com/watch?v=FR4QIeZaPeM
5. Database
• Powerful
• Able to search, filter combine data from many sources
• Fast
• Able to search/filter a database very quickly compared to a file
• Big
• Scale well up to very large data sizes
• Safe
• Built-in mechanisms for failure recovery (transactions)
• Multi-user
• Concurrency features let many use view/ edit data at same
time
6. Database
• Oracle
• Microsoft
• SQL Server
• Access
• MySQL
• Simple free open-source database system
• SQLite
• Transportable, lightweight free open source database system
7. SQL
• Structured Query Language (SQL):
• SQL is used to communicate with a database
• SQL statements are used to perform tasks such as update
data on a database, or retrieve data from a database
• Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc.
Although most database systems use SQL, most of them
also have their own additional proprietary extensions that
are usually only used on their system.
• However, the standard SQL commands such as "Select",
"Insert", "Update", "Delete", "Create", and "Drop" can be
used to accomplish almost everything that one needs to do
with a database.
8. The SQL CREATE TABLE
Statement
• Each value stored in an SQLite database (or manipulated by
the database engine) has one of the following storage
classes:
• NULL. The value is a NULL value. (Avoid It)
• INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or
8 bytes depending on the magnitude of the value.
• REAL. The value is a floating point value, stored as an 8-byte
IEEE floating point number.
• TEXT. The value is a text string, stored using the database
encoding (UTF-8, UTF-16BE or UTF-16LE).
• BLOB. The value is a blob of data, stored exactly as it was input.
• A BLOB is a Binary Large Object. It is used to store large
quantities of binary data in a database.
• You can use it to store any kind of binary data that you want,
includes images, video, or any other kind of binary data that
you wish to store.
9. Boolean Datatype
• SQLite does not have a separate Boolean storage class.
• Instead, Boolean values are stored as integers 0 (false)
and 1 (true).
12. The SQL CREATE TABLE
Statement
• CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
• How to create the table below:
matriNo Name phoneNumber
13. The SQL SELECT Statement
• SELECT column_name,column_name
FROM table_name;
14. The SQL SELECT Statement
• SELECT CustomerName, City FROM Customers;
• If want to select the Customer Name, Address and Postal
Code records, how?
15. The SQL SELECT Statement
• SELECT _________ FROM _____________;
• If want to select the Customer Name, Address and City
records, how?
16. The SQL SELECT DISTINCT
Statement
• In a table, a column may contain many duplicate values;
• and sometimes you only want to list the different (distinct)
values.
17. The SQL SELECT DISTINCT
Statement
• SELECT DISTINCT column_name,column_name
FROM table_name;
• SELECT DISTINCT City FROM Customers;
• If we want to know what country customers are come from.
How?
18. The SQL SELECT DISTINCT
Statement
• SELECT DISTINCT column_name,column_name
FROM table_name;
• SELECT DISTINCT ________FROM ________;
• If we want to know what country customers are come from.
How?
19. The SQL WHERE Clause
• SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be
written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
20. The SQL WHERE Clause
• SELECT * FROM Customers
WHERE Country='Mexico';
• SELECT * FROM Customers
WHERE CustomerID=1;
21. The SQL WHERE Clause
• SELECT __________
FROM ____________
WHERE ______________;
• If we want the information about the customers who come
from London?
22. The SQL AND & OR Operators
• The AND operator displays a record if both the first
condition AND the second condition are true.
• The OR operator displays a record if either the first
condition OR the second condition is true.
• SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
23. The SQL AND & OR Operators
• SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
24. The SQL AND & OR Operators
• SELECT * FROM Customers
WHERE City='Berlin'
OR City='München';
25. The SQL AND & OR Operators
• SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');
26. The SQL AND & OR Operators
• If we want the information about the customers who come
from London or Paris
• SELECT * FROM ______
WHERE _________
27. SQL ORDER BY Keyword
• The ORDER BY keyword is used to sort the result-set by
one or more columns.
• The ORDER BY keyword sorts the records in ascending
order by default. To sort the records in a descending order,
you can use the DESC keyword.
• SELECT column_name, column_name
FROM table_name
ORDER BY
column_name ASC|DESC, column_name ASC|DESC;
28. SQL ORDER BY Keyword
• SELECT * FROM Customers
ORDER BY Country;
29. SQL ORDER BY Keyword
• SELECT * FROM Customers
ORDER BY Country DESC;
30. SQL ORDER BY Keyword
• SELECT * FROM Customers
ORDER BY Country, CustomerName;
31. SQL INSERT INTO Statement
• The INSERT INTO statement is used to insert new records
in a table.
• It is possible to write the INSERT INTO statement in two
forms.
• The first form does not specify the column names where
the data will be inserted, only their values:
• INSERT INTO table_name
VALUES (value1,value2,value3,...);
• The second form specifies both the column names and the
values to be inserted:
• INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
32. SQL INSERT INTO Statement
• INSERT INTO Customers (CustomerName, ContactName,
Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway');
33. SQL INSERT INTO Statement
• It is also possible to only insert data in specific columns.
• INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
34. SQL INSERT INTO Statement
• How to insert your own data into the “Customers” table
• Name, Ali
• City, KL
• Country, Malaysia
35. SQL UPDATE Statement
• The UPDATE statement is used to update records in a
table.
• UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
36. SQL UPDATE Statement
• UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
37. Update Warning!
• Be careful when updating records. If we had omitted the
WHERE clause, in the example above, like this:
• UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg';
38. SQL DELETE Statement
• The DELETE statement is used to delete records in a
table.
• DELETE FROM table_name
WHERE some_column=some_value;
• DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND
ContactName='Maria Anders';
39. SQL DELETE Statement
• Delete All Data
• DELETE FROM table_name;
or
DELETE * FROM table_name;