If you haven’t
already created that database, you can create it now by executing the
following code:
USE [master]
GO
CREATE DATABASE [VetClinic]
GO
USE [master]
GO
ALTER DATABASE VetClinic
SET RECOVERY FULL
GO
USE [VetClinic]
GO
CREATE TABLE [Pets]
(pet_id INT PRIMARY KEY,
pet_name VARCHAR(50) NOT NULL,
pet_weight INT NULL)
GO
ALTER TABLE [Pets]
ADD [MicroChipID] VARCHAR(100) NOT NULL
CONSTRAINT [MicroChip_Unique] UNIQUE
GO
USE [VetClinic]
GO
INSERT INTO Pets
VALUES(1,'Zeus',185,'398BF49'),
(2,'Lady',155,'191ABBC'),
(3,'Deno',50,'790AG441'),
(4,'Rex',44,'CDD81322'),
(5,'Rover',15,'A8719841')
GO
CREATE TABLE [Owners]
(owner_id INT PRIMARY KEY,
pet_id INT REFERENCES Pets(pet_id),
owner_name VARCHAR(50) NOT NULL)
GO
INSERT INTO Owners VALUES(1,2,'Bryan'),
(2,3,'Rob'),
(3,1,'Rob')
CREATE TABLE MicroChips
(MicroChipID VARCHAR(100) UNIQUE)
GO
INSERT INTO MicroChips VALUES('34BA123')
GO
Data Types
By now, you should be familiar with some of the data types available.You can find the “Data Types
(Transact-SQL)” article at http://msdn.microsoft.com/en-us/library/ms187752(v=SQL.110).aspx
.
Data types are used in many places within SQL Server, including as
column definitions, as parameters to functions and stored procedures,
and as variables. Table 1 describes a few of the common data types you may use in everyday tasks.
Each data type has different characteristics, including how much space on disk each value takes up. A VARCHAR
data type can be defined for 20 characters but takes up only a few
bytes on disk. A quick way to determine how many bytes are used is to
use the DATALENGTH
function. In the following example, the @na
me variable has the text Rob
:
DECLARE @name VARCHAR(20)
SET @name='Rob'
SELECT DATALENGTH(@name)
When this script is run, the size of @name
is 3 bytes. If you were to change it to SET @name='Robert'
and rerun the query, the size would be 6 bytes.
Fixed data types, such as integers and money,
always take the same storage space regardless of the value. Taking up
more storage space than is needed not only wastes disk space but also
decreases query performance because there will be more disk I/O as a
result. SQL Server 2008 provides native data compression technologies
that compress at the row and page levels, which helps to mitigate the
performance and storage issues. Regardless of whether or not you decide
to compress your data, it’s a good idea to analyze your database to
determine whether the data types are defined appropriately for the
given data.
Unicode vs. ANSI
If you are new to the database world and
haven’t had a lot of experience programming, you may be curious about
Unicode character strings versus ANSI character strings. To truly
understand this, you must look to the origins of computers. The
American National Standards Institute (ANSI) came out with a standard
known as American Standard Code for Information Interchange (ASCII).
This standard defines letters, numbers, and symbols that are referred
to as a character set. The ASCII character set handles 256
different characters generally stored as 1 byte each within a computer.
With only 26 letters in the English language, you may be wondering why
anyone would need more than 256 characters. If the world spoke only
English, no one would, but our friends in China struggled to figure out
how to represent their thousands of characters within this
256-character limit. Unicode, however, allows the storage
of 2 bytes per character. These 2 bytes allow for more than 65,000
different characters. All modern operating systems use Unicode, and SQL
Server supports both the Unicode and ANSI character sets.
Living with NULL
NULL
is not your annoying cousin; it identifies that a value does not exist in the database. Take the Pets
table definition, for example:
CREATE TABLE [Pets]
(pet_id INT PRIMARY KEY,
pet_name VARCHAR(50) NOT NULL,
pet_weight INT NULL)
GO
In this scenario, it’s OK that the pet weight
is not entered. However, it is not acceptable for the pet to have no
name. In the table definition, you see pet_name
was defined with a NOT NULL
keyword. This means that the absence of a value cannot be a valid value for the pet_name
column. Conversely, pet_weight
was defined as NULL
,
meaning that you could insert a value into the table and leave out the
pet’s weight. If you queried the newly entered data, you would see that
pet_weight
is NULL
.