Docsity

SQL: Understanding Data Manipulation with Structured Query Language (SQL), Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL: Understanding Data Manipulation with Structured Query Language (SQL) and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Chapter 7:

SQL, the Structured

Query Language

Overview

Introduction DDL Commands DML Commands SQL Statements, Operators, Clauses Aggregate Functions

Some Facts on SQL

SQL data is case-sensitive, SQL commands are not.

First Version was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce. [SQL]

Developed using Dr. E.F. Codd's paper, “A Relational Model of Data for Large Shared Data Banks.”

SQL query includes references to tuples variables and the attributes of those variables

SQL: DDL Commands

CREATE TABLE: used to create a table.

ALTER TABLE: modifies a table after it was created.

DROP TABLE: removes a table from a database.

SQL: Attributes Types

Table 7.6 pg.164 Docsity.com

SQL: ALTER TABLE Statement

To add or drop columns on existing tables. ALTER TABLE statement syntax: ALTER TABLE

ADD attr datatype; or DROP COLUMN attr;

Example: CREATE TABLE FoodCart ( date varchar(10), food varchar(20), profit float );

ALTER TABLE FoodCart ( ADD sold int );

ALTER TABLE FoodCart( DROP COLUMN profit );

DROP TABLE FoodCart;

date food profit

date food profit sold

date food sold

FoodCart

FoodCart

FoodCart

SQL: DML Commands

INSERT: adds new rows to a table.

UPDATE: modifies one or more attributes.

DELETE: deletes one or more rows from a table.

SQL: UPDATE Statement

To update the content of the table: UPDATE statement syntax: UPDATE

SET = WHERE ; Example: UPDATE FoodCart SET sold = 349 WHERE date = ’02/25/08’ AND food = ‘pizza’; FoodCart

02/26/08 pizza 70

02/26/08 hotdog 500

02/25/08 pizza 350

date food sold

02/26/08 pizza 70

02/26/08 hotdog 500

02/25/08 pizza 349

date food sold

SQL: DELETE Statement

To delete rows from the table: DELETE statement syntax: DELETE FROM

WHERE ; Example: DELETE FROM FoodCart WHERE food = ‘hotdog’; FoodCart

Note: If the WHERE clause is omitted all rows of data are deleted from the table.

02/26/08 pizza 70

02/26/08 hotdog 500

02/25/08 pizza 349

date food sold

02/26/08 pizza 70

02/25/08 pizza 349

date food sold

SQL: SELECT Statement

A basic SELECT statement includes 3 clauses

SELECT FROM WHERE

SELECT Specifies the attributes that are part of the resulting relation

FROM Specifies the tables that serve as the input to the statement

WHERE Specifies the selection condition, including the join condition.

Note: that you don't need to use WHERE

Using a “*” in a select statement indicates that every attribute of the input table is to be selected. Example: SELECT * FROM … WHERE …;

To get unique rows, type the keyword DISTINCT after SELECT. Example: SELECT DISTINCT * FROM … WHERE …;

SQL: SELECT Statement (cont.)

SQL: Join operation

A join can be specified in the FROM clause

which list the two input relations and the

WHERE clause which lists the join condition.

Example:

1003 Biotech

1002 Sales

1001 IT

ID Division

1002 TN

1001 MA

1000 CA

ID State

Emp Dept

SQL: Join operation (cont.)

inner join = join SELECT * FROM emp join dept (or FROM emp, dept) on emp.id = dept.id;

1002 Sales

1001 IT

Dept.ID Dept.Division

1002 TN

1001 MA

Emp.ID Emp.State