Sie sind auf Seite 1von 30

Databases

Topic 7:
SQL 1

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.2

Scope and Coverage


This topic will cover:
History and purpose of SQL
Basic concepts of SQL
Datatypes in SQL

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.3

Learning Outcomes
By the end of this topic, students will be able to:
Explain the purpose of SQL
Outline the basic concepts of SQL
Understand that there are different flavours of
SQL

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.4

Objectives of SQL
Create the database and relation structures
Perform basic tasks such as inserts, updates and
deletes
Simple and complex queries

Structured Query Language

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.5

Data Definition Language: DDL


For defining database structures and controlling access to data

CREATE TABLE, CREATE INDEX , CREATE SEQUENCE ,


GRANT ACCESS etc.

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.6

Data Manipulation Language: DML


For retrieving and updating data

SELECT, INSERT, UPDATE, DELETE

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.7

History of SQL - 1
Developed from IBMs SYSTEM R

The database standard language by 1970s...

First standard published in 1987 by ISO

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.8

History of SQL - 2
Addition to standard published 1989
SQL 1992 Major revision SQL-92
SQL: 1999 - with object-relational features
SQL: 2003 - introduced concept of core SQL
SQL: 2006 user of SQL with XML
SQL: 2008 minor revisions.

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.9

Data Manipulation Language: DML


For retrieving and updating data

SELECT retrieving
INSERT, UPDATE, DELETE updating

Difference between INSERT and UPDATE?

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.10

Literals
Insert into Student (Stu_id, First_name,
Last_name)
Values (1,Satpal,Singh);

- Non-numeric in single quotes

- Numeric NOT in quotes

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.11

Select branchID, Count(staff_id)


From workers
Where branchType = Main
Group by branchID
Having Count (staff_id) > 1
Order by branchID

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.12

Which columns will appear?

Select branchID, Count(staff_id)


Which table?
From workers Condition
Where branchType = Main Groups by some column
Group by branchID Value

Having Count (staff_id) > 1 Restricts what will


Be grouped

Order by branchID
Specifies the order of the result

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.13

Database Update
Insert

Update

Delete

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.14

Insert
Insert into students (Student_id, first_name,
last_name)
Values (1,Satpal,Singh);

Insert into students


Values (1,Satpal,Singh,Overseas,Undergrad);

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.15

Update
Update Students
Set Student_type = Undergrad;

Update Students
Set student_type = Undergrad
Where student_id = 1;

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.16

Delete
Delete from Students;

Delete from Students


Where student_id = 1;

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.17

Activity
Departments

Department_Id Integer

Department_Name varchar 30

Location varchar 30

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.18

Departments
Department_Id Integer
Department_Name varchar 30
Location varchar 30

Insert into Departments


Values (8,Complaints,Glasgow):

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.19

Commit;

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.20

Rollback;

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.21

Datatypes

What is a datatype?
u ch
r e m t
What is a domain? s a re n
pe iff e
ta ty y d L
Da cted b f SQ
ff e s o
a o u r
v
fla

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.22

String Datatypes
Character or Char
Varying Character of Varchar
Bit (N)
Bit varying

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.23

Char or Varchar
Gary__ a 6 long Char

Gary a 6 long varchar

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.24

Numeric Datatypes
Numeric or Decimal, e.g. 8.23 with point set

Integer, e.g. 8

Float, e.g. 8.23 but could also be changed so that


point moves when needed

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.25

Datetime Types
Date

Time

Timestamp

Interval

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.26

Advantages of SQL
Universal

Easy to use

Fits (more or less) with relational model

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.27

Disadvantages of SQL
Does not support all features of relational model

No one standard

Has had to be extended

Much redundancy possible to do the same thing


many ways

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.28

Learning Outcomes Have We


Met Them?
By the end of this topic, students will be able to:
Explain the purpose of SQL
Outline the basic concepts of SQL
Understand that there are different flavours of
SQL

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.29

References
Benyon-Davis, P. (2003). Database Systems, 3rd
edition. Palgrave Macmillan. Chapters 11, 12 & 13.
Connolly, T. & Begg, C. (2004). Database Systems:
A Practical Approach to Design, Implementation,
and Management, 4th Edition. Addison Wesley.
Chapters 5, 6 & 7.
Dietrich, S. W. (2001). Understanding Relational
Database Query Languages, 1st edition. Prentice
Hall. Chapter 5.

V1.0 NCC Education Limited


SQL 1 Topic 7 - 7.30

Topic 7 SQL 1

Any Questions?

V1.0 NCC Education Limited

Das könnte Ihnen auch gefallen