SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Chapter 4 Hotel (hotelno, hotelname, city)
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Introduction to Structured Query Language (SQL)
Chapter 5 SQL Homework.
Using SQL to create tables Ways of using Databases.
Introduction to Structured Query Language (SQL)
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL (DDL & DML Commands)
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL queries ordering and grouping and joins
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
SQL queries basics. RHS – SOC 2 SQL query An SQL query is an SQL statement, which specifies a subset of the data in the database A subset in terms of.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL introduction 2013.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself.
INF 280 Database Systems SQL:Join
Populating and Querying tables Insert, Update, Delete and View (DML)
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Introduction to Business Information Systems by Mark Huber, Craig Piercy, Patrick McKeown, and James Norrie Tech Guide D: The Details of SQL, Data Modelling,
 CONACT UC:  Magnific training   
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
CHAPTER 7 DATABASE ACCESS THROUGH WEB
CS 3630 Database Design and Implementation
SQL Query Getting to the data ……..
Assignment 2 Relational Algebra Which tables? What operations?
Chapter 5 Introduction to SQL.
Writing Basic SQL SELECT Statements
Advanced Accounting Information Systems
CS 3630 Database Design and Implementation
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Introduction to Structured Query Language(SQL)
Assignment 2.
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
SQL Queries Chapter No 3.
Lesson 3 Chapter 10.
Presentation transcript:

SQL - DML

Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE updates existing data within a table –DELETE deletes all records from a table

INSERT Simple functionality – insert a row of data into a specified table: INSERT INTO VALUES ( ) Example: INSERT INTO hotel VALUES (1,'The Pope','Vaticanstreet Bishopcity');

INSERT Things to notice about INSERT –The value list must match the field list for the table into which the record is inserted –If we try to insert a record with a key field which already exists, we will get an error –Null values can be inserted if the table definition allows it –The field list can be specified explicitly

UPDATE Updates the value(s) for specified field(s), for the rows mathcing a given condition UPDATE SET field1 = value1, field2 = value2,… WHERE

UPDATE Example: update hotel set name ='The Great Pope' where hotel_no= 1;

UPDATE Things to notice about UPDATE –For each field update, the type of the value must match the type of the field –The WHERE clause is optional – if you leave it out, all records in the table are updated! –It is not considered an error if zero rows are changed, so pay attention to the condition in the WHERE clause…

Delete SQL syntax> –DELETE FROM table_name WHERE some_column=some_value Delete all rows from GUEST : –DELETE FROM Guest; Delete all rooms from Hotel with Hotel_No=1: –DELETE FROM Room where Hotel_no = 1;

What if? Try this command: –Delete from hotel where hotel_no = 1; Can you delete the following tables in this order: –DELETE FROM hotel; –DELETE FROM Room; –DELETE FROM Guest; –DELETE FROM Booking;

What if? Example 1: update hotel set hotel_no = 100 where hotel_no= 1; Example 2: update guest set guest_no = 10 where guest_no= 1;

Exercises With the data in place, run the below commands on the database –INSERT INTO Hotel VALUES ( specify your own values) –INSERT INTO Room VALUES (specify your own values) –UPDATE Booking SET Price = Price* 1.30; –DELETE FROM Room WHERE (Room_no = 8) Now formulate commands yourself, in order to: –Insert data about ” Scandic Roskilde” in the table Hotel (you can find the data on the Internet, or make it up yourself) –Insert data representing the fact that Hotel Scandic have 10 rooms with room numbers 101, 102, 103, 201, 202, 203, 301, 302, 303, 400 –Update the name of the Hotel ”Scandic Roskilde” to ”The new Scandic Roskilde” –Insert data for a booking of a room at the hotel ”THe new Scandic Roskilde”

SQL query An SQL query is an SQL statement, which specifies a subset of the data in the database A subset in terms of –Tables –Fields –Conditions on fields

HotelDB HOTEL:(Hotel_No, Name, Address) ROOM:(Room_No, Hotel_No, Types, Price) BOOKING:(BokingID, Hotel_No, Guest_No, Date_From, Date_To, Room_No) GUEST:(Guest_No, Name, Address)

Table: Guest Guest_NoNameAddress 1EvaParadisvej 3, 1111 Bispeborg 2AdamParadisvej 7, 1111 Bispeborg 3GoegSunset Blvd. 8, 2222 Hjemby 4GokkeSunset Blvd. 8, 2222 Hjemby 5FyKlovnevej 87, 3333 Lilleby 6BiBredgade 198, 3333 Lilleby 7RomeoKaerlighedstunellen 1, 4444 Borgerslev 8JulieKaerlighedstunellen 2, 4444 Borgerslev 9GodzillaDommervænget 16A, 4000 Roskilde 10KingKongHyrdevænget 38, 4000 Roskilde

SQL query The most basic SQL query looks like: SELECT FROM Which fields do I want From what table do I want the fields

SELECT Guest_no, Name FROM Guest Guest_NoNameAddress 1EvaParadisvej 3, 1111 Bispeborg 2AdamParadisvej 7, 1111 Bispeborg 3GoegSunset Blvd. 8, 2222 Hjemby 4GokkeSunset Blvd. 8, 2222 Hjemby 5FyKlovnevej 87, 3333 Lilleby 6BiBredgade 198, 3333 Lilleby 7RomeoKaerlighedstunellen 1, 4444 Borgerslev 8JulieKaerlighedstunellen 2, 4444 Borgerslev 9GodzillaDommervænget 16A, 4000 Roskilde 10KingKongHyrdevænget 38, 4000 Roskilde

SELECT * FROM Guest Guest_NoNameAddress 1EvaParadisvej 3, 1111 Bispeborg 2AdamParadisvej 7, 1111 Bispeborg 3GoegSunset Blvd. 8, 2222 Hjemby 4GokkeSunset Blvd. 8, 2222 Hjemby 5FyKlovnevej 87, 3333 Lilleby 6BiBredgade 198, 3333 Lilleby 7RomeoKaerlighedstunellen 1, 4444 Borgerslev 8JulieKaerlighedstunellen 2, 4444 Borgerslev 9GodzillaDommervænget 16A, 4000 Roskilde 10KingKongHyrdevænget 38, 4000 Roskilde

SQL query A slightly more complex SQL statement looks like: SELECT FROM WHERE Which fields do I want From what table do I want the fields What conditions must the fields fulfill

SQL query The WHERE part is a logical expression, specifying conditions on certain fields Five fundamental types of criteria –Comparison (, =) –Range ( ) –Set membership (belongs to a set of values) –Pattern match (for string fields) –Null (is the value of the field a null value)

Table: Guest Guest_NoNameAddress 1EvaParadisvej 3, 1111 Bispeborg 2AdamParadisvej 7, 1111 Bispeborg 3GoegSunset Blvd. 8, 2222 Hjemby 4GokkeSunset Blvd. 8, 2222 Hjemby 5FyKlovnevej 87, 3333 Lilleby 6BiBredgade 198, 3333 Lilleby 7RomeoKaerlighedstunellen 1, 4444 Borgerslev 8JulieKaerlighedstunellen 2, 4444 Borgerslev 9GodzillaDommervænget 16A, 4000 Roskilde 10KingKongHyrdevænget 38, 4000 Roskilde SELECT * FROM Guest WHEREGuest_No < 5

SQL query Note that we can build arbitrarily complex logical expressions, using the usual logical operators: AND, OR, NOT Rules are the same as for logical expres- sions in C# Use () to make expressions easier to read, and/or to ”overrule” evaluation rules

Table: Guest Guest_NoNameAddress 1EvaParadisvej 3, 1111 Bispeborg 2AdamParadisvej 7, 1111 Bispeborg 3GoegSunset Blvd. 8, 2222 Hjemby 4GokkeSunset Blvd. 8, 2222 Hjemby 5FyKlovnevej 87, 3333 Lilleby 6BiBredgade 198, 3333 Lilleby 7RomeoKaerlighedstunellen 1, 4444 Borgerslev 8JulieKaerlighedstunellen 2, 4444 Borgerslev 9GodzillaDommervænget 16A, 4000 Roskilde 10KingKongHyrdevænget 38, 4000 Roskilde SELECT Name FROM Guest WHEREGuest_No < 5

SQL query - range A range search is an SQL query where a value should be within a certain range Actually just a two-part comparision query SELECT * FROM Guest WHERE ((Guest_no = 3))

SQL query - range Another notation for range seach uses the keyword BETWEEN SELECT * FROM Guest WHERE Guest_no BETWEEN 1 AND 6

SQL query - range We can create a ”negated” version of a range query using NOT BETWEEN SELECT * FROM Guest WHERE Guest_no NOT BETWEEN 1 AND 6

Exercise – SQL queries Now formulate queries yourself, in order to retrieve the below data: –Get all fields for rooms where the type is ’F’ in the hotel with Hotel_no = 1 –Get all fields for rooms that are not a ’F’ family or a ’D’ double room –Get all bookings that are after the –Get all bookings that are after the but allso before the –Get all bookings for hotel_no = 1 and guest_no = 2 that are after the but allso before the –Get all booking for Hotel_no = 2 Formulate your queries.

SQL query – set membership A set membership search is an SQL query where a value must belong to a given set of values We use the IN keyword SELECT * FROM Guest WHERE Name IN (’Adam’,’Eva’)

SQL query – set membership Note that these two queries are equivalent SELECT * FROM Guest WHERE Name IN (’Adam’,’Eva’) SELECT * FROM Guest WHERE ((Name = ’Adam’) OR (Name = ’Eva’))

SQL query – set membership We can create a ”negated” version of a set membership query using NOT IN SELECT * FROM Guest WHERE Name NOT IN (’Adam’,’Eva’)

Exercise – SQL queries Now formulate queries yourself, in order to retrieve the below data: –Get all guests from where hotel_no 1, 3, 4 –Get all rooms from hotel_no 1 that are not a double og family room –Get all guest that did not book a room in the period to Formulate your own queries

SQL query – pattern match A pattern match search is an SQL query where a (string) value must match a given pattern We use the LIKE keyword The hard part is choosing the correct pattern to match against – several ways to formulate a pattern

SQL query – pattern match A pattern is formulated using two special characters % and _ % : wildcard: any sequence of zero or more characters _ : any single character

SQL query – pattern match PatternMeaning ’s%’ Any string starting with ’S’, of any length (at least 1) (’super’, ’s’, ’s123’, ’s 123’) ’s_ _ _’ Any string starting with ’S’, of length exactly 4 (’such’, ’s123’, ’ssss’, ’s 1’) ’%s’ Any string ending with ’s’, of any length (at least 1) (’Spurs’, ’s’, ’123s’, ’ s’, ’1 2s’) ’%s%’ Any string containing an ’s’, of any length (at least 1) (’Spurs’, ’s’, ’basin’, ’ s ’, ’12s34’) ’%s_ _ _% Exercise…

SQL query – pattern match SELECT * FROM Guest WHERE Name LIKE ’P%’ SELECT * FROM Guest WHERE Name LIKE ’_ _ _ _’

SQL query – pattern match We can create a ”negated” version of a pattern match query using NOT LIKE SELECT * FROM Hotel WHERE Name NOT LIKE 'D%'

SQL query – null A null search is an SQL query where a value must be a null value We use the IS NULL keyword A null value…? We may allow a field to have an ”unde- fined” or null value, if it makes sense SELECT * FROM Guest WHERE Address IS NULL

SQL query – pattern match We can create a ”negated” version of a null query using IS NOT NULL SELECT * FROM Guest WHERE Address IS NOT NULL

Exercise – SQL queries With the data in place, run the below queries on the database –SELECT * FROM Hotel WHERE name LIKE ’%D%’ –SELECT * FROM hotel WHERE Address LIKE '%n‘ –SELECT * FROM Hotel WHERE Address LIKE '%_ _ _ _ _%' –SELECT * FROM Booking WHERE Date_From IS NOT NULL Now formulate queries yourself, in order to retrieve the below data: –Get all hotels from Roskilde –Get all hotels –Get Bookings that have a date for Date_from but not for Date_to (insert a new row to test it) –Get all Hotels with a name starting with ’P’ and have a length of 4 characters –Get all Hotels containing a ’P’ or a ’p’ Formulate your own queries