Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.

Similar presentations


Presentation on theme: "1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language."— Presentation transcript:

1 1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language Lots of SQL websites: e.g. http://www.w3schools.com/SQL/ http://www.w3schools.com/SQL/ or http://www.w3schools.com/SQL/ http://www.free-ed.net/free-ed/InfoTech/informit/ITLC15.asphttp://www.free-ed.net/free-ed/InfoTech/informit/ITLC15.asp: Parts 3 & 4 http://www.free-ed.net/free-ed/InfoTech/informit/ITLC15.asp

2 2 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Connect to Your MySQL Server  mysql  show databases; information_schema information_schema test test  use information_schema;  show tables;

3 3 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Create a Table in the Database  use test;  CREATE TABLE Student ( stu_id int, name char(14), dept char(4), age int, city char(10) );  show tables;

4 4 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Insert Data to the Table   INSERT INTO table_name VALUES (value1, value2, value3,...) INSERT INTO Student VALUES (1, 'Alice', 'MATH', 16, 'Taipei');   INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) INSERT INTO Student(stu_id, name) VALUES(2, 'Bob'); INSERT INTO Student(name, age, stu_id) # Note the order of fields VALUES('Charlie', 22, 3);

5 5 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 5 SELECT Query Structure  Basic Form SELECT SELECT FROM FROM WHERE ; SELECT * FROM Student; * denotes all columns Semi-colon at end

6 6 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 6 Column Alias (AS)  Renames attributes for output result SELECT name, dept AS Department FROM Student; In Oracle, you can omit AS NameDepartment AliceMATH BobNULL

7 7 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 7 WHERE Clause  Any Boolean expression involving attribute conditions  Use column names, symbols =, <, etc., calculations, numbers, text column names, symbols =, <, etc., calculations, numbers, text  Combine conditions with AND, OR  Text strings must be enclosed in single quotes case sensitive (in Oracle)! case sensitive (in Oracle)! E.g. this will return nothing from your database E.g. this will return nothing from your database SELECT * FROM Student WHERE Age=20 ;

8 8 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 8 LIKE operator  Used for string comparisons and pattern matching in WHERE clause  Uses wildcards: _ (underscore) : any single character (? in Access) _ (underscore) : any single character (? in Access) % (percent): string of any length (* in Access) % (percent): string of any length (* in Access) SELECT * FROM PERSONNEL WHERE surname LIKE '_A%E' picks 'BATE' and 'MACRAE' but not 'HAMILTON' or 'RAINES' picks 'BATE' and 'MACRAE' but not 'HAMILTON' or 'RAINES'

9 9 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Change the Contents of Records   UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value UPDATE Student SET dept = 'CSIE', age=20, city='Taichung' WHERE stu_id=2; UPDATE Student SET dept = 'CHEM', city='Kaohsiung' WHERE stu_id>2; SELECT * FROM Student; +--------+---------+------+------+-----------+ | stu_id | name | dept | age | city | +--------+---------+------+------+-----------+ | 1 | Alice | MATH | 16 | Taipei | | 2 | Bob | CSIE | 20 | Taichung | | 3 | Charlie | CHEM | 22 | Kaohsiung | | 4 | Dennis | CHEM | 21 | Kaohsiung | +--------+---------+------+------+-----------+

10 10 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) DELETE Rows in a Table   DELETE FROM table_name WHERE some_column=some_value DELETE FROM Student WHERE city LIKE 'Tai%'; DELETE FROM Student WHERE city LIKE 'Tai%';+--------+---------+------+------+-----------+ | stu_id | name | dept | age | city | +--------+---------+------+------+-----------+ | 3 | Charlie | CHEM | 22 | Kaohsiung | | 4 | Dennis | CHEM | 21 | Kaohsiung | +--------+---------+------+------+-----------+

11 11 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Create Two More Tables  CREATE TABLE Course ( cou_id char(8), cou_name char(40), credit int );  CREATE TABLE Score ( stu_id int, cou_id char(8), score int );  source create_table2.sql  source insert.sql

12 12 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 12 Using two or more tables  Can use many tables E.g. E.g. SELECT S.name, cou_id FROM Student S, Score T WEHRE S.stu_id = Score.stu_id; List all tables used in FROM clause List all tables used in FROM clause Specify matching columns in WHERE clause!!!!!! Specify matching columns in WHERE clause!!!!!! Make it clear which table each column belongs to Make it clear which table each column belongs to  Use table.column notation where ambiguous Can use table aliases to shorten Can use table aliases to shorten Table aliases

13 13 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 13 ORDER BY  ORDER BY [ASC|DESC]  Sorts the result according to the column  Can use several levels, e.g. SELECT * FROM PERSONNEL ORDER BY JobTitle, Salary DESC;  Sorts results by jobtitle, and where jobtitle is the same, sorts by Salary, highest first

14 14 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 14 More SELECT features What if we wanted to strip out duplicates from the answer? Use DISTINCT word SELECT DISTINCT city From Student; Can we perform maths in the SELECT? YES!!! SELECT salary/12 AS monthPay SELECT salary + bonus AS totalPay

15 15 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 15 Aggregates  extends SQL  COUNT COUNT(*)  how many tuples? COUNT(*)  how many tuples?  SELECT COUNT(*) FROM Course; COUNT(DISTINCT )  how many unique values in field? COUNT(DISTINCT )  how many unique values in field?  SELECT COUNT(DISTINCT stu_id) FROM Score;  SUM, MAX, MIN, AVG SELECT MAX(age) from Student; SELECT MAX(age) from Student;  You may also calculate SUM(age), but it makes no sense!

16 16 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 16 GROUP BY  Applies aggregate to subsets of tuples (subtotals) DIVSUM(SALARY) 3098400 2095790 10179340 SELECT Div, SUM(Salary) FROM Personnel GROUP BY Div SELECT SUM(Salary) FROM Personnel SELECT Div, SUM(Salary) FROM Personnel Error! SUM(SALARY) 373530

17 17 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 17 Group conditions: HAVING  HAVING For conditions at the group level For conditions at the group level Can only be used with GROUP BY Can only be used with GROUP BY  WHERE is for conditions on individual rows SELECT div, max(salary)- min(salary) FROM PERSONNEL GROUP by div HAVING max(salary)- min(salary)>30000;

18 18 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 18 Use of Aliases  Renaming columns in the result output columns in the result output table abbreviations for use within SQL table abbreviations for use within SQL  Joining a table with itself to find multiples instances of an attribute, e.g. to find multiples instances of an attribute, e.g. Finds employees who share the same manager and the same job title SELECT p1.surname, p2.surname FROM personnel p1, personnel p2 WHERE p1.manager = p2.manager and p1.surname <> p2.surname and p1.jobtitle = p2.jobtitle;

19 19 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 19 Syntax of SELECT The full syntax of an SQL Select statement is SELECT [DISTINCT] FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ]; […] denotes optional parts. Explicit JOIN not included

20 20 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 20 Keyword Definitions  WHERE A condition on individual tuples determines whether it is included in the result A condition on individual tuples determines whether it is included in the result implicit joins (e.g. table1.key = table2.key ) implicit joins (e.g. table1.key = table2.key )  GROUP BY Collects together tuples which have the same value for the specified fields Collects together tuples which have the same value for the specified fields  HAVING A condition on each group determines whether that group is included in result A condition on each group determines whether that group is included in result  ORDER BY The result table is sorted with this clause The result table is sorted with this clause

21 21 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Exercise  List all courses and the average score of students enrolled to this course.


Download ppt "1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language."

Similar presentations


Ads by Google