Sei sulla pagina 1di 46

Chapter 7

Basic SQL- Structured Query


Language

1
Learning Objectives:
 Understand basic commands and functions of
SQL
 Understand the use of SQL for data
administration (to create tables and indexes)
 Understand the use of SQL for data
manipulation (to add, modify, delete, and
retrieve data)
 Understand the use of SQL to query a database
for useful information
2
Introduction to SQL
 What SQL can 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 database
 SQL can delete records in 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 database
 SQL can set permissions on tables, procedures 3and
views
SQL statement

SQL IS NOT CASE SENSITIVE


LANGUAGE

4
Data Definition Language (DDL)
 DDL manages table and index structure.
 The most basic items of DDL:
 CREATE – create an object (i.e. table) in the
database
 DROP – deletes an object in the database, usually
irretrievably
 ALTER – modifies the structure of an existing object
in various ways, for example, adding a column to an
existing table

5
Data Manipulation Language
(DML)
 DML is the subset of SQL used to add, update and delete
data.
 The acronym CRUD refers to all of the major functions that
need to be implemented in a relational database application
to consider it complete.
 Each letter in the acronym can be mapped to a standard SQL
statement:

6
7
(1) CREATE TABLE statement

 Data types for numbers, text dates, etc.


 Examples:
 Numbers: int, float
 Text/strings: varchar(X) – where X is the length of the string
 Dates: datetime
8
 Etc.
9
SQL Constraints
 Constraints are used to limit the type of data that can go into a
table.
 Constraints can be specified when a table is created (with the
CREATE TABLE statement) or after the table is created (with the
ALTER TABLE statement).
 Here are the most important constraints:
 PRIMARY KEY – a column contain unique value
 NOT NULL – enforces a column to NOT accept NULL values
 UNIQUE – uniquely identifies each record in a database table (can
have many UNIQUE constraints per table, but only one PRIMARY KEY)
 FOREIGN KEY
 CHECK
 DEFAULT
 IDENTITY / AUTO INCREMENT– to use running numbers, like 1,2,3,4,…
i.e. IDENTITY (1,1) means the first value will be 1 and then increasing
10

by 1
Foreign key

11
12
13
(2) ALTER TABLE statement

 This is used to add, delete or modify columns in an existing


table.

14
(3) INSERT INTO statement
 Is to insert new row in a table
 Can be written in 2 ways:
 Method (1) does not specify the column names where the data will be
inserted, only their values:

15
(3) INSERT INTO statement…
cont
 Method (2)

16
(4) UPDATE statement
 The UPDATE statement is used to update existing records in a
table.

17
(5) DELETE statement

18
DELETE statement

19
(6) SELECT statement
 This statement is used for retrieving rows from the
database and enables the selection of one or many rows
or columns from one or many table in the database
 This is probably the most used SQL command.

 Example 1 – to display all the columns in a customer


table:
 Select * from customer
 Example 2 – to display few columns, may specify the
names of the columns that want to retrieve
 Select customerID, LastName, FirstName from CUSTOMER

20
SELECT statement

21
(7) ORDER BY keyword

22
ORDER BY … desc keyword

23
(8) SELECT DISTINCT statement
 In a table, some of the columns may contain duplicate
values.
 The DISTINCT keyword can be used to return only distinct
values.

24
(9) WHERE clause

25
Operators

26
Like operator (1)

27
Like operator (2)

28
Example:

select employee_id, manager_id, department_id


from employees
where last_name IN (‘Alvin’, ‘Chris’)

29
Example:

select last_name, salary


from employees
where salary BETWEEN 2500 AND 3500
30
Wildcards

 SQL wildcards can substitute for one or more


characters when searching for data in a database.
 SQL wildcards must be used with the SQL LIKE
operator.

31
Wildcards - examples

32
AND & OR operators

33
Combination used of AND & OR
operators

34
SELECT TOP clause
 The TOP clause is used to specify the number of records to
return.
 The TOP clause can be very useful on large tables with
thousands of records. Returning a large number of records
can impact on performance

35
Alias – you can give a table or a column
another name by using alias.

 Example 1:
Select last_name, first_name, phone
From employees
AS contactList
 Example 2:
Select first_name, Last_name AS Name
36
From employees
JOINS
 SQL joins are used to query data from one or more tables,
based on a relationship between certain columns in these
tables.
 The JOIN is used to join the primary key in one table with
the foreign key in another table
 Different SQL joins:
 JOIN: Return rows when there is at least one match in both
tables
 LEFT JOIN: Return all rows from the left table, even if there are
no matches in the right table
 RIGHT JOIN: Return all rows from the right table, even if there
are no matches in the left table
 FULL JOIN: Return rows when there is a match in one of the
tables
37
38
JOIN

39
40
41
42
43
44
45
EXERCISE…

46

Potrebbero piacerti anche