Anda di halaman 1dari 11

INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Introduction to SQL

If you want to work with databases, you must learn to speak their language. Databases speak
STRUCTURED QUERY LANGUAGE, better known as SQL, which was invented by E. F.
Codd in the 1970’s.

SQL is an ANSI (American National Standards Institute) standard computer language


for accessing and manipulating database systems. SQL statements are used to retrieved and
update data in a database. SQL works with database programs like MS Access, DB2, Informix,
MS SQL Server, Oracle, Sybase, etc.

What is SQL?
• SQL stands for Structured Query Language.
• SQL is a standard computer language for accessing and manipulating databases.
• SQL can retrieve or extract data from a database.
• SQL can insert new records in a database.
• SQL can update and delete records from a database.
• SQL is easy to learn.

SQL encompasses two distinct categories of statements: Data Definition language (DDL)
and Data Manipulation Language (DML). The DDL subset includes a group of statements
that allow you to create database structures, such as tables, fields, indices and so on. The DML
subset includes all the commands that allow you to query and modify the data in the database,
add new records, or delete existing ones. Most of the time, you’ll use only DML statements to
retrieve and update data stored in a database.

Two Categories of SQL statements:

1. SQL Data Definition Language (DDL) – permits database tables to be created or


deleted. Using SQL DDL, you can define indexes (keys), specify links between tables,
and impose constraints between database tables.

The most important DDL commands in SQL are:


a. CREATE TABLE – creates a new database table.
b. ALTER TABLE – alters (changes) a database table.
c. DROP TABLE – deletes a database table.
d. CREATE INDEX – creates an index (search key)
e. DROP INDEX – deletes an index.

2. SQL Data Manipulation Language (DML) – commands that modify or manipulate


records in a database table.

Commands that form the Data Manipulation Language:


a. SELECT – extracts data from a database table.
b. UPDATE – updates data in a database table.
Information and Communication Technology Department 53
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
c. DELETE – deletes data from a database table.
d. INSERT – inserts new data into a database table.

SQL 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 (row) with data.

Below is an example of a table called “Persons”

LastName FirstName Address City


Hansen Ola Timotein 20 Sandnes
Stevendson Tove Borgvn 23 Sandnes
Pettersen Karl Storgt 20 Stavanger

The table above contains three records (one for each person) and four columns (LastName,
FirstName, Address, City).

The SELECT Statement

The SELECT statement selects columns of data from a database table. The tabular result is
stored in a result table called the resultset.

Syntax:

SELECT column-name(s) FROM table-name

Example 1: Select Columns from a Table.

To select the columns named “LastName” and “FirstName”, use a SELECT statement like this:

SELECT LastName, FirstName FROM Persons

The “Persons” table:


LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Stevendson Tove Borgvn 23 Sandnes
Pettersen Karl Storgt 20 Stavanger

The result:
LastName FirstName
Hansen Ola
Stevendson Tove
Pettersen Karl

Example 2: Select all Columns


Information and Communication Technology Department 54
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
To select all columns from the “Persons” table, use an * (asterisk) symbol instead of column
name, like this:

SELECT * FROM Persons

The result:
LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Stevendson Tove Borgvn 23 Sandnes
Pettersen Karl Storgt 20 Stavanger

The WHERE Clause

To conditionally select data from a table, a WHERE clause can be added to the SELECT
statement.

Syntax:

SELECT column-name FROM table-name WHERE column Operator value

With WHERE clause, the following operator can be used:

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern

Note: In some versions of SQL, the Not Equal operator <> can be written as !=

Example: To select the persons living in the city “Sandnes”, we add a WHERE clause to the
SELECT statement:

SELECT * FROM Persons WHERE City = 'Sandnes'

The “Persons” table:


LastName FirstName Address City Year
Hansen Ola Timotein 20 Sandnes 1951
Stevendson Tove Borgvn 23 Sandnes 1978
Stevendson Stale Kaivn 18 Sandnes 1980
Pettersen Karl Storgt 20 Stavanger 1960

The result:
Information and Communication Technology Department 55
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
LastName FirstName Address City Year
Hansen Ola Timotein 20 Sandnes 1951
Stevendson Tove Borgvn 23 Sandnes 1978
Stevendson Stale Kaivn 18 Sandnes 1980

The LIKE Condition

The LIKE condition is used to specify a search for a pattern in a column.

Syntax:

SELECT column-name FROM table-name WHERE column LIKE pattern

A “%” sign can be used to define wildcards (missing letters in the pattern) both before and after
the pattern.

Example 1: To extract record(s) with First Names that start with an ‘O’:

SELECT * FROM Persons WHERE FirstName LIKE 'O%'

Example 2: To retrieved record(s) from Persons table with First Names that end with an ‘a’:

SELECT * FROM Persons WHERE FirstName LIKE '%a'

Example 3: To return record(s) of persons with First Names that contains the pattern ‘la’:

SELECT * FROM Persons WHERE FirstName LIKE '%la%'

All the examples above will return the following result:


LastName FirstName Address City Year
Hansen Ola Timotein 20 Sandnes 1951

SQL AND & OR Operator

AND and OR operator join two or more conditions in a WHERE clause.

The AND operator displays a record if ALL conditions listed evaluates to TRUE. The OR
operator displays a record if ANY of the condition listed evaluates to TRUE.

Original Table (used in the examples)


LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Stevendson Tove Borgvn 23 Sandnes
Stevendson Stephen Kaivn 18 Sandnes

Information and Communication Technology Department 56


Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Example 1: Use AND to display each person with the First Name equal to “Tove”, and the Last
Name equal to “Stevendson”:

SELECT * FROM Persons WHERE FirstName = 'Tove' AND LastName = 'Stevendson'

Result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes

Example 2: Use OR to display each person with the First Name equal to “Tove”, or the Last
Name equal to “Stevendson”:

SELECT * FROM Persons WHERE FirstName = 'Tove' OR LastName = 'Stevendson'

Result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes
Stevendson Stephen Kaivn 18 Sandnes

Example 3: You can also combine AND and OR (use parenthesis to form complex
expressions):

SELECT * FROM Persons WHERE (FirstName = 'Tove' OR FirstName = 'Stephen')


AND LastName = 'Stevendson'

The result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes
Stevendson Stephen Kaivn 18 Sandnes

Using BETWEEN . . . AND Operator

The BETWEEN . . . AND operator selects an inclusive range of data between two values.
These values can be numbers, text, or dates.

Syntax:

SELECT column-name FROM table-name WHERE column-name BETWEEN value1 AND


value2

The “Persons” table:


LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Stevendson Tove Borgvn 23 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Karl Storgt 20 Stavanger

Information and Communication Technology Department 57


Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Example 1: To display the persons alphabetically between (and including) “Hansen” and
“Pettersen”, use the following SQL:

SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'

Result:
LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Karl Storgt 20 Stavanger

Example 2: To display the persons outside the range used in the previous example, use the
NOT operator:

SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Hansen' AND 'Pettersen'

The result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes

The DISTINCT Keyword

The DISTINCT keyword is used to return only distinct (unique) values.

Syntax:

SELECT DISTINCT column-name(s) FROM table-name

The “Orders” table:


Company OrderNumber
Sega 3412
Williams 2312
ABC Shop 4678
Williams 6798

Example 1: Select Companies from Orders table.

This SQL statement:


SELECT Company FROM Orders

Will return this result:


Company
Sega
Williams
ABC Shop
Williams

Note: The Company Williams is listed twice in the result.


Information and Communication Technology Department 58
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Example 2: Select Distinct Companies from Orders

To select distinct or unique values from the column named “Company”, we use the DISTINCT
keyword with the SELECT statement:

SELECT DISTINCT Company FROM Orders

The result:
Company
Sega
Williams
ABC Shop

Note: “Williams” is listed only once in the resultset.

SQL ORDER BY

The ORDER BY keyword is used to sort records.

Using the “Orders” table:


Company OrderNumber
Sega 3412
ABC Shop 5678
Williams 2312
Williams 6798

Example 1: To display the Companies and Order Number in alphabetical order:

SELECT * FROM Orders ORDER BY Company

Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
Williams 6798
Williams 2312

Example 2: To display the Companies in alphabetical order and the Order Number in numerical
order:

SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber

Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
Williams 2312
Williams 6798
Information and Communication Technology Department 59
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Example 3: To display the Companies in reverse alphabetical order:

SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC

Result:
Company OrderNumber
Williams 6798
Williams 2312
Sega 3412
ABC Shop 5678

SQL INSERT INTO Statement

The INSERT INTO statement inserts new rows into a table.

Syntax:

INSERT INTO table-name VALUES (value1, value2, . . ., valueN)

You can also specify the columns for which you want to insert data:

INSERT INTO table-name(column1, column2, . . ., columnN)


VALUES(value1, value2, . . ., valueN)

The “Persons” table:


LastName FirstName Address City
Pettersen Karl Storgt 20 Stavanger

Example 1: Inserting record to Persons table:

INSERT INTO Persons VALUES('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')

Result:
LastName FirstName Address City
Pettersen Karl Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

Example 2: Insert record in specified column.

This “Persons” table:


LastName FirstName Address City
Pettersen Karl Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

And this SQL statement:


INSERT INTO Persons(LastName, Address) VALUES('Rasmussen', 'Storgt 67')

Information and Communication Technology Department 60


Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Will give this result:
LastName FirstName Address City
Pettersen Karl Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
Rasmussen Storgt 67

SQL UPDATE Statement

The UPDATE statement updates or changes records.

Syntax:

UPDATE table-name SET column-name = new-value WHERE column-name = some-


value

The “Persons” table:


LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Storgt 67

Example 1: Update one column in a row.

UPDATE Persons SET FirstName = 'Nina' WHERE LastName = 'Rasmussen'

Result:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Storgt 67

Example 2: Update several columns in a row.

UPDATE Persons SET Address = 'Stien 12', City = 'Stavanger'


WHERE LastName = 'Rasmussen'

Result:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger

SQL DELETE Statement

The DELETE statement is used to delete rows in a table.

Syntax:
DELETE FROM table-name WHERE column-name = some-value

Information and Communication Technology Department 61


Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
The “Persons” table:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger

Example 1: Delete a row.

“Nina Rasmussen” is going to be deleted:

DELETE FROM Persons WHERE LastName = 'Rasmussen'

Result:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger

It is also possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will still be intact:

Syntax:

DELETE FROM table-name


Or
DELETE * FROM table-name

Example 2: Delete all rows in a table.

DELETE * FROM Persons

Result:
LastName FirstName Address City

SQL COUNT Function

The COUNT function returns the number of records in a database table.

Syntax:

SELECT COUNT(column-name) FROM table-name

With this “Persons” table:


Name Age
Hansen, Ola 34
Stevendson, Tove 45
Pettersen, Karl 19
Rasmussen, Nina 20

Information and Communication Technology Department 62


Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING

7
Example 1: To count the number of records in the Persons table:

SELECT COUNT(*) FROM Persons

Result:
4

Example 2: To return the number of persons older than 20 years:

SELECT COUNT(*) FROM Persons WHERE Age > 20

Result:
2

Information and Communication Technology Department 63


Palompon Institute of Technology

Anda mungkin juga menyukai