17
SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Embed Size (px)

Citation preview

Page 1: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

SQL: Data Manipulation I

Chapter 5

CIS 458

Sungchul Hong

Page 2: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Last Class

• EERD– Superclass– Subclass– Option/mandatory– And/Or

Page 3: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

This Class

• SQL (Structured Query Language)– Single Table Operation– Multiple tables Operation– Subquery v.s. Join

Page 4: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Introduction to SQL

• Structured Query Language– Create the database and relation structures– Perform basic data management tasks, such as insertion,

modification, and deletion of data.– Perform both simple and complex queries.

• SQL major components– Data Definition Language (DDL)– Data Manipulation Language (DML)

• 2 ways to use SQL– Use SQL interactively– Use SQL statements in a procedural language.

Page 5: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Introduction to SQL (2)

• It is a non-procedural language.– Specify what information you require

• It is a free format language.• SQL can be used by a range of users

including Database Administrators (DBA), management personnel, application developers, and many other types of end-user.

Page 6: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Introduction to SQL (3)

• ISO standard SQL … 1992

• Extensions or dialect provided by vendors.

Page 7: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Importance of SQL

• SQL is the first and, so far, only standard database language to gain wide acceptance.

• SQL has also become a Federal Information Processing Standard (FIPS), to which conformance is required for all sales of DBMSs to the US government.

• Specialized implementations of SQL are beginning to appear that are directed at new market, such as Online Analytical Processing (OLAP)

Page 8: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Writing SQL Commands

• SQL statement– Reserved words (fixed part of the SQL Lang.)– User-defined words.

• Tables, columns, views, indexes, …

– Terminator (;)– Components of an SQL statements are case

insensitive. (except literal character data)

Page 9: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

SQL

• SELECT ss#, Name, address, gpa

• FROM student

• WHERE gpa >= 3.0;

Page 10: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Suggested SQL Format

• Each clause in a statement should begin on a new line;• The beginning of each clause should line up with the

begging of other clauses;• If a clause has several parts, they should each appear

on a separate line and be indented under the start of the clause to show the relationship.

• Upper-case letters are used to represent reserved words.

• Lower-case letters are used to represent user-defined words

Page 11: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

SQL (MicroSoft)

Page 12: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Data Manipulation

• SELECT --- to query data in the database;

• INSERT --- to insert data into a table;

• UPDATE --- to update data in a table;

• DELETE --- to delete data from a table.

Page 13: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Simple Queries

• SELECT [DISTINCT|ALL]

• {*|[columnExpression[AS newName

• ]][,…]}

• FROM TableName [alias], […]

• [WHERE conditon]

• [GROUP BY columnlList][HAVING condition]

• [ORDER BY columnList]

Page 14: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Example 5.1 – 5.9

• Retrieve all columns, all rows

• SELECT staffNo, fName, lName, position, sex, DOB, slary, branchNO

• FROM staff;

• SELECT *• FROM staff;

Page 15: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Retrieve specific columns, all rows (5.2)

• SELECT staffNo, fName, lName, salary

• FROM Staff;

Page 16: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Use of DISTINCT (5.3)

• SELECT propertyNo

• FROM Viewing;

• SELECT DISTINCT propertyNo

• FROM Viewing;

Page 17: SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong

Calculated Field (5.4)

• SELECT staffNo, fName, lName, salary/12• FROM Staff;

• Calculated field, computed or derived field

• SELECT staffNo, fName, lName, salary/12 AS monthlySalary

• FROM Staff