February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Chapter 4 Joining Multiple Tables
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems More SQL Database Design -- More SQL1.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Introduction to SQL J.-S. Chou Assistant Professor.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Database Technical Session By: Prof. Adarsh Patel.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Server 7.0 Maintaining Referential Integrity.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Fundamentals of DBMS Notes-1.
More SQL: Complex Queries,
Top 50 SQL Interview Questions & Answers
Relational Database Design
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Module 5: Implementing Data Integrity by Using Constraints
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 4 Indexes.
CH 4 Indexes.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CH 4 Indexes.
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Chapter 8 Advanced SQL.
Presentation transcript:

February 18, 2012 Lesson 3 Standard SQL

Lesson 3 Standard SQL

Data Statements – query and modify tables and columns SELECT Statement – query tables and views in the database INSERT Statement – add rows to tables UPDATE Statement – modify columns in table rows DELETE Statement – remove rows from tables Schema Statements – maintain schema (catalog) CREATE TABLE Statement – create tables CREATE VIEW Statement – create views DROP TABLE Statement -- drop tables DROP VIEW Statement -- drop views Constraints & Programmability Constraints Procedures Function Triggers

Standard SQL – Data Statements SELECT Statement -- query tables and views in the database SELECT Statement Basics SELECT Clause FROM Clause WHERE Clause Extended Query Capabilities ORDER BY Clause Value Expressions Joining Tables Subqueries Grouping Queries (GROUP BY) Aggregate Queries (Set Functions) Union Queries (UNION) SQL Modification Statements INSERT Statement -- add rows to tables VALUES Clause UPDATE Statement -- modify columns in table rows SET Clause DELETE Statement -- remove rows from tables

SELECT Statement Basics The SQL SELECT statement queries data from tables in the database. The statement begins with the SELECT keyword. The basic SELECT statement has 3 clauses: SELECT FROM WHERE The SELECT clause specifies the table columns that are retrieved. The FROM clause specifies the tables accessed. The WHERE clause specifies which table rows are used. The WHERE clause is optional; if missing, all table rows are used. For example, SELECT name FROM s WHERE city='Rome' This query accesses rows from the table - s. It then It then filters those rows where the city column contains Rome. Finally, the query retrieves the name column from each filtered row.

SELECT - Extended Query Capabilities Sorting Query Results -- using the ORDER BY clause Expressions -- in the SELECT clause and WHERE clause Literal -- self-defining values Function Call -- expression functions System Value -- builtin system values Special Construct -- special expression construct Numeric or String Operator -- expression operators Joining Tables -- in the FROM clause Outer Join -- extended join Self Join -- joining a table to itself Subqueries -- embedding a query in another Predicate Subqueries -- subqueries in logical expressions Scalar Subqueries -- subqueries in scalar expressions Table Subqueries -- subqueries in the FROM clause Grouping Queries -- using the GROUP BY clause, Set Function and HAVING clause GROUP BY Clause -- specifying grouping columns Set Functions -- summary functions HAVING Clause -- filtering grouped rows Aggregate Queries -- using Set Functions and the HAVING clause Union Queries -- using the query operator, UNION Union-Compatible Queries -- query requirements for Union

Joining Tables The FROM clause allows more than 1 table in its list, however simply listing more than one table will very rarely produce the expected results. The rows from one table must be correlated with the rows of the others. This correlation is known as joining.

Join Example An example can best illustrate the rationale behind joins. The following query: SELECT * FROM sp, p Produces:

snopnoqtypnodescrcolor S1P1NULLP1WidgetBlue S1P1NULLP2WidgetRed S1P1NULLP3DongleGreen S2P1200P1WidgetBlue S2P1200P2WidgetRed S2P1200P3DongleGreen S3P11000P1WidgetBlue S3P11000P2WidgetRed S3P11000P3DongleGreen S3P2200P1WidgetBlue S3P2200P2WidgetRed S3P2200P3DongleGreen SELECT * FROM sp, p Each row in sp is arbitrarily combined with each

Inner Join Example A more usable query would correlate the rows from sp with rows from p, for instance matching on the common column -- pno: SELECT * FROM sp, p WHERE sp.pno = p.pno snopnoqtypnodescrcolor S1P1NULLP1WidgetBlue S2P1200P1WidgetBlue S3P11000P1WidgetBlue S3P2200P2WidgetRed This produces:

Outer Joins An outer join provides the ability to include unmatched rows in the query results. The outer join combines the unmatched row in one of the tables with an artificial row for the other table. This artificial row has all columns set to null. The outer join is specified in the FROM clause and has the following general format: table-1 { LEFT | RIGHT | FULL } OUTER JOIN table-2 ON predicate-1 predicate-1 is a join predicate for the outer join. It can only reference columns from the joined tables. The LEFT, RIGHT or FULL specifiers give the type of join: LEFT -- only unmatched rows from the left side table (table-1) are retained RIGHT -- only unmatched rows from the right side table (table-2) are retained FULL -- unmatched rows from both tables (table-1 and table-2) are retained

Outer Join Example: SELECT pno, descr, color, sno, qty FROM p LEFT OUTER JOIN sp ON p.pno = sp.pno pnodescrcolorsnoqty P1WidgetBlueS1NULL P1WidgetBlueS2200 P1WidgetBlueS31000 P2WidgetRedS3200 P3DongleGreenNULL

INSERT Statement The INSERT Statement adds one or more rows to a table. It has two formats: INSERT INTO table-1 [(column-list)] VALUES (value-list) & INSERT INTO table-1 [(column-list)] (query-specification) The first form inserts a single row into table-1 and explicitly specifies the column values for the row. The second form uses the result of query-specification to insert one or more rows into table-1. The result rows from the query are the rows added to the insert table. Note: the query cannot reference table-1.

UPDATE Statement The UPDATE statement modifies columns in selected table rows. It has the following general format: UPDATE table-1 SET set-list [WHERE predicate] The optional WHERE Clause has the same format as in the SELECT Statement. The WHERE clause chooses which table rows to update. If it is missing, all rows are in table-1 are updated.

SET Clause The SET Clause in the UPDATE Statement updates (assigns new value to) columns in the selected table rows. It has the following general format: SET column-1 = value-1 [, column-2 = value-2]... column-1 and column-2 are columns in the Update table. value-1 and value-2 are expressions that can reference columns from the update table. They also can be the keyword -- NULL, to set the column to null.

DELETE Statement The DELETE Statement removes selected rows from a table. It has the following general format: DELETE FROM table-1 [WHERE predicate] The optional WHERE Clause has the same format as in the SELECT Statement. The WHERE clause chooses which table rows to delete. If it is missing, all rows are in table-1 are removed. The WHERE Clause predicate can contain subqueries, but the subqueries cannot reference table-1. This prevents situations where results are dependent on the order of processing.

Schema Statements Maintain Schema (catalog) CREATE TABLE Statement – create tables CREATE VIEW Statement – create views DROP TABLE Statement – drop tables DROP VIEW Statement – drop views

Constraints Check Unique Primary Key Foreign Key Not Null

CHECK constrains A constraint is an object which tells the DBMS: don't permit updates of the database which would break rules established for business/integrity reasons. A CHECK constraint works thus: CREATE TABLE TABLE_1 (COLUMN_1 SMALLINT); -- make the table ALTER TABLE TABLE_1 ADD CONSTRAINT CONSTRAINT_1 CHECK (COLUMN_1 > 0); -- make the constraint INSERT INTO TABLE_1 VALUES (5); -- this succeeds UPDATE TABLE_1 SET COLUMN_1 = -5; -- this fails

PRIMARY KEY constraint A PRIMARY KEY constraint works thus: CREATE TABLE TABLE_1 (COLUMN_1 SMALLINT); ALTER TABLE TABLE_1 ADD CONSTRAINT CONSTRAINT_1 PRIMARY KEY (COLUMN_1); INSERT INTO TABLE_1 VALUES (5); A table's primary key must contain unique values, so the second INSERT in this example will fail. Alternative: if all we want to say is "values must be unique", we could say UNIQUE (COLUMN_1) instead of PRIMARY KEY (COLUMN_1).

FOREIGN KEY constraint A FOREIGN KEY constraint works thus: CREATE TABLE TABLE_1 (COLUMN_1 SMALLINT); ALTER TABLE TABLE_1 ADD CONSTRAINT CONSTRAINT_1 PRIMARY KEY (COLUMN_1); INSERT INTO TABLE_1 VALUES (5); CREATE TABLE TABLE_2 (COLUMN_1 SMALLINT); ALTER TABLE TABLE_2 ADD CONSTRAINT FOREIGN KEY (COLUMN_1) REFERENCES TABLE_1; INSERT INTO TABLE_2 VALUES (5); INSERT INTO TABLE_2 VALUES (6); The first INSERT into TABLE_2 will succeed, because our foreign-key value (5) exists in the primary-key table that we're referencing. But we never inserted a 6 into TABLE_1, so we can't insert a 6 into TABLE_2, so the second INSERT in this example will fail.

Programmability Stored Procedure Function Scalar value Table value Trigger

Stored Procedure

Function – Scalar value

Function – Table value

Trigger

Questions

Lab 2 Class Scheduling System in SQL Server Create ClassSchedule database Load data Execute Queries