Вы находитесь на странице: 1из 209

DATABASE

COMPILED BY:
ATEEQ HAIDER BAIG

(COURSE MATERIAL FOR DEPARTMENTAL PROMOTION EXAMINATION (DPE))


INTRODUCTION 2

1. INTRODUCTION.................................................................................................................... 4
2. DATABASE MODEL.............................................................................................................. 5
1.1. Hierarchical Model ......................................................................................................... 5
1.2. Network Model ............................................................................................................... 5
1.3. Relational Model ............................................................................................................. 6
1.4. Object Relational Model ................................................................................................. 6
1.5. Object Model .................................................................................................................. 6
3. RELATIONAL DATABASE .................................................................................................. 8
4. DATABASE KEYS .................................................................................................................. 9
4.1. Unique Key ..................................................................................................................... 9
4.2. Defining Primary Keys ................................................................................................... 9
4.3. Foreign Key .................................................................................................................. 11
4.4. Defining Foreign Keys .................................................................................................. 11
4.5. Surrogate Key ............................................................................................................... 12
4.6. Super Key...................................................................................................................... 13
4.7. Candidate Key ............................................................................................................... 41
5. DATABASE NORMALIZATION ....................................................................................... 47
5.1. Problems addressed by normalization ........................................................................... 26
5.2. Background to normalization: definitions ..................................................................... 36
5.3. Normal forms ................................................................................................................ 34
5.4. First normal form .......................................................................................................... 20
5.5. Second normal form ...................................................................................................... 50
5.6. Third normal form......................................................................................................... 21
5.7. Boyce-Codd normal form ............................................................................................. 51
5.8. Fourth normal form ....................................................................................................... 31
5.9. Fifth normal form .......................................................................................................... 30
5.10. Domain/key normal form .......................................................................................... 29
5.11. Sixth normal form ..................................................................................................... 29
6. REFERENTIAL INTEGRITY ............................................................................................. 28
7. RELANATIONAL DBMS .................................................................................................... 24
8. DISTRIBUTED DBMS ......................................................................................................... 54
9. ACID ....................................................................................................................................... 18
9.1. Atomicity ...................................................................................................................... 16
9.2. Consistency ................................................................................................................... 16
9.3. Isolation......................................................................................................................... 16
9.4. Durability ...................................................................................................................... 16
INTRODUCTION 3

9.5. Implementation ............................................................................................................. 16


10. Introduction TO MICROSOFT SQL SERVER ................................................................. 57
10.1. Microsoft SQL Server Setup ..................................................................................... 57
10.2. Getting Microsoft SQL Server .................................................................................. 57
10.3. Installation Steps ....................................................................................................... 58
11. Introduction to Database Creation....................................................................................... 71
11.1. The Name of a Database ........................................................................................... 71
11.2. The Location of a Database ...................................................................................... 73
11.3. The Primary Size of a Database ................................................................................ 74
12. Database Creation With Code .............................................................................................. 76
12.1. Database Maintenance .............................................................................................. 78
12.2. The Current Database................................................................................................ 80
12.3. Refreshing the List of Databases .............................................................................. 80
12.4. Microsoft SQL Server Primary Settings ................................................................... 80
12.5. The Microsoft SQL Server Management Studio ...................................................... 83
12.6. Using the Management Studio .................................................................................. 84
12.7. to Code ...................................................................................................................... 86
12.8. The Structured Query Language ............................................................................... 87
12.9. The SQL Interpreter .................................................................................................. 88
12.10. Executing a Statement ............................................................................................... 88
12.11. Accessories for SQL Code Writing........................................................................... 91
12.12. Comments ................................................................................................................. 91
12.13. The End of a Statement ............................................................................................. 92
12.14. SQL Operators and Operands ................................................................................... 92
12.15. Binary Operators ....................................................................................................... 98
12.16. Bit Manipulations.................................................................................................... 100
12.17. Bits Comparison: The Bitwise AND Operator & ................................................... 102
12.18. Variables Fundamentals .......................................................................................... 104
12.19. Initializing a Variable.............................................................................................. 106
12.20. Data Types .............................................................................................................. 107
12.21. Decimal Variables ................................................................................................... 111
12.22. Date and Time Variables......................................................................................... 115
12.23. Equality Operator =................................................................................................. 125
13. SUGGESTED TEXT ........................................................................................................... 237
14. Sample MCQs ...................................................................................................................... 237
INTRODUCTION 4

1. INTRODUCTION

The term or expression database originated within the computer industry.


Although its meaning has been broadened by popular use, even to include non-
electronic databases, this article takes a more technical perspective towards the
topic. A possible definition is that a database is a structured collection of records
or data which is stored in a computer so that a program can consult it to answer
queries. The records retrieved in answer to queries become information that can
be used to make decisions. The computer program used to manage and query a
database is known as a database management system (DBMS). The properties
and design of database systems are included in the study of information science.

The central concept of a database is that of a collection of records, or pieces of


knowledge. Typically, for a given database, there is a structural description of
the type of facts held in that database: this description is known as a schema.
The schema describes the objects that are represented in the database, and the
relationships among them. There are a number of different ways of organizing a
schema, that is, of modeling the database structure: these are known as
database models (or data models). The model in most common use today is the
relational model, which in layman's terms represents all information in the form
of multiple related tables each consisting of rows and columns (the true
definition uses mathematical terminology). This model represents relationships
by the use of values common to more than one table. Other models such as the
hierarchical model and the network model use a more explicit representation of
relationships.

The term database refers to the collection of related records, and the software
should be referred to as the database management system or DBMS. When the
context is unambiguous, however, many database administrators and
programmers use the term database to cover both meanings.
DATABASE MODEL 5

2. DATABASE MODEL

A database model is a theory or specification describing how a database is


structured and used. Several such models have been suggested.

Common models include:

1. Hierarchical Model

2. Network Model

3. Relational Model

4. Object Relational Model

5. Object Model

1.1. HIERARCHICAL MODEL


In a hierarchical data model, data are organized into a tree-like structure. The
structure allows repeating information using parent/child relationships: each
parent can have many children but each child only has one parent. All attributes
of a specific record are listed under an entity type. In a database, an entity type
is the equivalent of a table; each individual record is represented as a row and
an attribute as a column. Entity types are related to each other using 1: N
mapping, also known as one-to-many relationships.

An example of a hierarchical data model would be if an organization had records


of employees in a table (entity type) called "Employees". In the table there
would be attributes/columns such as First Name, Last Name, Job Name and
Wage. The company also has data about the employee’s children in a separate
table called "Children" with attributes such as First Name, Last Name, and DOB.
The Employee table represents a parent segment and the Children table
represents a Child segment. These two segments form a hierarchy where an
employee may have many children, but each child may only have one parent.

1.2. NETWORK MODEL


The network model is a database model conceived as a flexible way of
representing objects and their relationships. Its original inventor was Charles
Bachman, and it was developed into a standard specification published in 1969
by the CODASYL Consortium. Where the hierarchical model structures data as a
tree of records, with each record having one parent record and many children,
the network model allows each record to have multiple parent and child records,
forming a lattice structure.

The chief argument in favour of the network model, in comparison to the


hierarchic model, was that it allowed a more natural modeling of relationships
DATABASE MODEL 6

between entities. Although the model was widely implemented and used, it failed
to become dominant for two main reasons. Firstly, IBM chose to stick to the
hierarchical model with semi-network extensions in their established products
such as IMS and DL/I. Secondly, it was eventually displaced by the relational
model, which offered a higher-level, more declarative interface. Until the early
1980s the performance benefits of the low-level navigational interfaces offered
by hierarchical and network databases were persuasive for many large-scale
applications, but as hardware became faster, the extra productivity and
flexibility of the relational model replaced the network model in corporate
enterprise usage.

The navigational interface offered by the network model bears some


resemblance to the hyperlink-based models that have become popular with the
advent of the Internet and World Wide Web. However, the network model (like
the relational model) assumes that the entire database has a centrally-managed
schema, and as such it is not well suited to distributed, heterogeneous
environments.

1.3. RELATIONAL MODEL


The relational model for database management is a database model based on
predicate logic and set theory. It was first formulated and proposed in 1969 by
Edgar Codd with aims that included avoiding, without loss of completeness, the
need to write computer programs to express database queries and enforce
database integrity constraints.

1.4. OBJECT RELATIONAL MODEL


An object-relational database (ORD) or object-relational database management
system (ORDBMS) is a relational database management system that allows
developers to integrate the database with their own custom data types and
methods. The term object-relational database is sometimes used to describe
external software products running over traditional DBMSs to provide similar
features; these systems are more correctly referred to as object-relational
mapping systems.

Whereas RDBMS or SQL-DBMS products focused on the efficient management of


data drawn from a limited set of data types (defined by the relevant language
standards), an object-relational DBMS allows software developers to integrate
their own types and the methods that apply to them into the DBMS. The goal of
ORDBMS technology is to allow developers to raise the level of abstraction at
which they view the problem domain.

1.5. OBJECT MODEL


In the computing discipline object model has two related but distinct meanings:
DATABASE MODEL 7

1. The properties of objects in general, in a specific computer programming


language, technology, notation or methodology that uses them. For example,
the Java object model, the COM object model, or the object model of OMT. Such
object models are usually defined using concepts such as class, message,
inheritance, polymorphism, and encapsulation. There is an extensive literature
on formalized object models as a subset of the formal semantics of programming
languages.

2. A collection of objects or classes through which a program can examine and


manipulate some specific part of its world. In other words, the object-oriented
interface to some service or system. Such an interface is said to be the object
model of the represented service or system. For example, the HTML Document
Object Model (DOM) [1] is a collection of objects that represent a page in a web
browser, used by script programs to examine and dynamically change the page.
There is a Microsoft Excel object model for controlling Microsoft Excel from
another program, and the ASCOM Telescope Driver is an object model for
controlling an astronomical telescope.
RELATIONAL DATABASE 8

3. RELATIONAL DATABASE

A relational database is a database that conforms to the relational model, and


refers to a database's data and schema (the database's structure of how that
data is arranged). Common usage of the term "Relational Database Management
System" technically refers to the software used to create a relational database,
but sometimes mistakenly refers to a relational database.

The term relational database was originally defined and coined by E.F. Codd.
Codd's definition is no longer the only one in use, as many modern DBMS
manufacturers have adopted a more relaxed usage of the term.

A Relational Database Management System (RDBMS) is a software package such


as Oracle, Microsoft SQL Server, PostgreSQL, or MySQL. An RDBMS is used to
create relational databases. Sometimes an RDBMS is incorrectly called a
relational database. Most RDBMS software packages are not technically
considered "relational" because they do not fully conform to the relational
model.

Relational algebra, an offshoot of first-order logic, is a set of relations closed


under operators. Operators operate on one or more relations to yield a relation.
Relational algebra is a part of computer science.

Relation algebra in pure mathematics is an algebraic structure, relevant to


mathematical logic and set theory.
DATABASE KEYS 9

4. DATABASE KEYS
4.1. UNIQUE KEY

In relational database design, a unique key or primary key is a candidate key to


uniquely identify each row in a table. A unique key or primary key is comprised
of a single column or set of columns. No two distinct rows in a table can have
the same value (or combination of values) in those columns. Depending on its
design, a table may have arbitrarily many unique keys but at most one primary
key.

A unique key must uniquely identify all possible rows that exist in a table and
not only the currently existing rows. Examples of unique keys are Social Security
numbers (associated to a specific person) or ISBNs(associated to a specific
book). Telephone books and dictionaries cannot use names or words or Dewey
Decimal system numbers as candidate keys because they do not uniquely
identify telephone numbers or words.

A primary key is a special case of unique keys. The major difference is that for
unique keys the implicit NOT NULL constraint is not automatically enforced, while
for primary keys it is. Thus, the values in a unique key columns may or may not
be NULL. Another difference is that primary keys must be defined using another
syntax.

The relational model, as expressed through relational calculus and relational


algebra, does not distinguish between primary keys and other kinds of keys. In
the fourth normal form and higher, no relation has more than one key, so the
concept of a "primary key" becomes irrelevant. Primary keys were added to the
SQL standard mainly as a convenience to the application programmer and
because applications usually use at most third normal form.

Unique keys as well as primary keys can be referenced by foreign keys.

4.2. DEFINING PRIMARY KEYS


Primary keys are defined in the ANSI SQL Standard, through the PRIMARY KEY
constraint. The syntax to add such a constraint to an existing table is defined in
SQL:2003 like this:

ALTER TABLE <table identifier>

ADD [ CONSTRAINT <constraint identifier> ]

PRIMARY KEY ( <column expression> {, <column expression>}... )


DATABASE KEYS 10

The primary key can also be specified directly during table creation. In the SQL
Standard, primary keys may consist of one or multiple columns. Each column
participating in the primary key is implicitly defined as NOT NULL. Note that
some DBMS require that primary key columns are explicitly marked as being
NOT NULL.

CREATE TABLE table_name (

id_col INT,

col2 CHARACTER VARYING(20),

...

CONSTRAINT tab_pk PRIMARY KEY(id_col),

...

If the primary key consists only of a single column, the column can be marked
as such using the following syntax:

CREATE TABLE table_name (

id_col INT PRIMARY KEY,

col2 CHARACTER VARYING(20),

...

Defining Unique Keys

The definition of unique keys is syntactically very similar to primary keys.

ALTER TABLE <table identifier>

ADD [ CONSTRAINT <constraint identifier> ]

UNIQUE ( <column expression> {, <column expression>}... )

Likewise, unique keys can be defined as part of the CREATE TABLE SQL
statement.

CREATE TABLE table_name (

id_col INT,

col2 CHARACTER VARYING(20),

key_col SMALLINT,
DATABASE KEYS 11

...

CONSTRAINT key_uniqe UNIQUE(key_col),

...

CREATE TABLE table_name (

id_col INT PRIMARY KEY,

col2 CHARACTER VARYING(20),

...

key_col SMALLINT UNIQUE,

...

4.3. FOREIGN KEY


In the context of relational databases, a foreign key (FK) is a referential
constraint between two tables. The FK identifies a column or a set of columns in
one (referencing) table that refers to a column or set of columns in another
(referenced) table. The columns in the referenced table must form a primary key
or unique key. The values in one row of the referencing columns must occur in a
single row in the referenced table. Thus, a row in the referencing table cannot
contain values that don't exist in the referenced table. This way references can
be made to link information together and it is an essential part of database
normalization. Multiple rows in the referencing table may refer to the same row
in the referenced table.

The referencing and referenced table may be the same table, i.e. the foreign key
refers back to the same table. Such a foreign key is known in SQL: 2003 as self-
referencing or recursive foreign key.

A table may have multiple foreign keys, and each foreign key can have a
different referenced table. Each foreign key is enforced independently by the
database system. Therefore, cascading relationships between tables can be
established using foreign keys.

Improper foreign key/primary key relationships or not enforcing those


relationships are often the source of many database and data modeling
problems.

4.4. DEFINING FOREIGN KEYS


DATABASE KEYS 12

Foreign keys are defined in the ANSI SQL Standard, through a FOREIGN KEY
constraint. The syntax to add such a constraint to an existing table is defined in
SQL: 2003 like shown below. Omitting the column list in the REFERENCES clause
implies that the foreign key shall reference the primary key of the referenced
table.

ALTER TABLE <table identifier>

ADD [ CONSTRAINT <constraint identifier> ]

FOREIGN KEY ( <column expression> {, <column expression>}... )

REFERENCES <table identifier> [ ( <column expression> {, <column


expression>}... ) ]

[ ON UPDATE <referential action> ]

[ ON DELETE <referential action> ]

Likewise, foreign keys can be defined as part of the CREATE TABLE SQL
statement.

CREATE TABLE table_name (

id INTEGER PRIMARY KEY,

col2 CHARACTER VARYING(20),

col3 INTEGER,

...

CONSTRAINT col3_fk FOREIGN KEY(col3)

REFERENCING other_table(UNIQUE(key_col) ON DELETE CASCADE,

... )

If the foreign key is comprised of a single column only, the column can be
marked as such using the following syntax:

CREATE TABLE table_name (

id INTEGER PRIMARY KEY,

col2 CHARACTER VARYING(20),

col3 INTEGER FOREIGN KEY REFERENCES other_table(column_name),

... )

4.5. SURROGATE KEY


DATABASE KEYS 13

A Surrogate key in a database is a unique identifier for either an entity in the


modelled world or an object in the database. The Surrogate Key is not derived
from application data.

There appear to be two definitions of a surrogate in the literature. We shall call


these surrogate (1) and surrogate (2):

Surrogate (1) This definition is based on that given by Hall, Owlett and Todd
(1976). Here a surrogate represents an entity in the outside world. The
surrogate is internally generated by the system but is nevertheless visible by the
user or application.

Surrogate (2) This definition is based on that given by Wieringa and de Jung
(1991). Here a surrogate represents an object in the database itself. The
surrogate is internally generated by the system and is invisible to the user or
application.

We shall adopt the surrogate (1) definition throughout this article largely
because it is more data model rather than storage model oriented. See Date
(1998).

An important distinction exists between a surrogate and a primary key,


depending on whether the database is a current database or a temporal
database. A current database stores only currently valid data, therefore there is
a one-to-one correspondence between a surrogate in the modelled world and the
primary key of some object in the database; in this case the surrogate may be
used as a primary key, resulting in the term surrogate key. However, in a
temporal database there is a many-to-one relationship between primary keys
and the surrogate. Since there may be several objects in the database
corresponding to a single surrogate, we cannot use the surrogate as a primary
key; another attribute is required, in addition to the surrogate, to uniquely
identify each object.

4.6. SUPER KEY


A superkey is defined in the relational model as a set of attributes of a relation
variable (relvar) for which it holds that in all relations assigned to that variable
there are no two distinct tuples (rows) that have the same values for the
attributes in this set. Equivalently a superkey can also be defined as a set of
attributes of a relvar upon which all attributes of the relvar are functionally
dependent.

Note that if attribute set K is a superkey of relvar R, then at all times it is the
case that the projection of R over K has the same cardinality as R itself.

Informally, a superkey is a set of columns within a table whose values can be


used to uniquely identify a row. A candidate key is a minimal set of columns
DATABASE KEYS 14

necessary to identify a row, this is also called a minimal superkey. For example,
given an employee table, consisting of the columns employeeID, name, job, and
departmentID, we could use the employeeID in combination with any or all other
columns of this table to uniquely identify a row in the table. Examples of
superkeys in this table would be {employeeID, Name}, {employeeID, Name,
job}, and {employeeID, Name, job, departmentID}.

In a real database we don't need values for all of those columns to identify a
row. We only need, per our example, the set {employeeID}. This is a minimal
superkey – that is, a minimal set of columns that can be used to identify a single
row. So, employeeID is a candidate key.

Example

License Plates
Vehicle ID # State Plate # Color
Vehicle ID # State Plate # Color
State Plate # Color
Plate # Color
Color
10000000001 CM 333 Red
10000000001 CM 333 Red
CM 333 Red
333 Red
Red
10000000002 GA 399 Blue
10000000002 GA 399 Blue
GA 399 Blue
399 Blue
Blue
10000000003 FL 333 Blue
10000000003 FL 333 Blue
FL 333 Blue
333 Blue
Blue

In this example a superkey can be either {Vehicle ID}, {Vehicle ID, State},
{Vehice ID, State, Plate#}, or {State, Plate#}

In this example a superkey can be either {Vehicle ID}, {Vehicle ID, State},
{Vehice ID, State, Plate#}, or {State, Plate#}

4.7. CANDIDATE KEY


In the relational model, a candidate key of a relvar is a set of attributes of that
relvar such that

(1) at all times it holds in the relation assigned to that variable that there are no
two distinct tuples with the same values for these attributes and
DATABASE KEYS 15

(2) there is not a proper subset for which (1) holds.

Since a superkey is defined as a set of attributes for which (1) holds we can also
define candidate keys as minimal superkeys, i.e., superkeys of which no proper
subset is also a superkey.

The importance of candidate keys is that they tell us how we can identify
individual tuples in a relation. As such they are one of the most important types
of database constraint that should be specified when designing a database
schema. Since a relation is a set, it holds that every relation will have at least
one candidate key (because the entire heading is always a superkey). Since in
some RDBMSs tables may also represent multisets (which strictly means these
DBMSs are not relational), it is an important design rule to specify explicitly at
least one candidate key for each relation. For practical reasons RDBMSs usually
require that for each relation one of its candidate keys is declared as the primary
key, which means that it is considered as the preferred way to identify individual
tuples. Foreign keys, for example, are usually required to reference such a
primary key and not any of the other candidate keys.

In other words, a candidate key is a field or combination of fields that can act as
a primary key field for that table to uniquely identify each record in that table.

Example

The definition of candidate keys can be illustrated with the following (abstract)
example. Consider a relation variable (relvar) R with attributes (A, B, C, D) that
has only the following two legal values r1 and r2:

r1
r1
A B C D
A B C D
B C D
C D
D
a1 b1 c1 d1
a1 b1 c1 d1
b1 c1 d1
c1 d1
d1
a1 b2 c2 d1
a1 b2 c2 d1
b2 c2 d1
c2 d1
d1
a2 b1 c2 d1
a2 b1 c2 d1
b1 c2 d1
c2 d1
d1
DATABASE KEYS 16

r2
r2
A B C D
A B C D
B C D
C D
D
a1 b1 c1 d1
a1 b1 c1 d1
b1 c1 d1
c1 d1
d1
a1 b2 c2 d1
a1 b2 c2 d1
b2 c2 d1
c2 d1
d1
a1 b1 c2 d2
a1 b1 c2 d2
b1 c2 d2
c2 d2
d2

Here r2 differs from r1 only in the A and D values of the last tuple.

Here r2 differs from r1 only in the A and D values of the last tuple.

For r1 the following sets have the uniqueness property, i.e., there are no two
tuples in the instance with the same values for the attributes in the set:

{A,B}, {A,C}, {B,C}, {A,B,C}, {A,B,D}, {A,C,D}, {B,C,D}, {A,B,C,D}

For r2 the uniqueness property holds for the following sets;

{B,D}, {C,D}, {B,C}, {A,B,C}, {A,B,D}, {A,C,D}, {B,C,D}, {A,B,C,D}

Since superkeys of a relvar are those sets of attributes that have the uniqueness
property for all legal values of that relvar and because we assume that r1 and r2
are all the legal values that R can take, we can determine the set of superkeys
of R by taking the intersection of the two lists:

{B,C}, {A,B,C}, {A,B,D}, {A,C,D}, {B,C,D}, {A,B,C,D}

Finally we need to select those sets for which there is no proper subset in the
list, which are in this case:

{B,C}, {A,B,D}, {A,C,D}


DATABASE KEYS 17

These are indeed the candidate keys of relvar R.

We have to consider all the relations that might be assigned to a relvar to


determine whether a certain set of attributes is a candidate key. For example, if
we had considered only r1 then we would have concluded that {A,B} is a
candidate key, which is incorrect. However, we might be able to conclude from
such a relation that a certain set is not a candidate key, because that set does
not have the uniqueness property (example {A,D} for r1). Note that the
existence of a proper subset of a set that has the uniqueness property cannot in
general be used as evidence that the superset is not a candidate key. In
particular, note that in the case of an empty relation, every subset of the
heading has the uniqueness property, including the empty set.
DATABASE NORMALIZATION 18

5. DATABASE NORMALIZATION

Database normalization is a design technique by which relational database tables


are structured in such a way as to make them invulnerable to certain types of
logical inconsistencies and anomalies. Tables can be normalized to varying
degrees: relational database theory defines "normal forms" of successively
higher degrees of stringency, so, for example, a table in third normal form is
less open to logical inconsistencies and anomalies than a table that is only in
second normal form. Although the normal forms are often defined (informally) in
terms of the characteristics of tables, rigorous definitions of the normal forms
are concerned with the characteristics of mathematical constructs known as
relations. Whenever information is represented relationally—that is, roughly
speaking, as values within rows beneath fixed column headings—it makes sense
to ask to what extent the representation is normalized.

5.1. PROBLEMS ADDRESSED BY NORMALIZATION


A table that is not sufficiently normalized can suffer from logical inconsistencies
of various types, and from anomalies involving data operations. In such a table:

The same fact can be expressed on multiple records; therefore updates to the
table may result in logical inconsistencies. For example, each record in an
unnormalized "DVD Rentals" table might contain a DVD ID, Member ID, and
Member Address; thus a change of address for a particular member will
potentially need to be applied to multiple records. If the update is not carried
through successfully—if, that is, the member's address is updated on some
records but not others—then the table is left in an inconsistent state.
Specifically, the table provides conflicting answers to the question of what this
particular member's address is. This phenomenon is known as an update
anomaly.

There are circumstances in which certain facts cannot be recorded at all. In the
above example, if it is the case that Member Address is held only in the "DVD
Rentals" table, then we cannot record the address of a member who has not yet
rented any DVDs. This phenomenon is known as an insertion anomaly.

There are circumstances in which the deletion of data representing certain facts
necessitates the deletion of data representing completely different facts. For
example, suppose a table has the attributes Student ID, Course ID, and Lecturer
ID (a given student is enrolled in a given course, which is taught by a given
lecturer). If the number of students enrolled in the course temporarily drops to
zero, the last of the records referencing that course must be deleted—meaning,
as a side-effect, that the table no longer tells us which lecturer has been
assigned to teach the course. This phenomenon is known as a deletion anomaly.
DATABASE NORMALIZATION 19

Ideally, a relational database should be designed in such a way as to exclude the


possibility of update, insertion, and deletion anomalies. The normal forms of
relational database theory provide guidelines for deciding whether a particular
design will be vulnerable to such anomalies. It is possible to correct an
unnormalized design so as to make it adhere to the demands of the normal
forms: this is normalization. Normalization typically involves decomposing an
unnormalized table into two or more tables which, were they to be combined
(joined), would convey exactly the same information as the original table.

5.2. BACKGROUND TO NORMALIZATION: DEFINITIONS


Functional dependency:Attribute B has a functional dependency on attribute A if,
for each value of attribute A, there is exactly one value of attribute B. For
example, Member Address has a functional dependency on Member ID, because
a particular Member Address value corresponds to every Member ID value. An
attribute may be functionally dependent either on a single attribute or on a
combination of attributes. It is not possible to determine the extent to which a
design is normalized without understanding what functional dependencies apply
to the attributes within its tables; understanding this, in turn, requires
knowledge of the problem domain.

Trivial functional dependency: A trivial functional dependency is a functional


dependency of an attribute on a superset of itself. {Member ID, Member
Address} → {Member Address} is trivial, as is {Member Address} → {Member
Address}.

Full functional dependency: An attribute is fully functionally dependent on a set


of attributes X if it is a) functionally dependent on X, and b) not functionally
dependent on any proper subset of X. {Member Address} has a functional
dependency on {DVD ID, Member ID}, but not a full functional dependency, for
it is also dependent on {Member ID}.

Multivalued dependency: A multivalued dependency is a constraint according to


which the presence of certain rows in a table implies the presence of certain
other rows: see the Multivalued Dependency article for a rigorous definition.

Superkey: A superkey is an attribute or set of attributes that uniquely identifies


rows within a table; in other words, two distinct rows are always guaranteed to
have distinct superkeys. {DVD ID, Member ID, Member Address} would be a
superkey for the "DVD Rentals" table; {DVD ID, Member ID} would also be a
superkey.

Candidate key: A candidate key is a minimal superkey, that is, a superkey for
which we can say that no proper subset of it is also a superkey. {DVD ID,
Member ID} would be a candidate key for the "DVD Rentals" table.
DATABASE NORMALIZATION 20

Non-prime attribute: A non-prime attribute is an attribute that does not occur in


any candidate key. Member Address would be a non-prime attribute in the "DVD
Rentals" table.

Primary key: Most DBMSs require a table to be defined as having a single unique
key, rather than a number of possible unique keys. A primary key is a candidate
key which the database designer has designated for this purpose.

5.3. NORMAL FORMS


The normal forms (abbrev. NF) of relational database theory provide criteria for
determining a table's degree of vulnerability to logical inconsistencies and
anomalies. The higher the normal form applicable to a table, the less vulnerable
it is to such inconsistencies and anomalies. Each table has a "highest normal
form" (HNF): by definition, a table always meets the requirements of its HNF and
of all normal forms lower than its HNF; also by definition, a table fails to meet
the requirements of any normal form higher than its HNF.

The normal forms are applicable to individual tables; to say that an entire
database is in normal form n is to say that all of its tables are in normal form n.

Newcomers to database design sometimes suppose that normalization proceeds


in an iterative fashion, i.e. a 1NF design is first normalized to 2NF, then to 3NF,
and so on. This is not an accurate description of how normalization typically
works. A sensibly designed table is likely to be in 3NF on the first attempt;
furthermore, if it is 3NF, it is overwhelmingly likely to have an HNF of 5NF.
Achieving the "higher" normal forms (above 3NF) does not usually require an
extra expenditure of effort on the part of the designer, because 3NF tables
usually need no modification to meet the requirements of these higher normal
forms.

Edgar F. Codd originally defined the first three normal forms (1NF, 2NF, and
3NF). These normal forms have been summarized as requiring that all non-key
attributes be dependent on "the key, the whole key and nothing but the key".
The fourth and fifth normal forms (4NF and 5NF) deal specifically with the
representation of many-to-many and one-to-many relationships among
attributes. Sixth normal form (6NF) incorporates considerations relevant to
temporal databases.

5.4. FIRST NORMAL FORM


The criteria for first normal form (1NF) are:

1. A table must be guaranteed not to have any duplicate records;


therefore it must have at least one candidate key.

2. There must be no repeating groups, i.e. no attributes which occur a


different number of times on different records. For example,
DATABASE NORMALIZATION 21

suppose that an employee can have multiple skills: a possible


representation of employees' skills is {Employee ID, Skill1, Skill2,
Skill3 ...}, where {Employee ID} is the unique identifier for a
record. This representation would not be in 1NF.

Note that all relations are in 1NF. The question of whether a given
representation is in 1NF is equivalent to the question of whether it is a relation.

5.5. SECOND NORMAL FORM


The criteria for second normal form (2NF) are:

1. The table must be in 1NF.

2. None of the non-prime attributes of the table are functionally


dependent on a part (proper subset) of a candidate key; in other
words, all functional dependencies of non-prime attributes on
candidate keys are full functional dependencies. For example,
consider a "Department Members" table whose attributes are
Department ID, Employee ID, and Employee Date of Birth; and
suppose that an employee works in one or more departments. The
combination of Department ID and Employee ID uniquely identifies
records within the table. Given that Employee Date of Birth depends
on only one of those attributes – namely, Employee ID – the table
is not in 2NF.

Note that if none of a 1NF table's candidate keys are composite – i.e. every
candidate key consists of just one attribute – then we can say immediately that
the table is in 2NF.

5.6. THIRD NORMAL FORM


The criteria for third normal form (3NF) are:

1. The table must be in 2NF.

2. There are no non-trivial functional dependencies between non-


prime attributes. A violation of 3NF would mean that at least one
non-prime attribute is only indirectly dependent (transitively
dependent) on a candidate key, by virtue of being functionally
dependent on another non-prime attribute. For example, consider a
"Departments" table whose attributes are Department ID,
Department Name, Manager ID, and Manager Hire Date; and
suppose that each manager can manage one or more departments.
{Department ID} is a candidate key. Although Manager Hire Date is
functionally dependent on {Department ID}, it is also functionally
dependent on the non-prime attribute Manager ID. This means the
table is not in 3NF.
REFERENTIAL INTEGRITY 22

5.7. BOYCE-CODD NORMAL FORM


The criteria for Boyce-Codd normal form (BCNF) are:

1. The table must be in 3NF.

2. Every non-trivial functional dependency must be a dependency on a


superkey.

5.8. FOURTH NORMAL FORM


The criteria for fourth normal form (4NF) are:

1. The table must be in BCNF.

2. There must be no non-trivial multivalued dependencies on


something other than a superkey. A BCNF table is said to be in 4NF
if and only if all of its multivalued dependencies are functional
dependencies.

5.9. FIFTH NORMAL FORM


The criteria for fifth normal form (5NF and also PJ/NF) are:

1. The table must be in 4NF.

2. There must be no non-trivial join dependencies that do not follow


from the key constraints. A 4NF table is said to be in the 5NF if and
only if every join dependency in it is implied by the candidate keys.

5.10. DOMAIN/KEY NORMAL FORM


Domain/key normal form (or DKNF) requires that a table not be subject to any
constraints other than domain constraints and key constraints.

5.11. SIXTH NORMAL FORM


This normal form was, as of 2005, only recently proposed: the sixth normal form
(6NF) was only defined when extending the relational model to take into account
the temporal dimension. Unfortunately, most current SQL technologies as of
2005 do not take into account this work, and most temporal extensions to SQL
are not relational. See work by Date, Darwen and Lorentzos[3] for a relational
temporal extension, or see TSQL2 for a different approach.

6. REFERENTIAL INTEGRITY
RELANATIONAL DBMS 23

Referential integrity in a relational database is consistency between coupled


tables. Referential integrity is usually enforced by the combination of a primary
key or candidate key and a foreign key. For referential integrity to hold, any field
in a table that is declared a foreign key can contain only values from a parent
table's primary key or a candidate key. For instance, deleting a record that
contains a value referred to by a foreign key in another table would break
referential integrity. The relational database management system (RDBMS)
enforces referential integrity, normally either by deleting the foreign key rows as
well to maintain integrity, or by returning an error and not performing the
delete. Which method is used would be defined by the definition of the
referential integrity constraint.

Example

An employee database stores the department in which each employee works.


The field "DepartmentNumber" in the Employee table is declared a foreign key,
and it refers to the field "Index" in the Department table which is declared a
primary key. Referential integrity would be broken by deleting a department
from the Department table if employees listed in the Employee table are listed
as working for that the department.

7. RELANATIONAL DBMS
RELANATIONAL DBMS 24

A relational database management system (RDBMS) is a database management


system (DBMS) that is based on the relational model as introduced by Edgar F.
Codd. Relational databases are the most common kind of database in use today
(assuming one does not count a file system as a database).

E. F. Codd introduced the term in his seminal paper "A Relational Model of Data
for Large Shared Data Banks", published in 1970. In this paper and later papers
he defined what he meant by relational. One well-known definition of what
constitutes a relational database system is Codd's 12 rules. However, many of
the early implementations of the relational model did not conform to all of
Codd's rules, so the term gradually came to describe a broader class of database
systems. At a minimum, these systems:

1. presented the data to the user as relations (a presentation in


tabular form, i.e. as a collection of tables with each table consisting
of a set of rows and columns, can satisfy this property)

2. provided relational operators to manipulate the data in tabular form

The most popular definition of an RDBMS is a product that presents a view of


data as a collection of rows and columns not based strictly upon relational
theory. The majority of real world popular RDBMS products implement some of
Codd's 12 rules.

A second, theory-based school of thought argues that if a database does not


implement all of Codd's rules (or the current understanding on the relational
model, as expressed by Christopher J Date, Hugh Darwen and others), it is not
relational. This view, shared by many theorists and other strict adherents to
Codd's principles, would disqualify most DBMSs as not relational. For
clarification, they often refer to RDBMSs as Truly-Relational Database
Management Systems (TRDBMS), naming others Pseudo-Relational Database
Management Systems (PRDBMS).

Almost all DBMSes employ SQL as their query language. Alternative query
languages have been proposed and implemented, but very few have become
commercial products.
DISTRIBUTED DBMS 25

8. DISTRIBUTED DBMS
A distributed database management system is a software system that permits
the management of the distributed database and makes the distribution
transparent to the users. A distributed database is a collection of multiple,
logically interrelated databases distributed over a computer network. Sometimes
distributed database system is used to refer jointly to the distributed database
and the distributed DBMS.

Distributed database management systems can be architected as client-server


systems or peer-to-peer ones. In the former, one or more servers manage the
database and handle user queries that are passed on by the clients. The clients
usually have limited database functionality and normally pass the SQL queries
over to the servers for processing. In peer-to-peer systems, each site has equal
functionality for processing.

9. ACID
ACID 26

In databases, ACID stands for Atomicity, Consistency, Isolation, and Durability.


They are considered to be the key transaction processing features/properties of
a database management system, or DBMS. Without them, the integrity of the
database cannot be guaranteed. In practice, these properties are often relaxed
somewhat to provide better performance.

In the context of databases, a single logical operation on the data is called a


transaction. An example of a transaction is a transfer of funds from one account
to another, even though it might consist of multiple individual operations (such
as debiting one account and crediting another). The ACID properties guarantee
that such transactions are processed reliably.

9.1. ATOMICITY
refers to the ability of the DBMS to guarantee that either all of the tasks of a
transaction are performed or none of them is. The transfer of funds can be
completed or it can fail for a multitude of reasons, but atomicity guarantees that
one account won't be debited if the other is not credited as well.

9.2. CONSISTENCY
refers to the database being in a legal state when the transaction begins and
when it ends. This means that a transaction can't break the rules, or integrity
constraints, of the database. If an integrity constraint states that all accounts
must have a positive balance, then any transaction violating this rule will be
aborted.

9.3. ISOLATION
refers to the ability of the application to make operations in a transaction appear
isolated from all other operations. This means that no operation outside the
transaction can ever see the data in an intermediate state; a bank manager can
see the transferred funds on one account or the other, but never on both — even
if he ran his query while the transfer was still being processed. More formally,
isolation means the transaction history (or schedule) is serializable. For
performance reasons, this ability is the most often relaxed constraint. See the
isolation article for more details.

9.4. DURABILITY
refers to the guarantee that once the user has been notified of success, the
transaction will persist, and not be undone. This means it will survive system
failure, and that the database system has checked the integrity constraints and
won't need to abort the transaction. Typically, all transactions are written into a
log that can be played back to recreate the system to its state right before the
failure. A transaction can only be deemed committed after it is safely in the log.

9.5. IMPLEMENTATION
ACID 27

Implementing the ACID properties correctly is not simple. Processing a


transaction often requires a number of small changes to be made, including
updating indices that are used by the system to speed up searches. This
sequence of operations is subject to failure for a number of reasons; for
instance, the system may have no room left on its disk drives, or it may have
used up its allocated CPU time.

ACID suggests that the database be able to perform all of these operations at
once. In fact this is difficult to arrange. There are two popular families of
techniques: write ahead logging and shadow paging. In both cases, locks must
be acquired on all information that is updated, and depending on the
implementation, on all data that is being read. In write ahead logging, atomicity
is guaranteed by ensuring that information about all changes is written to a log
before it is written to the database. That allows the database to return to a
consistent state in the event of a crash. In shadowing, updates are applied to a
copy of the database, and the new copy is activated when the transaction
commits. The copy refers to unchanged parts of the old version of the database,
rather than being an entire duplicate.

Almost all databases used nothing but locking to ensure they were ACID until
recently. This means that a lock must be acquired anytime before processing
data in a database, even on read operations. Maintaining a large number of
locks, however, results in substantial overhead as well as hurting concurrency. If
user A is running a transaction that has read a row of data that user B wants to
modify, for example, user B must wait until user A's transaction is finished.

An alternative to locking is to maintain separate copies of any data that is


modified. This allows users to read data without acquiring any locks. Going back
to the example of user A and user B, when user A's transaction gets to data that
user B has modified, the database is able to retrieve the exact version of that
data that existed when user A started their transaction. This ensures that user A
gets a consistent view of the database even if other users are changing data that
user A needs to read.

It is difficult to guarantee ACID properties in a network environment. Network


connections might fail, or two users might want to use the same part of the
database at the same time.

Two-phase commit is typically applied in distributed transactions to ensure that


each participant in the transaction agrees on whether the transaction should be
committed or not.

Care must be taken when running transactions in parallel. Two phase locking is
typically applied to guarantee full isolation.
Introduction TO MICROSOFT SQL SERVER 28

10. INTRODUCTION TO MICROSOFT SQL SERVER

10.1. MICROSOFT SQL SERVER SETUP


Microsoft SQL Server is an application used to create computer databases for the
Microsoft Windows family of server operating systems. It provides an environment
used to generate databases that can be accessed from workstations, the web, or
other media such as a personal digital assistant (PDA).

On this site, we provide lessons and other topics on how to use Microsoft SQL Server
2005.

Before installing Microsoft SQL Server 2005, make sure you have installed all service
packs on your operating system. Also install IIS:

10.2. GETTING MICROSOFT SQL SERVER


In order to follow the lessons on this site, you must have access to a computer
in which Microsoft SQL Server is installed. To start, you must have a computer
that runs an appropriate operating system. If you are just learning, which is
assumed for all lessons on this site, you can use a Microsoft Windows XP
Professional (in this case, you must install Microsoft SQL Server 2000
Introduction TO MICROSOFT SQL SERVER 29

Developer Edition) or Windows Server 2003. I.

To make it possible for you and other students to study databases and SQL,
Microsoft made available a trial version of Microsoft SQL Server 2005. This full
version is available from the Microsoft web site. This is something beneficial
because most companies would not give such a long trial version of an
important software product: for 120 days, you should be able to learn as must
as possible and be able to move on, such as finding a job or starting a
business.

10.3. INSTALLATION STEPS


If you purchased Microsoft Visual Studio 2005 Professional, it may have come
with the SQL Server 2005 Developer Edition DVD. To install it, put the DVD in
the drive. A white window with links will come up. You can click the link under
x86 to install. If you are downloading, click the X86 Executable link. When a
dialog box comes up, click Save, locate a folder on your computer where to
save the file and click OK. The download will start. Depending on your
connection speed and your computer, this may take a while.

When the download is over, click Open

On the dialog box, click Next


Introduction TO MICROSOFT SQL SERVER 30

The InstallShield Wizard will start copying files to your hard drive. When it has
finished, click Finish. After the files have been copied, click Open.

If you have a DVD burner, from inside the SQLEVAL folder, create a that
contains the Servers folder, the Tools folder, and the autorun.inf file.

Practical Learning: Installing MS SQL Server

Start your computer or server if necessary.


If you are using the Microsoft SQL Server 2005 DVD, put it the drive.
If you had downloaded the Microsoft SQL Server 2005 Trial Software and
created a DVD of it, put it in
Introduction TO MICROSOFT SQL SERVER 31

Under the Install section, click Server Components

A message box with the End User License Agreement will be presented to you.
Read it. If you don't agree with the license, click Cancel. Otherwise, click the I
Accept check box and click Next:
Introduction TO MICROSOFT SQL SERVER 32

On the first page of the Welcome wizard, click Next

Read the Installing Prerequisites page of the wizard and click Install:

When this phase is over, click Next:

In the next dialog box, click Next


Introduction TO MICROSOFT SQL SERVER 33

The next page of the wizard will check the availability of the various options
that Microsoft SQL Server will need or will communicate with. When this phase
is over, click Next:

In the next page, change or accept the pieces of information: Name and
Introduction TO MICROSOFT SQL SERVER 34

Company. Click Next

Accept not to install any options or select the options you want and click Next

In the next page, accept the default instance or specify one. Then click Next
Introduction TO MICROSOFT SQL SERVER 35

The next step allows you to specify who is doing the installation. If you are an
administrator, specify the account's password. If you have an account that can
perform an installation on the server, enter its username, its password, and the
domain it belongs to:

Otherwise, you can click the Use The Built-In System Account radio button
Introduction TO MICROSOFT SQL SERVER 36

Click Next

On the next step, you can accept to exclusively use Windows Authentication
Mode with the first radio button. Otherwise, you can and should use this
opportunity to allow different persons to log in to this SQL Server. In this case,
you can select the second radio button and create a password for the SQL
Server Authentication, which would create and setup the sa account:

Click Next

Accept the options in the next page of the wizard and click Next
Introduction TO MICROSOFT SQL SERVER 37

To configuration the server, accept or click the Install The Default Configuration
radio button and click Next

The next page of the wizard allows you to specify whether to keep in touch
Microsoft. After making your selections, click Next
Introduction TO MICROSOFT SQL SERVER 38

Once Setup has enough information, on the last wizard page, click Install.
The wizard will start installing all the selected components

Once the components have been installed, click Next


Introduction TO MICROSOFT SQL SERVER 39

On the last page, click Finish

SQL Server Startup

To use SQL Server, it must start as a service. You have two options. You can
start it every time you want to use. You can also make it start whenever the
computer comes up from booting:
Introduction TO MICROSOFT SQL SERVER 40

Connecting to a Microsoft SQL Server

After installing Microsoft SQL Server, you can use. To perform an operation on
a database server, you must establish a connection to it. To do this, you have
to log on to the server. You have two options:

You can start Microsoft SQL Server Management Studio and a Connect To
Server dialog box would come

If you had clicked Cancel on the Connect To Server dialog box, to establish a
connection, in the Object Explorer, click Connect -> Database Engine...

If you had already established a connection to one server but you want to
create a connection to another server, in the Object Explorer, you can click
Connect -> Database Engine...

The Connect To Server dialog box allows you to identify yourself (or the
account you will use) to establish a connection with a server that is running
Microsoft SQL Server. You must provide the necessary credentials. You can uss
Windows Authentication. Here is an example:

Alternatively, you can use an existing SQL Server account. To do this, select
SQL Server Authentication from the Authentication combo box. Enter a
username in the Login combo box and enter a password in the corresponding
text box:
Introduction TO MICROSOFT SQL SERVER 41

Once you are ready, click Connect.


Introduction to Database Creation 42

11. INTRODUCTION TO DATABASE CREATION

Probably before using a database, you must first have one. A database is
primarily a group of computer files that each has a name and a location. Just as
there are different ways to connect to a server, in the same way, there are also
different ways to create a database.

To create a new database in SQL Server Management Studio, in the Object


Explorer, you can right-click the Databases node and click New Database... This
would open the New Database dialog box.

11.1. THE NAME OF A DATABASE


Probably the most important requirement of creating a database is to give it a
name. The SQL is very flexible when it comes to names. In fact, it is very less
restrictive than most other computer languages. Still, there are rules you must
follow when naming the objects in your databases:

A name can start with either a letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r,


s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V,
W, X, Y, or Z), a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), an underscore (_) or a non-
readable character. Examples are _n, act, %783, Second

After the first character (letter, digit, underscore, or symbol), the name can have
combinations of underscores, letters, digits, or symbols. Examples are _n24 or
act_52_t

A name can include spaces. Example are c0untries st@ts, govmnt (records), or
gl0b# $urvey||

Because of the flexibility of SQL, it can be difficult to maintain names in a


database. Based on this, there are conventions we will use for our objects. In fact,
we will adopt the rules used in C/C++, C#, Pascal, Java, and Visual Basic, etc. In
our databases:

Unless stated otherwise (we will mention the exception, for example with
variables, tables, etc), a name will start with either a letter (a, b, c, d, e, f, g, h, i,
j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N,
O, P, Q, R, S, T, U, V, W, X, Y, or Z) or an underscore

After the first character, we will use any combination of letters, digits, or
underscores

A name will not start with two underscores


Introduction to Database Creation 43

If the name is a combination of words, at least the second word will start in
uppercase. Examples are Countries Statistics, Global Survey, _RealSport,
FullName, or DriversLicenseNumber

After creating an object whose name includes space, whenever you use that
object, include its name between [ and ]. Examples are [Countries Statistics],
[Global Survey], or [Date of Birth]. Even if you had created an object with a name
that doesn't include space, when using that name, you can still include it in
square brackets. Examples are [UnitedStations], [FullName],
[DriversLicenseNumber], and [Country].

PRACTICAL LEARNING : STARTING THE MANAGEMENT STUDIO


To launch Microsoft SQL Server, click Start -> (All) Programs -> Microsoft SQL
Server 2005 -> SQL Server Management Studio

On the Connect to Server dialog box, make the correct selections and provide the
appropriate information, then click Connect

In the Object Explorer, expand the server name followed by the Databases node

Right-click Databases and click New Database...

In the Name text box, type MotorVehicleAdministration


Introduction to Database Creation 44

11.2. THE LOCATION OF A DATABASE


As you should be aware already from your experience on using computer, every
computer file must have a path. The path is where the file is located in one of the
drives of the computer. This allows the operating system to know where the file is so
that when you or another application calls it, the operating system would not be
confused.

By default, when you create a new database, Microsoft SQL Server assumed that it
would be located at Drive:C:\Program Files\Microsoft SQL
Server\MSSQL.2\MSSQL\Data folder. If you use the New Database dialog box of the
SQL Server Management Studio, if you specify the name of the database and click
OK, the interpreter automatically creates a new file, and appends the .MDF extension
to the file: this is the (main) primary data file of your database.

PRACTICAL LEARNING : C HECKING THE LOCATION OF THE DATA FILE


Under the Location header, click the ellipsis button to open the Locate Folder dialog
Introduction to Database Creation 45

box

Notice the file name and the location. Scroll up to the name (or letter) of the drive to
see where the path starts

Click Cancel

11.3. THE PRIMARY SIZE OF A DATABASE


When originally creating a database, you may or may not know how many lists, files,
or objects the project would have. Still, as a user of computer memory, the database
must use a certain portion, at least in the beginning. The amount of space that a
database is using is referred to as its size. If you use the New Database dialog box,
after specifying the name of the database and click OK, the interpreter automatically
specifies that the database would primarily use 2MB. This is enough for a starting
database. Of course, you can either change this default or later on you can increase it
when necessary.

If you want to specify a size different from the default, if you are using the New
Database to create your database, in the Database Files section and under the Initial
Size column, change the size as you wish.

PRACTICAL LEARNING : SETTING THE DATABASE FILE SIZE


In the Database Files section, click the box under the Initial Size column header, click
the up arrow of the spin button and increase its value to 5

Click OK
Introduction to Database Creation 46
Database Creation With Code 47

12. DATABASE CREATION WITH CODE

The command used to create a database in SQL uses the following formula:

CREATE DATABASE DatabaseName

The CREATE DATABASE (remember that SQL is not case-sensitive) expression is


required. The DatabaseName factor is the name that the new database will carry.
Although SQL is not case-sensitive, you should make it a habit to be aware of the
cases you use to name your objects. Every statement in SQL can be terminated
with a semi-colon. Although this is a requirement in many implementations of SQL,
in Microsoft SQL Server, you can omit the semi-colon. Otherwise, the above
formula would be

CREATE DATABASE DatabaseName;

Here is an example:

CREATE DATABASE GeneralCensus;

To assist you with writing code, tin the previous lessons, we saw that you could
use the query window.

PRACTICAL LEARNING : CREATING A DATABASE USING THE SQL QUERY ANALYZER


To open the code editor, in the Object Explorer, right-click the name of the server
and click New Query

In the empty window, type:

CREATE DATABASE RealEstate1;

GO

To execute the statement, press F5


Database Creation With Code 48

Using a Wizard

To specify more options with code, Microsoft SQL Server ships with various sample
codes you can use for different assignments. For example, you can use sample
code to create a database. The sample codes that Microsoft SQL Server are
accessible from the Template Explorer.

To access the Template Explorer, on the main menu, you can click View ->
Template Explorer. Before creating a database, open a new query window. Then:

To create a new database using sample code, in the Template Explorer, expand
the Databases node, then drag the Create Database node and drop it in the query
window. The new database would be created in the server that holds the current
connection

If you have access to more than one server, to create a database in another server
or using a different connection, in the Template Explorer, expand the Databases
node, right-click Create Database and click Open. In the Connect to Database
Engine dialog box, select the appropriate options, and can click OK

With any of these actions, Microsoft SQL Server would generate sample code for
you:

-- =============================================

-- Create database template

-- =============================================

USE master

GO
Database Creation With Code 49

-- Drop the database if it already exists

IF EXISTS (

SELECT name

FROM sys.databases

WHERE name = N'<Database_Name, sysname, Database_Name>'

DROP DATABASE <Database_Name, sysname, Database_Name>

GO

CREATE DATABASE <Database_Name, sysname, Database_Name>

GO

You would then need to edit the code and execute it to create the database. From
the previous lessons and sections, we have reviewed some characters uch as the
comments -- and some words or expressions such as GO, CREATE DATABASE, and
SELECT. We will study the other words or expressions in future lessons and
sections.

12.1. DATABASE MAINTENANCE

If you have created a database but don't need it anymore, you can delete it. It is
important to know, regardless of how you create a database, whether using SQL
Server Management Studio, code in the query window, or the Command Prompt,
every database can be accessed by any of these tools and you can delete any of
the databases using any of these tools.

As done with creating a database, every tool provides its own means.

SQL Server Management Studio

To delete a database in SQL Server Management Studio, in the Object Explorer,


expand the Databases node, right-click the undesired database, and click Delete. A
dialog box would prompt you to confirm your intention. If you still want to delete
the database, you can click OK. If you change your mind, you can click Cancel.

Practical Learning: Deleting a Database in the SQL Server Management Studio

In the Object Explorer, right-click MotorVehicleAdministration and click Delete


Database Creation With Code 50

In the Delete Object dialog box, click OK

Deleting a Database Using SQL

To delete a database in SQL Query Analyzer, you use the DROP DATABASE
expression followed by the name of the database. The formula used is:

DROP DATABASE DatabaseName;

Before deleting a database in SQL, you must make sure the database is not being
used or accessed by some one else or by another object.

PRACTICAL LEARNING : DELETING A DATABASE WITH CODE


On the Standard toolbar, click the New Query button

To delete a database, type:

DROP DATABASE RealEstate1;

GO

Press F5 to execute the statement

Database Routines
Database Creation With Code 51

12.2. THE CURRENT DATABASE


While writing code in a Query Window, you should always know what database you
are working on, otherwise you may add code to the wrong database. To
programmatically specify the current database, type the USE keyword followed by
the name of the database. The formula to use is:

USE DatabaseName;

Here is an example:

USE GovernmentStatistics;

12.3. REFRESHING THE LIST OF DATABASES


Some of the windows that display databases, like the SQL Server Management
Studio, don't update their list immediately if an operation occurred outside their
confinement. For example, if you create a database in the query windows, its
name would not be updated in the Object Explorer. To view such external changes,
you can refresh the window that holds the list.

In SQL Server Management Studio, to update a list, you can right-click its category
in the Object Explorer and click Refresh. Only that category may be refreshed. For
example, to refresh the list of databases, in the Object Explorer, you can right-
click the Databases node and click Refresh.

12.4. MICROSOFT SQL SERVER PRIMARY SETTINGS


When you install Microsoft SQL Server, it also installs 4 databases named master,
model, temp, msdb, and tempdb. These databases will be for internal use. This
means that you should avoid directly using them, unless you know exactly what
you are doing.

THE SYSTEM DATABASES


dOne of the databases installed with Microsoft SQL Server is named master. This
database holds all the information about the server on which your MS SQL Server
is installed. For example, we saw earlier that, to perform any operation on the
server, you must login. The master database identifies any person, called a user,
who accesses the database, about when and how.

Besides identifying who accesses the system, the master database also keeps
track of everything you do on the server, including creating and managing
databases.

You should not play with the master database; otherwise you may corrupt the
Database Creation With Code 52

system. For example, if the master database is not functionning right, the system
would not work.

A N AMESPACE
A namespace is a technique of creating a series of items that each has a unique
name. For example, if you start creating many databases, there is a possibility
that you may risk having various databases with the same name. If using a
namspace, you can isolate the databases in various namespaces. In reality, to
manage many other aspects dof your database server, you use namespaces and
you put objects, other than databases, within those namespaces. Therefore, a
namespace and its content can be illustrated as follows:

Notice that there are various types of objects within a namespace.

THE SCHEMA OF A DATABASE


Within a namespace, you can create objects as you wish. To further control and
manage the objects inside of a namespace, you can put them in sub-groups called
schemas. Therefore, a schema is a group of objects within a namespace. This also
means that, within a namespace, you can have as many schemas as you want:
Database Creation With Code 53

To manage the schemas in a namespace, you need a way to identify each schema.
Based on this, each schema must have a name. In our illustration, one schema is
named Schema1. Another schema is named Schema2. Yet another schema is
named Schema_n.

Inside of a schema, two objects cannot have the same name, but an object in one
schema can have the same name as an object in another schema. Based on this, if
you are accessing an object within its schema, you can simply use its name, since
that name would be unique. On the other hand, because of the implied possibility
of dealing with objects with similar names in your server, when accessing an
object outside of its schema, you must qualify it. To do this, you would type the
name of the schema that contains the object you want to use, followed by the
period operator, followed by the name of the object you want to use. From our
illustration, to access the Something1 object that belongs to Schema1, you would
type:

Schema1.Something1

There are two types of schemas you can use, those built-in and those you create.
When Microsoft SQL Server is installed, it also creates a few schemas. One of the
schemas is called sys.

The sys schema contains a list of some of the objects that exist in your system.
One of these objects is called databases (actually, it's a view). When you create a
database, its name is entered in the databases object using the same name you
gave it.

THE DATABASE OWNER


Database Creation With Code 54

In the previous lesson, we saw that, before using a database, you must establish a
connection with the server. You do this using a user account that can use the
server. As we saw earlier, once the connection exists, you can create a database.
In Microsoft SQL Server, the user who creates a database is referred to as the
database owner. To identify this user, when Microsoft SQL Server is installed, it
also creates a special user account named dbo. This account is automatically
granted various permissions on the databases of the server.

Because the dbo account has default access to all databases, to refer to an object
of a database, you can qualify it by typing dbo, followed by the period operator,
followed by the name of the object.

12.5. THE MICROSOFT SQL SERVER MANAGEMENT STUDIO


INTRODUCTIOND
There are many tools you will use in Microsoft SQL server. One of them is called
Microsoft SQL Server Management Studio. To access it, you can click Start -> (All)
Programs -> Microsoft SQL Server 2005 -> SQL Server Management Studio. A
dialog box would come up but you can click Cancel on it:

The top section of the SQL Server Management Studio displays the classic title bar
of a regular window, with an icon on the left, followed by the title of the application,
and the system buttons on the right side. Under the title bar, the menu bar displays
categories of menus that you will use to perform the various necessary operations. A
Database Creation With Code 55

toolbar displays under the main menu.

The left side of the interface displays the Object Explorer window, with its title bar
labeled Object Explorer. On the right side of the Object Explorer title, there are three
buttons. If you click the first button that points down, a menu would appear, which
allows you to specify whether you want the window to be floated, docked, or tabbed.

This window is dockable, meaning you can move it from the left side to another side
on the interface. To do this, you can click and drag its title bar to a location of your
choice. The window is also floatable, which means you can place it somewhere in the
middle of the interface:

To place the window back to its previous position, you can double-click its title bar.
The window can also be tabbed. This means that the window can be positioned
either vertically or horizontally.

The right side of the window is equipped by a window whose tab is labeled
Summary. This area will be used to display either the contents of what is selected in
the Object Explorer, or to show a result of some operation. As you will see later on,
many other windows will occupy the right section but they will share the same area.
To make each known it will be represented with a tab and the tab shows the name
(or caption) of a window.

12.6. USING THE MANAGEMENT STUDIO


After installing Microsoft SQL Server, you can use it to create an manage databases.
To assist you with this, you can use Microsoft SQL Server Management Studio.
Database Creation With Code 56

Microsoft SQL Server Management Studio is a window you will use to create and
manage databases. To assist you with this, it comes equipped with various tools,
some are graphically-based and some others required code writing for you. To
perform some operations, you will use the main menu. Some other operations will
be available from clicking one of the buttons on the toolbar. Many other operations
will start from the Object Explorer.

The Object Explorer displays a list of items as a tree-style. One of the most regular
used items will be the name of the server you are using. If you are just starting to
learn database development or you are a junior database developper, you may use
or see only one server. In some cases, you may be dealing with many. Regardless,
you should alway know what server you are currently connecting to. This is easy to
check with the first node of the Object Explorer. In the following example, the
server is named Central:

The name of the server is followed by parentheses.

In the previous section, we saw that, to establish a connection to a server, you must
authenticate yourself. In some cases you may use the same account over and over
again. In some other cases you may have different accounts that you use for
different scenatios, such as one account for database develpoment, one account for
database management, and/or one account for database testing. Some operations
cannot be performed by some accounts. When performing some operations, you
should always know what account you are using. You can check this in the
parentheses of the server name. In the following connection, an account called
Administrator is currently connecting to a server named Central:
Database Creation With Code 57

When the server name is selected in the Object Explorer, the whole caption of its
node displays in the Summary window.

Probably the most regular node you will be interested in, is labeled Databases. This
hold the names of databases on the server you are connecting. Also, from that node,
you can perform almost any necessary operation of a database. To see most of the
regularly available actions, you can expand the Databases node and some of this
children. You can then right-click either Databases or one of its child nodes.

12.7. TO CODE
Although you will perform many of your database operations visually, some other
operations will require that you write code. To assist with with this, Microsoft SQL
Server provides a code editor and various code templates.

To open the editor, on the Standard toolbar, you can:

Press Ctrl + N

On the main menu, you can click File -> New -> Query With Current Connection

On the Standard toolbar, click the New Query button

In the Object Explorer, right-click the name of the server and click New Query

This would create a new window and position it on the right side of the interface.
Whether you have already written code or not, you can save the document of the
code editor at any time. To save it:
Database Creation With Code 58

You can press Ctrl + S

On the main menu, you can click File -> Save SQLQueryX.sql...

On the Standard toolbar, you can click the Save button

You will be required to provide a name for the file. After saving the file, its name
would appear on the tab of the document.

to the Command Prompt

Besides the SQL Server Management Studio, you can also work on Microsoft SQL
Server from the DOS command prompt. This is done using an application or
command named SQLCMD.EXE. To use, open the Command Prompt, type SQLCMD
(case-insensitive) and press Enter.

12.8. THE STRUCTURED QUERY LANGUAGE

After establishing a connection, you can take actions, such as creating a database
and/or manipulating data.

The Structured Query Language, known as SQL, is a universal language used on


various computer systems to create and manage databases.

SQL can be pronounced Sequel or S. Q. L. In our lessons, we will


consider the Sequel pronunciation. For this reason, the abbreviation will
always be considered as a word, which would result in ―A SQL statement‖
instead of "An SQL statement". Also, we will regularly write, ―The SQL‖
instead of ―The SQL language, as the L already represents Language.

Like other non-platform specific languages such as C/C++, Pascal, or Java, the SQL
you learn can be applied to various database systems. To adapt the SQL to Microsoft
SQL Server, the company developed Transact-SQL as Microsoft's implementation of
SQL. Transact-SQL is the language used internally by Microsoft SQL Server and
MSDE. Although SQL Server highly adheres to the SQL standards, it has some
internal details that may not be applied to other database systems like MySQL,
Oracle, or even Microsoft Access, etc; although they too fairly conform to the
standard.

The SQL we will learn and use here is Transact-SQL. In other words, we will assume
that you are using Microsoft SQL Server as your platform for learning about
databases. This means that, unless specified otherwise, most of the time, on this
site, the word SQL refers to Transact-SQL or the way the language is implemented
in Microsoft SQL Server.
Database Creation With Code 59

12.9. THE SQL INTERPRETER


As a computer language, the SQL is used to give instructions to an internal program
called an interpreter. As we will learn in various sections, you must make sure you
give precise instructions. SQL is not case-sensitive. This means that CREATE, create,
and Create mean the same thing. It is a tradition to write SQL's own words in
uppercase. This helps to distinguish SQL instructions with the words you use for
your database.

As we will learn in this and the other remaining lessons of this site, you use SQL by
writing statements. To help you with this, Microsoft SQL Server provides a window,
also referred to as the Query Window, that you can use to write your SQL code. To
access it, on the left side of the window, you can right-click the name of the server
and click New Query. In the same way, you can open as many instances as the New
Query as you want.

When the Query window comes up, it display a blank child window in which you can
write your code. The code you write is a document and it can be saved as a file. The
file would have the extension .sql. Everytime you open a new query, it is
represented with a tab. To switch from one code part to another, you can click its
tab. To dismiss an instance of the query, first access it (by clicking its tab), then, on
the right side, click the close button . If you had written code in the query window,
when you close it, you would be asked to save your code. If you want to preserve
your code, then save it. If you had already executed the code in the window (we will
learn how to write and execute SQL code), you don't have to save the contents of
the window.

12.10. EXECUTING A STATEMENT


In the next sections and lessons, we will learn various techniques of creating SQL
statements with code. By default, when a new query window appears, it is made of
a wide white area where you write your statements:
Database Creation With Code 60

After writing a statement, you can execute it, either to make it active or simply to
test it. To execute a statement:

You can press F5

On the main menu, you can click Query -> Execute

On the SQL Editor toolbar, you can click the Execute button

You can right-click somewhere in the code editor and click Execute

When you execute code, code editor becomes divided into two horizontal sections:
Database Creation With Code 61

Also, when you execute code, the interpreter would first analyze it. If there is an
error, it would display one or more red lines of text in its bottom section. Here is an
example:

If there is no error in the code, what happens when you execute a statement
depends on the code and the type of statement.
Database Creation With Code 62

12.11. ACCESSORIES FOR SQL CODE WRITING

12.12. COMMENTS
A comment is text that the SQL interpreter would not consider as code. As such, a
comment is written any way you like. What ever it is made of would not be read.
Transact-SQL supports two types of comments. The style of comment that starts
with /* and ends with */ can be used. To apply it, start a line with /*, include any
kind of text you like, on as many lines as you want. To close the commented
section, type */. Here is an example of a line of comment:

/* First find out if the database we want to create exists already */

A comment can also be spread on more than one line, like a paragraph. Here is an
example:

/* First find out if the MotorVehicleDivision database we

want to create exists already.

If that database exists, we don't want it anymore. So,

delete it from the system. */

Transact-SQL also supports the double-dash comment. This comment applies to only
one line of text. To use it, start the line with --. Anything on the right side of -- is
part of a comment and would not be considered as code. Here is an example:

-- =============================================

-- Database: MotorVehicleDivision

-- =============================================

/* First find out if the MotorVehicleDivision database we

want to create exists already.

If that database exists, we don't want it anymore. So,

delete it from the system. */

-- Now that the database is not in the system, create it


Database Creation With Code 63

12.13. THE END OF A STATEMENT


In SQL, after writing a statement, you can end it with a semi-colon. In fact, if you
plan to use many statements in one block, you should end each with a semi-colon.
When many statements are used, some of them must come after others.

Time to GO

To separate statements, that is, to indicate when a statement ends, you can use the
GO keyword (in reality and based on SQL standards, it is the semi-colon that would
be required, but the Microsoft SQL Server interpreter accepts GO as the end of a
statement).

12.14. SQL OPERATORS AND OPERANDS

An operation is an action performed on one or more values either to modify the


value held by one or both of the variables or to produce a new value by combining
values. Therefore, an operation is performed by using at least one symbol and one
value. The symbol used in an operation is called an operator. A value involved in an
operation is called an operand.

PRINT Something

Like every language, SQL ships with some words used to carry its various
operations. One of these words is PRINT. To display something in plain text as a
result of a statement, type PRINT followed by what to display. Therefore, PRINT
uses the following formula:

PRINT WhatToPrint

The item to display can be anything that is allowed and it is provided on the right
side of PRINT. If it is a regular constant number, simply type it on the right side of
PRINT. Here is an example:
Database Creation With Code 64

The item to display can also be an operation or the result of an operation. If you
want to display a character, a word, or a sentence, include it between single-quotes.
If you want to include a single-quote in your statement, double it; that is, write it
twice. Here is an example:

You can also display an expression as a combination of number(s) and sentences as


we will learn later.
Database Creation With Code 65

SELECT Anything

The SELECT operator can be used, among other things, to display a value. The
SELECT keyword uses the following syntax:

SELECT What

Based on this, to use it, where it is needed, type SELECT followed by a number, a
word, a string, or an expression. The item to display follows some of the same rules
as PRINT. One of the differences between PRINT and SELECT is that:

PRINT is mostly used for testing a simple value, a string, or an expression.


Therefore, it displays its results in a regular white window under a tab labeled
Messages. PRINT can be used with only one value

SELECT is the most regularly used SQL operator. We will see that it is used to
retrieve records from a table. For this reason, SELECT displays its results in an
organized window made of categeories called columns, under a tab labeled Results.
SELECT can be used with more than one value

As done for PRINT, to display a sentence using SELECT, type it in single-quotes on


the right side of this operator. Here is an executed example:

As mentioned already, unlike PRINT, SELECT can be used to display more than one
value. The values must be separated by commas. Here is an example:

SELECT 'Hourly Salary', 24.85

This would produce:


Database Creation With Code 66

SELECT This AS That

In the above introductions, we used either PRINT or SELECT to display something in


the query window. One of the characteristics of SELECT is that it can segment its
result in different sections. SELECT represents each value in a section called a
column. Each column is represented with a name also called a caption. By default,
the caption displays as "(No column name)". If you want to use your own caption,
on the right side of an expression, type the AS keyword followed by the desired
caption. The item on the right side of the AS keyword must be considered as one
word. Here is an example:

SELECT 24.85 AS HourlySalary;

This would produce:


Database Creation With Code 67

You can also include the item on the right side of AS in single-quotes. Here is an
example:

SELECT 24.85 AS 'HourlySalary';

If the item on the right side of AS is in different words, you should include it in
single-quotes or put them in inside of an opening square bracket "[" and a closing
square bracket "]". Here is an example:

SELECT 24.85 AS 'Hourly Salary';

If you create different sections, separated by a comma, you can follow each with AS
and a caption. Here is an example:

SELECT 'James Knight' As FullName, 20.48 AS Salary;

This would produce:


Database Creation With Code 68

The above statement could also be written as follows:

SELECT 'James Knight' As [Full Name], 20.48 AS [Hourly Salary];

Unary Operators

The Positive Operator +

A unary operator is an operator that performs its operation on only one operand.

Algebra uses a type of ruler to classify numbers. This ruler has a middle position of
zero. The numbers on the left side of the 0 are referred to as negative while the
numbers on the right side of the rulers are considered positive:

-∞ -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 +∞

-∞ -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 +∞

A value on the right side of 0 is considered positive. To express that a number is


positive, you can write a + sign on its left. Examples are +4, +228, +90335. In this
case the + symbol is called a unary operator because it acts on only one operand.

The positive unary operator, when used, must be positioned on the left side of its
operand, never on the right side.

As a mathematical convention, when a value is positive, you do not need to express


Database Creation With Code 69

it with the + operator. Just writing the number without any symbol signifies that the
number is positive. Therefore, the numbers +4, +228, and +90335 can be, and are
better, expressed as 4, 228, 90335. Because the value does not display a sign, it is
referred as unsigned as we learned in the previous lesson.

To express a variable as positive or unsigned, you can just type it. here is an
example:

PRINT +1250

The Negative Operator -

As you can see on the above ruler, in order to express any number on the left side
of 0, it must be appended with a sign, namely the - symbol. Examples are -12, -
448, -32706. A value accompanied by - is referred to as negative.

The - sign must be typed on the left side of the number it is used to negate.

Remember that if a number does not have a sign, it is considered positive.


Therefore, whenever a number is negative, it MUST have a - sign. In the same way,
if you want to change a value from positive to negative, you can just add a - sign to
its left.

Here is an example that uses two variables. One has a positive value while the other
has a negative value:

SELECT -1250

12.15. BINARY OPERATORS

THE ADDITION
An operator is referred to as binary if it operates on two operands.

The addition, also called the sum, is an operation used to add one item to another.
The addition is performed using the + sign. To get the addition of two values, you
type + between them, as in Value1 to Value2. After the addition has been
performed, you get a new value that you can make available or display to the user.
You can perform the addition on two numbers. Here is an example:

dPRINT 125 + 4088

In Transact-SQL, you can also perform the addition on text. Here is an example:

PRINT 'Henry ' + 'Kono'


Database Creation With Code 70

You can also add more than two values, like a + b + c. The order you use to add
two or more values doesn't matter. This means Value1 + Value2 is the same as
Value2 + Value1. In the same way a + b + c is the same as a + c + b the same as b
+ a + c and the same as c + b + a.

THE SUBTRACTION
The subtraction operation, sometimes called the difference, is used to take out or
subtract one value from another value. It is essentially the opposite of the addition.
The subtraction is performed with the - sign. Here is an example:

PRINT 1240 - 608

Unlike the addition, the subtraction operation is not associative. This means that a -
b - c is not necessarily equal to c - b - a. This is illustrated in the following
statements:

PRINT 128 - 42 - 5

PRINT 5 - 42 - 128

This would produce:

81

-165

Notice that both operations of the addition convey the same result. In the
subtraction section, the numbers follow the same order but a different operation;
and the last two operations render different results.

THE MULTIPLICATION
The multiplication allows adding one value to itself a certain number of times, set by
a second value. As an example, instead of adding a value to itself in this manner: a
+ a + a + a, since the variable a is repeated over and over again, you could simply
find out how many times a is added to itself, then multiply a by that number which,
is this case, is 4. This would mean adding a to itself 4 times, and you would get the
same result.

The multiplication is performed with the * sign. Just like the addition, the
multiplication is associative: a * b * c = c * b * a. Here is an example:

PRINT 128 * 42

This would produce 5376

The Division

The division operation is similar to cutting an item in pieces or fractions of a set


value. Therefore, the division is used to get the fraction of one number in terms of
another. The division is performed with the forward slash /. Here is an example:
Database Creation With Code 71

PRINT 128 / 42

This would produce 3

When performing the division, be aware of its many rules. Never divide by zero (0).
Make sure that you know the relationship(s) between the numbers involved in the
operation.

THE MODULO
In the above division, 128/42, the result is 3. When you multiply 42 by 3, as in
42*3, you get 126. In some cases, you may be interested in knowing the amount
that was left out after the operation. The modulo operation is used to get the
remainder of a division as a natural number. The remainder operation is performed
with the percent sign (%). Here is an example:

PRINT 128 % 42

This would produce 2.

Parentheses

Like most computer languages, Transact-SQL uses parentheses to isolate a group of


items that must be considered as belonging to one entity. For example, as we will
learn soon, parentheses allow a function to delimit the list of its arguments.
Parentheses can also be used to isolate an operation or an expression with regards
to another operation or expression. For example, when studying the algebraic
operations, we saw that the subtraction is not associative and can lead to
unpredictable results. In the same way, if your operation involves various operators
such as a mix of addition(s) and subtraction(s), you can use parentheses to specify
how to proceed with the operations, that is, what operation should (must) be
performed first. Here is an example:

PRINT (154 - 12) + 8

PRINT 154 - (12 + 8)

This would produce:

150

134

As you can see, using the parentheses controls how the whole operation would
proceed. This difference can be even more accentuated if your operation includes 3
or more operators and 4 or more operands.

12.16. BIT MANIPULATIONS


Database Creation With Code 72

When you use a value in your database or application, the value must be stored
somewhere in the computer memory using a certain amount of space. A value
occupies space that resembles a group of small boxes. In our human understanding,
it is not always easy to figure out how a letter such as as B is stored in 7 seven
small boxes when we know that B is only one letter.

Bit manipulation or a bit related operation allows you to control how values are
stored in bits. This is not an operation you will need to perform very often, especially
not in the early stages of your database. Nevertheless, bit operations (and related
overloaded operators) are present in all or most programming environments, so
much that you should be aware of what they do or what they offer.

Bits Operators: The Bitwise NOT Operator ~

One of the operations you can perform on a bit consists of reversing its value. That
is, if a bit holds a value of 1, you may want to change it to 0 and vice-versa. This
operation can be taken care of by the bitwise NOT operator that is represented with
the tilde symbol ~

The bitwise NOT is a unary operator that must be placed on the left side of its
operand as in

~Value

Here is an example:

PRINT ~158

To perform this operation, the Transact-SQL interpreter considers each bit that is
part of the operand and inverts the value of each bit from 1 to 0 or from 0 to 1
depending on the value the bit is holding. This operation can be resumed in the
following table:

Bit ~Bit

1 0

0 1

Consider a number with a byte value such as 248. In our study of numeric systems,
we define how to convert numbers from one system to another. Based on this, the
binary value of decimal 248 is 1111 1000 (and its hexadecimal value is 0xF8). If you
apply the bitwise NOT operator on it to reverse the values of its bits, you would get
the following result:

Value 1 1 1 1 1 0 0 0

~Value 0 0 0 0 0 1 1 1
Database Creation With Code 73

12.17. BITS COMPARISON: THE BITWISE AND OPERATOR &


The bitwise & is a binary operator that uses the following syntax

OPERAND1 & OPERAND2


This operator considers two values and compares the bit of each with the
corresponding bit of the other value. If both corresponding bits are 1, the
comparison produces 1. Otherwise, that is, if either bit is 0, the comparison
produces 0. This comparison is resumed as follows:

Bit1 Bit2 Bit1 & Bit2

0 0 0

1 0 0

0 1 0

1 1 1

Imagine you have two byte values represented as 187 and 242. Based on our study
of numeric systems, the binary value of decimal 187 is 1011 1011 (and its
hexadecimal value is 0xBB). The binary value of decimal 242 is 1111 0010 (and its
hexadecimal value is 0xF2). Let’s compare these two values bit by bit, using the
bitwise AND operator:

Binary Decimal

N1 1 0 1 1 1 0 1 1 187

N2 1 1 1 1 0 0 1 0 242

N1 & N2 1 0 1 1 0 0 1 0 178

Most of the times, you will want the interpreter to perform this operation and use
the result in your program. This means that you can get the result of this operation
and possibly display it to the user. The above operation can be performed by the
following program:

PRINT 187 & 242

This would produce 178

BITS COMPARISON : THE BITWISE OR OPERATOR |


You can perform another type of comparison on bits using the bitwise OR operator
that is represented by |. Its syntax is:
Database Creation With Code 74

Value1 | Value2

Once again, the interpreter compares the corresponding bits of each operand. If at
least one of the equivalent bits is 1, the comparison produces 1. The comparison
produces 0 only if both bits are 0. This operation is resumed as follows:

Bit1 Bit2 Bit1 | Bit2

0 0 0

1 0 1

0 1 1

1 1 1

Once again, let’s consider decimals 187 and 242. Their bitwise OR comparison would
render the following result:

Binary Decimal

N1 1 0 1 1 1 0 1 1 187

N2 1 1 1 1 0 0 1 0 242

N1 | N2 1 1 1 1 1 0 1 1 251

You can also let the compiler perform the operation and produce a result. Here is an
example:

PRINT 187 | 242

This would produce 251

BITS COMPARISON : THE BITWISE-EXCLUSIVE XOR OPERATOR ^


Like the previous two operators, the bitwise-exclusive OR operator performs a bit
comparison of two values. It syntax is:

Value1 ^ Value2

The compiler compares the bit of one value to the corresponding bit of the other
value. If one of the bits is 0 and the other is 1, the comparison produces 1. In the
other two cases, that is, if both bits have the same value, the comparison produces
0. This operation is resumed as follows:

Bit1 Bit2 Bit1 ^ Bit2

0 0 0
Database Creation With Code 75

1 0 1

0 1 1

1 1 0

We will again consider decimals 187 and 242. Their bitwise-exclusive XOR
comparison would render the following result:

Binary Decimal

N1 1 0 1 1 1 0 1 1 187

N2 1 1 1 1 0 0 1 0 242

N1 ^ N2 0 1 0 0 1 0 0 1 73

If the interpreter performs this operation, it can produce a result as in the following
example:

PRINT 187 ^ 242;

This would produce 73.

12.18. V
ARIA
BLES
FUN
DA
MEN
TALS

In the previous lesson, we used some values such as 242 or 'James Knight'. These
types of values are referred to as constant because we certainly know them before
their and we don't change them in our statements. If you intend to use a certain
category of value over and over again, you can reserve a section of memory for
that value. This allows you to put the value in an area of memory, easily change
the value for another, over and over. For example, you can store the names of
Database Creation With Code 76

employees
one after
another in
the same
area of
memory as
needed.

To use the
same area
of memory
to store
and
remove
values as
needed,
the SQL
interpreter
needs two
primary
pieces of
informatio
n: a name
and the
desired
amount of
space in
memory
capable of
storing the
value.

PRACTICAL LEARNING : STARTING THE MANAGEMENT STUDIO


To launch Microsoft SQL Server, click Start -> (All) Programs -> Microsoft SQL Server
2005 -> SQL Server Management Studio

On the Connect to Server dialog box, make the correct selections and provide the
appropriate information, then click Connect

To open the code editor, on the main menu, click File -> New -> Query With Current
Connection
Database Creation With Code 77

Declaring Variables

A variable is an area of memory used to store values that can be used in a program.
Before using a variable, you must inform the interpreter. This is also referred to as
declaring a variable. To declare a variable, use the DECLARE keyword using the
following formula:

DECLARE Options

The DECLARE keyword lets the interpreter know that you are making a declaration. The
DECLARE keyword is followed by a name for the variable. In Transact-SQL, the name of
a variable starts with the @ sign. The name of a variable allows you to identify the area
of memory where the value of the variable is stored. While other languages like C/C++,
Pascal, Java, C#, etc, impose strict rules to names, Transact-SQL is extremely flexible.
A name can be made of digits only. Here is an example:

DECLARE @264

Such a name made of digits can create confusion with a normal number. A name can
also be made of one or more words.

Objects Names

To avoid confusion, here are the rules we will use in our lessons:

A name will start with either an underscore or a letter. Examples are @_n, @act, or
@Second

After the first character as an underscore or a letter, the name will have combinations of
underscores, letters, and digits. Examples are @_n24 or @act_52_t

A name will not include special characters such as !, @, #, $, %, ^, &, or *

If the name is a combination of words, each word will start in uppercase. Examples are
@DateHired, @_RealSport, or @DriversLicenseNumber

To declare a variable, as we will see in the next sections, after giving a name to a
dvariable, you must also specify the amount of memory that the variable would need.
The amount of memory is also called a data type. Therefore, the declaration of a
variable uses the following formula:

DECLARE @VariableName DataType;

You can also declare more than one variable. To do that, separate them with a comma.
The formula would be:

DECLARE @Variable1 DataType1, @Variable2 DataType2, @Variable_n DataType_n;

Unlike many other languages like C/C++, C#, Java, or Pascal, if you declare many
variables that use the same data type, the name of each variable must be followed by
Database Creation With Code 78

its own data type.

12.19. INITIALIZING A VARIABLE


After declaring a variable, the interpreter reserves a space in the computer memory for
it but the space doesn't necessarily hold a recognizable value. This means that, at this
time, the variable is null. One way you can change this is to give a value to the variable.
This is referred to as initializing the variable.

Remember that a variable's name starts with @ and whenever you need to refer to the
variable, you must make sure you include the @ sign. To initialize a variable, in the
necessary section, type the SELECT or the SET keyword followed by the name of the
variable, followed by the assignment operator "=", followed by an appropriate value.
The formula used is:

SELECT @VariableName = DesiredValue

or

SET @VariableName = DesiredValue

Once a variable has been initialized, you can make its value available or display it. This
time, you can type the name of the variable to the right side of PRINT or SELECT.

12.20. DATA TYPES

After setting the name of a variable, you must specify the amount of memory that the
variable will need to store its value. Since there are various kinds of information a
database can deal with, SQL provides a set of data types.

BOOLEAN VARIABLES
A Boolean value is a piece of information stated as being true or false, On or Off, Yes or
No, 1 or 0. To declare a variable that holds a Boolean value, you can use the BIT or bit
keyword. Here is an example:

DECLARE @IsOrganDonor bit;

After declaring a Boolean variable, you can initialize it with 0 or another value. If the
variable is initialized with 0, it receives the Boolean value of False. If it is initialized with
any other number, it receives a True value. Here is an example of using a Boolean
variable:
Database Creation With Code 79

Practical Learning: Using Boolean Variables

In the Query window, type the following:

DECLARE @IsMarried bit

SET @IsMarried = 1

SELECT @IsMarried AS [Is Married?];

GO

Execute the statement

INTEGER VARIABLES
An integer, also called a natural number, or a whole number, is a number that can start
with a + or a - sign and is made of digits. Between the digits, no character other than a
digit is allowed. In the real world, when a number is (very) long and becomes difficult to
ready, such as 79435794, you are allowed to type a symbol called the thousand
separator in each thousand increment. An example is 79,435,794. In your SQL
expressions, never include the thousand separator: you would receive an error.

When the number starts with +, such as +44 or +8025, such a number is referred to as
positive and you should omit the starting + sign. This means that the number should be
written as 44 or 8025. Any number that starts with + or simply a digit is considered as
greater than 0 or positive. A positive integer is also referred to as unsigned. On the
other hand, a number that starts with a - symbol is referred to as negative.

If a variable would hold natural numbers in the range of -2,147,483,648 to


2,147,483,647, you can declare it with the int keyword as data type. Here is an
example:
Database Creation With Code 80

DECLARE @Category int

SET @Category = 208

PRINT @Category

GO

This would produce:

(1 rows affected)

1> DECLARE @Category INT;

2> SET @Category = 1450;

3> PRINT @Category;

4> GO

1450

(1 rows affected)

The length of an integer is the number of bytes its field can hold. For an int type, that
would be 4 bytes.

If you want to use very small numbers such as student's ages, or the number of pages
of a brochure or newspaper, apply the tinyint data type to such a field. A variable with
the tinyint data type can hold positive numbers that range from 0 to 255. Here is an
example:

1> DECLARE @StudentAge tinyint;

2> SET @StudentAge = 14;

3> SELECT @StudentAge AS [Student's Age];

4> GO

Student's Age

-------------

14

(1 rows affected)

The smallint data type follows the same rules and principles as the int data type except
Database Creation With Code 81

that it is used to store smaller numbers that would range between -32,768 and 32,767.
Here is an example:

1> DECLARE @NumberOfPages SMALLINT;

2> SET @NumberOfPages = 16;

3> SELECT @NumberOfPages AS [Number of Pages];

4> GO

Number of Pages

---------------

16

(1 rows affected)

The bigint data type follows the same rules and principles as the int data type except
that its field can hold numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. Here is an example:

1> DECLARE @CountryPopulation BigInt;

2> SET @CountryPopulation = 16500000;

3> SELECT @CountryPopulation AS 'Country Population';

4> GO

Country Population

--------------------

16500000

(1 rows affected)

The binary data type is used for a variable that would hold hexadecimal numbers.
Examples of hexadecimal numbers are 0x7238, 0xFA36, or 0xAA48D. Use the binary
data type if all values of the variable would have the exact same length (or quantity). If
you anticipate that some entries would be different than others, then use the alternative
varbinary data type. The varbinary type also is used for hexadecimal numbers but
allows dissimilar entries, as long as all entries are hexadecimals.

PRACTICAL LEARNING : USING INTEGER VARIABLES


Change the statement as follows:
Database Creation With Code 82

DECLARE @IsMarried bit, @EmplStatus int;

SET @IsMarried = 1;

SET @EmplStatus = 2;

SELECT @IsMarried AS [Is Married?],

@EmplStatus AS [Employment Status];

GO

Execute the statement:

12.21. DECIMAL VARIABLES


A decimal number is a number that can have a period (or the character used as the
decimal separator as set in the Control Panel) between the digits. An example would be
12.625 or 44.80. Like an integer, a decimal number can start with a + or just a digit,
which would make it a positive number. A decimal number can also start with a -
symbol, which would make it a negative number. If the number represents a fraction, a
period between the digits specifies what portion of 1 was cut. If you anticipate such a
number for a field, specify its data type as numeric or decimal (either decimal or
Database Creation With Code 83

numeric would produce the same effect in SQL Server). Here is an example:

1> DECLARE @Distance DECIMAL;

2> SET @Distance = 648.16;

3> PRINT @Distance;

4> GO

648

A floating-point number is a fractional number, like the decimal type. Floating-point


numbers can be used if you would allow the database engine to apply an approximation
to the actual number. To declare such a variable, use the float or the real keyword. Here
is an example:

1> DECLARE @Radius FLOAT;

2> SET @Radius = 48.16;

3> SELECT @Radius AS Radius;

4> GO

Radius

------------------------

48.159999999999997

(1 rows affected)

A precision is the number of digits used to display a numeric value. For example, the
number 42005 has a precision of 5, while 226 has a precision value of 3. If the data
type is specified as an integer (the int and its variants) or a floating-point number (float
and real), the precision is fixed by the database and you can just accept the value set
by the Microsoft SQL Server interpreter. For a decimal number (decimal or numeric data
types), Microsoft SQL Server allows you to specify the amount of precision you want.
The value must be an integer between 1 and 38 (28 if you are using SQL Server 7).

A decimal number is a number that has a fractional section. Examples are 12.05 or
1450.4227. The scale of a number if the number of digits on the right side of the period
(or the character set as the separator for decimal numbers for your language, as
specified in Control Panel). The scale is used only for numbers that have a decimal part,
which includes currency (money and smallmoney) and decimals (numeric and decimal).
If a variable is declared with the decimal or numeric data type, you can specify the
amount of scale you want. The value must be an integer between 0 and 18. Here is an
example:
Database Creation With Code 84

PRACTICAL LEARNING : USING DECIMAL VARIABLES


Change the statement as follows:

DECLARE @IsMarried bit,

@EmplStatus int,

@WeeklyHours Decimal(6,2);

SET @IsMarried = 1;

SET @EmplStatus = 2;

SET @WeeklyHours = 36.50;

SELECT @IsMarried AS [Is Married?],

@EmplStatus AS [Employment Status],

@WeeklyHours AS Hours;

GO

Execute the statement


Database Creation With Code 85

Currency Variables

If a variable would hold monetary values, you can declare it with the money keyword. A
variable with a money data type can hold positive or negative values from -
922,337,203,685,477.5808 to +922,337,203,685,477.5807. Here is an example:

1> DECLARE @YearlyIncome Money;

2> SET @YearlyIncome = 48500.15;

3> SELECT @YearlyIncome AS [Yearly Income];

4> GO

Yearly Income

---------------------

48500.1500

(1 rows affected)

While the money data type can be used for a variable that would hold large quantities of
currency values, the smallmoney data type can be applied for a variable whose value
cannot be lower than -214,748.3648 nor higher than 214,748.3647.

The precision and scale of a money or smallmoney variable are fixed by Microsoft SQL
Server. The scale is fixed to 4.

PRACTICAL LEARNING : USING CURRENCY VARIABLES


Change the statement as follows:

DECLARE @EmplStatus int,

@IsMarried bit,

@WeeklyHours Decimal(6,2),

@HourlySalary SmallMoney,

@WeeklySalary SmallMoney;

SET @IsMarried = 1;

SET @EmplStatus = 2;

SET @WeeklyHours = 36.50;


Database Creation With Code 86

SET @HourlySalary = 15.72;

SET @WeeklySalary = @WeeklyHours * @HourlySalary;

SELECT @EmplStatus AS [Empl Status],

@IsMarried AS [Married?],

@WeeklyHours AS Hours,

@HourlySalary AS Hourly,

@WeeklySalary AS Weekly;

GO

Execute the statement

12.22. DATE AND TIME VARIABLES


A DATETIME data type is used for a column whose data would consist of date and/or
time values. The entries must be valid date or time values but Microsoft SQL Server
Database Creation With Code 87

allows a lot of flexibility, even to display a date in a non-traditional format. The date
value of a datetime field can be comprised between January 1st, 1753 and December
31, 9999.

To initialize a DATETIME variable, include its value between single-quote. If the value is
a date, separate the components of the value with the symbol recognized in Control
Panel as the Date Separator:

Here is an example:

1> DECLARE @IndependenceDay DATETIME;

2> SET @IndependenceDay = '01/01/1960';

3> SELECT @IndependenceDay AS [Independence Day];

4> GO

Independence Day

-----------------------

1960-01-01 00:00:00.000
Database Creation With Code 88

(1 rows affected)

If the value is a time period, still include it in single-quotes. Inside of the quotes, follows
the rules and formats specified in the Control Panel:

Here is an example:

1> DECLARE @ArrivalTime datetime;

2> SET @ArrivalTime = '18:22';

3> SELECT @ArrivalTime AS [Arrival Time];

4> GO

Arrival Time

-----------------------

1900-01-01 18:22:00.000

(1 rows affected)

The smalldatetime data type is an alternative to datetime. It follows the same rules and
Database Creation With Code 89

principles as the datetime data type except that a date value must be comprised
between January 1st, 1900 and June 6, 2079.

Practical Learning: Using Date/Time Variables

Change the statement as follows:

DECLARE @DateHired DateTime,

@EmplStatus int,

@IsMarried bit,

@WeeklyHours Decimal(6,2),

@HourlySalary SmallMoney,

@WeeklySalary SmallMoney;

SET @DateHired = '12/05/1998';

SET @IsMarried = 1;

SET @EmplStatus = 2;

SET @WeeklyHours = 36.50;

SET @HourlySalary = 15.72;

SET @WeeklySalary = @WeeklyHours * @HourlySalary;

SELECT @DateHired AS [Date Hired],

@EmplStatus AS [Empl Status],

@IsMarried AS [Married?],

@WeeklyHours AS Hours,

@HourlySalary AS Hourly,

@WeeklySalary AS Weekly;

GO

Execute the statement


Database Creation With Code 90

Character Variables

A field of characters can consist of any kinds of alphabetical symbols in any


combination, readable or not. If you want a variable to hold a fixed number of
characters, such as the book shelf numbers of a library, declare it with the char data
type. Here is an example:

DECLARE @Gender char;

By default, the char data type can be applied to a variable that would hold one character
at a time. After declaring the variable, when initializing it, include its value in single-
quotes. Here is an example:

1> DECLARE @Gender char;

2> SET @GENDER = 'M';

3> SELECT @Gender AS Gender;

4> GO

Gender
Database Creation With Code 91

------

(1 rows affected)

If you include more than one character in the single-quotes, only the first (most left)
character would be stored in the variable. Here is an example:

1> DECLARE @Gender char;

2> SET @Gender = 'Male';

3> SELECT @Gender AS Gender;

4> GO

Gender

------

(1 rows affected)

A string is a character or a combination of characters. If a variable will hold strings of


different lengths, declare it with the varchar data type. The maximum length of text that
a field of varchar type can hold is equivalent to 8 kilobytes.

In some circumstances, you will need to change or specify the number of characters
used in a string variable. Although a First Name and a Book Title variables should use
the varchar type, both variables would not have the same length of entries. As it
happens, people hardly have a first name that is beyond 20 characters and many book
titles go beyond 32 characters. In this case, both variables would use the same data
type but different lengths.

To specify the maximum number of characters that can be stored in a string variable, on
the right side of char or varchar, type an opening and a closing parentheses. Inside of
the parentheses, type the desired number. To initialize the variable, if you are using the
Command Prompt (SQLCMD.EXE), include its value between double-quotes. Here is an
example:

1> DECLARE @ShelfNumber char(6);

2> SET @ShelfNumber = "CI-422";

3> SELECT @ShelfNumber AS [Shelf #];


Database Creation With Code 92

4> GO

Shelf #

-------

CI-422

(1 rows affected)

If you are the query window, don't include the string value in double-quotes; otherwise,
you would receive an error:

Therefore, if using the query window, include the string in single-quotes:


Database Creation With Code 93

The text data type can be used on a variable whose data would consist of ASCII
characters. As opposed to a varchar type of field, a text type of field can hold text that
is longer than 8 kilobytes.

The nchar, nvarchar, and ntext types follow the same rules as the char, varchar, and
text respectively, except that they can be applied to variables that would hold
international characters, that is, characters of languages other than US English. This is
done following the rules of Unicode formats.

PRACTICAL LEARNING : USING CHARACTER VARIABLES


Change the statement as follows:

DECLARE @FirstName varchar(20),

@LastName varchar(20),

@FullName varchar(40),

@DateHired DateTime,

@EmplStatus int,

@IsMarried bit,

@WeeklyHours Decimal(6,2),

@HourlySalary SmallMoney,
Database Creation With Code 94

@WeeklySalary SmallMoney;

SET @FirstName = 'Samuel';

SET @LastName = 'Weinberg';

SET @FullName = @LastName + ', ' +@FirstName;

SET @DateHired = '12/05/1998';

SET @IsMarried = 1;

SET @EmplStatus = 2;

SET @WeeklyHours = 36.50;

SET @HourlySalary = 15.72;

SET @WeeklySalary = @WeeklyHours * @HourlySalary;

SELECT @FullName As [Full Name],

@DateHired AS [Date Hired],

@EmplStatus AS [Empl Status],

@IsMarried AS [Married?],

@WeeklyHours AS Hours,

@HourlySalary AS Hourly,

@WeeklySalary AS Weekly;

GO

Execute the statement


Database Creation With Code 95

Save the file as Variables in your My Documents folder

LOGICAL COMPARISONS

For your databases, you can create expressions that represent a combination
of values, variables, and operators. To support expressions, Transact-SQL
provides operators other than, or in addition to, those we saw in Lesson 2.
Database Creation With Code 96

A comparison is a Boolean operation that produces a true or a false result,


depending on the values on which the comparison is performed. A comparison is
performed between two values of the same type; for example, you can compare
two numbers, two characters, or the names of two cities. To support
comparisons, Transact-SQL provides all necessary operators.

12.23. EQUALITY OPERATOR =


To compare two values for equality, use the = operator. Its formula is:

Value1 = Value2

The equality operation is used to find out whether two values are the same.
From the above formula, the SQL interpreter would compare Value1 and Value2.
If Value1 and Value2 are the same, the comparison produces a TRUE result. If
they are different, the comparison renders FALSE.

The equality operation can be illustrated as follows:

Not Equal <>

As opposed to equality, to find out if two values are not equal, use the <>
operator. Its formula is:

Value1 <> Value2

The <> is a binary operator (like all logical operators) that is used to compare
two values. The values can come from two variables as in Variable1 <>
Variable2. Upon comparing the values, if both hold different values, the
Database Creation With Code 97

comparison produces a TRUE. Otherwise, the comparison renders FALSE or a


null value.

It can be illustrated as follows:

Notice that the Not Equal operator <> is the opposite to the Equality operator =.

Less Than <

To find out whether one value is lower than another, use the < operator. Its
formula is:

Value1 < Value2

The value held by Value1 is compared to that of Value2. As it would be done


with other operations, the comparison can be made between two variables, as in
Variable1 < Variable2. If the value held by Value1 is lower than that of Value2,
the comparison produces a true or positive result.

The Less Than operator "<" can be illustrated as follows:


Database Creation With Code 98

Less Than Or Equal To <=

The Equality and the Less Than operators can be combined to compare two
values. This allows you to know if two values are the same or if the first is less
than the second. The operator used is <= and its formula is:

Value1 <= Value2

If both Value1 and Value2 are the same, the result is true or positive. If the left
operand, in this case Value1, holds a value lower than the second operand, in
this case Value2, the result is still true. If the value of Value1 is strictly higher
than that of Value, the comparison produces a FALSE result.

A <= operation can be illustrated as follows:

Greater Than >

To find out if one value is strictly greater than another, you can use the >
Database Creation With Code 99

operator. Its formula is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left
operand can be a variable while the right operand is a constant. If the value on
the left of the > operator is greater than the value on the right side or a
constant, the comparison produces a true or positive value. Otherwise, the
comparison renders false or null.

The > operator can be illustrated as follows:

Notice that the > operator is the opposite to <=.

Greater Than or Equal To >=

The greater than and the equality operators can be combined to produce an
operator as follows: >=. This is the "greater than or equal to" operator. Its
formula is:

Value1 >= Value2

The comparison is performed on both operands: Value1 and Value2. If the value
of Value1 and that of Value2 are the same, the comparison produces a true or
positive value. If the value of the left operand is greater than that of the right
operand, the comparison produces true or positive also. If the value of the left
operand is strictly less than the value of the right operand, the comparison
produces a false or null result. This can be illustrated as follows:
Database Creation With Code 100

Notice that the >= operator is the opposite to <.

Conditional Statements

A condition statement is an expression you formulate to evaluate it. Most of the


time, the statement is written so that, when evaluated, it can produce a result
of true or false, then, depending on the outcome, you can take action. A
condition is usually written as simple as possible to make it clear to you and the
SQL interpreter. Although the interpreter never gets confused, if you create a
difficult statement, you may receive an unpredictable result.

In the next few sections, we will review the keywords and formulas that
Transact-SQL provides to help you formula clear expressions. Expressions
usually start with a keyword, followed by the expression itself. After the
expression, you can tell the interpreter what to do. The statement may appear
as follows:

Keyword Expression

Statement

BEGIN...END

With the above formula, we will always let you know what keyword you can use,
why, and when. After the expression, you can write the statement in one line.
This is the statement that would be executed if/when the Expression of our
formula is satisfied. In most cases, you will need more than one line of code to
specify the Statement.
Database Creation With Code 101

As it happens, the interpreter considers whatever comes after the Statement as


a unit but only the line immediately after the Expression. To indicate that your
Statement covers more than one line, start it with the BEGIN keyword. Then you
must use the END keyword to indicate where the Statement ends. In this case,
the formula of a conditional statement would appear as follows:

dKeyword Expression

BEGIN

Statement Line 1

Statement Line 2

Statement Line n

END

You can still use the BEGIN...END combination even if your Statement covers
only one line:

Keyword Expression

BEGIN

Statement

END

Using the BEGIN...END combination makes your code easier to read because it
clearly indicates the start and end of the Statement.

IF a Condition is True

Probably the primary comparison you can perform on a statement is to find out
whether it is true. This operation is performed using an IF statement in
Transact-SQL. Its basic formula is:

IF Condition

Statement

When creating an IF statement, first make sure you provide a Condition


expression that can be evaluated to produce true or false. To create this
Condition, you can use variables and the logical comparison operator reviewed
above.

When the interpreter executes this statement, it first examines the Condition to
evaluate it to a true result. If the Condition produces true, then the interpreter
Database Creation With Code 102

executes the Statement. Here is an example:

DECLARE @DateHired As DateTime,

@CurrentDate As DateTime

SET @DateHired = '1996/10/04'

SET @CurrentDate = '2007/04/11'

IF @DateHired < @CurrentDate

PRINT 'You have the experience required for a new promotion in this job'

GO

This would produce:

IF...ELSE

The IF condition we used above is appropriate when you only need to know if an
expression is true. There is nothing to do in other alternatives. Consider the
following code:

DECLARE @DateHired As DateTime,


Database Creation With Code 103

@CurrentDate As DateTime

SET @DateHired = '1996/10/04'

SET @CurrentDate = '2007/04/16'

IF @DateHired > @CurrentDate

PRINT 'You have the experience required for a new promotion'

GO

This would produce:

Notice that, in case the expression to examine produces a false result, there is
nothing to do. Sometimes this will happen.

CASE...WHEN...THEN

The CASE keyword is used as a conditional operator that considers a value,


examines it, and acts on an option depending on the value. The formula of the
CASE statement is:

CASE Expression

WHEN Value1 THEN Result

WHEN Value2 THEN Result

WHEN Value_n THEN Result

END
Database Creation With Code 104

In the following example, a letter that represents a student is provided. If the


letter is m or M, a string is created as Male. If the value is provided as f or F, a
string is created as Female:

DECLARE @CharGender Char(1),

@Gender Varchar(20);

SET @CharGender = 'F';

SET @Gender =

CASE @CharGender

WHEN 'm' THEN 'Male'

WHEN 'M' THEN 'Male'

WHEN 'f' THEN 'Female'

WHEN 'F' THEN 'Female'

END;

SELECT 'Student Gender: ' + @Gender;

GO

Here is the result of executing it:


Database Creation With Code 105

CASE...WHEN...THEN...ELSE

In most cases, you may know the only types of value that would be submitted to
a CASE statement. In some other cases, an unpredictable value may be
submitted. If you anticipate a value other than those you are aware of, the CASE
statement provides a "fit-all' alternative by using the last statement as ELSE. In
this case, the formula of the CASE statement would be:

CASE Expression

WHEN Value1 THEN Result

WHEN Value2 THEN Result

WHEN Value_n THEN Result

ELSE Alternative

END

The ELSE statement, as the last, is used when none of the values of the WHEN
statements fits. Here is an example:

DECLARE @CharGender Char(1),

@Gender Varchar(20);

SET @CharGender = 'g';

SET @Gender =

CASE @CharGender

WHEN 'm' THEN 'Male'

WHEN 'M' THEN 'Male'

WHEN 'f' THEN 'Female'

WHEN 'F' THEN 'Female'

ELSE 'Unknown'

END;

SELECT 'Student Gender: ' + @Gender;

GO
Database Creation With Code 106

This would produce:

If you don't produce an ELSE statement but a value not addressed by any of the
WHEN statements is produced, the result would be NULL. Here is an example:

This means that it is a valuable safeguard to always include an ELSE sub-


statement in a CASE statement.

WHILE

To examine a condition and evaluate it before taking action, you can use the
WHILE operator. The basic formula of this statement is:
Database Creation With Code 107

WHILE Expression

Statement

When implementing this statement, first provide an Expression after the WHILE
keyword. The Expression must produce a true or a false result. If the Expression
is true, then the interpreter executes the Statement. After executing the
Statement, the Expression is checked again. AS LONG AS the Expression is true,
it will keep executing the Statement. When or once the Expression becomes
false, it stops executing the Statement. This scenario can be illustrated as
follows:

Here is an example:

DECLARE @Number As int

WHILE @Number < 5

SELECT @Number AS Number

GO

To effectively execute a while condition, you should make sure you provide a
mechanism for the interpreter to get a referenced value for the condition,
variable, or expression being checked. This is sometimes in the form of a
variable being initialized although it could be some other expression. Such a
while condition could be illustrated as follows:
Database Creation With Code 108

This time, the statement would be implemented as follows:

DECLARE @Number As int

SET @Number = 1

WHILE @Number < 5

BEGIN

SELECT @Number AS Number

SET @Number = @Number + 1

END

GO

This would produce:


Database Creation With Code 109

Boolean Constants

Databases and other programming environments provide operators you can


use to perform data analysis. The operators used are called logical operators
because they are used to perform comparisons that produce a result of true or
false (there is no middle result; in other words, something is not half true or half
false or "Don't Know": either it is true or it is false).

The TRUE and FALSE Constants

In Boolean algebra, something is considered TRUE when it holds a value. The


value is also considered as 1 or Yes. By contrast, if something doesn't hold a
value, it is considered non-existent and non-worthy of consideration. Such a
thing has a value of FALSE, 0, or No. To retrieve such a value, you can just find
out if the value of a field is existent or not.

The comparison for a True or False value is mostly performed on Boolean fields,
such a case is the SPHome (which specifies whether a student lives in a single
parent home) field of the Students table of the HighSchool database. If a record
has a value of 1, the table considers that such a field is True. If the field has a 0
value, then it holds a FALSE value.

The NULL Constant


Database Creation With Code 110

After you have declared a variable, the SQL interpreter reserves a space in the
computer memory for it but doesn't put anything in that memory space. At that
time, that area of memory doesn't hold a significant value. Also at that time, the
variable is considered null.

Here is note to be careful about: when a variable is said to hold a null value, it
doesn't mean its value is 0. It doesn't even mean that the variable's memory
space is empty. It actually means that we cannot clearly determine the current
value that the variable is holding.

To support the null value, Transact-SQL provides a constant named NULL. The
NULL constant is mostly used for comparison purposes. For example, you can
use an IF statement to check the nullidy of a variable.

The IS Operator

To validate something as being possible, you can use the IS operator. For
example, to acknowledge that something is NULL, you can use the IS NULL
expression. Here is an example:

-- Square Calculation

DECLARE @Side As Decimal(10,3),

@Perimeter As Decimal(10,3),

@Area As Decimal(10,3);

SET @Perimeter = @Side * 4;

SET @Area = @Side * @Side;

IF @Side IS NULL

PRINT 'A null value is not welcome'

ELSE IF @Side > 0

BEGIN

SELECT @Side AS Side;

SELECT @Perimeter AS Perimeter ;

SELECT @Area AS Area;

END;

ELSE
Database Creation With Code 111

PRINT 'You must provide a positive value';

GO

This would produce:

To avoid having a NULL value, you can either initialuze the variable or you can
assign it a value. Here is an example:

-- Square Calculation

DECLARE @Side As Decimal(10,3),

@Perimeter As Decimal(10,3),

@Area As Decimal(10,3);

SET @Side = 48.126;

SET @Perimeter = @Side * 4;

SET @Area = @Side * @Side;

IF @Side IS NULL
Database Creation With Code 112

PRINT 'A null value is not welcome'

ELSE IF @Side > 0

BEGIN

SELECT @Side AS Side;

SELECT @Perimeter AS Perimeter ;

SELECT @Area AS Area;

END;

ELSE

PRINT 'You must provide a positive value';

GO

This would produce:

The NOT Operator

To deny the presence, the availability, or the existence of a value, you can use
Database Creation With Code 113

the NOT operator. This operator is primarily used to reverse a Boolean value. For
example, we have learned that FALSE is the opposite of TRUE. In the same way,
TRUE is the opposite of FALSE. If you want to compare a value as not being
TRUE, the NOT TRUE would produce the same result as the FALSE value. For the
same reason, the expression NOT FALSE is the same as TRUE.

Built-In Functions Fundamentals

While your primary job as a database developer consists of creating lists,


probably your second most important job is to assist your users with the various
assignments they must perform on your application. One way you can assist is
to use functions that perform otherwise complex tasks. We introduced and
described functions in the previous lesson. To assist your development with the
different tasks of a database, Transact-SQL ships with various already created
and tested functions. You just need to be aware of these functions, their syntax,
and the results they produce.

To help you identify the functions you can use, they are categorized by their
types and probably their usefulness.

Because of their complexities, some values can be easily recognized or fixed. For
example, a date such as January 6, 1995 is constant and can never change. This
type of value is referred to as deterministic because it is always the same. In the
same way, a time value such as 5PM is constant and cannot change. There are
other values that cannot be known in advance because they change based on
some circumstances. For example, the starting date of the school year changes
from one year to another but it always occurs. This means that, you know it will
happen but you don't know the exact date. Such a value is referred to as non-
deterministic.

To support determinism and non-determinism, Transact-SQL provides two


broad categories of functions. A function that always returns the same or
known value is referred to as deterministic. A function whose returned value
may depend on a condition is referred to as non-deterministic.

Casting a Value

In most cases, a value the user submits to your database is primarily


considered a string. This is convenient if that's what you are expecting. If the
value the user provides must be treated as something other than a string, for
example, if the user provides a number, before using such a value, you should
first convert it to the appropriate type, that is, from a string to the expected
type.
Database Creation With Code 114

To assist with conversion, you can use either the CAST() or the CONVERT()
function. The syntax of the CAST() function is:

CAST(Expression AS DataType)

The Expression is the value that needs to be cast. The DataType factor is the
type of value you want to convert the Expression to. The DataType can be one
of those we reviewed in Lesson 4.

In the following example, two variables are declared and initialzed as strings.
Because they must be involved in a multiplication, each is converted to a
Decimal type:

DECLARE @StrSalary Varchar(10),

@StrHours Varchar(6),

@WeeklySalary Decimal(6,2)

SET @StrSalary = '22.18';

SET @StrHours = '38.50';

SET @WeeklySalary = CAST(@StrSalary As Decimal(6,2)) *

CAST(@StrHours As Decimal(6,2));

SELECT @WeeklySalary;

GO

Here is an example of executing the above statement:


Database Creation With Code 115

Converting a Value

Like CAST(), the CONVERT() function is used to convert a value. Unlike CAST(),
CONVERT can be used to convert a value its original type into a non-similar
type. For example, you can use CONVERT to cast a number into a string and
vice-versa.

The syntax of the CONVERT() function is:

CONVERT(DataType [ ( length ) ] , Expression [ , style ])

The first argument must be a known data type, such as those we reviewed in
Lesson 4. If you are converting the value into a string (varchar, nvarchar, char,
nchar) or a binary type, you should specify the number of allowed characters
the data type's own parentheses. As reviewed for the CAST() function, the
Expression is the value that needs to be converted.

Here is an example:

-- Square Calculation

DECLARE @Side As Decimal(10,3),

@Perimeter As Decimal(10,3),

@Area As Decimal(10,3);
Database Creation With Code 116

SET @Side = 48.126;

SET @Perimeter = @Side * 4;

SET @Area = @Side * @Side;

PRINT 'Square Characteristics';

PRINT '-----------------------';

PRINT 'Side = ' + CONVERT(varchar(10), @Side, 10);

PRINT 'Perimeter = ' + CONVERT(varchar(10), @Perimeter, 10);

PRINT 'Area = ' + CONVERT(varchar(10), @Area, 10);

GO

This would produce:

String-Based Functions
Database Creation With Code 117

The string is the most basic, the primary value that is presented to a database.
This is because, any value, before being treated particularly, is firstly
considered a string. In an application, there are various ways you use or get a
string. You can get it or provide it to a function as a constant string, that is, a
string whose value you know certainly and that you pass to a function. You can
also get a string that a user provides. Other functions also can produce or
return a string.

To assist you with managing strings or performing operations on them,


Transact-SQL provides various functions. The functions can divide in categories
that include character-based, conversions, addition, sub-strings, etc.

Practical Learning: Introducing Transact-SQL Functions

Start Microsoft SQL Server and connect to the server

Right-click the Databases node and click New Database...

Set the Name to Exercise1 and click OK

Expand the Databases node

Expand Exercise1

Expand Programmability

Expand Functions

The Length of a String

Some operations performed on strings require that you know the number of
characters of a string. This is because some operations require a minimum
number of characters and some other functions require that the string have at
least one character. The number of characters of a string is also called the
length of the string.

To get the length of a string, you can use the LEN() function. Its syntax is:

int LEN(String)

This function takes one argument as the string to be considered. It returns the
number of characters in the string. Here is an example:

DECLARE @FIFA varchar(120)

SET @FIFA = 'Fédération Internationale de Football Association'

SELECT @FIFA AS FIFA


Database Creation With Code 118

SELECT LEN(@FIFA) AS [Number of Characters]

This would produce:

String Conversions: Converting From Integer to ASCII

As you may know already, a string is primarily one or a group of characters.


These characters are ASCII values. If you have a string, to get the ASCII code
of its leftmost character, you can use the ASCII() function. Its syntax is:

int ASCII(String)

This function takes as argument as string and returns the ASCII code of the
first (the left) character of the string. Here is an example:

DECLARE @ES varchar(100)

SET @ES = 'El Salvador'

SELECT @ES AS ES

SELECT ASCII(@ES) AS [In ASCII Format]

This would produce:


Database Creation With Code 119

String Conversions: Converting From ASCII to Integer

dIf you have the ASCII code of a character and want to find its actual
character, you can use the CHAR() function. Its syntax is:

char CHAR(int value)

This function takes as argument a numeric value as an integer. Upon


conversion, the function returns the ASCII equivalent of that number.

String Conversions: Lowercase

As you may know already, a string can be made of uppercase, lowercase, and
symbols that don't have a particular case. When you receive a string, if you
want to convert all of its characters to lowercase, you can use the LOWER()
function. Its syntax is:

varchar LOWER(String)

This function takes as argument a string. Any lowercase letter that is part of
the string would not change. Any letter that is part of the string would be
converted to lowercase. Any other character or symbol would be kept "as is".
After conversion, the LOWER() function returns a new string.

Here is an example:
Database Creation With Code 120

DECLARE @FIFA varchar(120)

SET @FIFA = 'Fédération Internationale de Football Association'

SELECT @FIFA AS FIFA

SELECT LOWER(@FIFA) AS Converted

This would produce:

Practical Learning: Converting a String to Lowercase

In the Object Explorer, right-click Exercise1 and click New Query...

To create a function, type the following:

--
=============================================

-- Function: GetUsername

--
=============================================
Database Creation With Code 121

CREATE FUNCTION GetUsername

(@FirstName varchar(40),

@LastName varchar(40))

dRETURNS varchar(50)

AS

BEGIN

DECLARE @Username AS varchar(50);

SELECT @Username = LOWER(@FirstName) +


LOWER(@LastName);

RETURN @Username;

END

GO

Press F5 to execute the statement

Save the code as Exercise

Press Ctrl + A to select the code and press Delete to remove the code

To test the function, type the following:

SELECT Exercise1.dbo.GetUsername('Francine', 'Moukoko');

GO

Press F5 to execute the statement


Database Creation With Code 122

In the Object Explorer, expand the Databases node if necessary, and expand
Exercise1

Expand Programmability

Expand Functions

Expand Scalar-Valued Functions

Right-click dbo.GetUserName and click Delete

In the Delete Object dialog box, click OK

Sub-Strings: The Starting Characters of a String

A sub-string is a section gotten from a string. The idea is to isolate one or a


group of characters for any necessary reason.

A left sub-string is one or a group of characters retrieved from the left side of a
known string. To get the left sub-string of a string, you can use the LEFT()
function. Its syntax is:

varchar LEFT(String, NumberOfCharacters)

This function takes two arguments. The first argument specifies the original
string. The second argument specifies the number of characters from the most-
left that will constitute the sub-string. After the operation, the LEFT() function
returns a new string made of the left character + the NumberOfCharacters on
its right from the String.
Database Creation With Code 123

Practical Learning: Creating a Sub-String With Left Characters

Delete the previous code from the query window

To use the LEFT() function, type the following:

-- =============================================

-- Function: GetUsername

-- =============================================

CREATE FUNCTION GetUsername

(@FirstName varchar(40),

@LastName varchar(40))

RETURNS varchar(50)

AS

BEGIN

DECLARE @Username AS varchar(50);

SELECT @Username = LOWER(LEFT(@FirstName, 1)) +

LEFT(LOWER(@LastName), 4)

RETURN @Username;

END

GO

Press F5 to execute the statement

Delete the code in the query window

To test the function, type the following:

SELECT Exercise1.dbo.GetUsername('Francine', 'Moukoko');

GO

Execute the statement in the window


Database Creation With Code 124

Change the call with a last name shorter than 5 characters such as "Um" and
execute the statement. Here is an example:

Sub-Strings: The Ending Characters of a String

Instead of the starting characters of a string, you may want to create a string
using the most-right characters of an existing string. To support this operation,
Transact-SQL provides the RIGHT() function. Its syntax is:

varchar RIGHT(String, NumberOfCharacters)


Database Creation With Code 125

This function takes two arguments. The first argument specifies the original
string. The second argument specifies the number of characters from the most-
right that will constitute the sub-string.

Practical Learning: Getting the Right Characters

Delete the previous code from the query window

To create a function, type the following:

--
=============================================

-- Function: Last4DigitsOfSSN

--
=============================================

CREATE FUNCTION Last4DigitsOfSSN(@SSN varchar(12))

dRETURNS char(4)

AS

BEGIN

RETURN RIGHT(@SSN, 4);

END

GO

Press F5 to execute the statement

Delete the previous code from the query window

To test the function, type the following:

SELECT Exercise1.dbo.Last4DigitsOfSSN('836483846');

GO

Execute the statement


Database Creation With Code 126

Sub-Strings: Replacing Occurrences in a String

One of the most annoying situations you may encounter with a string is to deal
with one that contains unexpected characters. This could be due to its
formatting or any other reason. For example, if you request a telephone
number from a user, there are various ways the string could be presented to
you. Examples are 000-000-0000, or 0000000000, or (000) 000-0000. Every
one of these formats is an acceptable US and Canadian telephone number but if
you involve that string in an operation, you could get an unpredictable result.
One way you can solve this type of problem is to remove any undesired
characters from the string. This operation can also consist of replacing some
character(s) with other(s).

To replace one character or a sub-string from a string, you can use the
REPLACE() function. Its syntax is:

varchar REPLACE(String, FindString, ReplaceWith)

or

binary REPLACE(String, FindString, ReplaceWith)

This function takes three arguments. The first is the string that will be used as
reference. The second argument, FindString, is a character or a sub-string to
look for in the String argument. If the FindString character or sub-string is
found in the String, then it is replaced with the value of the last argument,
ReplaceWith.

Practical Learning: Replacing Characters or Sub-Strings

In the Object Explorer, delete dbo.Last4DigitsOfSSN


Database Creation With Code 127

Delete the code in the query window

To use the REPLACE() function, change the code as follows:

-- =============================================

-- Function: Last4DigitsOfSSN

-- =============================================

CREATE FUNCTION Last4DigitsOfSSN(@SSN varchar(12))

RETURNS char(4)

AS

BEGIN

DECLARE @StringWithoutSymbol As varchar(12);

-- First remove empty spaces

SET @StringWithoutSymbol = REPLACE(@SSN, ' ', '');

-- Now remove the dashes "-" if they exist

SET @StringWithoutSymbol = REPLACE(@StringWithoutSymbol, '-', '');

RETURN RIGHT(@StringWithoutSymbol, 4);

END

GO

Execute the statement

Delete the code in the query window

To test the function, type the following

SELECT Exercise1.dbo.Last4DigitsOfSSN('244-04-8502');

GO

Execute the statement in the window

Arithmetic Functions
Database Creation With Code 128

The Sign of a Number

In arithmetic, a number is considered as being negative (less than 0), null


(equal to 0), or positive (higher than 0). When a number is negative, it must
have a - symbol to its left. If it is positive, it may display a + symbol to its left
or it can omit it. A number without the - or + symbol to its left is considered
positive, also referred to as unsigned. The symbol that determines whether a
number is positive or negative is referred to as its sign. The sign is easily
verifiable if you know the number already. In some cases, when a number is
submitted to your application, before taking any action, you may need to get
this piece of information.

To find out if a value is positive, null, or negative, Transact-SQL provides the


SIGN() function. Its syntax is:

SIGN(Expression)

This function takes as argument a number or an expression that can be


evaluated to a number. The interpreter would then examine the number:

If the Expression is positive, the function returns 1. Here is an example:

DECLARE @Number As int;

SET @Number = 24.75;

SELECT SIGN(@Number) AS [Sign of 1058];

GO
Database Creation With Code 129

If the Expression is null, the function returns 0

DECLARE @Number As int;

SET @Number = 0;

SELECT SIGN(@Number) AS [Sign of Number];

GO
Database Creation With Code 130

If the Expression is negative, the function returns -1

DECLARE @Number As int;

SET @Number = -57.05;

SELECT SIGN(@Number) AS [Sign of -57.05];

GO

Based on this, you can use the SIGN() function to find out whether a value is
Database Creation With Code 131

negative, null, or positive: simply pass the value (or a variable) to SIGN() and
use a logical operator to check its sign. Here is an example:

-- Square Calculation

DECLARE @Side As Decimal(10,3),

@Perimeter As Decimal(10,3),

@Area As Decimal(10,3);

SET @Side = 48.126;

SET @Perimeter = @Side * 4;

SET @Area = @Side * @Side;

IF SIGN(@Side) > 0

BEGIN

PRINT 'Square Characteristics';

PRINT '-----------------------';

PRINT 'Side = ' + CONVERT(varchar(10), @Side, 10);

PRINT 'Perimeter = ' + CONVERT(varchar(10), @Perimeter, 10);

PRINT 'Area = ' + CONVERT(varchar(10), @Area, 10);

END;

ELSE

PRINT 'You must provide a positive value';

GO

Here is an example of executing the statement:


Database Creation With Code 132

Here is another example of executing the statement:


Database Creation With Code 133

Notice that the interpreter acts differently in response to the result of the
SIGN() function.

The Absolute Value of a Number

The decimal numeric system counts from minus infinity to infinity. This means
that numbers are usually negative or positive, depending on their position from
0, which is considered as neutral. In some operations, the number considered
will need to be only positive even if it is provided in a negative format. The
absolute value of a number x is x if the number is (already) positive. If the
number is negative, its absolute value is its positive equivalent. For example,
the absolute value of 12 is 12, while the absolute value of –12 is 12.

To get the absolute value of a number, you can use the ABS() function. Its
syntax is:

ABS(Expression)

This function takes an expression or a number as argument and returns its


Database Creation With Code 134

absolute value. Here is an example:

DECLARE @NumberOfStudents INTEGER;

SET @NumberOfStudents = -32;

SELECT ABS(@NumberOfStudents) AS [Number of Students];

GO

dThis would produce:

The Ceiling of a Number

Consider a decimal value such as 12.155. This number is between integer 12


and integer 13

In the same way, consider a number such as –24.06. As this number is


negative, it is between –24 and –25, with –24 being greater.
Database Creation With Code 135

In algebra, the ceiling of a number is the closest integer that is greater than or
higher than the number considered. In the first case, the ceiling of 12.155 is 13
because 13 is the closest integer greater than or equal to 12.155. The ceiling of
–24.06 is –24.

To get the ceiling of a number, Transact-SQL provides the CEILING() function.


Its syntax is:

CEILING(Expression)

This function takes as argument a number or an expression that can evaluate


to a number. After the conversion, if the function succeeds, it returns a double-
precision number that is greater than or equal to Expression. Here is an
example:

DECLARE @Number1 As Numeric(6, 2),

@Number2 As Numeric(6, 2)

SET @Number1 = 12.155;

SET @Number2 = -24.06;

SELECT CEILING(@Number1) AS [Ceiling of 12.155],

CEILING(@Number2) AS [Ceiling of –24.06];

GO

This would produce:


Database Creation With Code 136

Here is another way of displaying the above results:

DECLARE @Number1 As Numeric(6, 2),

@Number2 As Numeric(6, 2)

SET @Number1 = 12.155;

SET @Number2 = -24.06;

PRINT 'The ceiling of 12.155 is ' +

CONVERT(varchar(10), CEILING(@Number1));

PRINT 'The ceiling of –24.06 is ' +

CONVERT(varchar(10), CEILING(@Number2));

GO

This would produce:


Database Creation With Code 137

The Floor of a Number

Consider two decimal numbers such as 128.44 and -36.72. The number 128.44
is between 128 and 129 with 128 being the lower. The number –36.72 is
between –37 and –36 with –37 being the lower. The lowest but closest integer
value of a number is referred to as its floor. Based on this, the floor of 128.44
is 128. The floor of –36.72 is –37.

To support finding the floor of a number, Transact-SQL provides the FLOOR()


function. Its syntax is:

FLOOR(Expression)

The FLOOR() function takes as argument a numeric value or an expression that


can be evaluated to a number. If the function succeeds during its conversion, it
produces the integer that is the floor of the argument. Here is an example:

DECLARE @Number1 As Numeric(6, 2),

@Number2 As Numeric(6, 2);

SET @Number1 = 128.44;

SET @Number2 = -36.72;


Database Creation With Code 138

SELECT FLOOR(@Number1) AS [Floor of 128.44],

FLOOR(@Number2) AS [Floor of –36.72];

GO

This would produce:

The Exponent of a Number

To calculate the exponential value of a number, Transact-SQL provides the


EXP() function. Its syntax is:

EXP(Expression)

This function takes one argument as a number or an expression that can be


evaluated to a number. Here is an example:

DECLARE @Number As Numeric(6, 2);

SET @Number = 6.48;


Database Creation With Code 139

SELECT EXP(@Number) AS [Exponent of 6.48];

GO

This would produce:

The Power of a Number

The power of a number is the value of that number when raised to another
number. This is done using the following formula:

ReturnValue = xy

To support finding the power of a number, Transact-SQL provides the POWER()


function. Its syntax is:

POWER(x, y)

This function takes two required arguments. The first argument, x, is used as
the base number to be evaluated. The second argument, y, also called the
exponent, will raise x to this value. Here is an example:

DECLARE @x As Decimal(6, 2),

@y As Decimal(6, 2);

SET @x = 20.38;
Database Creation With Code 140

dSET @y = 4.12;

SELECT POWER(@x, @y) AS [Power of 20.38 raised to 4.12];

GO

This would produce:

dThe Natural Logarithm of a Number

To assist with finding the natural logarithm of a number, Transact-SQL provides


the LOG() function. Its syntax is:

LOG(Expression)

This function takes one argument as a number or an expression that can


evaluate to a number. After the calculation, it returns the natural logarithm of
the argument. Here is an example:

DECLARE @Number As Decimal(6, 2);

SET @Number = 48.16;

SELECT LOG(@Number) AS [Natural Logarithm of 48.16];

GO
Database Creation With Code 141

This would produce:

The Base-10 Logarithm of a Number

To calculate the base 10 logarithm of a number, Transact-SQL provides the


LOG10() function. Its syntax is:

LOG10(Expression)

The number to be evaluated is passed as the argument X. The function returns


the logarithm on base 10 using the formula:

y = log10x

which is equivalent to

x = 10y

Here is an example:

DECLARE @Number As Decimal(6, 2);

SET @Number = 48.16;

SELECT LOG10(@Number) AS [Base-10 Logarithm of 48.16];


Database Creation With Code 142

GO

This would produce:

The Square Root

To support the calculation of a square root, Transact-SQL provides the SQRT()


function. Its syntax is:

SQRT(Expression)

This function takes one argument as a positive decimal number. If the number
is positive, after the calculation, the function returns the square root of x. Here
is an example:

DECLARE @Number As Decimal(6, 2);

SET @Number = 48.16;

SELECT SQRT(@Number) AS [The square root of 48.16 is];

GO

This would produce:


Database Creation With Code 143

If the number is negative, you would receive an error. Here is an example:

In this case, you can use a control statement to find out whether the
Expression is positive. Here is an example:

DECLARE @Number As Decimal(6, 2);


Database Creation With Code 144

SET @Number = 258.4062;

IF SIGN(@Number) > 0

PRINT 'The square root of 258.4062 is ' +

CONVERT(varchar(12), SQRT(@Number));

ELSE

PRINT 'You must provide a positive number';

GO

Here is one example of executing the statement:

Here is another example of executing the statement:


Database Creation With Code 145

Measure-Based Functions

A circle is a series of distinct opposite points positioned each at an exact same


distance from another point referred to as the center. The distance from the
center C to one of these equidistant points is called the radius, R. The line that
connects all of the points that are equidistant to the center is called the
circumference of the circle. The diameter is the distance between two points of
the circumference to the center. In other words, a diameter is double the
radius.

To manage the measurements and other related operations, the circumference


Database Creation With Code 146

is divided into 360 portions. Each of these portions is called a degree. The unit
used to represent the degree is the degree, written as ˚. Therefore, a circle
contains 360 degrees, that is 360˚. The measurement of two points A and D of
the circumference could have 15 portions of the circumference. In this case,
this measurement would be represents as 15˚.

The distance between two equidistant points A and B is a round shape


geometrically defined as an arc. An angle is the ratio of the distance between
two points A and B of the circumference divided by the radius R. This can be
written as:

PI

dThe letter п, also written as PI, is a number used in various mathematical


calculations. Its approximate value is 3.1415926535897932. The calculator of
Microsoft Windows represents it as 3.1415926535897932384626433832795.
To get the value of PI, Transact-SQL provides the PI() function. Its syntax is
simply:

PI()

Radians

An angle is the ratio of an arc over the radius. Because an angle is a ratio and
not a ―physical‖ measurement, which means an angle is not a dimension, it is
independent of the size of a circle. Obviously the angle represents the number
of portions covered by three points. A better unit used to measure an angle is
the radian or rad.

If you know the value of an angle in degrees and you want to get the radians,
Transact-SQL provides the RADIANS() function. Its syntax is:

RADIANS(Expression)

This function takes as argument a value in degrees. If it succeeds in its


Database Creation With Code 147

calculation, it returns the radians value.

A cycle is a measurement of the rotation around the circle. Since the rotation is
not necessarily complete, depending on the scenario, a measure is made based
on the angle that was covered during the rotation. A cycle could cover part of
the circle, in which case the rotation would not have been completed. A cycle
could also cover the whole 360˚ of the circle and continue there after. A cycle
is equivalent to the radian divided by 2 * Pi.

Degrees

If you know the radians but want to get the degrees of an angle, you can use
the DEGREES() function. Its syntax is:

DEGREES(Expression)

This function takes as argument a value in radians. If it succeeds, it returns the


equivalent value in degrees.

Trigonometric Functions

The Cosine of a Value

Consider AB the length of A to B, also referred to as the hypotenuse. Also


consider AC the length of A to C which is the side adjacent to point A. The
cosine of the angle at point A is the ratio AC/AB. That is, the ratio of the
adjacent length, AC, over the length of the hypotenuse, AB:

The returned value, the ratio, is a double-precision number between –1 and 1.

To get the cosine of an angle, you can call the COS() function. Its syntax is:

COS(Expression)

The angle to be considered is passed as the argument to this function. The


function then calculates and returns its cosine. Here is an example:
Database Creation With Code 148

DECLARE @Angle As Decimal(6, 3);

SET @Angle = 270;

SELECT COS(@Angle) AS [Cosine of 270];

GO

This would produce:

The Sine of a Value

Consider AB the length of A to B, also called the hypotenuse to point A. Also


consider CB the length of C to B, which is the opposite side to point A. The sine
represents the ratio of CB/AB; that is, the ratio of the opposite side, CB over
the hypotenuse AB.

To get the sine of an angle, you can use the SIN() function whose syntax is:

SIN(Expression)

The angle to be considered is passed as the argument. After its calculation, the
Database Creation With Code 149

function returns the sine of the angle between –1 and 1.

Here is an example:

DECLARE @Angle As Decimal(6, 3);

SET @Angle = 270;

SELECT SIN(@Angle) AS [Sine of 270];

GO

This would produce:

The Tangent of a Value

In geometry, consider AC the length of A to C. Also consider BC the length of B


to C. The tangent is the result of BC/AC, that is, the ratio of BC over AC.

To get the tangent of an angle, you can use the TAN() function of Transact-
SQL. Its syntax is:
Database Creation With Code 150

TAN(Expression)

Here is an example:

DECLARE @Angle As Decimal(6, 3);

SET @Angle = 270;

SELECT TAN(@Angle) AS [Tangent of 270];

GO

This would produce:

Date and Time Based Functions

Date and time values are highly used in database applications. They involve
sales, time sheets, taxes, overtime work, etc. Based on this usefulness, their
operations are supported by various libraries you will be using when developing
your application. Without being the most elaborate on this issue, Transact-SQL
provides its own level of support for date and time values.

Before using a date or a time value in a calculation, remember that you must
first get it one way or another. You can define a date or a time constant in your
application. An example would be '1992/10/28'. You can declare a DateTime or
Database Creation With Code 151

a SmallDateTime variable and initialize it as you see fit. You may get a date or
a time from another function. As the last alternative, you may get a date or
time from another application or from a user. Once you have an appropriate
date, you can use it.

The Current System Date and/or Time

One of the ways you can assist the user with date and time is to get the current
date or the current time. For example, if you create a time sheet, when the
user starts using it, it would be convenient to fill part of the time sheet with
such predictable values.

To get the current date and the current time of the computer that a user is
using, you can use the GETDATE() function of Transact-SQL. Its syntax is:

GETDATE()

This function simply returns the current date and time of the operating system.

Date/Time Addition

One of the primary operations you may want to perform on a date or a time
value would consist of adding a value to it. To support this operation, Transact-
SQL provides the DATEADD() function. Its syntax is:

DATEADD(TypeOfValue, ValueToAdd, DateOrTimeReferenced)

The third argument to this function is the value of a date or a time on which the
operation will be performed. It can be a constant value in the form of
'year/month/day' for a date or 'hour:minutes AM/PM' for a time.

The second argument is the value that will be added. It should be a constant
integer, such as 8, or a floating point value, such as 4.06.

When calling this function, you must first specify the type of value that you
want to add. This type is passed as the first argument. It is used as follows:

If you want to add a number of years to a date, specify the TypeOfValue as


Year or yy, or yyyy (remember that SQL is case-insensitive). Here is an
example:

DECLARE @Anniversary As DateTime;

SET @Anniversary = '2002/10/02';

SELECT DATEADD(yy, 4, @Anniversary) AS Anniversary;

GO
Database Creation With Code 152

If you want to add a number of quarters of a year to a date, specify the


TypeOfValue as Quarter or d, or qq. Here is an example:

DECLARE @NextVacation As DateTime;

SET @NextVacation = '2002/10/02';

SELECT DATEADD(Quarter, 2, @NextVacation) AS [Next Vacation];

GO
Database Creation With Code 153

If you want to add a number of months to a date, specify the TypeOfValue as


Month or m, or mm. The following example adds 5 months to its date:

DECLARE @SchoolStart As DateTime;

SET @SchoolStart = '2004/05/12';

SELECT DATEADD(m, 5, @SchoolStart) AS [School Start];

GO
Database Creation With Code 154

In the same way, you can add values as follows:

Type of
Abbreviation As a result
Value

yy
Year A number of years will be added to the date value
yyyy

q
A number of quarters of a year will be added to the date
quarter
value
qq

m
Month A number of months will be added to the date value
mm

y
dayofy
A number of days of a year will be added to the date value
ear
dy

d
Day A number of days will be added to the date value
dd

Week wk A number of weeks will be added to the date value


Database Creation With Code 155

ww

Hour hh A number of hours will be added to the time value

n
minute A number of minutes will be added to the time value
mi

s
second A number of seconds will be added to the time value
ss

millisec
ms A number of milliseconds will be added to the time value
ond

Date/Time Subtraction

Another regular operation performed on a date or a time value consists of


getting the number of units that has elapsed in the range of two dates or two
time values. To support this operation, Transact-SQL provides the DATEDIFF()
function. Its syntax is:

DATEDIFF(TypeOfValue, StartDate, EndDate)

This function takes three arguments. The second argument is the starting date
or the starting time of the range to be considered. The third argument is the
end or last date or time of the considered range. You use the first argument to
specify the type of value you want the function to produce. This argument uses
the same value as those of the DATEADD() function:

Type of
Abbreviation As a result
Value

yy
The function will return the number of years that have
Year
elapsed between the start and the end dates
yyyy

q
The function will return the number of quarters of a year
quarter
that have elapsed between the start and the end dates
qq

m
The function will return the number of months that have
Month
elapsed between the start and the end dates
mm
Database Creation With Code 156

y
dayofy The function will return the number of days of a year that
ear have elapsed between the start and the end dates
dy

d
The function will return the number of days that have
Day
elapsed between the start and the end dates
dd

wk
The function will return the number of weeks that have
Week
elapsed between the start and the end dates
ww

The function will return the number of hours that have


Hour hh
elapsed between the start and the end times or dates

n
The function will return the number of minutes that have
minute
elapsed between the start and the end times or dates
mi

s
The function will return the number of seconds that have
second
elapsed between the start and the end times or dates
ss

millisec The function will return the number of milliseconds that


ms
ond have elapsed between the start and the end times or dates

Here is an example that calculates the number of years that an employees has
been with the company:

DECLARE @DateHired As DateTime,;

@CurrentDate As DateTime;

SET @DateHired = '1996/10/04';

SET @CurrentDate = GETDATE();

SELECT DATEDIFF(year, @DateHired, @CurrentDate)

AS [Current Experience];

GO

This would produce:


Database Creation With Code 157

Practical Learning: Ending the Lesson

Close the query window without saving the file

In the Object Explorer, under the Databases node, right-click Exercise1 and
click Delete

In the dialog box, click OK

The Tables of a Database

Tables Fundamentals
Database Creation With Code 158

A table is primarily a list of items or a group of lists. To manage such a list, it should be
meticulously organized. To organize this information, it is divided in sections. Here is
an example:

Name Age Gender Relationship

Judie 18 Female Sister

Ernest 24 Male Cousin

Bill 52 Unknown Uncle

David 36 Male Brother

Hermine 12 Unknown Niece

Based on this, a list is simply an arrangement of information and this information, also
called data, is stored in tables.

Practical Learning: Starting a Database

Start Microsoft SQL Server, connect and, in the Object Explorer, expand the
Databases node

In the Object Explorer, right-click Databases and click New Database...

In the New Database dialog box, set the Database Name to BCR

Under Initial Size (MB) column, set the size of the PRIMARY filegroup to 10

Click OK

Visual Creation of a Table

The information of a table is organized in categories called columns and


horizontal arrangements called records or rows. A column holds a category of
data that is common to all records. A table must have at least one column. This
means that you cannot create a table without defining at least one column.
Database Creation With Code 159

Practical Learning: Creating a Table

In the Object Explorer, expand the BCR node (click its + button)

Under BCR, right-click Tables and click New Table...

As the cursor is blinking in the first empty field under the Column Name column,
type name and press Enter

Tables Names

To complete the creation of a table, you must save it. If you are freshly creating
a table and decide to save it, you would be prompted to name it. The name of a
table:

Can be made of digits only. For example you can have a table called 148

Can start with a digit, a letter, or an underscore

Can be made of letters, digits, and spaces

Besides these rules, you can make up yours. To avoid confusion, here are the
rules we will use to name our tables:

A name will start with a letter. Examples are act or Second


Database Creation With Code 160

After the first character as an underscore or a letter, the name will have
combinations of underscores, letters, and digits. Examples are _n24, act_52_t

Unless stated otherwise, a name will not include special characters such as !, @,
#, $, %, ^, &, or *

If the name is a combination of words, each word will start in uppercase.


Examples are Staff Members or Video Titles

Practical Learning: Naming a Table

To save your table, on the Standard toolbar, click the Save button

In the Choose Name dialog box, type Employees and press Enter

After saving the table, close it by clicking its system Close button

Creating a Table With SQL

In SQL, to create a table, you start with the following statement:

CREATE TABLE TableName;

The CREATE TABLE expression is required. The TableName factor specifies the
name of the new table. The TableName can use the rules and suggestions we
reviewed for the tables.

After specifying the name of the table, you must create at least one category,
called a column of data.

Using Sample Code

To assist you with creating a table, Microsoft SQL Server can generate sample
code for you. You can then simply modify or customize it. First display or open
an empty query window. To display the Templates Explorer, on the main menu,
you can click View -> Templates Explorer. In the Templates Explorer, expand
the Table node. Under table, drag Create Table and drop it in the query window.
Sample code would be generated for you.
Database Creation With Code 161

Tables Maintenance

Table maintenance consists of reviewing or changing its aspects. This includes


reviewing the list of tables of a database, renaming a table, or deleting it.

Viewing the Properties of a Table

Like every other object of a database or of the computer, a table possesses


some characteristics that are proper to it. To view these characteristics, in the
Object Explorer, right-click the table and click Properties.

Opening a Table

Most operations require that you open a table before using it. There are various
ways a table displays, depending on how you want to examine it:

To view the structure of a table, perhaps to change its columns, in the Object
Explorer, expand your database and its Tables node. Right-click the table and
click Modify. The table would open in design view, the same view you use to
visually create a table.

If you want to view the SQL code of a table, in the Object Explorer, right-click
the table, position the mouse on Script Table AS, CREATE To, and click New
Query Editor Window

To open a table to view its data, perhaps to perform data entry, in the Object
Explorer, right-click the table and click Open Table

Tables Review

To see the list of tables of a database in the Object Explorer, you can click the
Tables:
Database Creation With Code 162

To see the list of tables of a database using SQL, in a Query window, specify the
database (using a USE statement), and execute sp_help (it is a stored
procedure). Here is an example:
Database Creation With Code 163

Renaming a Table

If you find out that the name of a table is not appropriate, you can change it. To
change the name of a table in the SQL Server Management Studio, in the Object
Explorer, right-click the table and click Rename. Type the desired name and
press Enter.

To change the name of a table with code, execute sp_rename, followed by the
current name of the table, a comma, and the new desired name of the table.
The formula to use is:

sp_rename ExistingTableName, TableNewName;

The names of tables should be included in single-quotes. Here is an example:

sp_rename 'StaffMembers', 'Employees';

GO

In this case, the interpreter would look for a table named StaffMembers in the
current or selected database. If it find it, it would rename it Employees. If the
table doesn't exist, you would receive an error.
Database Creation With Code 164

Deleting a Table

If you have an undesired table in a database, you can remove it. To delete a
table in the SQL Server Management Studio, in the Object Explorer, right-click
the table under its database node and click Delete. You will receive a warning
giving you a chance to confirm your intentions. If you still want to remove the
table, click OK.

To delete a table using SQL, use the following formula:

DROP TABLE TableName

The DROP TABLE expression is required and it is followed by the name of the
undesired table. When you execute the statement, you will not receive a
warning before the table is deleted.

You can also use sample code that Microsoft SQL Server can generate for you.
First display an empty query window. Also display the Templates Explorer and
expand the Table node. Under Table, drag Drop Table and drop it in the empty
query window. Sample code would be generated for you. You can then simply
modify it and execute the statement.

Referring to a Table

In future lessons, we will write various expressions that involve the names of
tables. In those expressions, you will need to specify a particular table you want
to use. There are three main ways you can do this. To refer to, or to indicate, a
table:

You can simply type its name. An example would be Students

You can type dbo, followed by the period operator, followed by the name of the
table. An example would be dbo.Students

dYou can type the name of the database to which the table belongs, followed by
the period operator, followed by dbo, followed by the period operator, and
followed by the name of the table. An example would be
RedOakHighSchool.dbo.Students

The Columns of a Table

In our to tables, we saw that a list could be organized in categories called


columns. Here is the example we saw:

Name Age Gender Relationship


Database Creation With Code 165

Judie 18 Female Sister

Ernest 24 Male Cousin

Bill 52 Unknown Uncle

David 36 Male Brother

Hermine 12 Unknown Niece

As you can see from this arrangement, a column is used to particularly classify
one type of data. For example, one column can be used to list some names.
Another column can be used to list numbers. Yet another column can be used
for a select list of items that keep repeating those items.

To organize the information that a column holds, a table needs a series of


details about each column. Two aspects are particularly important: a name and
the type of data that a column should/must/can hold.

The Name of a Column

To be able to recognize the categories of information that a column holds, the


column should have a name. In Microsoft SQL Server, the name of a column
displays in the top, the header part, of the column. The name of a column
allows the database as a file to identify the column. The name of a column also
will help you, the database developer, to identify that column. There are rules
and suggestions you must or should follow when naming the columns of a table.

The name of a column:

Can start with a letter, a digit, or an underscore

Can include letters, digits, and spaces in any combination

After respecting these rules, you can add your own rules. In our lessons, here
are the rules we will use to name our columns:

A name will start with a letter. Examples are n, act, or Second

After the first character as an underscore or a letter, the name will have
dcombinations of underscores, letters, and digits. Examples are n24 or col_52_t

Unless specified otherwise, a name will not include special characters such as !,
@, #, $, %, ^, &, or *

If the name is a combination of words, each word will start in uppercase.


Examples are Date Hired, LastName, Drivers License Number, or EmailAddress
Database Creation With Code 166

Practical Learning: Setting Columns Names

Under the Column Name column, double-click name to highlight it

Type FirstName to replace it and press Enter

The Types of Data

After deciding on the name of a column, the database needs to know what kind
of information the column would hold. Since there are various kinds of
information a database can deal with, we saw in Lesson 3 the types of data that
Microsoft SQL Server supported. Therefore, you must specify the data type that
is necessary for a particular column.

Practical Learning: Setting Data Types

Click the arrow of the combo box under the Data Type column

Scroll down and select varchar from the list


Database Creation With Code 167

Click the first empty field under FirstName and type MI

Press the down arrow key to position the cursor under MI

Type LastName and press the down arrow key

Type DateHired

Press Tab and type d

Notice that the datetime data type is selected.

Press Enter three times to position the mouse cursor under DateHired

Type EmployeeNumber and press the down arrow key

Complete the table as follows:

Save the table

The Length of Data

A database deals with various types of data, appropriate or not for certain
fields. This means that you should take care of jobs behind the scenes as much
as you can. One way you can do this is by controlling the amount of information
that can be stored in a particular field. As various columns can hold different
Database Creation With Code 168

types of data, so can the same data type control its own mechanism of internal
data entry. The length of data means different things to different fields.
Columns that carry the same data type can have different lengths.

Bit Fields: We saw already that a bit column type is meant for one of two
answers. The user is supposed to simply let the database know that the answer
is yes or no, true or false, on or off, 1 or 0. Therefore, the only length of this
field is 1.

Integers: The length of an integer is the number of bytes its field can hold. For
an int type, that would be 4 bytes.

Decimal and Floating-Point Numbers: The Length specifies how many bytes the
field can store.

Strings: The Length of a character or string column specifies the maximum


number of characters that the field can hold.

In some circumstances, you will need to change or specify the length as it


applies to a particular field. For example, since you should use the varchar data
type for a string field whose content will change from one record to another, not
all varchar columns need to have the same length. Although a First Name and a
Book Title columns should use the varchar type, both columns would not have
the same length of entries. As it happens, people hardly have a first name that
is beyond 20 characters and many book titles go beyond 32 characters. In this
case, both fields would use the same data type but different lengths. On the
other hand, for columns of datetime and money data types, you should accept
the default length suggested by the database.

There are two ways you can change the length of a string-based column:

In the top section of the windo, to change the length of the field, in the
parentheses of the data type, enter the desired value

In the top section of the window, click the name of the column. In the bottom
section, click the Length field and type the desired value

Practical Learning: Setting Data Types

In the top section, click EmployeeNumber to select it

In the bottom section, click Length and type 6

In the top section of the table, click Address and press Tab

For the data type, type VARCHAR(100)

In the same way, complete the table as follows:


Database Creation With Code 169

Save the table

Programmatic Creation of Columns

We saw that the primary formula to create a table was:

CREATE TABLE TableName

After specifying the name of the table, you must list the columns of the table.
The list of columns starts with an opening parenthesis "(". The list ends with a
closing parenthesis ")". Each column must be separated from the next with a
comma, except for the last column. You can include all columns on the same
line if possible as follows:

CREATE TABLE Country(Column1, Column2, Column3)

Alternatively, to make your statement easier to read, you should create each
column on its own line as follows:

CREATE TABLE Country(

Column1,

Column2,

Column3);
Database Creation With Code 170

There are two primary pieces of information you must specify for each column:
its name and its type. Therefore, the syntax of creating a column is:

ColumnName DataType Options

The name of a column should follow the same rules and suggestions we
reviewed for the columns.

After typing the name of the column, type the desired or appropriate data type
for the column. For this example, use one of the (appropriate) data types we
reviewed.

Remember that some of the data types need to have a length. This is certainly
true for all string or text-based columns (char, text, varchar, etc). In the case of
text-based columns, when using SQL to create your columns, because it is less
visual than the table design of the SQL Server Management Studio, you cannot
rely on the default length of strings suggested by SQL (in fact, in MySQL, you
must specify a length for varchar). As it happens, the SQL Server Management
Studion specifies different default values for text-based columns. Therefore,
when using SQL to create your columns, you should (strongly) specify your own
default length for text-based columns.

We also saw that you could use sample code to create a table. This allows you
to have more control over the various columns you want the table to have. To
do this, open an empty query window and display the Templates Explorer.
Expand the Table node. Under Table, you can drag Create Table, Add Column,
or Drop Column, and drop it in the query window. If you use dropped Add
Column or Drop Column, you can delete the undesired sections of the code and
isolate only the part that handles table creation. Here is an example:

--
=================================================
=========================

-- Add column template

--

-- This template creates a table, then it adds a new column to the table.

--
=================================================
=========================

USE <database, sysname, AdventureWorks>

GO
Database Creation With Code 171

CREATE TABLE <schema_name, sysname, dbo>.<table_name, sysname,


sample_table>

column1 int,

column2 char(10)

GO

Practical Learning: Creating a Table Using the SQL Query Analyzer

In the Object Explorer, right-click BCR and click New Query

In the code editor, type the following:

CREATE TABLE Customers (

DrvLicNbr VarChar(50),

DateIssued DateTime,

DateExpired DateTime,

FullName varchar(120),

Address VARCHAR(120),

City varchar(50),

State varchar(100),

PostalCode varchar(20),

HomePhone varchar(20),

OrganDonor bit)

GO

To execute the statement, press F5

Close the SQL Query Analyzer window

When asked whether you want to save the text, click Yes

Type Customers as the name of the file and press Enter


Database Creation With Code 172

Referring to a Column

Introducion

We will write many expressions that include the names of columns. In such
expressions, you will need to indicate the particular column you are referring to.
There are various ways you can do this. To refer to, or to indicate, a table:

You must type the name of the table to which the column belongs, followed by
the period operator, followed by the name of the column. An example would be
Employee.LastName

You can type dbo, followed by the period operator, followed by the name of the
table to which the column belongs, followed by the period operator, followed by
the name of the column. An example would be dbo.Employee.LastName

You can type the name of the database that owns the table's column, followed
by the period operator, followed by dbo, followed by the period operator,
followed by the name of the table to which the column belongs, followed by the
period operator, followed by the name of the column. An example would be
RedOakHighSchool.dbo.Employee.LastName

Using the Alias Name of a Table

You can create an alias name of a table to use in an expression that involves a
column. To do this, type a letter or a word that will represent the table to which
the column belongs. The letter or the word is followed by a period operator, and
followed by the name of the column. An example would be empl.LastName. At
the end of the statement, you must type the name of the table, followed by
space, and followed by the letter or the word. An example would be Employee
empl.

Columns Maintenance

Column maintenance consists of reviewing or changing any of its aspects. This


includes reviewing the structure of columns of a table, renaming a column,
deleting a column, changing the data type or the nullity of a column, etc.

Column Review

To see the structure of a table in the SQL Server Management Studio, in the
Object Explorer, you can expand it:
Database Creation With Code 173

To view the columns of a table using SQL code, in a query window, execute
sp_columns followed by the name of the table the columns belong to. Here is an
example:

This action displays the list of columns in the COLUMN_NAME column and other
characteristics on the right columns.

The Properties of a Column

A column on a table controls what kind of data is appropriate for that particular
column. The characteristics that identify or describe such a table are defined as
its properties. As we have seen previously, three primary properties are
particularly important and required for each column: the name, the data type,
and the length. Besides these, some other properties can be used to further
control the behavior of a particular field.

Besides the name, data type and length of a column, you can control the
columns of a table using the Columns property sheet in the lower section of the
Database Creation With Code 174

table in Design View. These properties sometimes depend on the data type of
the column. Therefore, to specify the properties of a column, you must first
select it in the upper section of the table. This selection can be done by just
clicking either the name, the data type, or the length of the column. Then you
can either press F6 or click the first field in the lower section, select the desired
property and type the necessary value:

Description

Description: Common and enabled for all fields, the description is used for a
sentence that describes the column. You can type anything on that field.
Database Creation With Code 175

Collation

Because different languages use different mechanisms in their alphabetic


characters, this can affect the way some sort algorithms or queries are
performed on data, you can ask the database to apply a certain language
mechanism to the field by changing the Collation property. Otherwise, you
should accept the default specified by the table.

To specify the collation of a column when creating in, type COLLATE, followed
by the desired collation code. Here is an example:

CREATE TABLE Customers(

FullName varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS

);

Modifying a Column

When making a change on a column, you are also said to alter the table. To
support this operation, SQL starts with the following formula:

ALTER TABLE TableName

When using this statement, the ALTER TABLE expression is required and it is
followed by the name of the table.

Adding a New Column

dAfter a table has already been created, you can still add a new column to it.

To add a new column in SQL Server Management Studio, first right-click the
table and click Design Table. To add a new column to the end of the table, click
the first empty field under Column Name, type a name, and specify the other
options.

To insert a new column between two existing one, right-click the column that
will succeed it and click Insert Column:
Database Creation With Code 176

This would create a new empty field. Type the desired name and specify the
other options.

In SQL, the basic formula to add a new column to an existing table is:

ALTER TABLE TableName

ADD ColumnName Properties

The ColumnName factor is required. In fact, on the right side of the ADD
keyword, define the column by its name and using all the options we reviewed
for columns.

Here is an example:

ALTER TABLE StaffMembers

ADD Address varchar(100) NULL

GO

When this code is executed, a new column name Address, of type varchar, with
a limit of 100 characters, and that allow empty entry, will be added to a table
named StaffMembers in the current database.

You can also use sample code to add a new column to a table. First display an
empty query window and display the Templates Explorer. Expand the Table
node. Under Table, drag Add Column and drop it in the query window. Delete
the undesired sections of code and keep only the part that deals with adding a
column. Here is an example:

--
=================================================
=========================
Database Creation With Code 177

-- Add column template

--

-- This template creates a table, then it adds a new column to the table.

--
=================================================
=========================

USE <database, sysname, AdventureWorks>

GO

-- Add a new column to the table

ALTER TABLE <schema_name, sysname, dbo>.<table_name, sysname,


sample_table>

ADD <new_column_name, sysname, column3>

<new_column_datatype,, datetime>

<new_column_nullability,, NULL>

GO

Renaming a Column

If you find out that the name of a column is not appropriate, you can change it.
To rename a column in the Object Explorer, right-click the table that the column
belongs to and click Modify. In the design view, highlight the name of the
desired column to put it into edit mode and edit it.

In SQL, to change the name of a column, first open an empty query window. In
a query window, execute sp_rename using the following formula:

sp_rename 'TableName.ColumnName', 'NewColumnName', 'COLUMN'

The sp_rename factor and the 'COLUMN' string are required. The TableName
factor is the name of the table that the column belongs to. The ColumnName is
the current name of the column. The NewColumnName is the desired name you
want to give to the column.

Here is an example:

sp_rename 'StaffMembers.FullName', 'EmployeeName', 'COLUMN'

GO
Database Creation With Code 178

When this code is executed, the interpreter will look for a column named
FullName in the StaffMembers table of the current or selected database. If it
finds that column in the table, then it renames it EmployeeName.

Deleting a Column

If you have an undesired column that you don't want anymore in a table, you
can remove it. To visually delete a column, in the Object Explorer, expand the
database, the Tables, and the Columns nodes. Right-click the undesired column
and click Delete. The Delete Object dialog box would display. If you still want to
delete the column, click OK. To change your mind, click Cancel.

To delete a column using code, first open or access an empty query window,
and use the following formula:

ALTER TABLE TableName

DROP COLUMN ColumnName

On the right side of the ALTER TABLE expression, type the name of the table.
On the right side of the DROP COLUMN expression, enter the name of the
undesired column. Here is an example:

ALTER TABLE StaffMembers

DROP COLUMN CurrentResidence;

GO

When this code is executed, the interpreter will look for a column named
CurrentResidence in a table StaffMembers of the current or selected database.
If it finds that column, it will remove it from the table.

Microsoft SQL Server can also generate sample code you can use to delete a
column from a table. Before doing this, first display an empty query window and
display the Templates Explorer. Expand the Table node. In the Table section,
drag Drop Column and drop it in the query window. Delete the undesired
sections of code and keep only the part that deals with adding a column. Here is
an example:

--============================================

-- Drop column template

--

-- This template creates a table, then it

-- drops one of the columns of the table.

--============================================
Database Creation With Code 179

USE <database, sysname, AdventureWorks>

GO

-- Drop a column from the table

ALTER TABLE <schema_name, sysname, dbo>.<table_name, sysname,


sample_table>

DROP COLUMN <new_column_name, sysname, column3>

GO

Practical Learning: Ending the Lesson

Close the query window without saving the file

In the Object Explorer, under the Databases node, right-click BCR and click
Delete

In the dialog box, click OK

Records Fundamentals

A table is an object that holds the information of a database. Because a table is the
central part of a database, the information it holds must be meticulously organized. To
better manage its information, data of a table is arranged in a series of fields called
cells. Once a table contains information, you can review it using either SQL Server
Management Studio or an external application.

The tables of a database display in the Object Explorer under their database node. To
open a table, you can right-click it and click click Open Table.

Table Data Navigation in the SQL Server Management Studio

Data Navigation consists of displaying and viewing data. Because information of a


database is stored in tables, your primary means of viewing data consists of opening a
Database Creation With Code 180

table in a view that displays its information.

When a table displays its records, you navigate through its fields using the mouse
or the keyboard. With the mouse, to get to any cell, you can just click it. To
navigate through records using the keyboard, you can press:

The right arrow key to move to the right cell; if the caret is already in the most
right cell, it would be moved to the first cell of the next record, up to the last
empty cell of the first empty record

The left arrow key to move to the previous cell; if the caret is in, or reaches, the
most left cell of the first record, nothing would happen when you press the the left
arrow key

The down arrow key to move to the cell under the current one; if the caret is
already in the last cell of the current column, nothing would happen

The up arrow key to move to the cell just above the current one; if the caret is
already in the first cell of the current column, nothing would happen

The Page Down to move to the next group of cell that would correspond to the next
page; if the number of records is less than a complete page, the caret would move
to the last cell of the current column

The Page Up to move to the next group of cell that would correspond to the next
page; if the number of records is less than a complete page, the caret would move
to the first cell of the current column

Visual Data Entry

As you are probably aware already, columns are used to organize data by
categories. Each column has a series of fields under the column header. One of the
actual purposes of a table is to display data that is available for each field under a
particular column. Data entry consists of providing the necessary values of the
fields of a table. Data is entered into a field and every time this is done, the
database creates a row of data. This row is called a record. This means that
entering data also self-creates rows.

There are four main ways you can perform data entry for a Microsoft SQL Server
table:

You can use a table from the Object Explorer


Database Creation With Code 181

You can enter data by typing code in a query window

You can import data from another object or another database

You can use an external application such as Microsoft Access, Microsoft Visual
Basic, Borland C++ Builder, Microsoft Visual C++, Borland Delphi, Microsoft Visual
Basic, C#, Visual C#, J#, etc.

Using the SQL Server Management Studio

Probably the easiest and fastest way to enter data into a table is by using SQL
Server Management Studio. Of course, you must first open the desired table from
an available database. In the Object Explorer, after expanding the Databases and
the Tables nodes, open a table for data entry. If the table does not contain data, it
would appear with one empty row. If some records were entered already, their
rows would show and the table would provide an empty row at the end, expecting
a new record.

To perform data entry on a table, you can click in a field. Each column has a title,
called a caption, on top. This gray section on top is called a column header. In SQL
Server, it displays the actual name of the column. You refer to the column header
to know what kind of data should/must go in a field under a particular column. This
is why you should design your columns meticulously. After identifying a column,
you can type a value. Except for text-based columns, a field can accept or reject a
value if the value does not conform to the data type that was set for the column.
This means that in some circumstances, you may have to provide some or more
explicit information to the user.

Practical Learning: Introducing Data Entry

Start Microsoft SQL Server, select the appropriate options in the Connect To Server
dialog box and connect to the server

Right-click the server name and click New Query

To create a new database, in the empty window, type the following:

CREATE DATABASE WorldStatistics;

GO

USE WorldStatistics;

GO

CREATE TABLE Countries(

[Country Name] VARCHAR(80),


Database Creation With Code 182

Area INT,

Population BIGINT,

Capital VARCHAR(50),

[Internet Code] char(2)

);

GO

To execute the SQL statement, press F5

Close the query window

When asked whether you want to save it, click Yes

Type Countries and click Save

In the Object Explorer, right-click the Databases node and click Refresh. Expand
the Databases node.
Under Databases, expand WorldStatistics and expand Tables

If you don't see a table named Countries, right-click the Tables node and click
Refresh.
Right-click Countries and click Open Table, position the mouse on Open Table and
click Return All rows

As the cursor is positioned in the first empty field under CountryName, type Cote
d'Ivoire and press Enter

Type 322460 for the area and press Tab

Type 16,393,221 and press Enter

Notice that you receive an error because the commas are not allowed:

Click OK on the error message box.

Change the value to 16393221 People and press Tab


Database Creation With Code 183

Notice that you receive another error because the column is configured for a
natural number and not a string

Click OK on the error message box and delete People

Under Internet Code, type ci and press Enter

Click the field under Capital, type Yamoussoukro and press Enter twice

Complete the table as follows:

Country Name Area Population Capital Internet Code

Cote d'Ivoire 322460 16393221 Yamoussoukro ci

Panama 78200 3191319 Panama pa

Australia 7686850 20264082 Canberra au

Canada 9984670 33098932 Ottawa ca

Iran 1648000 68688433 Tehran ir

Close the table

Data Entry With SQL

In the SQL, data entry is performed using the INSERT combined with the VALUES
keywords. The primary statement uses the following syntax:

INSERT TableName VALUES(Column1, Column2, Column_n);

Alternatively, or to be more precise, you can use the INTO keyword between the
INSERT keyword and the TableName factor to specify that you are entering data in
the table. This is done with the following syntax:

INSERT INTO TableName VALUES(Column1, Column2, Column_n)

The TableName factor must be a valid name of an existing table in the database
you are usindg. If the name is wrong, the SQL interpreter would simply consider
that the table you are referring to doesn't exist. Consequently, you would receive
an error.

The VALUES keyword indicates that you are ready to list the values of the columns.
The values of the columns must be included in parentheses.
Database Creation With Code 184

If the column is a BIT data type, you must specify one of its values as 0 or 1.

If the column is a numeric type, you should pay attention to the number you type.
If the column was configured to receive an integer (int, bigint, smallint), you
should provide a valid natural number without the decimal separator.

If the column is for a decimal number (float, real, decimal, numeric), you can type
the value with its character separator (the period for US English).

If the column was created for a date data type, make sure you provide a valid
date.

If the data type of a column is a string type, you should include its entry between
single quotes. For example, a shelf number can be specified as 'HHR-604' and a
middle initial can be given as 'D'.

In the previous paragraphs, we were stating "you" as if you will be the one
performing data entry. In reality, the user will be performing data entry on
your products. Therefore, it is your responsibility to reduce, as much as
possible, the likelihood of mistakes. Of course, there are various ways,
through a "visual" application such as Borland C++ Builder, Microsoft Visual
Basic, C#, or MS Visual C++, etc, that you can take care of this.

Adjacent Data Entry

The most common technique of performing data entry requires that you know the
sequence of fields of the table in which you want to enter data. With this
subsequent list in mind, enter the value of each field in its correct position.

During data entry on adjacent fields, if you don't have a value for a numeric field,
you should type 0 as its value. For a string field whose data you don't have and
cannot provide, type two single-quotes '' to specify an empty field.

Practical Learning: Performing Adjacent Data Entry

To open a new query window, press Ctrl + N

In the query window, to create one record, type:

USE WorldStatistics;

GO

INSERT INTO Countries

VALUES('Angola', 1246700, 12127071, 'Luanda','ao');


Database Creation With Code 185

GO

Press F5 to execute

Delete the top section of the window

To enter various records at the same time, enter the following statement:

USE WorldStatistics;

GO

INSERT INTO Countries

VALUES('Mexico', 1972550, 107449525, 'Mexico City','mx');

GO

INSERT INTO Countries

VALUES('South Africa', 1219912, 44187637, 'Pretoria','za');

GO

INSERT INTO Countries

VALUES('Iraq', 0, 0, 'Baghdad','iq');

GO

INSERT INTO Countries

VALUES('United States', 9826630, 0, '', '');

GO

INSERT INTO Countries

VALUES('Saudi Arabia', 2149690, 0, 'Riyadh', '');

GO

Press F5 to execute the statement

Random Data Entry

The adjacent data entry we have performed requires that you know the position of
each column. The SQL provides an alternative that allows you to perform data
entry using the name of a column instead of its position. This allows you to provide
the values of columns in an order of your choice. We have just seen a few
examples where the values of some of the fields were not available during data
Database Creation With Code 186

entry. Instead of remembering to type 0 or NULL for such fields or leaving empty
quotes for a field, you can use the fields' names to specify the fields whose data
you want to provide.

To perform data entry in an order of your choice, you must provide your list of the
fields of the table. You can either use all columns or provide a list of the same
columns but in your own order. In the same way, you don't have to provide data
for all fields, just those you want, in the order you want.

Practical Learning: Performing Random Data Entry

To perform data entry, type and execute the following statement:

USE WorldStatistics;

GO

INSERT Countries([Country Name],Capital,[Internet


Code],Population,Area)

VALUES('China', 'Beijing', 'cn', 1313973713, 9596960)

GO

Press F5 to execute the statement

To perform other entries, type the following statement:

USE WorldStatistics;

GO

INSERT Countries(Capital, [Internet Code], [Country Name])

VALUES('Nouakchott', 'mr', 'Mauritania')

GO

INSERT Countries([Internet Code], Population, [Country Name])

VALUES('ro', 22303552, 'Romania')

GO

INSERT Countries(Area, [Country Name], Population)

VALUES(21040, 'El Salvador', 6822378)

GO
Database Creation With Code 187

INSERT Countries(Capital, [Country Name])

VALUES('Phnom Penh', 'Cambodia')

GO

To execute the statement, press F5

Close the query window

When asked whether you want to save it, click No

The Nullity of a Field

During data entry, users of your database will face fields that expect data.
Sometimes, for one reason or another, data will not be available for a particular
field. An example would be an MI (middle initial) field: some people have a middle
initial, some others either don't have it or would not (or cannot) provide it. This
aspect can occur for any field of your table. Therefore, you should think of a way to
deal with it.

A field is referred to as null when no data entry has been made to it:

Saying that a field is null doesn't mean that it contains 0 because 0 is a value

Saying that a field is null doesn't mean that it is empty. A field being empty could
mean that the user had deleted its content or that the field itself would not accept
what the user was trying to enter into that field, but an empty field can have a
value

A field is referred to as null if there is no way of determining the value of its


content (in reality, the computer, that is, the operating system, has its own internal
mechanism of verifying the value of a field) or its value is simply unknown. As you
can imagine, it is not a good idea to have a null field in your table. As a database
developer, it is your responsibility to always know with certainty the value held by
each field of your table.

A field is referred to as required if the user must provide a value for it before
moving to another record. In other words, the field cannot be left empty during
data entry.

To solve the problem of null and required fields, Microsoft SQL Server proposes one
of two options: allow or not allow null values on a field. For a typical table, there
are pieces of information that the user should make sure to enter; otherwise, the
data entry would not be validated. To make sure the user always fills out a certain
field before moving to the next field, that is, to require the value, if you are visually
Database Creation With Code 188

creating the table, clear the Allow Nulls check box for the field. On the other hand,
if the value of a field is not particularly important, for example if you don't intend to
involve that value in an algebraic operation, check its Allow Nulls check box.

NULL or NOT NULL?

If creating a table using SQL, to specify that it can allow null values, type NULL on
the right side of the column. To specify that the values of the column are required,
on the right side, type NOT NULL. If you don't specify NULL or NOT NULL, the
column will be created as NULL. Here are examples:

CREATE TABLE Persons

FirstName varchar(20) NULL,

LastName varchar(20) NOT NULL,

Gender smallint

);

GO

If the table was already created and it holds some values already, you cannot set
the Allow Nulls option on columns that don't have values.

Practical Learning: Applying Fields Nullity

In the Object Explorer, right-click Countries in the WorldStatistics node and click
Modify

Apply the nullity of fields as follows:

Save the table

Identity Columns
Database Creation With Code 189

One of the goals of a good table is to be able to uniquely identity each record. In
most cases, the database engine should not confuse two records. Consider the
following table:

Unit
Category Item Name Size
Price

Women Long-sleeve jersey dress Large 39.95

Iron-Free Pleated Khaki


Boys S 39.95
Pants

Men Striped long-sleeve shirt Large 59.60

Women Long-sleeve jersey dress Large 45.95

Girls Shoulder handbag 45.00

Women Continental skirt Petite 39.95

Imagine that you want to change the value of an item named Long-sleeve jersey
dress. Because you must find the item programmatically, you can start looking for
an item with that name. This table happens to have two items with that name. You
may then decide to look for an item using its category. In the Category column,
there are too many items named Women. In the same way, there are too many
records that have a Large value in the Size column, same thing problem in the Unit
Price column. This means that you don't have a good criterion you can use to
isolate the record whose Item Name is Long-sleeve shirt.

To solve the problem of uniquely identifying a record, you can create a particular
column whose main purpose is to distinguish one record from another. To assist
you with this, the SQL allows you to create a column whose data type is an integer
type but the user doesn't have to enter data for that column. A value would
automatically be entered into the field when a new record is created. This type of
column is called an identity column.

You cannot create an identity column one an existing table, only on a new table.

Visually Creating an Identity Column

To create an identity column, if you are visually working in the design view of the
table, in the top section, specify the name of the column. By tradition, the name of
this column ressembles that of the table but in singular. Also, by habit, the name of
the column ends with _id, Id, or ID.
Database Creation With Code 190

After specifyin the name of the column, set its data type to an integer-based type.
Usually, the data type used is int. In the bottom section, click and expand the
Identity Specification property. The first action you should take is to set its (Is
Identity) property from No to Yes.

Once you have set the value of the (Is Identity) property to Yes, the first time the
user performs data entry, the value of the first record would be set to 1. This
characteristic is controlled by the Identity Seed property. If you want the count to
start to a value other than 1, specify it on this property.

After the (Is Identity) property has been set to Yes, the SQL interpreter would
increment the value of each new record by 1, which is the default. This means that
the first record would have a value of 1, the second would have a value of 2, and
so on. This aspect is controlled by the Identity Increment property. If you want to
increment by more than that, you can change the value of the Identity Increment
property.

Practical Learning: Creating an Identity Column in the Design Table

In the Object Explorer, under WorldStatistics, right-click Tables and click New
Table...

Set the name of the column to ContinentID and press Tab

Set its data type to int and press F6.


In the lower section of the table, expand Identity Specification and double-click (Is
Identity) to set its value to Yes

Complete the table as follows:

Column
Data Type Allow Nulls
Name

ContinentID

Continent varchar(80) Unchecked

Area bigint

Population bigint

Save the table as Continents

Creating an Identity Column Using SQL

If you are programmatically creating a column, to indicate that it would be used as


an identity column after its name and data type, type identity followed by
Database Creation With Code 191

parentheses. Between the parentheses, enter the seed value, followed by a


comma, followed by the increment value. Here is an example:

CREATE TABLE StoreItems(

ItemID int IDENTITY(1, 1) NOT NULL,

Category varchar(50),

[Item Name] varchar(100) NOT NULL,

Size varchar(20),

[Unit Price] money);

GO

Functions and Data Entry

You can involve a function during data entry. As an example, you can call a
function that returns a value to assign that value to a column. You can first create
your own function and use it, or you can use one of the built-in functions.

Using Functions

In order to involve a function with your data entry, you must have and identity
one. You can use one of the built-in functions of Transact-SQL. You can check one
of the functions we reviewed in Lesson 7. Normally, the best way is to check the
online documentation to find out if the assignment you want to perform is already
created. Using a built-in function would space you the trouble of getting a function.
For example, imagine you have a database named AutoRepairShop and imagine it
has a table used to create repair orders for customers:

CREATE TABLE RepairOrders

RepairID int Identity(1,1) NOT NULL,

CustomerName varchar(50),

CustomerPhone varchar(20),

RepairDate DateTime

);

GO
Database Creation With Code 192

When performing data entry for this table, you can let the user enter the customer
name and phone number. On the other hand, you can assist the user by
programmatically entering the current date. To do this, you would call the
GETDATE() function. Here are examples:

INSERT INTO RepairOrders(CustomerName, CustomerPhone, RepairDate)

VALUES('Annette Berceau', '301-988-4615', GETDATE());

GO

INSERT INTO RepairOrders(CustomerPhone, CustomerName, RepairDate)

VALUES('(240) 601-3795', 'Paulino Santiago', GETDATE());

GO

INSERT INTO RepairOrders(CustomerName, RepairDate, CustomerPhone)

VALUES('Alicia Katts', GETDATE(), '(301) 527-3095');

GO

INSERT INTO RepairOrders(RepairDate, CustomerPhone, CustomerName)

VALUES(GETDATE(), '703-927-4002', 'Bertrand Nguyen');

GO

You can also involve the function in an operation, then use the result as the value
to assign to a field. You can also call a function that takes one or more arguments;
make sure you respect the rules of passing an argument to a function when calling
it.

If none of the Transact-SQL built-in functions satifies your requirements, you can
create your own, using the techniques we studied in Lesson 6.

Using Expressions For Data Entry

There are various ways you can assist the user with data entry. Besides using a
function, you can create an expression using operators such as those we reviewed
in lessons 3 and 5. You can create an expression when creating a table, whether in
the Table window or using SQL in a query window.

Visually Creating an Expression

To create an expression when visually creating a table, in the top section, specify
the column's name (only the column name is important). In the bottom section,
Database Creation With Code 193

expand the Computed Column Specification field and, in its (Formula) field, enter
the desired expression. Here is an example:

Creating a SQL Expression

You can also create an expression in SQL expression you are using to create a
table. To do this, in the placeholder of the column, enter the name of the column,
followed by AS, and followed by the desired expression. Here is an example:

CREATE TABLE Circle

CircleID int identity(1,1) NOT NULL,

Radius decimal(8, 3) NOT NULL,

Area AS Radius *Radius * PI()

);

GO

Using an Expression During Data Entry


Database Creation With Code 194

When performing data entry, you must not provide a value for a column that has
an expression; the SQL interpreter would provide the value automatically. Here is
an example of entering data for the above Circle table:

INSERT INTO Circle(Radius) VALUES(46.82);

GO

INSERT INTO Circle(Radius) VALUES(8.15);

GO

INSERT INTO Circle(Radius) VALUES(122.57);

GO

Other Features of Data Entry

Is RowGuid

This property allows you to specify that a column with the Identity property set to
Yes is used as a ROWGUID column.

Collation

Because different languages use different mechanisms in their alphabetic


characters, this can affect the way some sort algorithms or queries are performed
on data, you can ask the database to apply a certain language mechanism to the
field by changing the Collation property. Otherwise, you should accept the default
specified by the table.

Data Import

Another technique used to get data into one or more tables consists of importing
already existing data from another database or from any other recognizable data
file. Microsoft SQL Server provides various techniques and means of importing
data.

The easiest type of data that can be imported into SQL Server, and which is
available on almost all database environments, is the text file. Almost every
database environment allows you to import a text file but data from that file must
be formatted appropriately. For example, the information stored in the file must
define the columns as distinguishable by a character that serves as a separator.
This separator can be the single-quote, the double-quote, or any valid character.
Data between the quotes is considered as belonging to a distinct field. Besides this
information, the database would need to separate information from two different
columns. Again, a valid character must be used. Most databases, including
Microsoft SQL Server, recognize the comma as such a character. The last piece of
Database Creation With Code 195

information the file must provide is to distinguish each record from another. This is
easily taken car of by the end of line of a record. This is also recognized as the
carriage return.

These directives can help you manually create a text file that can be imported into
Microsoft SQL Server. In practicality, if you want to import data that resides on
another database, you can ask that application to create the source of data. Most
applications can do that and format the data. That is the case for the data we will
use in the next exercise: it is data that resided on a Microsoft Access database and
was prepared to be imported in Microsoft SQL Server.

After importing data, you should verify and possibly format it to customize its
fields.

Practical Learning: Importing Data From an External Source

Download the Students text file and save it to your hard drive

In the SQL Server Management Studio, right-click the Databases node and click
New Database...

Type ROSH and press Enter

In the Object Explorer, right-click ROSH, position the mouse on Tasks and click
Import Data

On the first page of the wizard, click Next

On the second page, click the arrow of the Data Source combo box and select Flat
File Source

On the right side of File Name, click the Browse button

Locate and select the Students.txt file you had saved


Database Creation With Code 196

Under Data Source, click Advanced

As Column is selected, in the right list, click Name and type StudentID

In the middle list, click each column and change its Name in the right column as
follows:

Column Name

Column0 StudentID

Column1 FirstName
Database Creation With Code 197

Column2 LastName

Column3 DateOfBirth

Column4 Gender

Column5 Address

Column6 City

Column7 State

Column8 ZIPCode

Column9 HomePhone

Column10 EmailAddress

Column11 ParentsNames

Column12 SPHome

Column13 EmrgName

Column14 EmrgPhone
Database Creation With Code 198

To see the list of columns, under Data Source, click Columns

Click Next 4 times

Click Finish
Database Creation With Code 199

Click Close

Back in the Object Explorer, expand the ROSH and its Tables nodes.
Right-click Students and click Design

As the StudentID field is selected, press Tab and change its data type to int

Press F6 and expand Identity Specification. Double-click (Is Identity) to set its
value to Yes

Change the other columns as follows:


Database Creation With Code 200

To save the table, click the Save button on the Standard toolbar:
Database Creation With Code 201

When a Validation Warnings dialog box presents a few warnings, click Yes

Close the table

To view data stored on the table, in the Object Explorer, right-click dbo.Students
and click Open Table

Checking Records

Checking the Existence of a Record

One of the simplest operations a user can perform on a table consists of looking for
a record. To do this, the user would open the table that contains the records and
visually check them, looking for a piece of information, such as a student's last
name.

As the database developer, you too can look for a record and there are various
techniques you can use. To assist you with this, Transact-SQL provides a function
named EXISTS. Its syntax is:

BIT EXISTS(SELECT Something)

This function takes one argument. The argument must be a SELECT statement that
would be used to get the value whose existence would be checked. For example, in
Lesson 2, we mentioned a system database names databases that contains a
record of all databases stored on your server. You can use the EXISTS() function to
check the existence of a certain database. The formula you would use is:
Database Creation With Code 202

IF EXISTS (

SELECT name

FROM sys.databases

WHERE name = N'DatabaseName'

In the DatabaseName placeholder, you can enter the name of the database.

Selecting Records

Before visually performing some operations on a table, you must first select one or
more records. In the Table window, to select one record, position the mouse on the
left button of the record and click:

To select a range of records, click the gray button of one of the records, press and
hold Shift, then click the gray button of the record at the other extreme.

To select the records in a random fashion, select one record, press and hold Ctrl,
then click the gray button of each desired record:

To select all records of a table, click the gray button on the left of the first column:
Database Creation With Code 203

To visually modify one or more records on a table, first open it (you right-click the
table in the Object Explorer and click Open Table) to view its records. Locate the
record and the field you want to work on and perform the desired operation.

Records Maintenance

Record maintenance includes viewing records, looking for one or more records,
modifying one or more records, or deleting one or more records.

To support record maintenance operations, the SQL provides the UPDATE keyword
that is used to specify the table on which you want to maintain the record(s). The
basic formula to use is:

UPDATE TableName

SET ColumnName = Expression

With this formula, you must specify the name of the involved table as the
TableName factor of our formula. The SET statement allows you to specify a new
value, Expression, for the field under the ColumnName column.

Consider the following code to create a new database named VideoCollection and to
add a table named Videos to it:

CREATE DATABASE VideoCollection;

GO

USE VideoCollection;

GO

CREATE TABLE Videos (

VideoID INT NOT NULL IDENTITY(1,1),


Database Creation With Code 204

VideoTitle varchar(120) NOT NULL,

Director varchar(100) NULL,

YearReleased SMALLINT,

VideoLength varchar(30) NULL,

Rating varchar(6)

);

GO

INSERT INTO Videos(VideoTitle, Director, YearReleased, VideoLength)

VALUES('A Few Good Men','Rob Reiner',1992,'138 Minutes');

INSERT INTO Videos(VideoTitle, Director, YearReleased, VideoLength)

VALUES('The Silence of the Lambs','Jonathan Demme',1991,'118 Minutes');

INSERT INTO Videos(VideoTitle, Director, VideoLength)

VALUES('The Distinguished Gentleman', 'James Groeling', '112 Minutes');

INSERT INTO Videos(VideoTitle, Director, VideoLength)

VALUES('The Lady Killers', 'Joel Coen & Ethan Coen', '104 Minutes');

INSERT INTO Videos(VideoTitle, Director, VideoLength)

VALUES('Ghosts of Mississippi', 'Rob Reiner', '130 Minutes');

GO

Updating all Records

Imagine that, at one time, on a particular table, all records need to receive a new
value under one particular column or certain columns. There is no particular way to
visually update all records of a table. You can just open the table to view its
records, and then change them one at a time.

In SQL, the primary formula of the UPDATE statement as introduced on our formula
can be used to update all records. Here is an example:
Database Creation With Code 205

USE VideoCollection;

GO

UPDATE Videos

SET Rating = 'R';

GO

dWith this code, all records of the Videos table will have their Rating fields set to a
value of R:

Editing a Record

Editing a record consists of changing a value in a field. It could be that the field is
empty, such as the © Year of the the 'The Lady Killers' video of the following table.
It could be that the value is wrong, such as the Director of the the 'The
Distinguished Gentleman' video of this table:

©
Video Title Director Length Rating
Year

138
A Few Good Men Rob Reiner 1992 R
Minutes

118
The Silence of the Lambs Jonathan Demme 1991
Minutes

The Distinguished 112


James Groeling R
Gentleman Minutes

Joel Coen & Ethan 104


The Lady Killers R
Coen Minutes

130
Ghosts of Mississippi Rob Reiner
Minutes
Database Creation With Code 206

To edit a record, first open the table to view its records. Locate the record, the
column on which you want to work, and locate the value you want to change, then
change it.

In SQL, you must provide a way for the interpreter to locate the record. To do this,
you would associate the WHERE operator in an UPDATE statement using the
following formula:

UPDATE TableName

SET ColumnName = Expression

WHERE Condition(s)

The WHERE operator allows you to specify how the particular record involved would
be identified. It is very important, in most cases, that the criterion used be able to
uniquely identify the record. In the above table, imagine that you ask the
interpreter to change the released year to 1996 where the director of the video is
Rob Reiner. The UPDATE statement would be written as follows:

UPDATE Videos

SET YearReleased = 1996

WHERE Director = 'Rob Reiner';

In the above table, there are at least two videos directed by Rob Reiner. When this
statement is executed, all video records whose director is Rob Reiner would be
changed, which would compromise existing records that didn't need this change.
This is where the identity column becomes valuable. We saw earlier that, when
using it with the IDENTITY feature, the interpreter appends a unique value to each
record. You can then use that value to identify a particular record because you are
certain the value is unique.

Here is an example used to specify the missing copyright year of a particular


record:

UPDATE Videos

SET YearReleased = 1996

WHERE VideoID = 5;

GO

Here is an example used to change the name of the director of a particular video:

UPDATE Videos

SET Director = 'Jonathan Lynn'


Database Creation With Code 207

WHERE VideoTitle = 'The Distinguished Gentleman';

Removing all Records

If you think all records of a particular table are, or have become, useless, you can
clear the whole table, which would still keep its structure. To delete all records from
a table, first select all of them, and press Delete. You would receive a warning:

If you still want to delete the records, click Yes. If you change your mind, click No.

Using SQL, to clear a table of all records, use the DELETE operator with the
following formula:

DELETE TableName;

When this statement is executed, all records from the TableName factor would be
removed from the table. Be careful when doing this because once the records have
been deleted, you cannot get them back.

Removing a Record

If you find out that a record is not necessary, not anymore, or is misplaced, you
can remove it from a table. To remove a record from a table, you can right-click its
gray box and click Delete. You can also first select the record and press Delete. You
would receive a warning to confirm your intention.

In SQL, to delete a record, use the DELETE FROM statement associate the WHERE
operator. The formula to follow is:

DELETE FROM TableName

WHERE Condition(s)
SUGGESTED TEXT 208

The TableName factor is used to identify a table whose record(s) would be


removed.

The Condition(s) factor allows you to identify a record or a group of records that
carries a criterion. Once again, make sure you are precise in your criteria so you
would not delete the wrong record(s).

Here is an example used to remove a particular record from the table:

DELETE FROM Videos

WHERE VideoTitle = 'The Lady Killers';

Here is an exampe used to clear the table of all videos:

DELETE FROM Videos;

Practical Learning: Ending the Lesson

Close the query window without saving the file

In the Object Explorer, under the Databases node, right-click WorldStatistics and
click Delete

In the dialog box, click OK

13. SUGGESTED TEXT

Modern Database Management (8th Edition) by Jeffrey A. Hoffer, Mary Prescott,


and Fred McFadden

Database Processing: Fundamentals, Design, and Implementation (10th Edition)


by David Kroenke

14. SAMPLE MCQS


Q 1. Database means

(a) Collection of records (b) knowledge (c)None

Q 2. DBMS means

(a)Database Management System (b)what is this (c) Nothing

(d) output device

Q 3. Database model is a
Sample MCQs 209

(a)Theory of specification (b) A history (c) Both (d) none

Q 4. In a hierarchical data model data are organized into a

(a)array (b)tree-like structure (d) Pages

Q 5. Network model is

(a) Flexible (b)Hard (c) what is this

Q 6. Relational Model is based on

(a) Predicate logic(b)set theory (c) Both (d) None

Q 7. Which model allows the user to custom data types and methods?

(a)Relational Model (b)Object Relational Model(c)None

Q 8. Which key is uniquely identify each row in table?

(a) Primary key (b) Candidate key (c) Both (d) None

Q 9. Database normalization is a design technique to certain types of

(a) Anomalies (b) Mistakes (d)None

Q 10. The criteria for 4th Normal form are:

(a)Table must be in BCNF (b)No non-trivial multivalued dependencies (c) None

Q 11. Sixth normal form has been proposed in

(a)2000 (b)2005(c) 2001 (d) none

Q 12. Referential integrity in a relational database is consistency between

(a) Two tables (b)Coupled tables (c) I do not know(d) None

Q 13. Distributed DBMS means

(a) Management of Distributed database (b) Centralized Database (c)Single


Database

Q 14. ACID stands for

Q 15. Implementing the ACID properties correctly is

(a)Simple (b)Not Simple (c)None

Вам также может понравиться