IM 101_Fundamentals of Database Systems_Unit 12
IM 101_Fundamentals of Database Systems_Unit 12
All rights reserved. No part of this module may be reproduced, repurposed, distributed, or transmitted in
any form or by any means including photocopying, reprinting, or other electronic or mechanical
methods without the prior written permission of PLV and the individual developers of instructional
materials (IMs) except in the case of brief quotations embodied in critical and creative reviews and
certain other noncommercial uses permitted by the Copyright Law. For permission request, address your
written correspondence whether printed or electronic to the Chair of the University Committee on
Instructional Materials Development and Evaluation at the address below:
Essential Question
What are the basic Data Manipulation Language commands ?
How do we call database alias ?
How do operators follow precedence in an Structured Query Language query ?
Lessons Input
MySQL (open-source)
2
For instance, if you want to create a database for client reviews, the result will look like this:
To move on to using your database, you need to continue your SQL query with the ‘USE’ statement:
USE client_reviews;
This will make this database a target for all your future commands.
Keep in mind: if the database with this name already exists, you will get an error message. However, to
avoid this, you can use the ‘IF NOT EXISTS’ statement. Here’s how it looks like in MySQL:
It is clear what we should insert in the table name and column name, but let’s take a closer look at the
types of constraints.
A constraint is a declaration of data type, which indicates what kind of data the column will include.
There are several kinds of constraints that you can include in a column:
If we create a table for each of the people who have given their customer reviews, the final result will
look like this:
4
When creating a table, you can encounter the same problem as with a database if the table does not exist.
To avoid it, to the ‘CREATE TABLE’ statement, you should also add the ‘IF NOT EXISTS’ statement.
You can also include values – the data that you want to include in each column.
Here’s how the final query would look like for our customer reviews table after inserting data into it:
The final result will give you a numbered list of the names, birth dates, and reviews in the form of a
table.
We’re now going to do some sample inserts, so if you haven’t done so already, run the script to create
the esqlSalesPerson table.
Let’s assume we want to insert a new sales person into the table. The command to run is:
INSERT INTO dbo.esqlSalesPerson
(FullName, SalesLastYear,
City, rowguid)
VALUES ('George Hitson', NULL,
'Midland', '794310D2-6293-4259-AC11-71D96689A3DD')
6
Notice we didn’t specify the SalesPersonID in the column list. This is an identity column and
automatically populated.
You can switch around the columns; they don’t have to be in order. Also notice not all the columns are
specified:
Before we continue on with more complicated example, it’s important to step back and consider the
SQL INSERT statement’s behavior and some best practices.
Considerations
Data Type Considerations (padding)
Keep in mind that when inserting data into columns whose data type is CHAR, VARCHAR, or
VARBINARY, the padding or truncation of data depends upon the SET ANSI_PADDING setting.
Inserting the value ‘Kris’ into a field defined as CHAR(10) results in a value padded with six additional
spaces. The value inserted is ‘Kris ‘ The padding rule can be confusing when using INSERT INTO.
Error handling
You can handle errors when executing an INSERT INTO statement using a TRY…CATCH construct.
There are several common reason an INSERT statement may fail. Some of the common ones are:
7
Unique Key Violation – you’re trying to insert a record into a table which causes a duplicate
key value.
Primary Key Violation – you’re trying to insert a record into a table which already has a
row with the same primary key.
Foreign Key Violation – your trying to insert a row into a “child” table, yet the “parent”
doesn’t exist.
Data Type Conversion – You’re trying to insert a row, where one of the values can’t be
correctly convert into the corresponding columns data type.
In these cases, the INSERT statement execution stops and the INSERT generates an error. No rows are
inserted into the table, even rows
This “all or nothing” behavior can be modified for arithmetic errors. Consider a divide by zero error.
INSERT INTO myNumbers (x, y)
VALUES (10/0, 5),
(10/5, 2),
(10/2, 5)
Will generate an error if SET ARITHABORT is set to ON. In this case, the inserted is stopped, no rows
are inserted, and an error is thrown.
However, if SET ARITHABORT is set to OFF and ANSI_WARNINGS are OFF, then the same
statement will successfully complete. Where there is a mathematical error, the result is replaced with
NULL.
For example,
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
INSERT INTO myNumbers (x, y)
VALUES (10/0, 5),
(10/5, 2),
(10/2, 5)
When adding rows to tables it is important to understand there are some columns requiring special
handling.
Inserts a new row into the esqlSalesPerson. If you run the command again, another row is added, but the
rowguid value is different.
NEWID() generates a new value each time it is called.
Identity Column Property
A new value is generated for the column whenever a row is inserted into a table with a identity column
property. Because of this, since esqlSalesPerson.SalesPersonID is an identity column, we don’t specify
it in our INSERT statement. Each time a row is added the identity value is incremented by one.
If you try to insert a row using your own value, you’ll throw an error.
The INSERT statement
If a value isn’t provided by the statement and the engine is unable to provide a
value, the row cannot be inserted. This typically happens with a value is missing
and the column is NOT NULL.
Let’s assume the Adventure Works sales manager would like to create a SalesPerson table and only
include sales people who’s last year’s sales were greater than $1,000,000.
To populate this table, you could run the following INSERT SELECT:
In order for this to work properly the columns returned from the SELECT statement have to be in the
same order as specified int the INSERT column list. In this example notice that rowguid is a required
field. To populate this value, we use the NEWID() function.
You can also use a common table expression to define the rows to insert.
You can also write the example as a CTE (Common Table Expression):
10
Though there is more typing, I like the CTE method. I think it makes the INSERT statement easier to
read.
Remember, when using SELECT statement to insert data into another table it is best practice to first just
run the SELECT statement as is to ensure you are selecting the correct rows. Also, always develop and
test your code! I highly recommend using a development copy of your database.
2) ‘SELECT’ clause
This clause is used to select and extract data from one or different tables. The syntax for this clause will
follow this pattern:
The combination of the ‘SELECT’ and ‘FROM’ clauses allows you to fetch information from one or
several columns within one or different tables. If you want to select all columns in one table, you can
simply use the combination of ‘SELECT’, ‘FROM’ clauses, and the name of the table.
3) ‘WHERE’ clause
By inserting this clause in a table, you can select data based on a certain condition. It is helpful if you
want to select for an update or entirely delete one of the records in a row or a column based on a
condition like a name, birth date, etc. Here’s the basic syntax using this clause:
As you can see, the WHERE clause is used together with the ‘SELECT’ and ‘FROM’ clauses to help
you fetch the right data from the right column or row.
You can also use the ‘WHERE’ clause to filter different records based on a certain condition.
To make sure that you select the right data, this clause allows you to use several basic comparison
operators:
11
= (equal)
> (greater than)
< (less than)
>= (greater than or equal)
<= (less than or equal)
LIKE (to find similar patterns)
IN (finding matches according to a certain value)
BETWEEN (defining the range of values)
To make these operators work, include them right after the ‘WHERE’ clause.
Respectively, the OR operator combines two different conditions and gives you the results if there is
data that matches one of these conditions. The syntax, in this case, looks like this:
To write a more complex SQL query, you can also combine the ‘AND’ and ‘OR’ operators, which will
allow you to retrieve data that matches many different conditions.
5) ‘IN’ and ‘BETWEEN’ Operators
These operators help you select an exclusive variety of values based on certain conditions.
The IN operator allows you to select all the data that falls under a certain criterion. The syntax of the
query, including the ‘IN’ operator, should look like this:
12
For instance, you can retrieve information about customers coming from the same country using this
operator. Similarly, you can add ‘NOT’ to the ‘IN’ operator, if you want to exclude some results.
If you want to get results within a certain range of values, you can use the BETWEEN operator. In this
case, the syntax will look as follows:
As you can see, the ‘ORDER BY’ clause is also used in this case, because it helps identify the values to
be limited.
8) ‘UPDATE’ statement
Once you’ve inserted and selected data, you can now proceed to writing an SQL query to update this
data. To do it, you will need to use the UPDATE statement, for which the syntax looks similar to this:
UPDATE table_name
SET column1_name = value 1…
WHERE condition1, condition2…
The ‘SET’ clause allows you to identify a specific column or row that needs to be updated. You can also
use all the above-mentioned operators, like ‘AND’ and ‘OR’ to update the table.
Also, let’s initially populate the table with some data using the following INSERT statement:
WITH topSalesPerson
(FullName, SalesLastYear, City, rowguid)
AS (SELECT S.FirstName + ' ' + S.LastName,
13
S.SalesLastYear,
S.City ,
NEWID()
FROM Sales.vSalesPerson S
WHERE S.SalesLastYear > 1000000 )
INSERT INTO esqlSalesPerson (FullName, SalesLastYear,
City, rowguid)
SELECT FullName,
SalesLastYear,
City,
rowguid
FROM topSalesPerson
Basic Structure
The SQL UPDATE statement is used to change column values. Though an update statement can modify
columns data from many sources, such as literal values or other query results, the basic format is the
same.
There are three components to an UPDATE statement:
UPDATE tableName
SET column1 = value1,
column2 = value2,
…
We’re now going to do some sample updates, so if you haven’t done so already, run the script to create
the esqlSalesPerson table.
Let’s assume we want to the city for every salesperson to Ann Arbor. The command to run is:
UPDATE esqlSalesPerson
SET City = 'Ann Arbor'
You can also update more than one column at a time. To update both the City and rowguid we can run
14
UPDATE esqlSalesPerson
SET City = 'Ann Arbor',
rowguid = NEWID()
UPDATE esqlSalesPerson
SET FullName = 'Don Sax'
WHERE SalesPersonID = 10027
Note: The SalesPersonID generated for your table’s rows may be different than what is show in the
exercises, since this primary key is auto generated.
15
Suppose every SalesPerson whose ID is less than 10031 worked in Saline. To update the rows to reflect
this we could use this UPDATE statement:
UPDATE esqlSalesPerson
SET City = 'Saline'
WHERE SalesPersonID < 10031
Unique Key Violation – you’re trying to update a value which causes a duplicate key value.
16
Data Type Conversion – You’re trying to update a value which can’t be correctly convert
into the corresponding columns data type.
In these cases, the UPDATE statement execution stops and the UPDATE generates an error. No rows
from the UPDATE statement are saved into the table, even those rows that didn’t generate an error.
This “all or nothing” behavior can be modified for arithmetic errors. Consider a divide by zero error.
UPDATE myNumbers
SET X = 10/0
Will generate an error if SET ARITHABORT is set to ON. In this case the inserted is stopped, no rows
are updated and an error thrown.
9) ‘DELETE’ statement
This statement allows you to remove one or several rows. The syntax for this query is simple and looks
like this:
The ‘WHERE’ clause specifies the record that should be deleted according to the condition assigned to
it. Don’t omit it, otherwise, you will delete all the records from the table.
DELETE
FROM tableName
WHERE searchCondition
…
We’re now going to do some sample DELETEs, so if you haven’t done so already, run the script to
create the esqlSalesPerson table.
17
DELETE
FROM esqlSalesPerson
I would recommend, for the purposes of our example, to wrap the DELETE command in a transaction
so you don’t have to repeatedly insert rows into the example database. Here you’ll see the same
DELETE statement.
BEGIN TRANSACTION
SELECT COUNT(1) FROM esqlSalesPerson
DELETE
FROM esqlSalesPerson
The first count statement return 13 rows; whereas, the second returns 0. Since I ROLLBACK the
transaction, the delete operation isn’t permanent.
Suppose we want to delete Jillian Carson’s row. To do so we would issue the following:
DELETE
FROM esqlSalesPerson
WHERE SalesPersonID = 10095
You may be wondering how you would know the primary key.
You can imagine if you had a web app that listed every sales person, that the grid may contain the sales
person’s Full Name, and Last Year’s Sales, but hidden on the grid would also be their SalesPersonID.
When a user selected a row on the grid, and elected to remove the row, the application would retrieve
the hidden primary key for the row, and then issue the delete command to the database.
DELETE
FROM esqlSalesPerson
WHERE SalesLastYear > 2000000.00
You can run the following command to try the script within a transaction:
BEGIN TRANSACTION
SELECT COUNT(1) FROM esqlSalesPerson
DELETE
FROM esqlSalesPerson
WHERE SalesLastYear < 2000000.00
19
The blue arrow shows there were originally 13 records, and the red the remaining whose sales are
greater than or equal to two million dollars.
You can also create more complex filtering conditions. Later on in the article we’ll show how to use a
subquery. You can also use Boolean conditions, in the WHERE clause as well just as you would with
the SELECT statement.
Error handling
If a DELETE statement throw an error all rows are restored to their state prior to the statement being
run. If an error is triggered no rows are removed.
There are many reasons why a delete statement may fail. Some of the more typical ones include:
Orphan Rows – if a FOREIGN KEY constraint is defined between two tables, such as
parentTable and childTable, then DELETE a parentTable row will cause an error if
childTable rows related to the parent exist. They way around this is to first remove the
corresponding childTable rows, then the parentTable row.
20
Locking Behavior
A delete statement places an exclusive (X) lock on the table. This mean no other query can modify the
table’s data until the DELETE transaction completes.
You can still read data, but need to use the NOLOCK hint or read uncommitted isolation level.
BEGIN TRANSACTION
SELECT COUNT(1) FROM esqlSalesPerson
DELETE
FROM esqlSalesPerson
WHERE esqlSalesPerson.City IN
(SELECT DISTINCT City
FROM Person.Address A
INNER JOIN Person.StateProvince S
ON A.StateProvinceID = S.StateProvinceID
AND S.CountryRegionCode = 'US' )
Please Note, I wrapped the example in a transaction so I wouldn’t permanently delete my test data.
The subquery is colored blue. Notice that it is no different from any other correlated subquery you may
have used in the past.
21
1. Find distinct cities for all people that live in the US.
2. DELETE sales people whose city is in this list.
The first step results from the subquery. The second step deletes rows per the WHERE clause.
While removing the rows from the table, this statement doesn’t actually delete the structure of the table.
You can use this statement if you want to rework the entire table.
Misspelled commands. This is the most common SQL query mistake. To solve this
problem, you can use an SQL editor to fix the broken SQL statements and clauses. If your
query contains text under the TEXT constraint, you also need to proofread it. You can do it
quickly using different thesis sites containing automated proofreaders.
The omission of punctuation signs. A proper SQL query also needs to have brackets and
quotation marks to operate properly. So check your query for single quotes and double
quotes and make sure everything displays properly.
Improper order of the statement. If your query doesn’t work, the statements in it might be
in an improper order. Having syntax templates at hand might help if you don’t have enough
experience writing SQL queries.
To make your SQL queries work, you need to write them while keeping in mind where performance
problems might appear within your query. Then, if you get an error message, you might already have an
understanding, where to look for an issue and how to solve it.
INSERT command – used to enter data into the table. Its basic syntax looks like this
22
SELECT command – used to list contents of the table.The syntax of the command is:
SELECT columnnames FROM tablename
You could also use a wildcard character (*) to select all columns from the table.Example:
SELECT * FROM tablename
ROLLBACK command – undo any changes to the made since the last COMMIT command.It should be
executed before any COMMIT command is made, otherwise, all changes would be made final.
Subquery – or nested or inner query is a query that is embedded or nested inside another query.
Rules of Precedence
Rules that establish the order to which computations are computed.
1. Perform operations within parentheses
1. The instructor during the last five minutes of class, will give students one minute (or two to
five minutes) to write the answers to one or two questions, such as, What was the most
important thing you learned? and What question or questions remain unanswered?.
2. The instructor will give the students two to three problems related to the use of Data
Manipulation commands and Rules of Precedence in Structured Query Language
References
Garcia-Molina, H.,Ullman, J.,Widom, J. (2008). Database Systems: The Complete Book (2nd ed., pp 189-
501). Pearson