CH 3 SQL
CH 3 SQL
CH 3 SQL
SQL
1 06/07/2023
What is SQL?
2 06/07/2023
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
3 06/07/2023
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database
systems such as MS SQL Server, IBM DB2, Oracle, MySQL,
and Microsoft Access.
The data in RDBMS is stored in database objects called
tables.
A table is a collection of related data entries and it consists of
columns and rows.
4 06/07/2023
SQL Commands
5 06/07/2023
Data Definition Language (DDL)
6 06/07/2023
Data Manipulation Language (DML)
7 06/07/2023
Data Control Language (DCL)
8 06/07/2023
SQL Data Types for Various DBs
Text AutoNumber
Memo Date/Time
Integer Yes/No
Single Ole Object
Double Lookup Wizard
Currency Hyperlink
Byte
9 06/07/2023
SQL Syntax
Database Tables
A database most often contains one or more tables.
Each table is identified by a name (e.g. "Customers"
or "Orders"). Tables contain records (rows) with
data.
Sample database (included in MS Access and MS
SQL Server).
Below is a selection from the "Customers" table:
10 06/07/2023
Customers table
Customer Customer Contact Address city Postal Country
ID Name Name Code
11 06/07/2023
cont’d…
12 06/07/2023
SQL Statements
13 06/07/2023
Semicolon after SQL Statements?
15 06/07/2023
SQL SELECT Statement
and
SELECT * FROM table_name;
16 06/07/2023
SELECT Column Example
Example:
17 06/07/2023
SELECT * Example
Example:
18 06/07/2023
SQL SELECT DISTINCT statement
19 06/07/2023
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name
FROM table_name;
20 06/07/2023
SQL WHERE Clause
21 06/07/2023
WHERE Clause Example
22 06/07/2023
SQL AND & OR Operators
23 06/07/2023
AND Operator Example
Example:
24 06/07/2023
OR Operator Example
Example:
25 06/07/2023
Combining AND & OR
complex expressions).
The following SQL statement selects all customers from the
country "Germany" AND the city must be equal to "Berlin" OR
"München", in the "Customers" table:
Example:
26 06/07/2023
SQL CREATE TABLE Statement
27 06/07/2023
Cont’d…
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
28 06/07/2023
Cont’d…
29 06/07/2023
SQL CREATE TABLE Example
31 06/07/2023
cont’d…
33 06/07/2023
The SQL UPDATE Statement
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
34 06/07/2023
SQL UPDATE Example
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
35 06/07/2023
SQL DELETE Statement
DELETE FROM table_name
WHERE some_column=some_value;
36 06/07/2023
SQL DELETE Example
37 06/07/2023
Delete All Data
DELETE FROM table_name;
or
DELETE * FROM table_name;
38 06/07/2023
SQL LIKE Operator
SELECT column name(s)
FROM table_name
WHERE column name LIKE pattern;
39 06/07/2023
SQL LIKE Operator Examples
Example
40 06/07/2023
cont’d…
Example
41 06/07/2023
The SQL BETWEEN Operator
SELECT column name(s)
FROM table_name
WHERE column name BETWEEN value1 AND value2;
42 06/07/2023
BETWEEN Operator Example
Example
43 06/07/2023
BETWEEN Operator with Text Value
Example
44 06/07/2023