Академический Документы
Профессиональный Документы
Культура Документы
COMPILED BY:
ATEEQ HAIDER BAIG
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
1. INTRODUCTION
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
1. Hierarchical Model
2. Network Model
3. Relational Model
5. Object Model
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.
3. 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.
4. DATABASE KEYS
4.1. UNIQUE 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 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.
id_col INT,
...
...
If the primary key consists only of a single column, the column can be marked
as such using the following syntax:
...
Likewise, unique keys can be defined as part of the CREATE TABLE SQL
statement.
id_col INT,
key_col SMALLINT,
DATABASE KEYS 11
...
...
...
...
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.
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.
Likewise, foreign keys can be defined as part of the CREATE TABLE SQL
statement.
col3 INTEGER,
...
... )
If the foreign key is comprised of a single column only, the column can be
marked as such using the following syntax:
... )
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).
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.
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#}
(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
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:
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:
Finally we need to select those sets for which there is no proper subset in the
list, which are in this case:
5. DATABASE NORMALIZATION
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
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
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.
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.
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.
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.
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.
6. REFERENTIAL INTEGRITY
RELANATIONAL DBMS 23
Example
7. RELANATIONAL DBMS
RELANATIONAL DBMS 24
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:
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.
9. ACID
ACID 26
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
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.
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
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:
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.
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.
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
Read the Installing Prerequisites page of the wizard and click Install:
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
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
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
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
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.
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||
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
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].
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
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.
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
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.
Click OK
Introduction to Database Creation 46
Database Creation With Code 47
The command used to create a database in SQL uses the following formula:
Here is an example:
To assist you with writing code, tin the previous lessons, we saw that you could
use the query window.
GO
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:
-- =============================================
-- =============================================
USE master
GO
Database Creation With Code 49
IF EXISTS (
SELECT name
FROM sys.databases
GO
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.
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.
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:
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.
GO
Database Routines
Database Creation With Code 51
USE DatabaseName;
Here is an example:
USE GovernmentStatistics;
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.
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:
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.
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.
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
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.
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:
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.
Press Ctrl + N
On the main menu, you can click File -> New -> Query With Current Connection
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
On the main menu, you can click File -> Save SQLQueryX.sql...
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.
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.
After establishing a connection, you can take actions, such as creating a database
and/or manipulating data.
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
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.
After writing a statement, you can execute it, either to make it active or simply to
test it. To execute a statement:
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.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:
A comment can also be spread on more than one line, like a paragraph. Here is an
example:
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
-- =============================================
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).
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:
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:
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 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:
You can also include the item on the right side of AS in single-quotes. Here is an
example:
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:
If you create different sections, separated by a comma, you can follow each with AS
and a caption. Here is an example:
Unary Operators
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 +∞
The positive unary operator, when used, must be positioned on the left side of its
operand, never on the right side.
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
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.
Here is an example that uses two variables. One has a positive value while the other
has a negative value:
SELECT -1250
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:
In Transact-SQL, you can also perform the addition on text. Here is an example:
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:
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
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
The Division
PRINT 128 / 42
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
Parentheses
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.
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.
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
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:
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:
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:
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:
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:
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.
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
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:
You can also declare more than one variable. To do that, separate them with a comma.
The formula would be:
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
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:
or
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.
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:
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
SET @IsMarried = 1
GO
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.
PRINT @Category
GO
(1 rows affected)
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:
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:
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:
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.
SET @IsMarried = 1;
SET @EmplStatus = 2;
GO
numeric would produce the same effect in SQL Server). Here is an example:
4> GO
648
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
@EmplStatus int,
@WeeklyHours Decimal(6,2);
SET @IsMarried = 1;
SET @EmplStatus = 2;
@WeeklyHours AS Hours;
GO
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:
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.
@IsMarried bit,
@WeeklyHours Decimal(6,2),
@HourlySalary SmallMoney,
@WeeklySalary SmallMoney;
SET @IsMarried = 1;
SET @EmplStatus = 2;
@IsMarried AS [Married?],
@WeeklyHours AS Hours,
@HourlySalary AS Hourly,
@WeeklySalary AS Weekly;
GO
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:
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:
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.
@EmplStatus int,
@IsMarried bit,
@WeeklyHours Decimal(6,2),
@HourlySalary SmallMoney,
@WeeklySalary SmallMoney;
SET @IsMarried = 1;
SET @EmplStatus = 2;
@IsMarried AS [Married?],
@WeeklyHours AS Hours,
@HourlySalary AS Hourly,
@WeeklySalary AS Weekly;
GO
Character Variables
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:
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:
4> GO
Gender
------
(1 rows affected)
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:
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:
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.
@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 @IsMarried = 1;
SET @EmplStatus = 2;
@IsMarried AS [Married?],
@WeeklyHours AS Hours,
@HourlySalary AS Hourly,
@WeeklySalary AS Weekly;
GO
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
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.
As opposed to equality, to find out if two values are not equal, use the <>
operator. Its formula is:
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
Notice that the Not Equal operator <> is the opposite to the Equality operator =.
To find out whether one value is lower than another, use the < operator. Its
formula is:
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:
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.
To find out if one value is strictly greater than another, you can use the >
Database Creation With Code 99
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 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:
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
Conditional Statements
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
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 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
@CurrentDate As DateTime
PRINT 'You have the experience required for a new promotion in this job'
GO
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:
@CurrentDate As DateTime
GO
Notice that, in case the expression to examine produces a false result, there is
nothing to do. Sometimes this will happen.
CASE...WHEN...THEN
CASE Expression
END
Database Creation With Code 104
@Gender Varchar(20);
SET @Gender =
CASE @CharGender
END;
GO
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
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:
@Gender Varchar(20);
SET @Gender =
CASE @CharGender
ELSE 'Unknown'
END;
GO
Database Creation With Code 106
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:
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:
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
SET @Number = 1
BEGIN
END
GO
Boolean Constants
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.
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
@Perimeter As Decimal(10,3),
@Area As Decimal(10,3);
IF @Side IS NULL
BEGIN
END;
ELSE
Database Creation With Code 111
GO
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
@Perimeter As Decimal(10,3),
@Area As Decimal(10,3);
IF @Side IS NULL
Database Creation With Code 112
BEGIN
END;
ELSE
GO
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.
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.
Casting a Value
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:
@StrHours Varchar(6),
@WeeklySalary Decimal(6,2)
CAST(@StrHours As Decimal(6,2));
SELECT @WeeklySalary;
GO
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 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
@Perimeter As Decimal(10,3),
@Area As Decimal(10,3);
Database Creation With Code 116
PRINT '-----------------------';
GO
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.
Expand Exercise1
Expand Programmability
Expand Functions
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:
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:
SELECT @ES AS ES
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:
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
--
=============================================
-- Function: GetUsername
--
=============================================
Database Creation With Code 121
(@FirstName varchar(40),
@LastName varchar(40))
dRETURNS varchar(50)
AS
BEGIN
RETURN @Username;
END
GO
Press Ctrl + A to select the code and press Delete to remove the code
GO
In the Object Explorer, expand the Databases node if necessary, and expand
Exercise1
Expand Programmability
Expand Functions
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:
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
-- =============================================
-- Function: GetUsername
-- =============================================
(@FirstName varchar(40),
@LastName varchar(40))
RETURNS varchar(50)
AS
BEGIN
LEFT(LOWER(@LastName), 4)
RETURN @Username;
END
GO
GO
Change the call with a last name shorter than 5 characters such as "Um" and
execute the statement. Here is an example:
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:
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.
--
=============================================
-- Function: Last4DigitsOfSSN
--
=============================================
dRETURNS char(4)
AS
BEGIN
END
GO
SELECT Exercise1.dbo.Last4DigitsOfSSN('836483846');
GO
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:
or
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.
-- =============================================
-- Function: Last4DigitsOfSSN
-- =============================================
RETURNS char(4)
AS
BEGIN
END
GO
SELECT Exercise1.dbo.Last4DigitsOfSSN('244-04-8502');
GO
Arithmetic Functions
Database Creation With Code 128
SIGN(Expression)
GO
Database Creation With Code 129
SET @Number = 0;
GO
Database Creation With Code 130
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
@Perimeter As Decimal(10,3),
@Area As Decimal(10,3);
IF SIGN(@Side) > 0
BEGIN
PRINT '-----------------------';
END;
ELSE
GO
Notice that the interpreter acts differently in response to the result of the
SIGN() function.
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)
GO
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.
CEILING(Expression)
@Number2 As Numeric(6, 2)
GO
@Number2 As Numeric(6, 2)
CONVERT(varchar(10), CEILING(@Number1));
CONVERT(varchar(10), CEILING(@Number2));
GO
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.
FLOOR(Expression)
GO
EXP(Expression)
GO
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
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:
@y As Decimal(6, 2);
SET @x = 20.38;
Database Creation With Code 140
dSET @y = 4.12;
GO
LOG(Expression)
GO
Database Creation With Code 141
LOG10(Expression)
y = log10x
which is equivalent to
x = 10y
Here is an example:
GO
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:
GO
In this case, you can use a control statement to find out whether the
Expression is positive. Here is an example:
IF SIGN(@Number) > 0
CONVERT(varchar(12), SQRT(@Number));
ELSE
GO
Measure-Based Functions
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˚.
PI
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)
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)
Trigonometric Functions
To get the cosine of an angle, you can call the COS() function. Its syntax is:
COS(Expression)
GO
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
Here is an example:
GO
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:
GO
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.
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:
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:
GO
Database Creation With Code 152
GO
Database Creation With Code 153
GO
Database Creation With Code 154
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
ww
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
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
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
Here is an example that calculates the number of years that an employees has
been with the company:
@CurrentDate As DateTime;
AS [Current Experience];
GO
In the Object Explorer, under the Databases node, right-click Exercise1 and
click Delete
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:
Based on this, a list is simply an arrangement of information and this information, also
called data, is stored in tables.
Start Microsoft SQL Server, connect and, in the Object Explorer, expand the
Databases node
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
In the Object Explorer, expand the BCR node (click its + button)
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
Besides these rules, you can make up yours. To avoid confusion, here are the
rules we will use to name our tables:
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 *
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
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.
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
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:
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.
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 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
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.
After respecting these rules, you can add your own rules. In our lessons, here
are the rules we will use to name our columns:
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 *
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.
Click the arrow of the combo box under the Data Type column
Type DateHired
Press Enter three times to position the mouse cursor under DateHired
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.
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
In the top section of the table, click Address and press Tab
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:
Alternatively, to make your statement easier to read, you should create each
column on its own line as follows:
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:
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:
--
=================================================
=========================
--
-- This template creates a table, then it adds a new column to the table.
--
=================================================
=========================
GO
Database Creation With Code 171
column1 int,
column2 char(10)
GO
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
When asked whether you want to save the text, click Yes
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
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 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.
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
To specify the collation of a column when creating in, type COLLATE, followed
by the desired collation code. Here is an example:
);
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:
When using this statement, the ALTER TABLE expression is required and it is
followed by the name of the table.
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:
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:
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
--
-- This template creates a table, then it adds a new column to the table.
--
=================================================
=========================
GO
<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:
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:
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:
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:
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:
--============================================
--
--============================================
Database Creation With Code 179
GO
GO
In the Object Explorer, under the Databases node, right-click BCR and click
Delete
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.
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
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 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.
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.
Start Microsoft SQL Server, select the appropriate options in the Connect To Server
dialog box and connect to the server
GO
USE WorldStatistics;
GO
Area INT,
Population BIGINT,
Capital VARCHAR(50),
);
GO
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
Notice that you receive an error because the commas are not allowed:
Notice that you receive another error because the column is configured for a
natural number and not a string
Click the field under Capital, type Yamoussoukro and press Enter twice
In the SQL, data entry is performed using the INSERT combined with the VALUES
keywords. The primary statement uses the following syntax:
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:
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.
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.
USE WorldStatistics;
GO
GO
Press F5 to execute
To enter various records at the same time, enter the following statement:
USE WorldStatistics;
GO
GO
GO
VALUES('Iraq', 0, 0, 'Baghdad','iq');
GO
GO
GO
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.
USE WorldStatistics;
GO
GO
USE WorldStatistics;
GO
GO
GO
GO
Database Creation With Code 187
GO
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 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.
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:
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.
In the Object Explorer, right-click Countries in the WorldStatistics node and click
Modify
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
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.
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.
In the Object Explorer, under WorldStatistics, right-click Tables and click New
Table...
Column
Data Type Allow Nulls
Name
ContinentID
Area bigint
Population bigint
Category varchar(50),
Size varchar(20),
GO
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:
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:
GO
GO
GO
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.
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.
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:
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:
);
GO
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:
GO
GO
GO
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
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.
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...
In the Object Explorer, right-click ROSH, position the mouse on Tasks and click
Import Data
On the second page, click the arrow of the Data Source combo box and select Flat
File Source
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
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
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
To view data stored on the table, in the Object Explorer, right-click dbo.Students
and click Open Table
Checking Records
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:
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
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
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:
GO
USE VideoCollection;
GO
YearReleased SMALLINT,
Rating varchar(6)
);
GO
VALUES('The Lady Killers', 'Joel Coen & Ethan Coen', '104 Minutes');
GO
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
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
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
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
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.
UPDATE Videos
WHERE VideoID = 5;
GO
Here is an example used to change the name of the director of a particular video:
UPDATE Videos
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:
WHERE Condition(s)
SUGGESTED TEXT 208
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).
In the Object Explorer, under the Databases node, right-click WorldStatistics and
click Delete
Q 2. DBMS means
Q 3. Database model is a
Sample MCQs 209
Q 5. Network model is
Q 7. Which model allows the user to custom data types and methods?
(a) Primary key (b) Candidate key (c) Both (d) None