SQL Refresher Complete Notes PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 352

1

Note for the Course Developer / Author:


Give an overview of the whole course content structure. This slide should be
used if the course is longer then a day. Otherwise, the slide "Today's Agenda"
might be sufficient.

Note for the Trainer:


■ Use this slide to give an overview of the whole course content, for example,
at the beginning of the course or whenever it is required. Slide should be used
if the course is longer than one day. Otherwise the slide "Today's Agenda"
might be sufficient.
■ You can use it to check which lessons are already completed.

4
Note for Course Developer / Author:
Please define collaboration rules, example:
■ During the course, mobile phones and LapTops are switched off.
■ After breaks, we will start in time.
■ Everybody is asked to contribute actively.
■ There are no stupid questions.

Note for the Trainer:


Use this slide to introduce collaboration rules.

3
4
Note for the Course Developer / Author:
■ Please list the learning objectives of the course. If the course is complex, it
might make sense to define learning objectives also for each unit (chapter/
topic) and lesson (sub-chapter).
■ Please consider the guidelines on how to write learning objectives (guidelines
on how to write learning objectives can be found in the training enablement
community).

Note for the Trainer:


■ Show this slide at the beginning of the course / unit / lesson to demonstrate
the learning objectives of the course / unit / lesson.

5
Note for the Course Developer / Author:
Give an overview of the whole course content structure. This slide should be
used if the course is longer then a day. Otherwise, the slide "Today's Agenda"
might be sufficient.

Note for the Trainer:


■ Use this slide to give an overview of the whole course content, for example,
at the beginning of the course or whenever it is required. Slide should be used
if the course is longer than one day. Otherwise the slide "Today's Agenda"
might be sufficient.
■ You can use it to check which lessons are already completed.

4
Terminology
Dr. E.F. Codd, a researcher at IBM, invented the relational database model in
the late 1960s. Codd applied mathematical principles to the field of
database management; for example, a relation is a mathematical term for a
table.
Computer professionals frequently apply a different set of database terms. As
a result, three sets of terms are used interchangeably to refer to database
elements, as summarized in the following table:

Database Mathematical Data Processing


Table Relation File
Row Tuple Record
Column Attribute Field
Tables are discussed in the units, “Create Table,” “Querying Data in Tables” and
“Modifying Data in Tables”
Views are discussed in the module, “Using Views and Indexes.”
Indexes are discussed in the module, “Using Views and Indexes.”
Stored procedures are not discussed in this course, as they are database
vendor specific.
Triggers are not discussed in this course, as they are database vendor specific.
Notes:
To install pubs2, your System Administrator must execute the install script. For
the UNIX version of the product, the script is called installpubs2. For the NT
version, the script is called instpbs2.sql. In either case, it is located in the
scripts directory.
The System Administrator will also need to grant you permission to access
pubs2. Consult your System Administrator for more details.
Business Rules:
Business rules are requirements that the business imposes on data. The
following are a few examples from pubs2:
 For a given order, the customer can order from 1 to 20,000 copies of each
title.
 A book can be entered into the database, even if the book’s publisher or its
price have not yet been determined.
 Every book must have a unique identification code.
 A publisher’s ID must be either 1389, 0736, 0877, 1622, or a four-digit
number that starts with the digits “99”.
 The total sales for each book must reflect every sale entered into the
database.
Methods for enforcing business rules are discussed in the modules, “Creating
Tables,” “Querying Data in Tables,” “Using Constraints to Enforce Data
Integrity,” “Employing Reusable Database Objects (Defaults and Rules),” and
“Using Triggers to Enforce Referential Integrity.”
The shaded row(s) in each table indicate the column or columns that comprise
the primary key. Primary keys are discussed in Module 7, “Constraints.”
The relationship lines between tables indicate tables with related columns. The
name of the related column or columns appear along the relationship line.
The “N”s and “1”s indicate the type of relationship shared by two tables:
 In a “1 to 1” relationship, one row in the first table corresponds to (at most)
one row in the second table.
 In a “1 to N” relationship, one row in the first table can correspond to many
rows in the second table. However, one row in the second table can
correspond to only one row in first table.
 In an “N to N” relationship, one row in either table can correspond to many
rows in the other table. (There are no “N to N” relationships in the pubs2
diagram.)
For more information on primary keys and table relationships, refer to Sybase’s
Logical Database Design course or a book on relational database design.
The pubs2 Database

titleauthor titles roysched


N title_id 1 1 title_id N
au_id id title_id tid title_id tid
title_id tid title varchar(80) lorange int
au_ord tinyint type char(12) hirange int
N
royaltyper int pub_id char(4) royalty int
N price money
advance money pub_id
au_id total_sales int
Notes varchar(200)
1 pubdate datetime publishers
contract bit 1
authors pub_id char(4)
au_id id 1 pub_name varchar(40)
au_lname varchar(40) City varchar(20)
au_fname var char(20) State char(2)
phone char(12) title_id
address varchar(12)
city varchar(20)
state char(2)
country varchar(12) N
postalcode char(10) 1 salesdetail N 1 sales
stor_id char(4) stor_id stor_id char(4)
1 ord_num varchar(20) ord_num ord_num varchar(20)
title_id tid date datetime
qty smallint
discount float N
1 N stor_id
blurbs
au_id id
stor_id
copy text 1
stores
1
stor_id char(4)
au_pix 1
stor_name varchar(40)
au_id id discounts stor_address varchar(40)
pic image discounttype varchar(40) stor_id city varchar(20)
format_type char(11) stor_id char(4) N state char(2)
bytesize int lowqty smallint 1 country varchar(12)
pixwidth_hor char(14) highqty smallint postalcode char(10)
pixwidth_vert char(14) discount float payterms varchar(2)
Note for the Course Developer / Author:
Give an overview of the whole course content structure. This slide should be
used if the course is longer then a day. Otherwise, the slide "Today's Agenda"
might be sufficient.

Note for the Trainer:


■ Use this slide to give an overview of the whole course content, for example,
at the beginning of the course or whenever it is required. Slide should be used
if the course is longer than one day. Otherwise the slide "Today's Agenda"
might be sufficient.
■ You can use it to check which lessons are already completed.

4
Notes:
•The ANSI/ISO SQL standard is governed by the National Committee on
Information Technology Standards (NCITS H2)
•Their website is http://www.incits.org/tc_home/h2.htm
Notes:
•Stored procedures and triggers go through a similar process. For more
details, see the module “Writing Effective Stored Procedures”
Data definition language
 Data definition language statements create, alter, and drop databases, tables, and other objects in
the server.
 DDL statements are introduced throughout this course.
Data manipulation language
 Data manipulation language statements insert, update, and delete the data in a database as well as
select data that you want to read.
 DML statements are the focus of modules, “Querying Data in Tables,” and “Modifying Data in
Tables.”
Data control language
 Data control language statements grant and revoke permissions to the users of a database.
 DCL statements are discussed briefly in the modules, “Creating Tables,” “Querying Data in Tables,”
and “Writing Basic Stored Procedures.”
 For further information on DCL statements, refer to the Sybase Adaptive Server Enterprise Security
Administration Guide or the System and Database Administration course.
Adaptive Server and ANSI compliance
 There are two ANSI standards for SQL: the 1989 standard (also known as “SQL”), and the 1992
standard (also known as “SQL2”). The 1992 standard has several possible levels of compliance
(Entry SQL, Intermediate SQL, and Full SQL). Adaptive Server complies with the 1989 standard and
Entry SQL level compliance with the 1992 standard.
Note for the Course Developer / Author:
Give an overview of the whole course content structure. This slide should be
used if the course is longer then a day. Otherwise, the slide "Today's Agenda"
might be sufficient.

Note for the Trainer:


■ Use this slide to give an overview of the whole course content, for example,
at the beginning of the course or whenever it is required. Slide should be used
if the course is longer than one day. Otherwise the slide "Today's Agenda"
might be sufficient.
■ You can use it to check which lessons are already completed.

4
Note for Course Developer / Author:
Summarize the content of the course and/or the last unit / lesson. The
summary should be more than a listing of the objectives. It should be result
oriented and review the main learning points.

Note for the Trainer:


Use this slide at the end of course / unit / lesson to recap what the participants
learned.

10
27
28
4
Examples of client/server relationship
 An e-mail program that displays mail (the client) and the central e-mail
program that sends and receives e-mail across the Internet (the server)
 A Web browser (the client) and a Web server (the server)
 An ATM (the client) and the bank’s computer (the server)
Notes
 Clients are also known as “front-end applications.”
References in this course to Adaptive Server Enterprise 15 are abbreviated as
“ASE” or “ASE 15.”
When a login is created, its default database is initially set to the master
database. In most cases, the System Administrator changes this to a different
database before the login is put into use.
Technically, a batch can consist of a single SQL statement. However, the term
is generally used for a series of two or more statements.
TSQL statements submitted one at a time are said to be executed
“interactively.”
Example:
select * from titles
go
select * from authors
go
4
SQL Advantage is still currently available with older versions
ASE 15 uses Interactive SQL which is very similar
 The name of the executable is dbisql
10
4
45
46
4
Syntax conventions
 plaintext is used for literal key words.
 italic is used for user-supplied items.
 [ ] denotes optional items.
 {choice1 | choice2 } denotes a required item with two or more choices.
4
Avoid using SQL reserved characters such as +, -, *, /, and % in column
headings.
When using the ANSI-89 method, the as is recommended, but it is
technically optional. The following query is syntactically equivalent to the
example in the slide above:
select au_id "Social Security Number",
au_lname "Last Name",
au_fname "First Name"
from authors
Although the as is optional, it is recommended that you always include it. If
you omit the as and inadvertently omit a comma, then a column name
could instead be treated as a column heading.
 Example:
select au_id "SS Number",
au_lname au_fname
from authors

SS Number au_fname
---------- --------
172-32-1176 Johnson
213-46-8915 Marjorie
238-95-7766 Cheryl
267-41-2394 Michael
274-80-9391 Dick
....
Character strings can be used in any select statement, including
statements that do not reference any table columns. The string appears in
the result set once for each row in the table.
Example:
select "sample text"
from publishers

-
sample text
sample text
sample text
Recall that when an asterisk (*) is used in the select list, columns appear
in the order in which they were declared when the table was created.
4
Example without distinct
select state from authors

state
-----
CA
CA
CA
CA
CA
KS
CA
CA
CA
CA
TN
OR
...
Example without distinct
select au_lname, state from authors

au_lname state
-------- -----
...
Panteley MD
Hunter CA
McBadden CA
Ringer UT
Ringer UT
In ANSI standard code, columns in the order by clause must appear in the
select list.
When ordering data by multiple columns, each column has its own sort
order (ascending or descending).
 Example:
select title_id, type, price
from titles
order by type asc, price desc

title_id type price


-------- ---- -----
MC3026 UNDECIDED NULL
BU1032 business 19.99
BU7832 business 19.99
BU1111 business 11.95
BU2075 business 2.99
MC2222 mod_cook 19.99
MC3021 mod_cook 2.99
...
Examples
select title_id, advance, advance + $5000 from titles
select title_id, advance, advance - $5000 from titles
select title, total_sales/12 as month_sales from titles
*Modulo
 Modulo is not an ANSI sql function, however it is supported by most
database management systems. It finds the integer remainder after
dividing two whole numbers.
 Example:
 14 % 5 = 4 because 14 divided by 5 equals 2 with a remainder of 4.
4
Less than and greater than
 < means earlier in the sort order.
 > means later in the sort order.
 In comparing dates, < means earlier and > means later.
 In comparing strings, < and > depend on the sort order selected. For
example, uppercase letters appear first in sorted output in some
cases.
 Uppercase letters are greater than numbers
You can use arithmetic operators in a where clause.
Both is NULL and = NULL are ANSI standard.
Recall that NULLs are not considered greater or lesser than other values.
The condition where price < $5.00 does not return any NULLs.
Recall that NULLs are not considered equal to one another. Adaptive
Server cannot determine whether two NULLs are the same, only that both
are unknown. The condition where projected_sales = actual_sales does
not return any rows when both values are NULL.
To search for values outside of a given list, use the keyword not before the
keyword in. For example, the following query would return all authors not
living in Oregon, Idaho, or Utah:
select au_lname, au_fname, state
from authors
where state not in ("OR", "ID", "UT")

Example that uses money


select title, price
from titles
where price in ($10.95, $11.95)

title price
----- -----
Cooking with Computers... 11.95
Is Anger the Enemy? 10.95
Fifty Years in Buckingham... 11.95
Adaptive Server treats between inclusively. That is, it returns values that
are either between the values given or equal to the values given.
 between can be used to search for numeric and character data.
To search for values outside a given list, use the keyword not before the
keyword between. For example, the following query returns all books
priced below $7.00 or above $12.00:
select title, price
from titles
where price not between $7.00 and $12.00
To search for values that do not match a given search pattern, use the
keyword not before the keyword like. For example, the following query
returns all book titles that do not include the word “Computer”:
select title
from titles
where title not like "%Computer%"
Other Examples
 To find authors whose last name ends in "ley", enter:
select au_fname, au_lname
from authors
where au_lname like "%ley"

au_fname au_lname
-------- --------
Chastity Locksley
Sylvia Panteley

 To find titles containing the word "Psychology", enter:


select title
from titles
where title like "%Psychology%“

title
-----
The Psychology of Computer Cooking
For the ANSI-compliant method, the phrase ’escape "x"’ is included at the
end of the where clause, where x is a user-supplied escape character. Any
character in the where clause followed by the escape character is treated
as a literal, not as a wildcard.
You can combine up to 250 conditions in a single where clause.
The above query does not work as intended because the and operator
takes precedence over the or operator.
 Thus, the statement is parsed as:
where (type = "business") or
(type = "psychology" and advance > $5500)
 when it was intended to be parsed as:
where (type = "business" or type = "psychology") and (advance
> $5500)
10
4
97
98
4
Aggregates and datatypes
 Some aggregates can be used only with certain datatypes:
 count(*) – All datatypes
 max and min – All datatypes except text, image, and bit
 sum and avg – Numeric and money datatypes
select price from titles
price
--------
19.99
11.95
2.99
19.99
19.99
2.99
NULL
22.95
20.00
NULL
21.59
10.95
7.00
19.99
7.99
20.95
11.95
14.99
select price from titles
price
--------
19.99
11.95
2.99
19.99
19.99
2.99
NULL
22.95
20.00
NULL
21.59
10.95
7.00
19.99
7.99
20.95
11.95
14.99
select price from titles
price
--------
19.99
11.95
2.99
19.99
19.99
2.99
NULL
22.95
20.00
NULL
21.59
10.95
7.00
19.99
7.99
20.95
11.95
14.99
Note: The use of the ISNULL/IFNULL/COALESCE function is not limited to
aggregate functions

The function name is DBMS specific


 Sybase ASE & SQL Server:
ISNULL(column_name, non-
NULL-value)
 Oracle:
NVL(column_name, non-NULL-
value)
 MySQL and HANA:

ISNULL(column_name, non-
NULL-value)
 ANSI SQL and MySQL:
COALESCE(column_name, non-
NULL-value)
Aggregates cannot be used in a where clause because the where clause is
applied while rows are being read, and aggregates are not applied until after
all the rows have been read.
4
The group by clause is generally used with aggregates.
When used with aggregates, the data is sorted into groups first, and then the
aggregate is applied to each group.
The rows filtered out by the where clause in the above
query appear below in bold.

select type, price from titles


order by type, price
type price
---- -----
UNDECIDED NULL
business 2.99
business 11.95
business 19.99
business 19.99
mod_cook 2.99
mod_cook 19.99
popular_comp NULL
popular_comp 20.00
popular_comp 22.95
psychology 7.00
psychology 7.99
psychology 10.95
psychology 19.99
psychology 21.59
trad_cook 11.95
trad_cook 14.99
trad_cook 20.95
Results: select avg(price), type from titles group by type
type
----------- ----
NULL UNDECIDED
13.73 business
11.49 mod_cook
21.48 popular_comp
13.50 psychology
15.96 trad_cook

Results: select avg(price), type from titles group by type


having avg(price) > $12.00

type
----------- ----
13.73 business
21.48 popular_comp
13.50 psychology
15.96 trad_cook
10
10
4
118
119
4
Notes:
•The differences in the ANSI SQL/92 standard are:
• The type of join is specified in the from clause: inner, outer, cross, etc.
• The join clause is specified in the on clause
The join conditions discussed here apply to inner joins. Outer joins are
discussed in Mastering Transact SQL
A join condition is also known as a “join predicate.”
It is considered good programming practice to prefix all column names in a join
with table names, even if there is no ambiguity. This can help later developers
to read and understand the join code.
Example:
select titles.title, publishers.pub_name
from titles, publishers
where titles.pub_id = publishers.pub_id
Notes:
•A left outer join specifies that all rows will be selected from the table listed first
in the from clause
•A right outer join specifies that all rows will be selected from the table listed
second in the from clause
•However, the two from clauses below are identical:
from A left outer join B
from B right outer join A
The above query returned 54 rows because 54 is the product of 3 publisher
rows times 18 title rows.
Column names can be ambiguous, whether they are used in the where clause
or in the select clause.
 Example of an ambiguous column in the select clause:
select title, pub_id, pub_name
from titles, publishers
where titles.pub_id = publishers.pub_id
Msg 209, Level 16, State 1:
Line 1:
Ambiguous column name pub_id
If you qualify a table with its database name in the from clause, then you must
also qualify that table with its database name in the where clause.
 Example:
select title, pub_name
from pubs2..titles, pubs2..publishers
where pubs2..titles.pub_id =
pubs2..publishers.pub_id
select statement
Tables can be located in the same database or in different databases.
Tables in a different database must be qualified with the database and owner
name, such as pubs2.dbo.titles (or pubs2..titles if the Database Owner and the
table creator are the same person).
from clause
This is a comma-separated list of all the tables involved in the query, whether or
not they contribute to the select list.
where clause
Join columns in the where clause need not use the same datatype, but either
the columns must be a type that the server can implicitly convert, or you must
explicitly convert them.
 int, smallint, tinyint, decimal, real, or float implicitly convert to one
another.
 char, varchar, datetime, and smalldatetime implicitly convert to one
another.
An alias can be used only in the query in which it is created.
When an alias is created, you cannot use the original table name outside of the
from clause. You must use the alias name.
 Example:
select p.pub_id, p.pub_name, t.title
from titles t, publishers p
where t.pub_id = p.pub_id
order by p.pub_id, t.title
The join above, when written using ANSI 89 syntax, is:
select title, price
from titles, salesdetail
where titles.title_id = salesdetail.title_id
and titles.price > 22.0
.

Version notes
Adaptive Server Enterprise 11.9.2 and prior:
 Only Transact-SQL join syntax is supported.
Adaptive Server Enterprise 12.0 and later:
 In addition to Transact-SQL join syntax, ANSI 92 SQL join syntax is
supported.

ANSI 89 and 92 left and right outer joins are discussed in the Mastering
Transact SQL course.
4
Results of second query
title
---------------------------------------------
You Can Combat Computer Stress!
Is Anger the Enemy?
Life Without Fear
Prolonged Data Deprivation: Four Case Studies
Emotional Security: A New Algorithm
Results of either query
title
---------------------------------------------
You Can Combat Computer Stress!
Is Anger the Enemy?
Life Without Fear
Prolonged Data Deprivation: Four Case Studies
Emotional Security: A New Algorithm
The subquery is resolved first and the results substituted into the outer query’s
where clause.
Only columns from the select list in the outermost statement are displayed.
The column list of a subquery cannot include more than one column name.
There are some occasions where a join can accomplish something that cannot
be accomplished with a subquery. For example, a non-join query with a
subquery cannot return data from two or more tables to the result set.
Example explanation
1. Find the au_id of Mr. Blotchet-Halls.
2. Find the title_id to go with the au_id from Step 1.
3. Find the title to go with the title_id from Step 2.
Subquery example and explanation
List the publisher of the book(s) with a price of $22.95.
select pub_name, pub_id
from publishers
where pub_id in
(select pub_id
from titles
where price = $22.95)
pub_name pub_id
----------------------------- -------
Algodata Infosystems 1389
(1 row affected)
Explanation:
1. Find the pub_id to go with the highest book price.
2. Find the publisher to go with the pub_id from Step 1.
Example results
stor_id title_id
------- --------
7066 BU2075
7066 BU7832
7066 MC3021
7066 PC1035
7066 PC8888
...
(25 rows affected)

Example: An error
select title_id, price
from titles
where price >
(select price
from titles)
Msg 512, Level 16, State 1:
Line 1:
Subquery returned more than 1 value. This is illegal when
the subquery follows =, !=, <, <= , >, >=, or when the
subquery is used as an expression.
Results:
select title_id, price
from titles
where price >
(select avg(price)
from titles)
go
title_id price
-------- ------------------------
BU1032 19.99
BU7832 19.99
MC2222 19.99
PC1035 22.95
PC8888 20.00
PS1372 21.59
PS3333 19.99
TC3218 20.95
TC7777 14.99
(9 rows affected)
Examples
List the titles given an advance greater than the minimum
advance paid by Algodata Infosystems:
select distinct title, advance
from titles
where advance > (
select min(advance)
from titles, publishers
where titles.pub_id = publishers.pub_id
and pub_name = "Algodata Infosystems")
List the titles given an advance greater than the largest advance
paid by Algodata Infosystems:
select distinct title, advance
from titles
where advance > (
select max(advance)
from publishers, titles
where titles.pub_id =
publishers.pub_id
and pub_name = "Algodata Infosystems")
Results:
select title_id, price
from titles
where price >
(select avg(price)
from titles)
go
title_id price
-------- ------------------------
BU1032 19.99
BU7832 19.99
MC2222 19.99
PC1035 22.95
PC8888 20.00
PS1372 21.59
PS3333 19.99
TC3218 20.95
TC7777 14.99
(9 rows affected)
Results:
select title_id, price
from titles
where price >
(select avg(price)
from titles)
go
title_id price
-------- ------------------------
BU1032 19.99
BU7832 19.99
MC2222 19.99
PC1035 22.95
PC8888 20.00
PS1372 21.59
PS3333 19.99
TC3218 20.95
TC7777 14.99
(9 rows affected)
You can refer back to this slide when introducing views.
A select statement based on a view (defined as the union
of stores and publishers) looks pretty much the same when
“expanding” the view name by its definition.
10
4
162
163
164
165
166
4
Enforcing business rules
Recall that business rules are requirements that the business imposes on data.
Datatypes can enforce business rules.
 Sample business rule:
 For a given order, the customer can order from 1 to 20,000 copies of each title.
 Implementation in pubs2:
 In the salesdetail table, the qty column uses the smallint datatype, which restricts the
data to whole numbers from -32,768 to 32,767.
Unsigned integers can be defined using (requires same storage as signed
version of the data type)
unsigned bigint - 0 to 2*263-1
unisigned int - 0 to 4,294,967,295
unsigned smallint - 0 to 65,535
All the approximate numeric datatypes are ANSI standard.
Approximate numeric datatypes are appropriate if the application can tolerate
rounding; otherwise, you must use a more precise datatype.
Approximate numeric datatypes round during arithmetic operations as follows:
 The number of digits to the right of the decimal point can change,
depending on the magnitude of the numbers used in the calculations.
For approximate datatypes, isql displays only six significant digits after the
decimal point and rounds the remainder.
Specifying Precision
The float datatype lets you specify the precision of the floating-point number:
 For example, float(6) specifies that values in that column have an accuracy
of six digits.
You cannot specify the location of the significant digits relative to the decimal
point, as you can with the decimal and numeric datatypes.
char(n) provides a performance edge over the variable character datatypes.
Use char(n) when the length of the character data is predictable or small (five characters or
fewer).
Multibyte character strings
Use nchar(n) to store fixed-length national strings.
 nchar(n) can store multibyte characters, for example, Japanese; char(n) can store only one byte
characters.
 The number of bytes required for nchar(n) is n * @@ncharsize.
Use nvarchar(n) to store variable-length national strings.
 The number of bytes required for nvarchar(n) is equal to @@ncharsize * the number of characters.
Large Binary Objects are not ANSI compliant but available in a variety of data
types depending on the Database Management System.
4
Auto-Incremement
 ANSI 2003 included an auto increment sytax called Identity. By the time the
ANSI standard was released all of the DBMS vendors had already provided
an auto-identity syntax and few conform to the ANSI standard. Check the
syntax guide for the specific DBMS to determine the correct auto-identity
syntax.
Enforcing business rules
Recall that business rules are requirements that the business imposes on data.
Column properties can enforce business rules.
 Sample business rule:
 A book can be entered into the database, even if the book’s publisher or its price have
not yet been determined.
 Implementation in pubs2:
 In the titles table, the pub_id column and price column allow for NULL values.
.
Guidelines for using NULLs
Use NULLs:
 When the value of a column will be unknown at the time data is inserted
 Only when necessary
Tha above description of an Identity column is an example of an auto-increment
column specifically for ASE
Typically, users do not supply values for auto-increment columns.
For a given table, Adaptive Server uses a unique value for each attempted
insert. If the insert fails, the value is lost.
 For example, if a new table with an IDENTITY column has two successful
inserts and the third insert fails, the values of 1 and 2 are inserted, but the
value of 3 is lost. During the next attempted insert, Adaptive Server uses
the value 4.
Must be
 a numeric datatype with 0 decimal places
Example: numeric(5,0)
 an integer datatype
Cannot be updated
Does not accept NULL values
Starts at 1 by default
4
NULL can be used as a default value if the column is nullable.
Quotation marks are required when assigning a text string or a date as a
default value. Quotation marks are not required around numeric default values.
A column can have only one default.
 The default must match or be convertible to the datatype of the given
column.
Dropping defaults
To drop a default, you must use the alter table syntax as if the default were a
constraint.
 Syntax:
alter table table_name
drop constraint default_name
 Example:
alter table publishers
drop constraint publishers_city_464004684

The default name is system-generated. To determine the name of the default,


you can use the system procedure sp_helpconstraint. (See the previous page.)
Constraints are discussed in the module, “Using Constraints to Enforce Data
Integrity.”
4
The IDENTITY column in a table may be dropped if the column is not part of an
index, and if set identity_insert is not on. Indexes are discussed in the
module, “Using Views and Indexes to Affect Query Performance.”
Successfully modifying a column’s datatype depends on both the compatibility
of existing and target datatypes and the data in the table.
Note that there is no comma between add, drop, and modify clauses.
In a single statement, add, drop, and modify clauses can appear in any order,
and can be repeated.
When using the add clause, you must always include a datatype.
When using the modify clause, you do not need to include a datatype if only
the column’s nullability is being changed.
This is not an ANSI compliant command. However it is a convenient
command for labs and try-its.
10
4
195
196
4
select is reviewed briefly in the module, “Querying Data in Tables.”
The column list is optional. If it is not supplied, the first value is inserted into
the first column, the second value into the second column, and so on.
When inserting data, you should not supply a value for IDENTITY columns
unless identity_insert has been turned on for that table.
If a value is not supplied for a column, the column must allow NULLs, have a
default clause, or be an IDENTITY column. Otherwise, the insert fails.
(Default clauses are discussed later in this module.)
If a value supplied for a column does not match the datatype of that column,
the insert fails.
When inserting data into a datetime column, if a time is not specified, the
server sets the time to midnight.
In this course, inserts are represented using the following graphic:

If the value supplied for a character column is too large for the column, the
character value will be truncated before insertion. Adaptive Server provides
no warning when this occurs
update can modify one or more columns in a given table.
update can modify either all the rows of a table, or a subset of rows
matching a given condition.
If a value supplied for a column does not match the datatype of that column,
the update fails.
You cannot include an IDENTITY column in an update statement.
In this course, updates are represented using the following graphic:
4
In this course, deletes are represented using the following graphic:
truncate table deletes all rows in a table but not the table itself.
Only the table owner can use truncate table.
10
4
209
210
4
A view does not store any table data. When executed, the view merely
retrieves data from the underlying table(s).
A view is also known as a “virtual table.”
You can rename columns in the view. This is an example:
create view vw_author_names (last_name, first_name)
as
select au_lname, au_fname
from authors
If you do not specify column names after the view name, the view columns
inherit the headings of the result set columns.
Suggested naming convention
Compose view names in the form vw_xxxxx..._xxxxx..., where:
 vw is a constant indicating that the object is a view.
 xxxxx... are descriptive and meaningful singular nouns, separated by
underscores, that refer to the tables, columns, or rows the view
represents.
If you specify a column list when using a view, you must use the column
names of the view, not of the underlying table.
However, if no column names were specified when the view was created,
then the view inherits the column names from the query’s result set.
Additional restrictions
You cannot modify data through a view that uses distinct.
You cannot modify data through a view if the modification refers to a
column that is a computation (such as price * qty) or an aggregate (such
as avg(price)).
To create a view that restricts access to a table, the table owner must:
Create the view.
Grant the appropriate permissions (select, insert, update,
delete) for the view to the appropriate users.
Revoke the appropriate permissions on the table from the
appropriate users.
To grant access to a table or view, use:
grant [select | insert | update | delete | all]
on [ table_name | view_name ] to [ user_list ]
To revoke access to a table or view, use:
revoke [select | insert | update | delete | all]
on [ table_name | view_name ] from [ user_list ]
with check option error message
The update above generates this error message:
Msg 550, Level 16, State 1:
Line 1:
The attempted insert or update failed because the target view
was either created WITH CHECK OPTION or spans another view
created WITH CHECK OPTION. At least one resultant row from
the command would not qualify under the CHECK OPTION
constraint.
Example: nondistinct view
create view vw_nondistinct
as
select type, pub_id from titles
go
select * from vw_non_distinct

type pub_id
---- ------
business 1389
business 1389
business 0736
business 1389
popular_comp 1389
popular_comp 1389
popular_comp 1389
Example: distinct view
create view vw_distinct
as
select distinct type, pub_id from titles
go
select * from vw_distinct

type pub_id
---- ------
business 0736
business 1389
popular_comp 1389
4
Table scans can be costly in terms of performance, especially if the table
is large.
An index can be created on one or more specific columns for a given
table.
The index stores the values of that column in sorted order.
Queries requesting data based on that column can be completed more
rapidly because the index identifies the relevant rows.
For these types of queries, the server does not need to scan every row.
Indexes can also be used to enforce entity integrity. This topic is discussed
in the module, “Using Constraints to Enforce Data Integrity.”
Step-by-Step Explanation of an Index Search
The user executes the query:
select * from authors
where au_lname = "Green"
The query optimizer chooses to use index idx_authors_2.
The server searches page 1001 for “Green”. The largest value smaller
than “Green” is “Bennet”, which points to index page 1007.
The server searches page 1007 for “Green”. The largest value smaller
than “Green” is “Greane”, which points to index page 1133.
The server searches page 1133 for “Green”. It finds only one entry for
Green, which points to data page 1421, row 2.
The server reads the table data on page 1421, row 2.
Notes
An index is composed of one or more levels. Each level is a series of
pages.
Neither the entire index nor the entire table are shown.
Row pointers are used to uniquely identify every entry in the index.
The index above is a nonclustered index on an allpages locking table.
There would be differences in the structure if the index was clustered or if
the table was not an allpages locking table.
Indexes and permissions
To create or drop an index on a given table, you must be the owner of that
table.
An index does not have separate object permissions. Any user with
permissions to query or modify a given table has permission to use the
relevant indexes on that table.
Suggested naming convention
Compose index names in the form idx_[u][c]_ttttt..._n, where:
 idx is a constant indicating that the object is an index.
 u is a constant indicating a unique index (otherwise omitted).
 c is a constant indicating a clustered index (otherwise omitted).
 ttttt... is the name of the table referred to by the index.
 n is a sequential identifier, where:
1 indicates the index on the primary key.
2 and up are used for all other indexes.
Every index has three attributes, one from each pair above, in any
combination.
Examples:
 A noncomposite, unique, clustered index.
 A composite, nonunique, nonclustered index.
If a unique index is created on a nullable column, the index can have at
most one NULL value.
If a unique index is created on a column that does not allow NULLs, the
index cannot have any NULL values.
Enforcing business rules
Recall that business rules are requirements that the business imposes on
data.
Unique indexes can enforce business rules.
 Sample business rule:
 Every book must have a unique identification code.
 Implementation in pubs2:
 There is a unique index on the title_id column of the titles table.
Default nonclustered indexes
This type of index has three types of levels:
 The root level is the highest level of the index. There is exactly one
page at the root level.
 The leaf level is the lowest level of the index.
 For every row in the table, there is a row in the leaf level. Each row in the leaf
level points to one row in the table.
 The rows in the leaf level are sorted in indexed order. The rows in the table,
however, are not typically in indexed order.
 The levels between the root and leaf level are the intermediate
levels. The number of intermediate levels depends on the size of the
table. (Larger tables require more intermediate levels.)
L
When created, clustered indexes temporarily require an additional 120% of
the table size to sort the table during index creation.
When a clustered index is created, the data must be sorted into indexed
order and rebuild any nonclustered indexes on the table. This could result
in a temporary impairment of performance.
A table without a clustered index is known as a heap table. (This is
because newly inserted data is “heaped” at the end of the table.)
Default clustered indexes
This type of index has two types of levels:
 The root level is the highest level of the index. Like a nonclustered
index, there is exactly one page at the root level.
 The levels between the root level and the table itself are the
intermediate levels. Like a nonclustered index, the number of
intermediate levels depends on the size of the table. (Larger tables
require more intermediate levels.)
When a default clustered index is created, the table’s data is sorted in
index order. When modifications are made, data is maintained in sorted
order. Thus, a clustered index does not need a leaf level.
 Some people refer to the data pages themselves as the leaf level.
If a table:
 Has two or more columns that can identify the desired rows, and
 One or more columns is indexed while one or more is not; then
 You should list all the columns that can identify the desired rows in
your query’s where clause
Given that an indexed column may be unindexed in the future and/or an
unindexed column may be indexed in the future, the best long-term
strategy is to list all columns that can identify the desired rows.
 The more information you give the query optimizer in terms of how it
can execute the query, the more likely it is that the query optimizer will
locate the best method
If permissions are at the bottom of the script that creates an object, then
rerunning the script creates the object and grants permissions for it at the
same time.
10
4
244
245
4
The acceptable set of values for a given column is also known as the column’s
“domain.”
“Declarative integrity” refers to integrity maintained by constraints created via
the create table or alter table commands.
Indexes are discussed in the module, “Using Views and Indexes.”
Enforcing business rules
Recall that business rules are requirements that the business imposes on data.
Constraints can enforce business rules.
 Sample business rule:
 Every book must have a unique identification code.
 Possible implementation in pubs2:
 The title_id column of the titles table could have a unique constraint which prevents
duplicate title ID values from being entered into the table.
4
A check constraint specifies:
 A condition
 A list of values
 A range of values
 A character pattern
A character pattern is also known as an “edit picture.”
Domain integrity is also enforced by datatypes. However, datatypes cannot
always restrict the range as precisely as needed. Check constraints help to
improve the restriction.
Multicolumn check constraints can be declared at the column level or table
level.
A single column can have multiple check constraints. This is useful when two or
more restrictions apply to a column and you would like to know which rule was
violated when an insert fails.
Example:

create table publishers (


pub_id char(4) NOT NULL,
pub_name varchar(40) NULL,
city varchar(20) NULL,
state char(2) NULL
constraint chk_state_in_caps
check (state like "[A-Z][A-Z]")
constraint chk_state_in_region
check (state in ("WA","OR","CA","UT"))
Suggested naming convention
Compose constraint names in the form ttt_[c_]xxxxx..., where:
 ttt represents the type of constraint:
 chk for check constraints
 pky for primary key constraints
 unq for unique constraints
 ref for references constraints
 c is a constant included if the constraint is implemented as a clustered
index (applicable only for primary key and unique constraints).
 xxxxx... describes the nature of the constraint or column(s) on which the
constraint is applied.
A table-level check constraint can reference any column or columns in the
table.
If there are two or more table-level constraints, every table-level constraint
except for the last must be terminated by a comma.
Example:
create table discounts (
discounttype varchar(40) NOT NULL,
stor_id char(4) NULL,
lowqty smallint NULL,
highqty smallint NULL,
discount float NOT NULL,
constraint chk_low_high
check (lowqty <= highqty),
constraint chk_discount_range
check (discount between 0 and 20)
)
4
A table with a primary key is sometimes referred to as a “primary key table” or a
“parent table.”
Primary keys are typically created for every table in a relational database
Example:
 au_id uniquely identifies each author
A composite key is a key that consists of more than one column
Example:
 stor_id and ord_num uniquely identify each sale
 Two sales could use the same order number if they occurred at different
stores, but not if they occurred at the same store
Indexes created by primary key constraints can be dropped using the alter
table command. This command is discussed later in the module.
A column-level primary key constraint cannot reference any columns. It is
created automatically on the column on which it is declared.
A table-level primary key constraint can reference any column or columns in the
table.
Multicolumn primary key constraints must be declared at the table level.
Indexes created by unique constraints can be dropped using the alter table
command. This command is discussed later in the module.
A column-level unique constraint cannot reference any columns. It is created
automatically on the column on which it is declared.
A table-level unique constraint can reference any column or columns in the
table.
Multicolumn unique constraints must be declared at the table level.
4
A table with a foreign key is sometimes referred to as a “foreign key table” or a
“dependent table.”
Recall that a table with a primary key is sometimes referred to as a “primary
key table” or a “parent table.”
A column-level references constraint applies only to the column on which it is
created.
A table-level references constraint can apply to any column(s) in the table.
Multicolumn references constraints must be declared at the table level.
The restriction in the first bullet above exists because a references constraint
can reference only a column or columns with a unique index. The three sub-
bullets enumerate the three ways to create a unique index.
4
Example: adding a unique constraint
alter table publishers
add constraint unq_pub_name
unique (pub_name)
Notes
Adding a constraint does not affect existing data in the table. A constraint
applies only to data modified after the constraint is added.
When a primary key constraint or unique constraint is added, Adaptive Server
must build an index for the constraint. This could result in a temporary
compromise in performance.
When a clustered primary key constraint or a clustered unique constraint is
added the data in the table must be sorted into indexed order and rebuild any
nonclustered indexes on the table. This could result in a temporary compromise
in performance.
To drop a constraint, you must know the constraint name. Use
sp_helpconstraint table_name to find the names of all constraints on a table.
When you drop a table, all constraints on that table are automatically dropped.
10
4
290
291
4
The ACID properties define four important aspects of a transaction:
 Atomicity
 A transaction is an all or nothing process.
 Consistency
 A transaction changes the database from one consistent state to another.
 Isolation
 A transaction’s changes are not visible to other users until the changes are complete or
rolled back.
 Durability
 Once complete, a transaction cannot be undone.
Examples of when to use a transaction:
 Transfer of funds
 Debiting money from first account
 Adding money to second account
 Modifying prices based on sales
 Checking to see which titles have not sold
 Modifying the prices of those titles
 Maintaining database integrity
 Adding a sale to the sales tables
 Adding a sale to the salesdetail table
 Undoing both modifications if either one generated an error
4
commit tran and rollback tran can be used in chained mode, but because
chained mode is often used to meet with ANSI compliance, it may be more
appropriate to use commit work and rollback work.
Transaction names can be provided to improve readability. However,
transaction names do not affect how the begin tran or commit tran statements
are processed.
You can execute a rollback tran after committing a transaction, but the
rollback tran will have no effect. In other words, you cannot roll back a
transaction that is already committed.
You can execute a rollback tran after committing a transaction, but the
rollback tran will have no effect. In other words, you cannot roll back a
transaction that is already committed.
10
4
317
318
319
320
4
Transaction 1 increases the price of “Emotional Security: A New Algorithm” from
$7.99 to $12.99, but has not yet committed.
Transaction 2 increases the price of all books priced below $10.00 and
commits.
Transaction 1 is rolled back, returning the price of “Emotional Security: A New
Algorithm” to $7.99. However, the price is now inaccurate because it should
have been increased by transaction 2.
Concurrency – The state in which several users can access a given body of
data at the same time with little or no wait time.
Contention – The state in which several users have to wait for an
unacceptable length of time to access a given body of data.
Put to sleep – The state that a transaction is placed in by the server when the
transaction wants to access locked data and must wait for the lock to be
released.
Structure of a table
Recall that the basic unit of storage in an Adaptive Server is a page. A page
may hold 2 kilobytes (2K), 4K, 8K, or 16K of data.
A table is a chain of pages.
Each page stores one or more rows.
 Except for text and image data, a single row cannot span two pages.
Structure of an index
An index is made up of a series of levels. Each level contains one or more
pages.
Rows in an index tend to be smaller than rows in a table. Therefore, pages in
an index tend to have more rows per page than pages in a table.
Determining the granularity of locks
Adaptive Server automatically decides what granularity of lock to use.
 If it is likely that a process will need to lock only a small amount of data,
Adaptive Server may use row-level locks (if they are available for the given
table) or page-level locks.
 If it is likely that a process will need to lock a large amount of data, Adaptive
Server may use a table-level lock.
The lock promotion threshold is a configurable value. If the number of pages (or
rows) locked by a single process exceeds this value, then Adaptive Server
attempts to promote the page locks (or row locks) to a single table lock.
Adaptive Server does not promote row locks to page locks.
For more information about how Adaptive Server chooses the granularity of
lock and how you can influence this choice, refer to the Adaptive Server
Enterprise Performance and Tuning Guide or the Performance and Tuning:
Configuring Adaptive Server Enterprise course.
Multiple transactions can place shared locks on a given set of data at the same
time. This means that each transaction can read the data, but no transaction
can alter the data.
Shared locks are identified by the letter “S.”
Exclusive locks are identified by the letter “X.”
Deadlocks occur when two or more transactions have locks on data, and each
transaction is waiting to acquire a lock on data that the other transaction has
locked.
Preventing deadlocks
To minimize the possibility of deadlocks:
 Have all transactions access tables in the same order.
 Make transactions as small as possible.
 Commit transactions as soon as possible.
 Avoid using the isolation level 3 option, except when repeatable reads are

required. (Isolation levels are discussed later in


this module.)
 Avoid requesting user input within a transaction.
4
Dirty reads, nonrepeatable reads, and phantom reads are not necessarily bad.
There are circumstances in which using these types of reads can significantly
improve performance without seriously impacting the usability of data. This
issue is discussed later in the module.
Transaction 1 increases a book’s price in the titles table from $9.99 to $12.99,
but has not yet been committed.
Then, transaction 2 reads and returns the titles of all books in the titles table
with the price of $12.99.
Transaction 1 is rolled back, returning the price of the title to $9.99.
The read executed by transaction 2 is a dirty read because it read data that
was not yet committed.
The current_stock table indicates that there are seven copies of the book “Is
Anger the Enemy?”
Transaction 1 attempts to order five copies of the book. First, it reads the table
to see whether there are at least five copies in stock, which there are.
Transaction 2 orders four copies of “Is Anger the Enemy?” It reduces the
number in stock from seven to three.
Transaction 1 tries to reduce the number in stock by five. However, this
statement is aborted because there are now fewer than five copies in stock.
The initial read of seven books could not be repeated by Transaction 1. The
first read is a nonrepeatable read.
In the titles table, there are two titles priced below $4.00 (“You Can Combat
Computer Stress!” at $2.99 and “The Gourmet Microwave” at $2.99).
Transaction 1 reads all titles priced below $4.00.
Transaction 2 reduces the price of all psychology books by 50%. Because of
this change, two titles originally priced above $4.00 (“Life Without Fear” at
$7.00 and “Emotional Security: A New Algorithm” at $7.99) are now priced
below $4.00.
Transaction 1 increases the price of all books below $4.00, and inadvertently
modifies the extra books that were not originally in the range.
The second read executed by transaction 1 is a phantom read because it
contains additional rows (“phantoms”) that were not in the original read.
Preventing dirty reads
Level 1 prevents dirty reads by forcing select statements to wait for exclusive
locks to be released. Exclusive locks are not released until the transaction is
complete. Thus, a select can read only committed data.
Allowing nonrepeatable reads
Level 1 isolation allows nonrepeatable reads because a shared lock is released
when select finishes reading the data, even if the transaction containing the
select still has statements to execute.
If the original transaction later attempts to read the data read by the first select,
there is no guarantee that the data has not already been modified by another
transaction. The read may not be repeatable.
Allowing phantom reads
Level 1 allows phantom reads because a shared lock is released when a select
finishes reading the data, even if the transaction containing the select still has
statements to execute.
If the original transaction later attempts to read the rows read by the first
select, there is no guarantee that the rows in the second select contain the
same rows as the first select.
Notes
Level 1 is sometimes referred to as the “read committed” level.
Preventing dirty reads
Level 2 prevents dirty reads in the same way that they are prevented at level 1.
Allowing nonrepeatable reads
Level 2 prevents nonrepeatable reads by using row-level shared locks that are
not released until the transaction is complete. (At level 1, they are released as
soon as the data has been scanned.) Thus, if the transaction that executed the
select later needs to modify that data, the transaction can be sure that the data
has not been modified by another transaction.
Allowing phantom reads
Level 2 allows phantom reads as does level 1. (Although shared locks are held
until the end of transaction, they are held at the row level. There is nothing to
prevent a row from being added into a range read by a previous select
statement, thereby becoming a phantom.)
Notes
Level 2 is sometimes referred to as the “read repeatable” level.
Preventing dirty reads
Level 3 prevents dirty reads in the same way that they are prevented at level 1.
Allowing nonrepeatable reads
Level 3 prevents nonrepeatable reads by using page-level or table-level shared
locks that not released until the transaction is complete. (At level 1, they are
released as soon as the data has been scanned.) Thus, if the transaction that
executed the select later needs to modify that data, the transaction can be sure
that the data has not been modified by another transaction.
Allowing phantom reads
Level 3 prevents phantom reads by using page-level or table-level shared locks
that are not released until the transaction is complete. (At level 1, they are
released as soon as the data has been scanned.) Thus, if the transaction that
executes the select later needs to modify that data, the transaction can be sure
that no rows have been added to or removed from any previously read range.
Notes
Level 3 is sometimes referred to as the “serializable” level.
Because all locks are held until a transaction is complete, level 3 offers the
lowest likelihood of invalid data, but also has the slowest performance.
Preventing dirty reads
Level 0 allows dirty reads by allowing selects to ignore exclusive locks and read
data that has not been committed.
Allowing nonrepeatable reads
Level 0 allows nonrepeatable reads to happen in the same way that they are
allowed for level 1.
Allowing phantom reads
Level 0 allows phantom reads to happen in the same way that they are allowed
for level 1.
Notes
Level 0 is sometimes referred to as the “read uncommitted” level.
Because selects are not impeded by any locks, level 0 offers the fastest
performance, as well as the highest likelihood of invalid data.
Level 0 is useful in decision support and circumstances in which rough
estimates can be used in place of exact figures. However, if your system
cannot tolerate invalid data, you should not use level 0.
Choosing the isolation level
If the isolation level is set too high, a large amount of data is locked, and users
who should have access to the data are unable to complete their work
efficiently.
If an isolation level is set too low, there is a greater risk that invalid data will get
into the system.
10
4
351
352

You might also like