View
212
Download
0
Category
Preview:
Citation preview
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
• 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
SQL language command• Create Table• Alter Table• Drop Table• Manipulate Table Data• Data Query
Database Application Development - Lecture 3
• 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
• 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
• 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)
• 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;
• 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);
• Drop TableDelete existed table.Syntax of dropping table:DROP TABLE TableName;
Example:DROP TABLE UserInfo;
Database Application Development - Lecture 3
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
• 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
• 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
• UPDATE: multiple rows operation
Database Application Development - Lecture 3
update
Update:UPDATE Grade SET Grade = ‘A’
WHERE Gender= ‘Male’;
Table: Grade
Table: Grade
• 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
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
• 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
• 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
• 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
• 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
Example:SELECT store_name FROM Store_Information
WHERE Sales > 1000 OR (Sales < 500 AND Sales > 275)
Database Application Development - Lecture 3
• 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
• 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
• 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
• 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
• 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
• 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
You can find more detail on following website:http://sql.1keydata.com/cn/
Database Application Development - Lecture 3
Recommended