Defining Table Location
As databases scale in size,
the physical location of database objects, particularly tables and
indexes, becomes crucial. Consider two tables, Authors and Titles,
that are always queried together. If they are located on the same
physical disk, contention for hardware resources may slow performance.
SQL Server addresses this issue by enabling you to specify where a table
(or an index) is stored.
The mechanism for
specifying the physical table location is the filegroup. Filegroups are
aligned to physical data files. By default, each database has a primary
filegroup and a data file that matches the name of the database. You can
create additional filegroups and align them to other data files. When
these filegroups are created, SQL Server enables you to create your
database tables on a specific filegroup.
Note
Using partitioned tables is a way
to specify table location. This SQL Server 2008 feature allows you to
divide a table into partitions and align those partitions with
filegroups.
The placement of tables on
separate filegroups has some distinct advantages, including performance
benefits. You can achieve performance improvements by storing filegroups
on different disks. You can also achieve some manageability
improvements by using filegroups because you can back up and manipulate
filegroups separately. This capability is particularly important for
large tables.
You specify the location of a table by using the ON clause during table creation. Listing 1 shows an example of creating two filegroups in the BigPubs2008
database, followed by the creation of two new tables on those
filegroups. Note that the filegroups must exist before the tables are
created.
Listing 1. An Example of Creating Tables on Specific Filegroups
--Add the filegroups
ALTER DATABASE BigPubs2008 ADD FILEGROUP FG1
ALTER DATABASE BigPubs2008 ADD FILEGROUP FG2
GO
--Add files to the filegroups
ALTER DATABASE BigPubs2008 ADD FILE
( NAME = FG1_File,
FILENAME = 'c:\BigPubs2008FG1.ndf',
SIZE = 2MB) TO FILEGROUP FG1
go
ALTER DATABASE BigPubs2008 ADD FILE
( NAME = FG2_File,
FILENAME = 'c:\BigPubs2008FG2.ndf',
SIZE = 2MB) TO FILEGROUP FG2
go
CREATE TABLE [dbo].[authors_NEW](
[au_id] [dbo].[id] NOT NULL,
[au_lname] [varchar](40) ,
[au_fname] [varchar](20) ,
[phone] [char](12),
[address] [varchar](40) NULL,
[city] [varchar](20) NULL,
[state] [char](2) NULL,
[zip] [char](5) NULL,
[contract] [bit] NOT NULL,
) ON FG1
go
CREATE TABLE [dbo].[titles_NEW](
[title_id] [dbo].[tid] NOT NULL,
[title] [varchar](80) NOT NULL,
[type] [char](12) NOT NULL,
[pub_id] [char](4) NULL,
[price] [money] NULL,
[advance] [money] NULL,
[royalty] [int] NULL,
[ytd_sales] [int] NULL,
[notes] [varchar](400) NULL,
[pubdate] [datetime] NOT NULL,
) ON FG2
Defining Table Constraints
Constraints provide a means to enforce data integrity. In addition to NULL/NOT NULL, SQL Server provides five constraint types: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, and DEFAULT. These constraints help further define the type of data you can store in tables.
You can add constraints at the time
of table creation, or you can add them after a table has been created,
by using the ALTER TABLE statement.
Listing 2 shows a CREATE TABLE statement that has an example of each one of the five constraint types listed. The PRIMARY KEY constraint is created at the bottom of the script and is named PK_TitleHistory. The FOREIGN KEY constraint is created on the title_id column and is named FK_titles_titleHistory. The UNIQUE constraint is part of the primary key and can be identified with the UNIQUE keyword. The CHECK constraint is created on the price column; it checks to make sure the price is greater than zero. Finally, a DEFAULT constraint is created on the modify_user column; it sets the user to the value of system if no explicit value is specified.
Listing 2. Example of Creating Constraints with CREATE TABLE
CREATE TABLE dbo.TitleHistory(
title_id dbo.tid
CONSTRAINT FK_titles_titleHistory
REFERENCES titles (title_id)NOT NULL ,
change_date datetime NOT NULL,
title varchar(80) NOT NULL,
type char(12) NOT NULL,
price money NULL
CONSTRAINT CK_TitleHistory_Price CHECK (Price>0),
modify_user nchar(10) NOT NULL
CONSTRAINT DF_TitleHistory_modify_user DEFAULT (N'system'),
CONSTRAINT PK_TitleHistory UNIQUE CLUSTERED
( title_id ASC,
change_date ASC ) )
|
You can create the same constraints as in Listing 2 by using the ALTER TABLE statement. This means you can first create the table (without the constraints) and then add the constraints afterward. Listing 3 shows the creation of the same titleHistory table, with the constraints added later via the ALTER TABLE statement.
Listing 3. Example of Creating Constraints with ALTER TABLE
IF EXISTS (SELECT * FROM dbo.sysobjects
WHERE id = OBJECT_ID(N'[dbo].[TitleHistory]')
AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROP TABLE [dbo].[TitleHistory]
CREATE TABLE dbo.TitleHistory(
title_id dbo.tid NOT NULL,
change_date datetime NOT NULL,
title varchar(80) NOT NULL,
type char(12) NOT NULL,
price money NULL,
modify_user nchar(10) NOT NULL )
GO
--PRIMARY KEY/UNIQUE CONSTRAINT
ALTER TABLE dbo.TitleHistory
ADD CONSTRAINT PK_TitleHistory UNIQUE CLUSTERED
(
title_id ASC,
change_date ASC
)WITH (SORT_IN_TEMPDB = OFF, ONLINE = OFF)
go
--FOREIGN KEY CONSTRAINT
ALTER TABLE dbo.TitleHistory WITH CHECK
ADD CONSTRAINT FK_titles_titleHistory FOREIGN KEY( title_id)
REFERENCES dbo.titles (title_id)
GO
--CHECK CONSTRAINT
ALTER TABLE dbo.TitleHistory WITH CHECK
ADD CONSTRAINT CK_TitleHistory_Price CHECK ((Price>(0)))
GO
--DEFAULT CONSTRAINT
ALTER TABLE dbo.TitleHistory
ADD CONSTRAINT DF_TitleHistory_modify_user
DEFAULT (N'system') FOR modify_user