introduction to structured query language (sql)

30
Introduction to Structured Query Language (SQL) COM S 461 2013- Fall Instructor: Ying Cai Iowa State University 1

Upload: efrat

Post on 22-Feb-2016

88 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Structured Query Language (SQL). COM S 461 2013-Fall Instructor: Ying Cai Iowa State University. SQL: What and Why ? (1/2). Programming Language:. Instructions. Result. Programmers. Computer. SQL: What and Why ? (2/2). Query Language:. Query. Result. DB Users. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Structured Query  Language (SQL)

1

Introduction toStructured Query Language (SQL)

COM S 461 2013-FallInstructor: Ying Cai

Iowa State University

Page 2: Introduction to Structured Query  Language (SQL)

2

SQL: What and Why ? (1/2)

Programmers Computer

Instructions

Result

Programming Language:

Page 3: Introduction to Structured Query  Language (SQL)

3

SQL: What and Why ? (2/2)

DB Users DBMS(Database Server)

Query

Result

Query Language:

Page 4: Introduction to Structured Query  Language (SQL)

4

ContentsSetup a Database Server on your Computer.How to define data using SQL:

Data Definition Language (DDL)Create Tables/Update Tables…

How to manipulate/access data using SQL:Data Manipulation Language (DML) Insert Data/Update Data/Read Data…SQL Functions

Page 5: Introduction to Structured Query  Language (SQL)

5

Setup Database Server (1/2)Step 1. Download (Installer/Source code available)

MySQL http://dev.mysql.com/downloads/

MariaDB https://downloads.mariadb.org/

Step 2. Setup & Configuration Port. Remote Access. Root User.

Step 3. Connect to server. You’ll need: User/PSW, Server IP/Port.

Page 6: Introduction to Structured Query  Language (SQL)

6

Setup Database Server (2/2)

Database Server

Client 1

Client 2

Client 3

The Clinet/Server Structure

DB Connection

Page 7: Introduction to Structured Query  Language (SQL)

7

Programming with SQLSQL

Data Definition Language

1.Create DB/Table2.Change DB/Table3.Delete DB/Table

Data Manipulation Language

1.Add Data2.Update Data3.Remove Data

4. Get Data

Data Control Language

Access ControlAuthorization

Page 8: Introduction to Structured Query  Language (SQL)

Data Definition Language

8

(Basic) Keywords of DDL:

• Create a new Database/TableCREATE

• Change the definition of an existing DB/table

ALTER

• Delete an existing DB/table.DROP

Page 9: Introduction to Structured Query  Language (SQL)

9

Syntax of CREATECreate a new database.

Create a new tableCREATE TABLE table_name(

column_1 INT,column_2 CHAR(10) NOT

NULL,column_3 VARCHAR(256),column_4 INT DEFAULT 0,… …

) ;

CREATE DATABASE db_name;

Data types:

• INT/INTEGER(size)

• DECIMAL(size,d) • CHAR(size)• VARCHAR(size)• Date(yyyymmdd)

Page 10: Introduction to Structured Query  Language (SQL)

10

Syntax of CREATECreate a new database.

Create a new tableCREATE TABLE table_name(

column_1 INT,column_2 CHAR(10) NOT

NULL,column_3 VARCHAR(256),column_4 INT DEFAULT 0,… …

) ;

CREATE DATABASE db_name;

Data types:

• INT/INTEGER(size)

• DECIMAL(size,d) • CHAR(size)• VARCHAR(size)• Date(yyyymmdd)

Colunm_1 Colunm_2 Colunm_3 Colunm_4

(Empty) (Empty) (Empty) (Empty)

Page 11: Introduction to Structured Query  Language (SQL)

11

Syntax of ALTERAdd a column to an exiting table.

Change a column of an exiting table.

Delete a column from an exiting table.

ALTER TABLE table_nameADD column_name

data_type;

ALTER TABLE table_nameALTER COLUMN column_name

new_data_type;

or CHANGDE COLUMN old_name new_name data_type;or MODIFY COLUMNB column_name new_data_type;ALTER TABLE table_name

DROP COLUMN column_name;

Page 12: Introduction to Structured Query  Language (SQL)

12

Syntax of DROPDelete a Database

Delete a Table.

DROP DATABASE db_name

DROP TABLE table_name

Page 13: Introduction to Structured Query  Language (SQL)

13

Data Manipulation Language(Basic) Keywords of DML:

• Read data from DBSELECT

• Change the value of existing items UPATE

• Add new items into DBINSERT

• Remove existing items from DBDELETE

Page 14: Introduction to Structured Query  Language (SQL)

14

Syntax of SELECTUse SELET to get desired data in given format:

Example 1: Get students’ Uid and name, who have GPA higher than 3.5, sorted by their GPA in descending order.

SELECT column_1 column_2 …

FROM table_1 table_2 …WHERE conditionORDER BY expression

ASC/DESC

SELECT uid, student_name

FROM studentsWHERE gpa > 3.5ORDER BY gpa DESC

Page 15: Introduction to Structured Query  Language (SQL)

15

Syntax of SELECTExample 2: Get all information about female

students who is older than 20.

Example 3: Get the average speed of all cars in a table where only moving distance and time are stored.

SELECT *FROM studentsWHERE ( 2013 – YoB ) > 20 AND gener = ‘F’

SELECT (distance/time) AS speedFROM carsWHERE time > 0

Page 16: Introduction to Structured Query  Language (SQL)

16

Syntax of SELECTSelect from Multiple Tables: (Multiple Table Query)

The DBMS will combine the two tables to generate a new Virtual Table (Cross Join).

SELECT *FROM students course

Uid Name001 Andy002 Jack

cid Name Students

100 Database 001,002101 Algorith

m002

Page 17: Introduction to Structured Query  Language (SQL)

17

Syntax of SELECTSelect from Multiple Tables: (Multiple Table Query)

The DBMS will combine the two tables to generate a new Virtual Table (Cross Join).

SELECT *FROM students course

Uid Name cid Name Students

001 Andy 100 Database

001,002

001 Andy 101 Algorithm

002

002 Jack 100 Database

001,002

002 Jack 101 Algorithm

002

Page 18: Introduction to Structured Query  Language (SQL)

18

Syntax of SELECTExample 4: Show the uid and name of all students

who registered for the Database course.SELECT student.uid student.name

FROM students courseWHERE course.name = “Database”

AND student.id IN course.students

Page 19: Introduction to Structured Query  Language (SQL)

19

Syntax of SELECTExample 5: We have three tables:

Show the name of each course and the name of the TA assigned to this course.

Uid Name001 Andy002 Jack

CID Name100 Database111 Algorithm

Uid CID001 100002 111

SELECT course.name, student.nameFROM students course TAWHERE Courese.Cid = TA.Cid

AND Student.Uid = TA.Uid

Student Course TA

Page 20: Introduction to Structured Query  Language (SQL)

20

Syntax of SELECTSelect Distinct values (No duplicates)

Example 6: Show students with distinct birthday and name.

SELECT DISTINCT column_1, column_2 …FROM table_1, table_2 …WHERE conditions …

SELECT DISTINCT Name DoBFROM Student Course

Page 21: Introduction to Structured Query  Language (SQL)

21

Syntax of SELECTSelect a given number of itemsSELECT TOP NUMBER column_1 column_2

FROM table_1 table_2WHERE conditions…

or

SELECT column_1 column_2FROM table_1 table_2WHERE conditions…LIMIT NUMBER

Page 22: Introduction to Structured Query  Language (SQL)

22

Syntax of SELECTExample 7: Show the name of 100 students with

the highest GPA.

SELECT nameFROM studentsORDER BY gpa DESCLIMIT 100

Page 23: Introduction to Structured Query  Language (SQL)

23

Syntax of UpdateUpdate the value of existing item(s).Update table_name

SET column_1 =

value_1column_2 =

value_2… …

WHERE conditions…

Page 24: Introduction to Structured Query  Language (SQL)

24

Syntax of InsertInsert new items into a tableINSERT INTO table_name

VALURES (value_1, value_2 … ), # row 1

(value_1, value_2 … ), # row 2…. …

Page 25: Introduction to Structured Query  Language (SQL)

25

Syntax of DeleteDelete items from a table

DELETE without any condition will delete all items in the table, which is forbidden by many DBMS.

DELETE FROM table_nameWHERE conditions…

Page 26: Introduction to Structured Query  Language (SQL)

26

SQL FunctionsAVG()Calculate the Average of selected data column

ORDER BY/LIMIT is no longer necessory.

SELECT AVG( column_name| expression) AS value_name

FROM table_1, table_2 …WHERE conditions …

Page 27: Introduction to Structured Query  Language (SQL)

27

SQL FunctionsCOUNT ()

Count the number of items selected.

MAX () / MIN () Return the Maximal/Minimal value in selected data.

SELECT COUNT( column_name| expression) AS value_name

FROM table_1, table_2 …WHERE conditions …

SELECT MAX/MIN( column_name| expression) AS value_name

FROM table_1, table_2 …WHERE conditions …

Page 28: Introduction to Structured Query  Language (SQL)

28

SQL FunctionsSUM()Calculate the sum of selected values.SELECT SUM( column_name| expression) AS value_name

FROM table_1, table_2 …WHERE conditions …

Page 30: Introduction to Structured Query  Language (SQL)

30

Q&A