Presentation is loading. Please wait.

Presentation is loading. Please wait.

11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language.

Similar presentations


Presentation on theme: "11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language."— Presentation transcript:

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

2 11.11.2015 Structured Query Language Database 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

3 11.11.2015 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 – SQL ORACLE : Procedural Language/SQL (PL/SQL) Database Application Development - Lecture 3

4 11.11.2015 SQL language command Create Table Alter Table Drop Table Manipulate Table Data Data Query Database Application Development - Lecture 3

5 11.11.2015 Create Table Three 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

6 11.11.2015 Command for creating a table CREATE 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_IDINTEGERNOT NULL PRIMARY KEY, FIRST_NAMEVARCHAR(20) NOT NULL, LAST_NAMEVARCHAR(30) NOT NULL, STREETVARCHAR(50) NOT NULL ); Database Application Development - Lecture 3

7 11.11.2015 Alter Table : Add column Sometimes, after creating table, we need to alter table, like add, alter or remove a column. Database Application Development - Lecture 3 UserInfo FirstName LastName Age UserInfo FirstName LastName Age Gender Add Syntax of Adding a column: AlTER TABLE TableName ADD newColumnName (size) Example: ALTER TABLE UserInfo ADD Gender char(1)

8 11.11.2015 Alter Table : remove column Database Application Development - Lecture 3 UserInfo FirstName LastName Age UserInfo FirstName LastName Age Gender remove Syntax of removing a column ALTER TABLE TableName DROP COLUMN columnName; Example: ALTER TABLE UserInfo DROP COLUMN Gender;

9 11.11.2015 Alter Table : alter column Database Application Development - Lecture 3 UserInfo FirstName LastName Gender UserInfo FirstName LastName Age alter Syntax of alter column: ALTER TABLE TableName CHANGE OldColumnName NewColumnName Type(Size); Example: ALTER TABLE UserInfo CHANGE Age Gender Char(1);

10 11.11.2015 Drop Table Delete existed table. Syntax of dropping table: DROP TABLE TableName; Example: DROP TABLE UserInfo; Database Application Development - Lecture 3

11 11.11.2015 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

12 11.11.2015 INSERT Database Application Development - Lecture 3 NameGender AmyFemale BobMale NameGender AmyFemale BobMale CindyFemale insert Syntax of INSERT: INSERT INTO TableName (Column1, Column2,...) VALUES ( Value1, Value2,...); Example: INSERT INTO UserInfo (Name, Gender) VALUES (‘Cindy’, ‘Female’); Table: UserInfo

13 11.11.2015 UPDATE: single row operation Database Application Development - Lecture 3 NameGender AmyFemale BobMale CindyFemale update NameGender AmyFemale BobMale CindyMale Syntax of Update: UPDATE TableName SET Column1 = [NewValue] WHERE {Condition} Example: UPDATE UserInfo SET Gender = ‘Female’ WHERE Name = ‘Cindy’; Table: UserInfo

14 11.11.2015 UPDATE: multiple rows operation Database Application Development - Lecture 3 NameGenderGrade AmyFemaleA BobMaleA CharlieMaleA DerryMaleA update NameGenderGrade AmyFemaleA BobMaleB CharlieMaleC DerryMaleD Update: UPDATE Grade SET Grade = ‘A’ WHERE Gender= ‘Male’; Table: Grade

15 11.11.2015 DELETE operation Database Application Development - Lecture 3 NameGender AmyFemale BobMale delete NameGender AmyFemale BobMale CindyFemale Syntax of DELETE operation: DELETE FROM TableName WHERE {Condition} Example: DELETE FROM UserInfo WHERE Name = ‘Cindy’; Table : UserInfo

16 11.11.2015 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

17 11.11.2015 The SELECT statement is the heart of a SQL query. Syntax of SELECT: SELECT Column1, Column2,.. FROM TableName Database Application Development - Lecture 3 NameGender AmyFemale BobMale CindyFemale Name Amy Bob Cindy SELECT Example: SELECT Name FROM UserInfo

18 11.11.2015 WHERE Clause you 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

19 11.11.2015 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

20 11.11.2015 AND, OR You can use AND, OR to combine multiple conditions to filter query result. Syntax of AND, OR SELECT ColumnName FROM TableName WHERE Condition1 {[AND|OR] ConditionN } + Database Application Development - Lecture 3

21 11.11.2015 Example: SELECT store_name FROM Store_Information WHERE Sales > 1000 OR (Sales 275) Database Application Development - Lecture 3 store_nameSalesDate Los Angeles$1500Jan-05-1999 San Diego$250Jan-07-1999 San Francisco$300Jan-08-1999 Boston$700Jan-08-1999

22 11.11.2015 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 NameCellPhoneNumber Amy123456 Amy456789 Bob258393 Cindy759403 Name Amy Bob Cindy Table: UserInfo Select Distinct result

23 11.11.2015 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

24 11.11.2015 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 Name FROM UserInfo A1; Database Application Development - Lecture 3

25 11.11.2015 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 NameAge Amy24 Bob37 Cindy21 Derry42 NameAge Derry42 Bob37 Amy24 Cindy21 Table: UserInfoResult set Select Order by

26 11.11.2015 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

27 11.11.2015 Multiple tables joint query Database Application Development - Lecture 3 IDName 1Anny 2Bob 3Charlie IDCourseGrade 1MathA 1EnglishB 2GeographyA 3HistoryC 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” s.IDs.Nameg.IDg.Courseg.Grade 2Bob2GeographyA Query result

28 11.11.2015 You can find more detail on following website: http://sql.1keydata.com/cn/ Database Application Development - Lecture 3


Download ppt "11.11.2015 DAT602 Database Application Development Lecture 3 Review of SQL Language."

Similar presentations


Ads by Google