Presentation is loading. Please wait.

Presentation is loading. Please wait.

IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL.

Similar presentations


Presentation on theme: "IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL."— Presentation transcript:

1 IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL

2 Important Events Back from Spring Break (hopefully) Welcome Back

3 Keepin Up Read Pol and Ahuja – Chapter 8 (should have already done this) Pol and Ahuja – Chapter 9 – SQL Pol and Ahuja – Chapter 10 – Visual Studio

4 SQL SQL – Structured Query Language Standard language for managing and manipulating Relational Databases and Relational DataBase Management Systems RDBMS? Standard – because it is widely used in RDBMSs  …but not in all

5 SQL SQL – used in MS Access (behind the “curtain”) MS SQL Server Oracle DB2 MySQL PostGres …

6 SQL SQL – standard, but… Some variations in SQL across platforms Usually syntactic differences Function differences  Subsets  Arguments

7 SQL SQL – Important Point! SQL is a database management language SQL is not a programming language What does this mean? Why does this matter? What can we do about this?

8 SQL SQL – four components to the language DDL – Data Definition Language  Define/delete db objects DML – Data Manipulation Language  Language for using db DCL – Data Control Language  Defines/sets control features of db DSPL – Data Stored Procedure Language  Tools for creating SQL modules

9 SQL SQL – four components to the language DCL and DSPL – outside the scope of this class (mostly) DDL and DML – we will take a closer look at these two languages

10 SQL SQL – four components to the language DCL and DSPL – outside the scope of this class (mostly) DDL and DML – we will take a closer look at these two languages

11 DDL DDL – Data Definition Language Create  TABLES  PRIMARY KEYS  FOREIGN KEYS  INDEXES

12 DDL CREATE tables CREATE TABLE [tblName] ({ }, {},…) CONSTRAINT { PRIMARY KEY (fieldname1, fieldname2,…) {}… ;

13 DDL CREATE tables CREATE TABLE tblDepartment (DeptIDVARCHAR2 (10) NOT NULL, NameVARCHAR2 (20), Address VARCHAR2 (200), PhoneNUMBER (30), CollegeIDVARCHAR2(3) ) CONSTRAINT deptPK PRIMARY KEY (DeptID), deptFK FOREIGN KEY (CollegeID) REFERENCES tblCollege (CollegeID) ;

14 DDL CREATE tables Standard SQL datatypes  VARCHAR(maxbytes) -- TEXT same in MySQL Max maxbytes – Oracle = 4000; SQL Server=8000; MySQL=65,532 (see docs for specific DB platform  DECIMAL(precision, scale) (optional) Numeric base10 to a specific precision and scale (number of fractional decimal positions) DECIMAL(9,2) could store 9,999,999.99  DATE (some variation across DB platforms) DATE TIME TIMESTAMP

15 DDL CREATE tables Standard SQL datatypes  SMALLINT 2 bytes -32,768 to 32,767  INTEGER 4 bytes -2,147,483,648 to a 2,147,483,647  BIGINT 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

16 DDL CREATE Indexes CREATE INDEX tblDeptAddressIndex ON tblDepartment (Address) ;

17 DDL DDL – Data Definition Language DROP – deletes db objects DROP TABLE – deletes a table (and its data) from a db …not used to drop a field from a table or a record from a table

18 DDL DDL – Data Definition Language DROP TABLE [tblName];  DROP TABLE tblInstructors; DROP INDEX [tblNameindex]  DROP INDEX tblStudentIndex;

19 DDL DDL – Data Definition Language ALTER – modifies db objects Add a field to a table ALTER tblDepartment ADD COLUMN Chair Number (10) ADD CONSTRAINT deptFK2 FOREIGN KEY (Chair) REFERENCES tblFaculty(FacultyID) ;

20 DDL DDL – Data Definition Language ALTER – modifies db objects Delete a field from a table ALTER tblDepartment DROP Chair DROP CONSTRAINT DeptFK2 ;

21 DML DML – Data Manipulation Language Language to use DB Language to do queries and other DB functions Why do you need to know this?

22 DML Select Queries SELECT [fieldName, fieldName2,…] FROM [tblName]; SELECT Name, Phone FROM tblDept; SELECT * FROM tblStudent;

23 DML Select Queries - JOIN SELECT [fieldName, fieldName2,…] FROM [tblName1, tblName2, tblName3,…]; SELECT Name, Email, DeptName FROM tblFaculty tblDept; Simple Query with Join But what does this do?

24 DML Select Queries – JOIN SELECT Name, Email, DeptName FROM tblFaculty tblDept; Simple Query with Join But what does this do? Cartesian Product What’s that?

25 DML Select Queries – JOIN and the WHERE clause SELECT Name, Email, DeptName FROM tblFaculty tblDept WHERE tblFaculty.DeptID=tblDept.DeptID;

26 DML Select Queries – JOIN and the WHERE clause How about this – Name in tblFaculty, Name in tblDept SELECT Name, Email, Phone, Name FROM tblFaculty tblDept WHERE tblFaculty.DeptID=tblDept.DeptID; Two fields in query with same identifier? Oh my!

27 DML Select Queries – JOIN and the WHERE clause Use a qualifier – fully specify field name using its table name SELECT tblFaculty.Name, Email, Phone, tblDept.Name FROM tblFaculty tblDept WHERE tblFaculty.DeptID=tblDept.DeptID;

28 DML Select Queries – JOIN and the WHERE clause WHERE clause operators –  = >= !=  BETWEEN, LIKE, IN  AND, OR, NOT

29 DML Select Queries – JOIN and the WHERE clause SELECT Name, Email, Salary, StartDate, tblDept.Name FROM tblFaculty, tblDept WHERE tblFaculty.DeptID=tblDept.DeptID AND (Salary > 80000) AND (StartDate > #1/1/1995#);

30 DML Select Queries – JOIN and the WHERE clause BETWEEN – matching a range SELECT Name, Email, Salary, StartDate, tblDept.Name FROM tblFaculty, tblDept WHERE tblFaculty.DeptID=tblDept.DeptID AND (Salary BETWEEN 70000 AND 80000) AND (StartDate > #1/1/1995#);

31 DML Select Queries – JOIN and the WHERE clause BETWEEN – matching a range SELECT Name, Email, Salary, StartDate, tblDept.Name FROM tblFaculty, tblDept WHERE tblFaculty.DeptID=tblDept.DeptID AND (Salary BETWEEN 70000 AND 80000) AND (StartDate > #1/1/1995#);

32 DML Select Queries – JOIN and the WHERE clause LIKE – matching a string or part of one SELECT Name, Email, Rank FROM tblStudent, tblRegister, tblCourses WHERE (tblStudent.SID=tblRegister.StudentID) AND (tblRegister.CourseID=tblCourses.CourseID) AND (tblCourses.Cname LIKE “Cooking*” OR tblCourses.Cname LIKE “Food*” );

33 DML Select Queries – JOIN and the WHERE clause LIKE – matching a string or part of one LIKE match operators * - zero or more characters ? – zero or one character % - any number of characters ( %science% ) _ - any one character

34 DML Select Queries – JOIN and the WHERE clause Using aliases AS -- lets your rename objects  Within the query  Alias does not persist SELECT Name, Email, Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID=tR.StudentID) AND (tR.CourseID=tC.CourseID) AND (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

35 DML Select Queries – JOIN and the WHERE clause Using aliases AS -- lets your rename objects  Within the query  Alias does not persist SELECT CONCAT(FirstName,” “,LastName) AS FullName, Email, Rank FROM tblStudent;

36 DML Select Queries – JOIN and the WHERE clause Sometimes we only want one result per entity

37 DML Select Queries – JOIN and the WHERE clause Remember this? What would we get as a result?

38 DML Select Queries – JOIN and the WHERE clause Remember this? What would we get as a result? SELECT Name, Email, Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID IN SELECT DISTINCT StudentID FROM tR WHERE tR.CourseID IN SELECT CourseID FROM tC WHERE (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

39 DML Select Queries – JOIN and the WHERE clause The keywork DISTINCT causes only one record per criteria match to be returned SELECT Name, Email, Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID IN SELECT DISTINCT StudentID FROM tR WHERE tR.CourseID IN SELECT CourseID FROM tC WHERE (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

40 DML Select Queries – JOIN and the WHERE clause Did you notice something else new? The keyword IN SELECT Name, Email, Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID IN SELECT DISTINCT StudentID FROM tR WHERE tR.CourseID IN SELECT CourseID FROM tC WHERE (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

41 DML Select Queries – JOIN and the WHERE clause Did you notice something else new? The keyword IN  Allows you to nest queries in queries

42 DML Select Queries – Ordering the results Use the ORDER BY clause SELECT [fields] FROM [tables] WHERE [select_criteria] ORDER BY [field1 ;

43 DML Select Queries – Ordering the results Use the ORDER BY clause SELECT Name, Email, Phone, StartDate FROM tblFaculty ORDER BY StartDate ASC; SELECT Name, Email, Phone, StartDate FROM tblFaculty ORDER BY StartDate DESC, Name ASC;

44 DML Select Queries – Summarizing data – getting some stats The GROUP BY clause SELECT tblDept.Name, COUNT(tblStudent.SID) AS StudentCount), AVG(tblStudent.GPA) AS MeanGPA FROM tblStudent, tblDept WHERE tblStudent.DeptID = tblDept.DeptID AND (tblDept.College=“CEMR”) GROUP BY tblDept.Name;

45 DML Select Queries – Summarizing data – getting some stats The HAVING clause, like WHERE but for GROUP BY SELECT tblDept.Name, COUNT(tblStudent.SID) AS StudentCount), AVG(tblStudent.GPA) AS MeanGPA FROM tblStudent, tblDept WHERE tblStudent.DeptID = tblDept.DeptID AND (tblDept.College=“CEMR”) GROUP BY tblDept.Name HAVING COUNT(tblStudent.SID >25);

46 DML Action Queries INSERT Queries DELETE Queries UPDATE Queries

47 DML Action Queries INSERT Queries INSERT INTO [table] (field1, field2, …) Values(“value1”, value2,…);

48 DML Action Queries INSERT Queries INSERT INTO tblStudent (Name, Age, Email) Values(“Bob Smith”, 21, “bob.smith@mix.wvu.edu”); Order is important Datatypes must match

49 DML Action Queries DELETE Queries Deletes records from a table DELETE FROM [table] WHERE [delete_criteria];

50 DML Action Queries DELETE Queries Deletes records from a table DELETE FROM tblStudent WHERE Rank=“Senior”;

51 DML Action Queries Update Queries UPDATE [table] SET [field=value, field=value, …] WHERE [update_criteria];

52 DML Action Queries Update Queries UPDATE tblFaculty SET Salary = Salary *1.01 WHERE StartDate < 1/1/1920;

53


Download ppt "IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL."

Similar presentations


Ads by Google