SQL Refresher Complete Notes PDF
SQL Refresher Complete Notes PDF
SQL Refresher Complete Notes PDF
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.
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).
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.
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:
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.
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.
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 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
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
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.
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
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: