Database Management Systems,

Similar documents
Week 4 & 5: SQL. SQL as a Query Language. Example Database. SQL Queries

Database Management Systems,

Database Management Systems,

CSC Web Programming. Introduction to SQL

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Ian Kenny. November 28, 2017

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Database Programming with SQL

CS 582 Database Management Systems II

Database Management Systems

2.2.2.Relational Database concept

Insertions, Deletions, and Updates

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Structure Query Language (SQL)

Data Manipulation Language (DML)

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

Unit 1 - Chapter 4,5

Basic SQL. Basic SQL. Basic SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Retrieving Data from Multiple Tables

CS 338 Basic SQL Part II

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

SQL STRUCTURED QUERY LANGUAGE

SQL. Char (30) can store ram, ramji007 or 80- b

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Fundamentals of Database Systems

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Tables From Existing Tables

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

RESTRICTING AND SORTING DATA

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

UFCEKG 20 2 : Data, Schemas and Applications

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

CSCC43H: Introduction to Databases. Lecture 4

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

E-R diagrams and database schemas. Functional dependencies. Definition (tuple, attribute, value). A tuple has the form

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Chapter 3: Introduction to SQL

SELF TEST. List the Capabilities of SQL SELECT Statements

Exam code: Exam name: Database Fundamentals. Version 16.0

Sample Question Paper

Midterm Review. Winter Lecture 13

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

30. Structured Query Language (SQL)

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

King Fahd University of Petroleum and Minerals

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

QQ Group

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

SQL functions fit into two broad categories: Data definition language Data manipulation language

Assignment Grading Rubric

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

II. Structured Query Language (SQL)

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Chapter 3: Introduction to SQL

Who is the borrower whose id is 12345? σ borrower borrower_id = select * from borrower where borrower_id = '12345'

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

CSEN 501 CSEN501 - Databases I

The Relational Model and Relational Algebra

RETRIEVING DATA USING THE SQL SELECT STATEMENT

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

SQL - Data Query language

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

Lesson 2. Data Manipulation Language

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

Examination examples

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Database Management Systems,

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Introduction to SQL Server 2005/2008 and Transact SQL

Relational Algebra and SQL

II. Structured Query Language (SQL)

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

SQL Structured Query Language Introduction

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Database design process

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Getting Information from a Table

chapter 2 G ETTING I NFORMATION FROM A TABLE

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Outer Join, More on SQL Constraints

L e a r n S q l select where

1) Introduction to SQL

Relational Database Languages

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

Transcription:

Database Management Systems SQL Query Language (2) 1

Topics Update Query Delete Query Integrity Constraint Cascade Deletes Deleting a Table Join in Queries Table variables More Options in Select Queries Like Clause Distinct Queries Ordering 2

Update Query Update query is used for changing the values of attributes. UpdateTable_name Set Attribute_name1 = Value1, Attribute_name2 = Value2, Where condition 3

Update Query Example Raise the salary of any employee by 20% if the salary is less than or equal to 40 update Employee set Salary = Salary * 1.2 where Salary <= 40 4

Update Example 5

Update Query Order Update Query is set oriented. Therefore the order of the queries is important. For example the following order gives some employees a double raise update Employee set Salary = Salary * 1.2 where Salary <= 40 update Employee set Salary = Salary * 1.15 where Salary > 40 6

Delete Query To delete tuples Delete query is used. Delete from Table_name Where condition Delete query without Where deletes all tuples. (Table schema is preserved) 7

Delete Query Example Delete From Employee Where City = London 8

Integrity Constraint If a table contains a foreign key attribute, the value of the foreign key must refer to a valid record in the second table. This constraint is called Integrity Constraint 9

Sample Database 10

Integrity Constraint In Sample Database DeptCode in EMPLOYEE table is a foreign key referring to DeptCode in DEPARTMENT table If a department is deleted, its employees will not have a valid department 11

Integrity Constraint Violation Example Delete From Department Where DepartmentName = Administration After this query, Mary Brown s department is invalid. The database integrity is violated after this query. 12

Solution to Integrity Violation: Cascade Delete To preserve database integrity we should either Not allow deleting records with references from other tables Delete records in other tables which are referring to this record. This is called cascade delete Syntax: Delete From tablename where condition Cascade 13

Cascade Delete Example 14

Deleting a Table A table including its schema and records can be deleted using Drop command Syntax: Drop TableTableName e.g. Drop Table Cars If Cascade is used with Drop command, all references to the table will be removed 15

Deleting Tables: Example Drop Table Department Cascade 16

Join in Queries Join can be used in SQL queries Syntax: SELECT AttributesList FROM Table1 [JoinType] JoinTable2 On JoinCond Where Condition 17

Example: Natural Join 18

Join Example Select FirstName, Surname, Department.City From Employee join Department on Dept = DeptName 19

Left Join, Right Join, Full Join Use Left, Right, or Full as join type to have a left, right, or full join e.g. Select * From Table1 Left Join Table2 on Att1=Att2 WhereTable1.Att3 > 100 20

Left Join Example Select FirstName, Surname, Driver.DriverID, CarRegNo, Make, Model From Driver left join Automobile on (Driver.DriverID = Automobile.DriverID) 21

The Result of Left Join Example 22

Right Join Example Select FirstName, Surname, Driver.DriverID, CarRegNo, Make, Model From Driver right join Automobile on (Driver.DriverID = Automobile.DriverID) 23

Full Join Example Select FirstName, Surname, Driver.DriverID, CarRegNo, Make, Model From Driver full join Automobile on (Driver.DriverID = Automobile.DriverID) 24

Table Variables Table variables correspond to the renaming operator ρ of relational algebra e.g. Find all the same surname (but different first names) employees: Select E1.FirstName, E1.Surname From Employee E1, Employee E2 Where E1.Surname = E2.Surname AND E1.FirstName <> E2.FirstName 25

Query Results 26

Like Clause Like is used in condition expressions when a pattern is used instead of an exact value. _ in like represents one character % in like represents any number of characters e.g. A%A matches all strings starting and ending with character A such as ANKARA e.g. _an matches all 3 letter strings ending with an such as man or can 27

Like Clause Example Find the employees with surnames that have r as the second letter and n as the last letter: select * from Employee where Surname like _r%n 28

Distinct Option in Queries In SQL, tables may have identical rows if no primary key is defined Duplicates can be removed from a query using the keyword distinct 29

Query with Distinct Keyword 30

Ordering The order by clause, at the end of the query, orders the rows of the result. Syntax: order by Attribute [ asc desc ] More than one attribute can be used in order by clause 31

Ordering Example Extract all records from the Automobile table in descending order of make and model: Select * From Automobile Order by Make desc, Model desc 32

Summary SQL queries are also used for updating records, deleting records or deleting tables. In deleting records or tables, the integrity of the database should be preserved. Queries can be written by joining tables The result of a query can be put in order of an attribute and the repeated rows can be removed. 33

Questions? 34