Presentation is loading. Please wait.

Presentation is loading. Please wait.

M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)

Similar presentations


Presentation on theme: "M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)"— Presentation transcript:

1 M Taimoor Khan taimoorkhan@ciit-attock.edu.pk

2 Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs) 5) Mappings (ERDs) 6) Formulating queries (Relational algebra) 7) Implementing Schema 8) Built-in Functions 9) Extracting data 10) Working with Joins 11) Normalization 12) Improving performance 13) Advanced topics

3 9) Extracting Data o DML o SHOW, DESCRIBE o SELECT o Projecting on certain columns o Applying conditions on records o Other KEYWORDS / OPERATORS

4 Select Statement  Maximum used command in DML  Used not only to select certain rows but also the columns  Also used for different forms of product, that is, different joins

5 Select  Selecting rows from one or more tables  SELECT {*|col_name[,….n]} FROM table_name  Example (list all students)  SELECT * FROM student;

6

7 PROJECT on certain columns SELECT stName, prName FROM student

8

9 Attribute Alias SELECT stName as ‘Student Name’, prName ‘Program’ FROM Student

10

11 Formatting reports Select stId, crCode, mTerm + sMrks ‘Total out of 50’ from enroll

12

13 Formatting reports SELECT stName + ‘ studies in ‘ + prName FROM student

14

15 Unique values  The DISTINCT keyword is used to return only distinct (different) values

16 Select Distinct  Just add a DISTINCT keyword to the SELECT statement  SELECT DISTINCT column_name(s) FROM table_name

17 Select Distinct Q: Get the program names in which students are enrolled SELECT prName FROM Student

18

19 Q: Get the program names in which students are enrolled SELECT prName FROM Student SELECT DISTINCT prName FROM Student Select Distinct

20

21 The WHERE Clause  We used names to limit columns, but rows cannot be named due to the dynamicity  We limit the rows using conditions  Condition comes after the WHERE clause  Condition is applied on a single or multiple columns providing it with some particular value or set of values

22 Student table stuIDstuNamecitypostCodeSem 100037AftabIslamabad440004 100343NaveedLahore350003 100014KamalKarachi230003 200033FawadMardan740002 200087ZamanRawal pindi430001 100031AdnanAttock210001 100011FaisalHazro650002

23 Example SELECT stuName, sem FROM student WHERE city=“islamabad”;

24 LIMIT  It limits the number of records in the result set as per the request  Examples  SELECT * FROM student LIMIT 0, 100;  SELECT stuID, stuName FROM student LIMIT 2, 5; SELECT stuID, stuName FROM student WHERE postCode > 21000 LIMIT 0, 10;

25 Examples

26 Selecting All Data  The simplest form of SELECT retrieves everything from a table mysql> select * from pet; +----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f | 1999-02-04 | NULL | | Claws | Gwen | cat | f | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1999-08-27 | NULL | | Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+--------+---------+------+------------+------------+ 8 rows in set (0.00 sec) 26

27 Selecting Particular Rows  You can select only particular rows from your table.  For example, if you want to verify the change that you made to Bowser's birth date, select Bowser's record like this: mysql> SELECT * FROM pet WHERE name = "Bowser"; +--------+-------+---------+------+------------+----------- -+ | name | owner | species | sex | birth | death | +--------+-------+---------+------+------------+----------- -+ | Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 | +--------+-------+---------+------+------------+----------- -+ 1 row in set (0.00 sec) 27

28 Selecting Particular Rows  To find all animals born after 1998 SELECT * FROM pet WHERE birth >= "1998-1-1";  To find all female dogs, use a logical AND SELECT * FROM pet WHERE species = "dog" AND sex = "f";  To find all snakes or birds, use a logical OR SELECT * FROM pet WHERE species = "snake" OR species = "bird"; 28

29 Selecting Particular Columns mysql> select name, birth from pet; +----------+------------+ | name | birth | +----------+------------+ | Fluffy | 1999-02-04 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | | Fang | 1999-08-27 | | Bowser | 1998-08-31 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | +----------+------------+ 8 rows in set (0.01 sec) 29

30 Sorting Data  To sort a result, use an ORDER BY clause.  For example, to view animal birthdays, sorted by date: 30 mysql> SELECT name, birth FROM pet ORDER BY birth; +----------+------------+ | name | birth | +----------+------------+ | Buffy | 1989-05-13 | | Claws | 1994-03-17 | | Slim | 1996-04-29 | | Whistler | 1997-12-09 | | Bowser | 1998-08-31 | | Chirpy | 1998-09-11 | | Fluffy | 1999-02-04 | | Fang | 1999-08-27 | +----------+------------+ 8 rows in set (0.02 sec)

31 Sorting Data  To sort in reverse order, add the DESC (descending keyword) 31 mysql> SELECT name, birth FROM pet ORDER BY birth DESC; +----------+------------+ | name | birth | +----------+------------+ | Fang | 1999-08-27 | | Fluffy | 1999-02-04 | | Chirpy | 1998-09-11 | | Bowser | 1998-08-31 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | +----------+------------+ 8 rows in set (0.02 sec)

32 Selecting Particular Columns mysql> select name, birth from pet; +----------+------------+ | name | birth | +----------+------------+ | Fluffy | 1999-02-04 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | | Fang | 1999-08-27 | | Bowser | 1998-08-31 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | +----------+------------+ 8 rows in set (0.01 sec) 32

33 Sorting Data  To sort a result, use an ORDER BY clause.  For example, to view animal birthdays, sorted by date: 33 mysql> SELECT name, birth FROM pet ORDER BY birth; +----------+------------+ | name | birth | +----------+------------+ | Buffy | 1989-05-13 | | Claws | 1994-03-17 | | Slim | 1996-04-29 | | Whistler | 1997-12-09 | | Bowser | 1998-08-31 | | Chirpy | 1998-09-11 | | Fluffy | 1999-02-04 | | Fang | 1999-08-27 | +----------+------------+ 8 rows in set (0.02 sec)

34 Sorting Data  To sort in reverse order, add the DESC (descending keyword) 34 mysql> SELECT name, birth FROM pet ORDER BY birth DESC; +----------+------------+ | name | birth | +----------+------------+ | Fang | 1999-08-27 | | Fluffy | 1999-02-04 | | Chirpy | 1998-09-11 | | Bowser | 1998-08-31 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | +----------+------------+ 8 rows in set (0.02 sec)

35 9) Extracting Data o DML o SHOW, DESCRIBE o SELECT o Projecting on certain columns o Applying conditions on records o Other KEYWORDS / OPERATORS

36 Next Lecture  SELECT with keywords


Download ppt "M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)"

Similar presentations


Ads by Google