Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.

Slides:



Advertisements
Similar presentations
Using the SQL Access Advisor
Advertisements

Structured Query Language (SQL)
ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access.
Advanced SQL Topics Edward Wu.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Structured Query Language (SQL)
Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Refreshing Materialized Views
10 Copyright © 2005, Oracle. All rights reserved. Dimensions.
12 Copyright © 2005, Oracle. All rights reserved. Query Rewrite.
Using the Set Operators
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Introduction to Relational Database Systems 1 Lecture 4.
Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax might slightly differ different features might be.
Views-basics 1. 2 Introduction a view is a perspective of the database different users may need to see the database differently; this is achieved through.
Relational data integrity
1 Term 2, 2004, Lecture 6, Views and SecurityMarian Ursu, Department of Computing, Goldsmiths College Views and Security 3.
Data Definition and Integrity Constraints
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL4.
1 Constraints and Updating Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section 7: Constraints.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL3.
SQL: The Query Language Part 2
Chapter 7 Working with Databases and MySQL
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
More SQL Data Definition
MySQL Access Privilege System
Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course.
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
1 Chapter 12 Data Manipulation Language (DML) View Dr. Chitsaz Objectives Definition Create a view Retrieve data from a view Insert, delete, update to/from.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Addition 1’s to 20.
Week 1.
13 Copyright © Oracle Corporation, All rights reserved. Controlling User Access.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Database Systems More SQL Database Design -- More SQL1.
Security and Integrity
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.
Database Programming Sections 13–Creating, revoking objects privileges.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
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.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
 CONACT UC:  Magnific training   
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Controlling User Access
Controlling User Access
More SQL: Complex Queries,
Objectives User access Create users Create roles
TABLES AND INDEXES Ashima Wadhwa.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
IST 318 Database Administration
Presentation transcript:

Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5

CS252 Fundamentals of Relational Databases 2 Interim Summary Topics covered: Indexes, Sequences, Relational Algebra and Advanced Joins

CS252 Fundamentals of Relational Databases 3 This lecture Query re-write NULLs and 3-valued logic Top N Analysis Security Privileges

CS252 Fundamentals of Relational Databases 4 CASE The CASE operation returns one of a specified set of scalar values depending on some condition. SELECT artist, year, (CASE WHEN year >= 2000 THEN 'Noughties' WHEN year >= 1990 THEN 'Nineties' WHEN year >= 1980 THEN 'Eighties' WHEN year >= 1970 THEN 'seventies' ELSE 'Before my time' END) label FROM Collection;

CS252 Fundamentals of Relational Databases 5 Redundancy of GROUP BY For every select expression that involves GROUP BY or HAVING there is an equivalent expression that does not. SELECT student,SUM(mark) Total FROM CS_marks GROUP BY student; can be re-written to be : SELECT DISTINCT student, ( SELECT SUM(mark) FROM CS_marks CSM WHERE CS_marks.student = CSM.student ) Total FROM CS_marks CSM;

CS252 Fundamentals of Relational Databases 6 Example with HAVING SELECT student,SUM(mark) Total FROM CS_marks GROUP BY student HAVING SUM(mark) > 100; can be re-written as SELECT student, Total FROM ( SELECT student, SUM(mark) AS Total FROM CS_marks GROUP BY student) t WHERE Total > 100

CS252 Fundamentals of Relational Databases 7 NULL What does NULL mean ? SQL represents the fact that some piece of information is missing by the use of a special marker 'NULL'. Mark of student Tim is NULL This means : We know that Tim exists We know that Tim has a mark We don't know what the mark is

CS252 Fundamentals of Relational Databases 8 NULL and scalar expressions If x or y (or both) is NULL then x + y x - y x * y x / y all evaluate to NULL

CS252 Fundamentals of Relational Databases 9 NULL and aggregate functions Aggregate functions ignore NULL 's apart from COUNT(*) So SUM(column) is not the same as adding up all the values in the column with '+' Aggregate functions where the input column contains no values return NULL COUNT(*) returns 0 when counting no rows.

CS252 Fundamentals of Relational Databases 10 NULL and three-valued logic SQL conditions can evaluate to true, false or unknown. This is a three-valued logic and our logic operators AND/OR/NOT need to be defined over all 3 values : AND t u f t t u f u u u f f f

CS252 Fundamentals of Relational Databases 11 OR t u f t t u t u u f t u f NOT t f u f t If our WHERE condition evaluates to True then the row is included in the result; otherwise (if it is false or unknown) it is discarded.

CS252 Fundamentals of Relational Databases 12 Default Column Values When defining a table using CREATE TABLE the default value of a column is NULL. This can be changed in the CREATE TABLE command : CREATE TABLE CS_marks ( student VARCHAR(20), course CHAR(3), mark INTEGER DEFAULT 50 ); INSERT INTO CS_marks (student,course) VALUES ('Paul Smith','DBS'); Inserts the row ('Paul Smith', 'DBS', 50) into the CS_marks table.

CS252 Fundamentals of Relational Databases 13 TOP N Analysis We can sort the results of a SELECT statement with ORDER BY and ORDER BY... DESC Once we have defined an ORDER we may only want to return the top 10 records. In SQL Server you can write SELECT TOP 10 FROM CS_marks ORDER BY mark In MySQL you can write SELECT FROM CS_marks ORDER BY mark LIMIT 10

CS252 Fundamentals of Relational Databases 14 In Oracle things are more complicated. When Oracle evaluates a select query it appends a column to the result called 'ROWNUM' before any ORDER BY, GROUP BY or DISTINCT clauses are run. So the query: SELECT Student,Mark,ROWNUM FROM CS_marks ; Gives : STUDENT MARK ROWNUM Paul Smith 43 1 Rachel Sewell 57 2 Helen Treacy 72 3 Paul Smith 65 4 Rachel Sewell 42 5

CS252 Fundamentals of Relational Databases 15 while SELECT Student,Mark,ROWNUM FROM CS_marks ORDER BY Mark; Gives : STUDENT MARK ROWNUM Rachel Sewell 42 5 Paul Smith 43 1 Rachel Sewell 57 2 Paul Smith 65 4 Helen Treacy 72 3 Which isn't very helpful, but if we force Oracle to append the ROWNUM after it has done the order then we can use it to limit the rows we get.

CS252 Fundamentals of Relational Databases 16 SELECT Student,Mark,ROWNUM FROM (SELECT Student,Mark FROM CS_marks ORDER BY Mark); STUDENT MARK ROWNUM Rachel Sewell 42 1 Paul Smith 43 2 Rachel Sewell 57 3 Paul Smith 65 4 Helen Treacy 72 5

CS252 Fundamentals of Relational Databases 17 SELECT Student,Mark,ROWNUM FROM (SELECT Student,Mark,ROWNUM FROM CS_marks ORDER BY Mark) WHERE ROWNUM < 3; Gives us the lowest two marks : STUDENT MARK ROWNUM Rachel Sewell 42 1 Paul Smith 43 2

CS252 Fundamentals of Relational Databases 18 SQL Syntax for Security Rules GRANT [privilege-commalist | ALL PRIVILEGES] ON object-name TO [authorisation_id_list | PUBLIC] [WITH GRANT OPTION] Each privilege is one of the following: SELECT DELETE INSERT [ (attribute-commalist)] UPDATE [ (attribute-commalist) ] REFERENCES [ (attribute-commalist) ] The REFERENCES allows privileges to be granted on named table(s) in integrity constraints of CREATE TABLE. The GRANT OPTION allows the named users to pass the privileges on to other users.

CS252 Fundamentals of Relational Databases 19 Exercise: Base relation STATS looks like this: STATS (USERID, SEX, DEPENDENTS, JOB, SALARY, TAX, AUDITS) PRIMARY KEY (USERID) Write rules for the following: (a) User John SELECT privileges over the entire table. (b) User Fred UPDATE privileges over the TAX column only. (c) How would you grant user Pope full privileges over rows for job type 'Priest' only?

CS252 Fundamentals of Relational Databases 20 Grant and Revoke If a user A grants privileges to user B, then they can also revoke them e.g. REVOKE ALL PRIVILEGES ON STATS FROM John; SQL REVOKE syntax REVOKE [GRANT OPTION FOR] [privilege_list | ALL PRIVILEGES] ON object_name FROM [authorisation_list|PUBLIC] [RESTRICT|CASCADE] If RESTRICT option is given then the command is not executed if any dependent rules exist i.e. those created by other users through the WITH GRANT OPTION. CASCADE will force a REVOKE on any dependent rules.

CS252 Fundamentals of Relational Databases 21 Listing your security privileges Under Oracle you can list your security privileges by using SELECT * FROM SESSION_PRIVS; PRIVILEGE CREATE SESSION ALTER SESSION CREATE TABLE CREATE CLUSTER CREATE SYNONYM CREATE VIEW CREATE SEQUENCE CREATE DATABASE LINK 8 rows selected.

CS252 Fundamentals of Relational Databases 22 Interim Summary Query re-write NULLs and 3-valued logic Top N Analysis Security Privileges