T-SQL for Data Definition
Vu Tuyet Trinh
Hanoi University of Technology
1
MicrosoftMicrosoft
Overview of Transact-SQL
Based on AINSI SQL 92 standard
Composing of three categories
Data Manipulation Language (DML)
Data Definition Language (DDL)
Data Control Language (DCL)
Having some Microsoft specific extensions
Beyond relational data
.net framework integration
MicrosoftMicrosoft
Data Definition Language
Create
used to create databases and their objects.
Use
allows you to specify the database you wish to work with within
your DBMS.
Alter
used to modify the definition of it without deleting it
Drop
used to remove entire database objects
MicrosoftMicrosoft
Overview of Database Objects
MicrosoftMicrosoft
Outline
√
Data Definition Language
Managing Databases
Data Types
Managing Tables
Managing other SQL Server Objects
MicrosoftMicrosoft
Databases in SQL Server
Database
Storing data and other database
objects
Database Snapshot
Maintain historical data for report
generation
Safeguard data against
administrative error
Safeguard data against user error
SQL Server
Enterprise Edition
MicrosoftMicrosoft
Creating a New Database
Factors to consider
Default: Sysadmin, dbcreator
Creator becomes the owner
Maximum of 32,767 per server
Follow naming rules
MicrosoftMicrosoft
Creating a New Database
Some arguments:
The name of the database
The size of the database
The files where the database will reside
CREATE DATABASE Sample
ON
PRIMARY ( NAME=SampleData,
FILENAME='c:\Program Files\..\..\Data\Sample.mdf',
SIZE=10MB,
MAXSIZE=15MB,
FILEGROWTH=20%)
LOG ON
( NAME=SampleLog,
FILENAME= 'c:\Program Files\..\..\Data\Sample.ldf',
SIZE=3MB,
MAXSIZE=5MB,
FILEGROWTH=1MB)
COLLATE SQL_Latin1_General_Cp1_CI_AS
MicrosoftMicrosoft
Setting & Viewing Database Options
Set Database Options By Using:
SQL Server Management Studio
ALTER DATABASE statement
Database Option Categories
Auto options
Cursor options
Recovery options
SQL options
State options
MicrosoftMicrosoft
Retrieving Database Information
Determining database properties by using the
DATABASEPROPERTYEX Function
SELECT DATABASEPROPERTYEX
(‘pubs’,’useraccess’)
SELECT DATABASEPROPERTYEX
(‘pubs’,’recovery’)
Using system stored procedures to display
information about databases and its parameters
sp_helpdb
sp_helpdb database_name
sp_spaceused [objname]
MicrosoftMicrosoft
Attaching an Existing Database
MicrosoftMicrosoft
Creating a Snapshot Database
MicrosoftMicrosoft
Managing Databases
Managing Data and Log File Growth
Monitoring and Expanding a Transaction Log
Shrinking a Database or File
Dropping a Database
MicrosoftMicrosoft
Managing Data and Log File Growth
ALTER DATABASE Sample
MODIFY FILE ( NAME = 'SampleLog',
SIZE = 15MB)
GO
ALTER DATABASE Sample
ADD FILE
(NAME = SampleData2,
FILENAME='c:\Program Files\..\..\
Data\Sample2.ndf',
SIZE=15MB,
MAXSIZE=20MB)
GO
Using Automatic File Growth
Expanding Database Files
Adding Secondary Database Files
MicrosoftMicrosoft
Monitoring and Expanding a Transaction Log
Monitoring the log
Monitoring situations that produce extensive
log activity
Mass loading of data into indexed table
Large transactions
Performing logged text or image operations
Expanding the log when necessary
MicrosoftMicrosoft
Shrinking a Database or File
Shrinking an Entire Database
Shrinking a Data File in the Database
Shrinking a Database Automatically
Set autoshrink database option to true
DBCC SHRINKDATABASE (Sample, 25)
DBCC SHRINKFILE (Sample_Data, 10)
MicrosoftMicrosoft
Dropping a Database
DROP DATABASE Northwind, pubs
Methods of Dropping a Database
SQL Server Enterprise Manager
DROP DATABASE statement
Restrictions on Dropping a Database
While it is being restored
When a user is connected to it
When publishing as part of replication
If it is a system database
MicrosoftMicrosoft
Outline
√
Data Definition Language
√
Managing Databases
Data Types
Managing Tables
Managing other SQL Server Objects