29
SQL

SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

Embed Size (px)

Citation preview

Page 1: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

SQL

Page 2: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

คำ��สั่��ง SQL

SQL stands for Structured Query Language

• is a standard language for accessing and manipulating databases

Page 3: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

What is SQL?

• SQL stands for Structured Query Language• SQL lets you access and manipulate databases• SQL is an ANSI (American National Standards

Institute) standard

Page 4: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

What Can SQL do?• SQL can execute queries against a database• SQL can retrieve data from a database• SQL can insert records in a database• SQL can update records in a database• SQL can delete records from a database• SQL can create new databases• SQL can create new tables in a database• SQL can create stored procedures in a database• SQL can create views in a database• SQL can set permissions on tables, procedures, and

views

Page 5: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

Database Tables

Page 6: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

• SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).

Page 7: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

DML

• The query and update commands form the DML part of SQL:

• SELECT - extracts data from a database• UPDATE - updates data in a database• DELETE - deletes data from a database• INSERT INTO - inserts new data into a

database

Page 8: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

DDL• The DDL part of SQL permits database tables to be

created or deleted. It also define indexes (keys), specify links between tables, and impose constraints between tables. The most important DDL statements in SQL are:

• CREATE DATABASE - creates a new database• ALTER DATABASE - modifies a database• CREATE TABLE - creates a new table• ALTER TABLE - modifies a table• DROP TABLE - deletes a table• CREATE INDEX - creates an index (search key)• DROP INDEX - deletes an index

Page 9: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The SQL SELECT Statement

• The SELECT statement is used to select data from a database.

• The result is stored in a result table, called the result-set.

Page 10: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

SQL SELECT Syntax

• SELECT column_name(s) FROM table_name

หรื�อ

• SELECT * FROM table_name

Page 11: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ต�รื�ง Persons

SELECT LastName,FirstName FROM Persons

P_Id LastName FirstName Address City

1Hansen Ola Timoteivn 10 Sandnes

2Svendson Tove Borgvn 23 Sandnes

3Pettersen Kari Storgt 20 Stavanger

Page 12: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ผลล�พท์�

LastName FirstName

Hansen OlaSvendson Tove

Pettersen Kari

Page 13: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

• SELECT * FROM Persons

Page 14: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ผลล�พท์�

P_Id LastName FirstName Address City

1Hansen Ola Timoteivn 10 Sandnes

2Svendson Tove Borgvn 23 Sandnes

3Pettersen Kari Storgt 20 Stavanger

Page 15: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The WHERE Clause

• SQL WHERE Syntax• SELECT column_name(s)

FROM table_nameWHERE column_name operator value

Page 16: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ต�วอย่��งก�รืใช้� where

• SELECT * FROM Persons WHERE City='Sandnes'

P_Id LastName FirstName Address City

1Hansen Ola Timoteivn 10 Sandnes

2Svendson Tove Borgvn 23 Sandnes

3Pettersen Kari Storgt 20 Stavanger

ต�รื�ง Persons

Page 17: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ผลล�พท์�

P_Id LastName FirstName Address City

1Hansen Ola Timoteivn 10 Sandnes

2Svendson Tove Borgvn 23 Sandnes

Page 18: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The AND & OR Operators

• The AND operator displays a record if both the first condition and the second condition is true.

• The OR operator displays a record if either the first condition or the second condition is true.

Page 19: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ผลล�พท์�• SELECT * FROM Persons

WHERE FirstName='Tove'AND LastName='Svendson'

P_Id LastName FirstName Address City

1Hansen Ola Timoteivn 10 Sandnes

2Svendson Tove Borgvn 23 Sandnes

3Pettersen Kari Storgt 20 Stavanger

Page 20: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

ผลล�พธ์�

P_Id LastName FirstName Address City

2Svendson Tove Borgvn 23 Sandnes

Page 21: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The ORDER BY Keyword

• The ORDER BY keyword is used to sort the result-set by a specified column.

• The ORDER BY keyword sort the records in ascending order by default.

• If you want to sort the records in a descending order, you can use the DESC keyword.

Page 22: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

SQL ORDER BY Syntax

• SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC

Page 23: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The INSERT INTO Statement

• The INSERT INTO statement is used to insert a new row in a table.

Page 24: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

SQL INSERT INTO Syntax

• It is possible to write the INSERT INTO statement in two forms.

• The first form doesn't specify the column names where the data will be inserted, only their values:

• INSERT INTO table_nameVALUES (value1, value2, value3,...)

Page 25: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

• The second form specifies both the column names and the values to be inserted:

• INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

Page 26: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The UPDATE Statement

• The UPDATE statement is used to update existing records in a table.

Page 27: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

SQL UPDATE Syntax

• UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

Page 28: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

The DELETE Statement

• The DELETE statement is used to delete rows in a table.

Page 29: SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases

SQL DELETE Syntax

• DELETE FROM table_nameWHERE some_column=some_value