+ All Categories
Home > Documents > LOGO 1 Lab_02: Basic SQL. 2 Outline Database Tables SQL Statements Semicolon after SQL...

LOGO 1 Lab_02: Basic SQL. 2 Outline Database Tables SQL Statements Semicolon after SQL...

Date post: 27-Dec-2015
Category:
Upload: tabitha-park
View: 244 times
Download: 1 times
Share this document with a friend
17
LOGO 1 Lab_02: Basic SQL Lab_02: Basic SQL
Transcript
Page 1: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

LOGO

1

Lab_02: Basic SQLLab_02: Basic SQLLab_02: Basic SQLLab_02: Basic SQL

Page 2: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

2

OutlineDatabase TablesSQL StatementsSemicolon after SQL Statements?SQL DML and DDLSQL SELECT StatementSQL WHERE Clause

Page 3: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

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.Below is an example of a table called "Persons":

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

Database Tables

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

Page 4: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

Most of the actions you need to perform on a database are done with SQL statements.

The following SQL statement will select all the records in the "Persons" table:

SELECT * FROM Persons

Keep in Mind That...SQL is not case sensitive

4

SQL Statements

Page 5: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

Some database systems require a semicolon at the end of each SQL statement.

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

We are using Oracle and we do not have to put or have to put a semicolon after each SQL statement, but some database programs force you to use it.

5

Semicolon after SQL Statements?

Page 6: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).

The query and update commands form the DML part of SQL:SELECT - extracts data from a databaseUPDATE - updates data in a databaseDELETE - deletes data from a databaseINSERT INTO - inserts new data into a database

6

SQL DML and DDL

Page 7: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are:

CREATE DATABASE - creates a new databaseALTER DATABASE - modifies a databaseCREATE TABLE - creates a new tableALTER TABLE - modifies a tableDROP TABLE - deletes a tableCREATE INDEX - creates an index (search key)DROP INDEX - deletes an index

7

SQL DML and DDL

Page 8: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

The SQL SELECT StatementThe SELECT statement is used to select data from a

database.The result is stored in a result table, called the result-set.

SQL SELECT Syntax

SELECT column_name(s)FROM table_name

andSELECT * FROM table_name

Note: SQL is not case sensitive. SELECT is the same as select.8

SQL SELECT Statement

Page 9: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

The "Persons" table:

Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.

9

An SQL SELECT Example

P_Id

LastNameFirstName

Address

City

1 Hansen OlaTimoteivn 10

Sandnes

2 Svendson ToveBorgvn 23

Sandnes

3 Pettersen KariStorgt 20

Stavanger

Page 10: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

We use the following SELECT statement:

10

An SQL SELECT Example

LastName FirstName

Hansen Ola

Svendson Tove

Pettersen Kari

SELECT LastName,FirstName FROM Persons

The result-set will look like this:

Page 11: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

Now we want to select all the columns from the "Persons" table.We use the following SELECT statement: 

11

SELECT * Example

SELECT * FROM Persons

Tip: The asterisk (*) is a quick way of selecting all columns!

The result-set will look like this:

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

Page 12: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

The WHERE clause is used to filter records.

12

SQL WHERE Clause

The WHERE Clause The WHERE clause is used to extract only those records that fulfill a specified criterion.

SQL WHERE Syntax

SELECT column_name(s)FROM table_nameWHERE column_name operator value

Page 13: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

The "Persons" table:

Now we want to select only the persons living in the city "Sandnes" from the table above. 13

WHERE Clause Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

Page 14: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

14

WHERE Clause Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

We use the following SELECT statement:

SELECT * FROM PersonsWHERE City='Sandnes‘

The result-set will look like this:

Page 15: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

15

Quotes Around Text Fields

SQL uses single quotes around text values (most database systems will also accept double quotes).Although, numeric values should not be enclosed in quotes.

For text values:

This is correct:

SELECT * FROM Persons WHERE FirstName='Tove'

This is wrong:

SELECT * FROM Persons WHERE FirstName=Tove

Page 16: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

16

Quotes Around Text Fields

For numeric values:This is correct:

SELECT * FROM Persons WHERE Year=1965

This is wrong:

SELECT * FROM Persons WHERE Year='1965'

Page 17: LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.

17

Operators Allowed in the WHERE Clause

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

INIf you know the exact value you want to return for at least one of the columns

With the WHERE clause, the following operators can be used:

Note: In some versions of SQL the <> operator may be written as !=


Recommended