11.11.2015 dat602 database application development lecture 3 review of sql language

28
DAT602 Database Applic ation Development Lecture 3 Review of SQL L anguage

Upload: adelia-george

Post on 04-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

DAT602 Database Application Development

Lecture 3 Review of SQL Language

Page 2: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Structured Query LanguageDatabase computer language designed for managing data in relational database management systems (RMDBS).

Its scope includes data query and update, schema creation and modification, and data access control.

Database Application Development - Lecture 3

Page 3: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Different database systems have different SQL languages. These SQL language are extensions of standard SQL language, they provide some additional support to the ANSI SQL-92 standard.

Microsoft SQL Server: Transact – SQLORACLE : Procedural Language/SQL (PL/SQL)

Database Application Development - Lecture 3

Page 4: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

SQL language command• Create Table• Alter Table• Drop Table• Manipulate Table Data• Data Query

Database Application Development - Lecture 3

Page 5: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Create TableThree aspects you should consider carefully before you create a table.- Data types. These include CHARACTER,INTEGER, FLOAT, and so on.- Data constraints. These include such restrictions as whether NULLS are permitted.- Default values. Default values can be assigned for each column.

Database Application Development - Lecture 3

Page 6: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Command for creating a tableCREATE TABLE tableName

( column1Name dataType[(size)] [constraints] [default value],column2Name dataType[(size)] [constraints] [default value],column3Name dataType[(size)] [constraints] [default value],...);

CREATE TABLE CONTACT_INFO(CONTACT_ID INTEGERNOT NULL PRIMARY KEY,FIRST_NAME VARCHAR(20) NOT NULL,LAST_NAME VARCHAR(30) NOT NULL,STREET VARCHAR(50) NOT NULL);

Database Application Development - Lecture 3

Page 7: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Alter Table : Add columnSometimes, after creating table, we need to alter table, like add, alter or remove a column.

Database Application Development - Lecture 3

Add

Syntax of Adding a column:AlTER TABLE TableName ADD newColumnName (size)Example:ALTER TABLE UserInfo ADD Gender char(1)

Page 8: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Alter Table : remove column

Database Application Development - Lecture 3

remove

Syntax of removing a columnALTER TABLE TableName DROP COLUMN columnName;

Example:ALTER TABLE UserInfo DROP COLUMN Gender;

Page 9: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Alter Table : alter column

Database Application Development - Lecture 3

alter

Syntax of alter column:ALTER TABLE TableName CHANGE OldColumnName

NewColumnName Type(Size);

Example:ALTER TABLE UserInfo CHANGE Age Gender

Char(1);

Page 10: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Drop TableDelete existed table.Syntax of dropping table:DROP TABLE TableName;

Example:DROP TABLE UserInfo;

Database Application Development - Lecture 3

Page 11: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

Data Manipulation• INSERT

Insert content into a table• UPDATE

Modify content of a table• DELETE

Delete content of a table

Database Application Development - Lecture 3

Page 12: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• INSERT

Database Application Development - Lecture 3

insert

Syntax of INSERT:INSERT INTO TableName (Column1, Column2, ...)

VALUES ( Value1, Value2, ...);

Example:INSERT INTO UserInfo (Name, Gender)

VALUES (‘Cindy’, ‘Female’);

Table: UserInfo

Table: UserInfo

Page 13: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• UPDATE: single row operation

Database Application Development - Lecture 3

update

Syntax of Update:UPDATE TableName SET Column1 = [NewValue]

WHERE {Condition}

Example:UPDATE UserInfo SET Gender = ‘Female’

WHERE Name = ‘Cindy’;

Table: UserInfo

Table: UserInfo

Page 14: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• UPDATE: multiple rows operation

Database Application Development - Lecture 3

update

Update:UPDATE Grade SET Grade = ‘A’

WHERE Gender= ‘Male’;

Table: Grade

Table: Grade

Page 15: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• DELETE operation

Database Application Development - Lecture 3

delete

Syntax of DELETE operation:DELETE FROM TableName

WHERE {Condition}

Example:DELETE FROM UserInfo WHERE Name = ‘Cindy’;

Table : UserInfo

Table : UserInfo

Page 16: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

Data Query• Retrieve data from a database in response to

a query• Following part covers terms: SELECT, WHERE,

ORDER BY, DISTINCT, AND, OR

Database Application Development - Lecture 3

Page 17: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• The SELECT statement is the heart of a SQL query.Syntax of SELECT:SELECT Column1, Column2,.. FROM TableName

Database Application Development - Lecture 3

SELECT

Example:SELECT Name FROM UserInfo

Page 18: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• WHERE Clauseyou can restrict the query to return the requested fields from only records that match some specific criteria.

SELECT * FROM Table_Student WHERE Gender = ‘Male’

Database Application Development - Lecture 3

Page 19: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Comparison operators- Equality (=)- Inequality (<>)- Greater Than (>) and Greater Than or Equal To (>=)- Less Than (<) and Less Than or Equal To (<=)- IS NULL- IS NOT NULL

Database Application Development - Lecture 3

Page 20: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• AND, ORYou can use AND, OR to combine multiple conditions to filter query result.

Syntax of AND, ORSELECT ColumnName FROM TableName WHERE Condition1 {[AND|OR] ConditionN } +

Database Application Development - Lecture 3

Page 21: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

Example:SELECT store_name FROM Store_Information

WHERE Sales > 1000 OR (Sales < 500 AND Sales > 275)

Database Application Development - Lecture 3

Page 22: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• The DISTINCT keyword eliminates duplicate rows from the results of a SELECT statement.Syntax of DISTINCT:SELECT DISTINCT Column FROM Table

Example: SELECT DISTINCT Name FROM UserInfo

Database Application Development - Lecture 3

Table: UserInfo

SelectDistinct

result

Page 23: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• The ORDER BY clause sorts query results by one or more columns.Syntax of ORDER BY:SELECT Column1, Column2… FROM Table

ORDER BY ColumnN

A sort can be ascending (ASC) or descending (DESC). If neither is specified, ASC is assumed.

Database Application Development - Lecture 3

Page 24: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Alias. We can give a simple, short table or column names to represent original long, complex names.

• Syntax of alias:SELECT “TableAlias”.”ColumnName“ “ColumnAlias” FROM “TableName” “TableAlias"

• Example of Alias:SELECT A1.UserFullName NameFROM UserInfo A1;

Database Application Development - Lecture 3

Page 25: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Sort function is very useful for locating the maximal or minimal value.

• Example:SELECT * FROM UserInfo ORDER BY Age DESC

Database Application Development - Lecture 3

Table: UserInfo

Result set

SelectOrder

by

Page 26: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Multiple table joint query.In well designed database, data in different tables are not overlapped.

• Most of time, the data we need is distributed in several different tables. So we need combine these related tables to do a joint query.

Database Application Development - Lecture 3

Page 27: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

• Multiple tables joint query

Database Application Development - Lecture 3

Table: STUDENT_INFO

Table: GRADE_INFO

Get Anny’s grade:SELECT * FROM STUDENT_INFO s, GRADE_INFO g WHERE s.ID = g.ID AND s.Name = “Bob”

Query result

Page 28: 11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language

You can find more detail on following website:http://sql.1keydata.com/cn/

Database Application Development - Lecture 3