1 Introduction to SQL

13
SQL Introduction

Transcript of 1 Introduction to SQL

Page 1: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 1/13

SQL

Introduction

Page 2: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 2/13

Key Terms

Data ± Any important information like an employee¶s salar y.

Database

 ± An organized collection of  data or  information.

DBMS  ± Database Management System

 ± A Program that stor es, r etrieves, modifies data in 

database. Relational database

 ± Collection of  two-dimensional tables.

Page 3: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 3/13

Relational Database Components

Page 4: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 4/13

How a database is cr eated?

Model of 

system

in client¶s

mind

Entity model of 

client¶s model

Tables on disk

Oracle

server 

Table modelof entity model

Entity becomes a table

 Attribute becomes columns of  a table

Page 5: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 5/13

ER Diagram

 ± ER model consists of  the various entities in a business and the r elationships among them.

 ± Scenario:

³. . . Assign one or  mor e employees to a 

department . . .´

³. . . Some departments do not yet have assigned 

employees«.´

EMPLOYEE

#* number 

* name

o job title

DEPARTMENT

#* number 

* nameo location

assigned to

composed of 

Page 6: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 6/13

Relating Multiple Tables

 ± Each row of  data in a table is uniquely identified by a 

primar y key.

 ± You can logically r elate data from multiple tables 

using for eign keys.

Table name: EMPLOYEES

Table name:D

EPARTMENTS

Primary key Primary keyForeign key

«

Page 7: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 7/13

Relational Database Terminology

1

23

4

6

5

Page 8: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 8/13

Using SQL to Quer y Your  

Database Structur ed quer y language (SQL) is:

 ± The  ANSI standard language for  operating 

r elational databases

 ± Efficient, easy to learn, and use ± Functionally complete (With SQL, you can define, 

r etrieve, and manipulate data in the tables.)

SELECT department_name

FROM departments;Oracle

server 

Page 9: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 9/13

Cr eateReadUpdateDelete

Update:update xxx_ temp 

set id = 4

wher e id < 10

Delete:

delete from xxx_ temp 

wher e id < 10

Cr eate:insert into  xxx_ temp 

values (2, 'Test 2');

Read:select id, text 

from xxx_ temp 

wher e id < 10

Page 10: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 10/13

SQL Statements

SELECTINSERT

UPDATE

DELETE

MERGE

CREATE

 ALTER

DROP

RENAME

TRUNCATE

COMMENT

GRANT

REVOKE

COMMIT

ROLLBACK

SAVEPOINT

Data manipulation 

language (DML)

Data definition language

(DDL)

Transaction control

Data control language (DCL)

Page 11: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 11/13

Oracle 9i Components

Page 12: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 12/13

(HR) Schema generally used for  

training

DEPARTMENTSdepartment_id

department _ namemanager  _ idlocation _ id

LOCATIONSlocation_id

str eet _ addr esspostal_ code

citystate_ province

countr y_ id

COUNTRIEScountry_id

countr y_ namer egion _ id

REGIONSregion_id

r egion _ name

EMPLOYEESemployee_id

first _ namelast _ name

email

phone_ number hir e_ date job _ idsalar y

commission _ pctmanager  _ id

department _ id

JOBS job_id

 job _ titlemin _ salar ymax_ salar y

JOB_HISTORYemployee_id

start_date

end _ date

 job _ id

department _ id

Page 13: 1 Introduction to SQL

8/8/2019 1 Introduction to SQL

http://slidepdf.com/reader/full/1-introduction-to-sql 13/13

Key Points For  SQL Statements

SQL statements ar e not case-sensitive. The data enter ed is case sensitive.

SQL statements can be enter ed on one or  many lines.

Keywords cannot be split across lines or  abbr eviated.

Clauses ar e usually placed on separate lines for  r eadability

and ease of editing. Indents should be used to make code mor e r eadable.

Keywords typically ar e enter ed in uppercase; all other  words, such as table names and columns names ar eenter ed in lowercase.

Null is a value that is unavailable, unassigned, unknown, or  inapplicable. Null is not the same as zero or  a blank space.Zero is a number  and blank space is a character .

A literal is a character, a number, or  a date that is included in the SELECT statement.

Date and character  literal values must be enclosed within single quotation marks.