Updates - Insert. SQL Other Features and Commands. Updates Bulk Insert. Outline. Update commands Views: more details NULL values Constraints

Similar documents
SQL - Basics. SQL Overview

SQL Aggregation and Grouping. Outline

SQL Aggregation and Grouping. Davood Rafiei

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

Querying Data with Transact SQL

SQL (Structured Query Language)

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

RDBMS- Day 4. Grouped results Relational algebra Joins Sub queries. In today s session we will discuss about the concept of sub queries.

SQL. SQL DDL Statements

Querying Data with Transact-SQL

Querying Data with Transact-SQL

Chapter 2: Relational Model

Database Technology. Topic 3: SQL. Structured Query Language. Creating Tables SQL DDL. Creating Tables (Example) Modifying Table Definitions

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Midterm Review. Winter Lecture 13

Overview of Query Processing and Optimization

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Relational Data Model ( 관계형데이터모델 )

Chapter 8: Relational Algebra

DATABASE DESIGN I - 1DL300

CMPT 354: Database System I. Lecture 4. SQL Advanced

Chapter 4. The Relational Model

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

CMPT 354: Database System I. Lecture 3. SQL Basics

Chapter 4: SQL. Basic Structure

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

Chapter 3: Relational Model

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

CGS 3066: Spring 2017 SQL Reference

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

RELATIONAL DATA MODEL

3ISY402 DATABASE SYSTEMS

II. Structured Query Language (SQL)

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

SQL - Lecture 3 (Aggregation, etc.)

The Relational Model. Week 2

DATABASE DESIGN I - 1DL300

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

II. Structured Query Language (SQL)

Silberschatz, Korth and Sudarshan See for conditions on re-use

20461: Querying Microsoft SQL Server 2014 Databases

Simple SQL Queries (2)

Insertions, Deletions, and Updates

CSIT5300: Advanced Database Systems

Database System Concepts

Basant Group of Institution

Database Management

Sql Server Syllabus. Overview

Mahathma Gandhi University

DATABASE TECHNOLOGY - 1MB025

Multisets and Duplicates. SQL: Duplicate Semantics and NULL Values. How does this impact Queries?

DATABASTEKNIK - 1DL116

Querying Data with Transact-SQL (20761)

Querying Microsoft SQL Server 2014

Querying Data with Transact-SQL

CMP-3440 Database Systems

UNIT- II (Relational Data Model & Introduction to SQL)

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

The Relational Model

EECS 647: Introduction to Database Systems

Chapter 12: Query Processing

Can We Trust SQL as a Data Analytics Tool?

QQ Group

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Relational Algebra. B term 2004: lecture 10, 11

SQL Data Query Language

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

CS 2451 Database Systems: More SQL

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

Review. Support for data retrieval at the physical level:

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Database System Concepts

CSEN 501 CSEN501 - Databases I

Section 2.2: Relational Databases

Upon completion of this Unit, students will be introduced to the following

DATABASE TECHNOLOGY - 1MB025

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Oracle Database 10g: Introduction to SQL

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Chapter 3: SQL. Chapter 3: SQL

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

The SQL database language Parts of the SQL language

Chapter 5: Other Relational Languages.! Query-by-Example (QBE)! Datalog

Assignment 6: SQL III Solution

20461: Querying Microsoft SQL Server

Chapter 5: Other Relational Languages

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

After completing this course, participants will be able to:

CSCI 127 Introduction to Database Systems

Relational Algebra for sets Introduction to relational algebra for bags

Transcription:

Updates - Insert SQL Other Features and ommands Davood afiei emp(sin, name, phone, city) INSET INSET INTO emp VLUES ( 999, John Doe, 444-555, Edmonton ) INSET INTO emp(sin,name,city) VLUES ( 999, Jim Gray, Ottawa ) Same as: INSET INTO emp VLUES ( 999, Jim Gray,NULL, Ottawa ) 1 3 Outline Update commands Views: more details NULL values onstraints Updates ulk Insert emp (sin, name, phone, city) Edmonton_phonebook (name, phone) INSET INTO Edmonton_phonebook SELET name, phone FOM emp WHEE city = Edmonton 2 4

Updates Delete & Update (a 1,, a n ) DELETE DELETE FOM WHEE condition UPDTE UPDTE SET a i =v i,, a k =v k WHEE condition Update Examples Increase by 5% the balance of every customer who lives in Edmonton and has a balance of more than $5,000. UPDTE deposit SET balance = balance*1.05 WHEE balance > 5000 ND cname IN (SELET cname FOM customer WHEE city = Edmonton ) 5 7 Update Examples Insert a new customer record for John Smith who lives on 345 Jasper venue, Edmonton. INSET INTO customer VLUES ( John Smith, 345 Jasper venue, Edmonton ) Delete all customers who have less than $1,000 in their accounts. DELETE FOM customer WHEE cname IN (SELET cname FOM deposit WHEE balance < 1000) View Definition Examples reate a view of customers who live in Jasper and name it jasper_customers. ETE VIEW jasper_customers S SELET * FOM customer WHEE city = Jasper List the names of all customers in Jasper. SELET cname FOM jasper_customers In queries, a view is exactly like a base table. 6 8

View Definition Examples reate a view (called cust_info) which gives for each customer the name, city, the number of deposit accounts owned and the total balance. ETE VIEW cust_info(name, city, Num, Total) S SELET c.cname, city, OUNT(accno), SUM(balance) FOM deposit d, customer c WHEE d.cname=c.cname GOUP Y c.cname, city Views: Summary View = query table derived table whose definition, not the table itself, is stored. Provides a degree of data independence. Queried like a table. Updated under some conditions. 9 11 View Definition Examples reate a view (called deposit_holders) which includes the name and the city of every deposit account holder. ETE VIEW deposit_holders(name, city) S SELET distinct c.cname, city FOM deposit d, customer c WHEE d.cname=c.cname View Updates onsider the following inserts: INSET INTO jasper_customers VLUES ( John Smith, 111-87 ve, Jasper ) INSET INTO jasper_customers VLUES ( Joe Smith, 112-99 ve, Edmonton ) Is Joe Smith in jasper_customers? To prevent this, add WITH HEK OPTION to the view definition. What can we say about the following insert: INSET INTO cust_info VLUES ( Jim arey, algary, 2, 30000) 10 12

View Update - Example Insert into deposit_holders values ( John, Edmonton ) Insert into deposit_holders values ( Mary, algary ) accno cname bname balanace 1123 John Main St 2000 1144 Joe Whyte ve 2200 1126 John Pine St 1100 cname Joe John city cname street city Edmonton Edmonton deposit_holders Joe 111-22 St Edmonton Mary 98-88 ve algary John 33-34 St Edmonton Unknown Value - NULL Useful when we don t know a column value very common in practice. Simple usage in queries: WHEE phone IS NULL WHEE phone IS NOT NULL SELET cname FOM USTOME WHEE city IS NOT NULL omplication: SELET cname FOM USTOME WHEE city = Edmonton What happens if city for some customers is NULL? deposit customer 13 15 Updatable Views in SQL/92 view defined on a single table is updatable if the view attributes contain the primary key or some other candidate key. Views defined on multiple tables using joins are generally not updatable. Views defined using aggregate functions are not updatable. asic idea: each row and each column in an updatable view must correspond to a distinct row and column in a base table NULL (ont.) The predicate city= Edmonton evaluates to UNKNOWN when city is NULL. the customer name will not be printed. What if the WHEE clause consists of several predicates? E.g. city= Edmonton O street LIKE 100% use three-valued logic: values TUE, FLSE, UNKNOWN. NULL is much different from a constant! 14 16

Left Outer Join S Full Outer Join S select,., from left outer join S using (); Select * from full outer join S using (); 17 19 ight Outer Join Select * from right outer join S using (); S Set Operations Operations: UNION, INTESET, MINUS Duplicates: y default, duplicates are removed from the result of a set operation. To keep duplicates, use UNION LL (Oracle doesn t accept INTESET LL or MINUS LL!) What if and S have more than one join columns? an list them all in the using clause. 18 5 4 25 select from union all select from where < 10 5 4 25 5 4 20

onstraints in SQL/92 User-Defined Domains lready seen: primary key and foreign key constraints. NOT NULL specifies that an attribute cannot contain values should be specified for all primary keys (if it is not default) UNIQUE specifies the alternate keys Domain onstraints HEK onstraints Example ETE DOMIN Gender S H(1) dd some domain constraints ETE DOMIN Gender S H(1) HEK (VLUE = M O VLUE = F ) ETE DOMIN Gender S H(1) HEK (VLUE IN ( M, F )) ETE TLE TEMP (, sex Gender, ) 21 23 Example Domain onstraints ETE TLE branch ( bname H(15) UNIQUE, address VH(20), city H(9) NOT NULL, assets DEIML(10,2) DEFULT 0.00) specifies the condition that each row has to satisfy Example: ETE TLE branch (bname H(15)NOT NULL, address VH(20), city H(9), assets DEIML(10,2) DEFULT 0.00 HEK (assets >= 0)); 22 24

Tuple onstraints hecked every time a tuple is inserted or updated violations are rejected. Example ETE TLE deposit ( accno H(9)NOT NULL, cname VH(15), bname VH(15), balance DEIML(10,2)DEFULT 0.00) HEK (cname = ill linton O balance > 100000); Summary overed asic Queries, nested queries and aggregate Queries Update commands, constraints, Nulls, Views Will over Embedded SQL Note minor syntactic differences between SQL/92 and Oracle SQL. 25 27 ssertions Global constraints of the form ETE SSETION name HEK (condition) Example: No branch can have a customer who has more than 2 loans over $100,000. ETE SSETION deposit HEK (NOT EXISTS (SELET bname, cname FOM loan WHEE amount > 100000 GOUP Y bname, cname HVING OUNT(*) > 2)); 26