CSC443 Integrity Set operations List operations. Review Commands: Create Drop Alter Insert Select.

Slides:



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

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Constraints, Triggers and Active Databases Chapter 9.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Virtual training week 4 structured query language (SQL)
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
More sql Session 4 – subselects, union, aggregate functions.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Lists, Views, Describing tables Kris Pepper. Where are we Finishing up integrity, sets and lists, and now adding in views and describe. Finishing homework.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS CS AT00000.
Database Systems More SQL Database Design -- More SQL1.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
The Relational Model These slides are based on the slides of your text book.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Oracle Developer. Create Table SQL> create table catalog ( 2 cno varchar2(7), 3 ctitle varchar2(50), 4 primary key (cno)); Table created. SQL> describe.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Constraints and Triggers
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
The Relational Model Relational Data Model
مقدمة في قواعد البيانات
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
Structured Query Language – The Basics
CS 405G: Introduction to Database Systems
Presentation transcript:

CSC443 Integrity Set operations List operations

Review Commands: Create Drop Alter Insert Select

Syllabus New syllabus in course info Reading this week ; 8.6-8; finishing chap 3 Test: mostly on executing sql statements

Integrity - Primary key Primary key uniqueness was serious SQL> insert into odetails values(1020, 10506, 1); 2 insert into odetails * ERROR at line 1: ORA-00001: unique constraint (S06_011.SYS_C ) violated

Integrity - Foreign key Foreign key existence was serious SQL> insert into customers values (1113,'James','123 Park Ave',66666,' '); insert into customers values (1113,'James','123 Park Ave',66666,' ') * ERROR at line 1: ORA-02291: integrity constraint (PEPPERK.SYS_C ) violated - parent key not found

Integrity - Check or Not Null qoh int check(qoh >= 0), -> set to -3 SQL> insert into parts values (3352,'bolt', - 3,55.2,1); insert into parts values (3352,'bolt', -3,55.2,1) * ERROR at line 1: ORA-02290: check constraint (PEPPERK.SYS_C ) violated

Large Object Types Clob = characters Blob = binary Book_review clob(10KB) Image blob(20MB)

Assertions and Domains Domain - a new data type with a pre- defined check Assertion - statement that the database always validates is true DOES NOT EXIST IN SQL

Triggers Procedure to keep tables in balance: First, created a reorder table: SQL> create table reorder 2 (pno number(5) primary key, 3 qty number(38));

Triggers - cont Trigger example: create trigger reorder_rule after update on parts referencing new as nrow for each row when (nrow.qoh < 200) begin insert into reorder values (:nrow.pno, 300); end reorder_rule;

Triggers - last one Result: SQL> update parts set qoh = 50 where qoh = 60; 1 row updated. SQL> select * from reorder; PNO QTY

Commit and Rollback Transactions - Need all together in order to be in sync. Example: SQL> commit; Commit complete. SQL> update parts set qoh = 30 where qoh = 100; 1 row updated. SQL> select * from parts where qoh = 100; no rows selected

Commit & Rollback Cont SQL> rollback; Rollback complete. SQL> select * from parts where qoh = 100; PNO PNAME QOH PRICE OLEVEL Dr. Zhivago

Back into Query Swing List the first name of each student with a grade of less than 75 in any course. List the average grade of each student assuming every component equal weight Change the query above to exclude compname “programs” and to only show students with avg over 80.

Answers Select distinct fname from students s, scores g where s.sid = g.sid and points<75; Select avg(points), sid from scores group by sid; Select avg(points) as pavg, sid from scores where compname != 'programs' group by sid having avg(points) > 80;

Set Operations Union Intersect Except –Optional: all

Union Example: Show the max points from components mixed as another row in scores select * from (select * from scores union all (select 'MAX',term, lineno, compname, maxpoints from components)) order by lineno, compname, sid;

Intersect Find all sids with scores: select sid from students intersect select sid from scores; Find all sids with no scores select sid from students where sid not in (select sid from scores intersect select sid from students);

Except - Minus Select all students with no grades Select distinct sid from students minus (select sid from scores);

SubQueries and Lists Using a sub-query to get one value Select distinct sid from scores where points > (select avg(points) from scores);

List Comparison - in Compare to values in a list - in or not in List student first names of sid 1111 and 2222 Select distinct fname from students where SID in (1111, 2222);

List Comparison - in Can use a subquery to create the list: List student first names who are enrolled in term f96 Select distinct fname from enrolls where term = ‘f96’); You try: List first names of students in course 1030, 1031 and 1035

List Comparison - any, all, some >,,=, >=, <= Select scores greater than all in the list: Select distinct sid from scores where points >= all (90, 340, 70) Substitute a query listing all points

List Comparison - any, all, some Select scores greater than all in the list: Select distinct sid from scores where points >= all (select points from scores); 1111 You try: List scores less than all in the list

List Comparison - any, all, some Select distinct sid from scores where points <= all (select points from scores); 2222 Try one more: List all scores which are greater than the average score

Sub-Query using outer Sub query acts for every row in the main (outer) query Use the fields from the outer query in the inner query List of courses in which all individual components grades are higher than B: Select term, lineno, cno from courses where b <all (select points from scores where scores.lineno = courses.lineno and scores.term = courses.term);

Exists If subquery has any row in result, then exists is TRUE If no row results, then FALSE Select all courses in which 1111 is enrolled: select distinct cno from courses where exists (select 'a' from enrolls where courses.lineno = enrolls.lineno and sid = ‘1111’); You try: list all the fields from the student table where any score is greater than 90. (use exists)

Exists select * from students where exists (select ‘a’ from scores where students.sid = scores.sid and points > 90);

Important Stuff Database checks integrity Subqueries help make lists for where conditions In, All, Some, Any Exists