SQL Server 2012 Internal Note_Databases and database file
SQL Server 2012 Internal Note_Databases and database file
A Microsoft SQL Server database is a collection of objects that hold and manipulate data.
- It’s a collection of many objects (tables, views, stored procedures, functions, and constraints)
- It’s owned by a single SQL Server login account
- It maintains a set of user accounts, roles, schemas, and security
- It has a set of system tables
- It’s the primary unit of recovery and maintains logical consistency among objects within it
- It has and manages its transaction logs
- It can span multiple disk drives and operating system files
- It can range in size from 2 MB to a technical limit of 1524,272 TB.
- It can grow and shrink automatically or manually
- It can have specific properties enabled or disabled
Understanding database files
A database file is an operating system file
A database spans at least two (one for data one for the transaction log) or more database files, and the files are specified when a database is created or altered.
Three types of database files:
- Primary data files
One for each database
Keeps track of all the rest of the files and stores data
.mdf
Has pointers into a table in the master database that contains information about all the files belonging to the database
Use
SELECT * FROM sys.database_files
to see the table
- Secondary data files
- Zero or more for each data files
.ndf
- Log files
- At least one for each database
- Contains the information necessary to recover all transactions in a database
.ldf
Five properties of each database file:
- a logical filename
- a physical filename
- an initial size
- a maximum size
- a growth increment
Information on database files can be seen in
sys.database_files
view.
1 |
|
Creating a database
Use Object Explorer in Management Studio or
CREATE DATABASE
.

CREATE DATABASE newdb
- Create a
newdb
database with default values, with default size, on two files with logical namesnewdb
andnewdb_log
and corresponding physical files,newdb.mdf
andnewdb_log.ldf
are created in the default data directory - Permission (who can create a database):
- Anyone in the sysadmin role
- Anyone has CONTROL or ALTER permission on the server
- Anyone has CREATE DATABASE permission
- This command is case-insensitive
- Default size
- Data file: 3 MB (size of the primary data file of the model database)
- Log file: 0.75 MB
- Database owner
- The login account that created the database
- A database can have only one actual owner
- When the owner uses the database, the user name would be
dbo
The model database
- SQL Server copies the model database when creating a new database
- Objects and database options are copied in all subsequently created databases
- Use
SELECT * FROM sys.objects
to see objects in the model database
Using CREATE DATABASE: an example
1 |
|
If you find that by executing the query there is error message:
Could not obtain exclusive lock on database 'model'. Retry the operation later.
Use this to see which session is occupying the model database:
1 |
|
Then just KILL
the session.
Expanding or shrinking a database
Automatic file expansion
FILEGROWTH
property determines how the file expands with
suffix TB, GB, MB, KB, or %.
MAXSIZE
property sets an upper limit on the file
size
Enabling autogrow might prevent some failures due to unexpected increases in data volume.
Shortcomings:
- Time-consuming if fast file initialization is not activated
- Result in physical fragmentation
1 |
|
Manual file expansion
Use ALTER DATABASE
command with MODIFY FILE
option to change the SIZE
property of the files.
Note that the new file size must be larger than the current size.
Fast file initialization
Instant file initialization adds space to the data file (not log
files) without filling the newly added space with zeros (If want files
are zeroed out, use tracefalge 1806
or deny
SE_MANAGE_VOLUME_NAME
rights of your account), the disk is
overwritten when the new data is written to the files.
Automatic shrinkage
Never recommended (resource-intensive)
Database property autoshrink
controls the option, and
has the same effect as running
DBCC SHRINKDATABASE (dbname, 25)
.
Manual shrinkage
DBCC SHRINKFILE
1 |
|
DBCC SHRINKDATABASE
1 |
|
Using database filegroups
Filegroups are data files in a database grouped for allocation and administration purposes.
Primary filegroup
- The filegroup containing the primary data file
- Only one primary filegroup exists
- By default, the primary filegroup is the default filegroup
Create filegroups
Use FILEGROUP
keyword in the
CREATE DATABASE
or ALTER DATABASE
statement.
The default filegroup
One filegroup always has the property of DEFAULT.
One default filegroup for each database.
Change the default filegroup by using ALTER DATABASE
When an object (table or index) is created, it’s created in the default filegroup if no specific filegroup is specified.
Proportional fill
- Data is stored in a proportional fill manner in the file group
- Creating all your files to be the same size is recommended to avoid the issues of proportional fill
Why use multiple files
- Partial restore
- The flexibility of easily moving the database onto separate drives
Partial restore using filegroups
- Partial restore and backup (objects created on the restored filegroups are available)
- The database goes online as soon as the primary filegroup is restored
- For large databases and a failure of a subset of the disks
A FILEGROUP CREATION example
1 |
|
Filestream filegroups
Filestream filegroups
- Contain one file reference, and the file is specified as an operating system folder
- The path up to the last folder must exist, and the last folder must not exist
- No space is preallocated to the filegroup
- The file and filegroup grow as data is added to tables that are created with filestream columns
- Note that you can’t move a file from one filegroup to another.
Create filestream filegroups
Before creating it, we need to enable the feature.

1 |
|


filestream.hdr
file is a FILESTREAM container header
file, which shouldn’t be modified or removed.
When filestream with a filestream filegroup is enabled, we can manage file data using Windows applications.
Altering a database
The ALTER DATABASE
statement can only make changes each
time it is executed.
ALTER DATABASE examples
1 |
|
1 |
|
Databases under the hood
A database consists of user-defined space (allocated in one or more operating system files) for the permanent storage of user objects such as tables and indexes.
Databases are divided into logical pages (of 8 KB each), numbered contiguously from 0 to N.
Enlarge/Shrink a file
- new space is added to the end of the file
- pages are removed starting at the end of the pages
New a database
A unique database ID created
View
sys.databases
shows basic information about each database
Space allocation
Extents
- eight logically contiguous pages (or 64 KB of space)
- Types
- Uniform extents
- All eight pages owned by a single object
- Mixed extents
- shared by up to eight objects
- Uniform extents
- Allocation
- For a new table or index, allocates mixed extents
- When the table or index grows to eight pages, all future allocations use uniform extents
GAM pages
- record which extents have been allocated for any type of use.
- On page 2, another GAM appears every 511,230 pages after the first GAM on page 2
- Each GAM covers about 64,000 extents, or almost 4 GB of data
SGAM pages
- record which extents are now used as mixed extents and have at least one unused page
- On page 3, another SGAM appears every 511,230 pages after the first SGAM on page 3
- each SGAM covers about 64,000 extents, or almost 4 GB of data

Setting database options
By default, all the options that require ON or OFF have an initial value of OFF unless the option was set to ON in the model database.
All databases created after an option is changed in the model have the same values as the model.
sys.databases
lists the current values of all the
options.
State options
State options control who can use the database and for what operations:
- User access state determines which users can use the database.
- Status state determines whether the database is available to anybody for use
- Updateability state determines what operations can be performed on the database
SINGLE_USER | RESTRICTED_USER | MULTI_USER
1 |
|
Describe the user access property of a database
Mutually exclusive
SINGLE_USER
- Allow only one connection at a time
RESTRICTED_USER
- Allow qualified connections
- Qualified: dbcreator, sysadmin server role, db_owner role
MULTI_USER (default)
- Allow connections with a valid user name
Check current option
1
2SELECT USER_ACCESS_DESC FROM sys.databases
WHERE name = '<name of database>';
OFFLINE | ONLINE | EMERGENCY
1 |
|
Describe the status of a database
Mutually exclusive
ONLINE (default)
OFFLINE
- it’s closed and shut down cleanly and marked as offline
- cannot be modified
EMERGENCY
- allows you to access the data while you copy it to a new location.
Check current status
1
2SELECT state_desc from sys.databases
WHERE name = 'AdventureWorks';Can have another status:
- RESTORING
- RECOVERING
- RECOVERY_PENDING
READ_ONLY | READ_WRITE
1 |
|
describe a database’s ability to be updated
mutually exclusive
READ_WRITE (default)
- any user with the appropriate permissions can carry out data modification operations
READ_ONLY
- no INSERT, UPDATE, or DELETE operations can be executed
- automatic recovery will not run when the SQL server is restarted
- no locks are needed for SELECT
- Cannot shrink databases
- Cannot set to READ_ONLY if any connections are made to the database
Check
1
2SELECT name, is_read_only FROM sys.databases
WHERE name = 'AdventureWorks';1 for READ_ONLY, 0 for READ_WRITE
Termination options
Termination options determine the behavior of the SQL Server when several state options cannot be set.
1 |
|
Wait is the default option
ROLLBACK AFTER integer [SECONDS]
- wait for the specified number of seconds and then break unqualified connections.
- Incomplete transactions are rolled back.
ROLLBACK IMMEDIATE
- breaks unqualified connections immediately
- All incomplete transactions are rolled back
NO_WAIT
- check for connections before attempting to change the database state and causes the ALTER DATABASE statement to fail if certain connections exist (generates an error)
Cursor options
The cursor options control the behavior of server-side cursors defined using one of the following T-SQL commands for defining and manipulating cursors: DECLARE, OPEN, FETCH, CLOSE, and DEALLOCAT
CURSOR_CLOSE_ON_COMMIT {ON | OFF}
- ON
- any open cursors are closed when a transaction is committed or rolled back
- OFF (default)
- cursors remain open after a transaction is committed
CURSOR_DEFAULT {LOCAL | GLOBAL}
- LOCAL and cursors aren’t specified as GLOBAL when they are created
- The cursor name is valid only within this batch
- GLOBAL and cursors aren’t specified as LOCAL when they are created
- The cursor name can be referenced in any stored procedure or batch executed by the connection.
Auto options
Affect actions that SQL Server might take automatically
AUTO_CLOSE
- On
- the database is closed and shut down cleanly when the last user of the database exits
AUTO_SHRINK
- On
- all of a database’s files are candidates for periodic shrinking
- never recommended
AUTO_CREATE_STATISTICS
- On (default)
- the SQL Server Query Optimizer creates statistics on columns referenced in a query’s WHERE, ON, GROUP BY, or DISTINCT clauses.
AUTO_UPDATE_STATISTICS
- On (default)
- existing statistics are updated if the data in the tables has changed
SQL options
The SQL options control how various SQL statements are interpreted.
Default is OFF for all.
ANSI_NULL_DEFAULT
- ON
- if you don’t specifically indicate whether a column in a table allows NULL values, NULLs are allowed
- OFF
- newly created columns don’t allow NULLs if no nullability constraint is specified
ANSI_NULLS
- ON
- any comparisons with a NULL value result in UNKNOWN
- OFF
- comparisons of non-Unicode values to NULL result in a value of TRUE if both values being compared are NULL
ANSI_PADDING
- ON
- strings being compared with each other are set to the same length before the comparison takes place
- OFF
- No padding
ANSI_WARNINGS
- ON
- errors or warnings are issued when conditions such as division-by-zero or arithmetic overflow occur
ARITHABORT
- ON
- a query is terminated when an arithmetic overflow or division-by-zero error is encountered during the execution of a query
- OFF
- the query returns NULL as the result of the operation
CONCAT_NULL_YIELDS_NULL
- ON
- concatenating two strings results in a NULL string if either string is NULL
- OFF
- a NULL string is treated as an empty (zero-length) string for concatenation
NUMERIC_ROUNDABORT
- ON
- an error is generated if an expression results in a loss of precision
- OFF
- the result is simply rounded
QUOTED_IDENTIFIER
- ON
- identifiers such as table and column names can be delimited by double quotation marks, and literals must then be delimited by single quotation marks
- OFF
- identifiers can’t be in quotation marks and must follow all T-SQL rules for identifiers
RECURSIVE_TRIGGERS
- ON
- triggers can fire recursively, either directly or indirectly
- OFF
- triggers can’t be fired recursively.
Database recovery options
RECOVERY (FULL, BULK_LOGGED, or SIMPLE)
- determines how much recovery can be done on a SQL Server database
- controls how much information is logged and how much of the log is available for backups
PAGE_VERIFY
Discover damaged database pages caused by disk I/O path errors, which can cause database corruption problems.
- CHECKSUM
- SQL Server calculates a checksum over the contents of each page and stores the value in the page header when a page is written to the disk
- When reading the page, a checksum is calculated and compared with the stored one, if not match, error message 824
- run
DBCC CHECKDB
to determine the type of database page or pages affected
- TORN_PAGE_DETECTION
- allows SQL Server to detect incomplete I/O operations caused by power failures or other system outages
- NONE
Understanding database security
Authentication
SQL Server validates and establishes the identity of an individual who wants to access a resource
Authorization
SQL Server decides whether a given identity is allowed to access a resource
Securable
- This is an entity on which permissions can be granted.
- Securable include databases, schemas, and objects.
Principal
- This entity can access securable
- A primary principal represents a single user
- A secondary principal represents multiple users
Database access
Authentication logins
Windows authentication
- SQL Server login security is integrated directly with Windows security to authenticate SQL Server users
- The SID is the same as the OS
SQL Server authentication
- An administrator creates SQL Server login accounts within SQL Server
Login names
Check login names in
sys.server_principals
view
Security modes
Windows authentication mode
- Use only Windows authentication
- The default SQL Server sa login is disabled
Mixed mode
- Use both Windows authentication and SQL Server authentication
- The sa login can be enabled using the
ALTER LOGIN
command
Change mode
Database security
Databases are owned by login names.
Most objects within a database are owned by database principals.
The SID used by a principal determines which databases that principal has access to.
sys.database_principals
is a mapping table that maps
login names to users in that particular database
Database user types
- a user that maps to a SQL Server login
- a user that maps to a Windows login
- a user who doesn’t map to any login
- have a user to be the owner of objects, who never actually uses the database
- a user without a login
- have access to only that database.
- Within a database, users own objects and are granted permissions
1 |
|

Databases vs. schemas
Schemas
A schema is defined as a collection of database objects that are owned by a single user and form a single namespace (a container of objects)
No two tables in the same schema can have the same name.
Namespace
A namespace is a set of objects that can’t have duplicate names
Principals and schemas
Users and schemas are two separate things
Permissions are granted to users, but objects are placed in schemas.
Schemas contain objects, not users
The statement
GRANT CREATE TABLE
TO sue refers to the user sue.Then sue creates a table
1
CREATE TABLE mytable (col1 varchar(20));
Then the table is placed in sue's default schema, maybe the schema sue.
If another user wants to select from this table, then
1
SELECT col1 FROM sue.mytable;
Here, sue refers to the schema that contains the table
An object is referenced by the schema n which it is contained
In most cases, the owner of the schema is the same as the owner of all objects within the schema
The column pricipal_id
in the view
sys.objects
contains the user_id of an object’s owner if it
isn’t the same as the owner of the object’s schema.

The default for the default schema is the dbo schema.
Default schemas
Every database also has a schema called sys, which provides a way to access all the system tables and views.
A user can have at most one default schema at any time
A user’s default schema is used for name resolution during object creation or object reference
You should always specify the schema name for all object access as well as object management
To create an object in a schema
- The schema must exist
- The user creating the object must have permission to create the object
- The user creating the object must have permission to alter the schema
Moving or copying a database
Detaching and reattaching a database
Detach
1 |
|
- Requires that no one is using the database.
- User
ALTER DATABASE
toSINGLE_USER
mode to break connections
- User
- Ensure no incomplete transactions and no dirty pages exist
- The database is removed from the
sys.databases
view and underlying system tables
DROP DATABASE
- Doesn’t check for dirty pages or open transactions
- Will remove the physical files
Reattach
1 |
|
- The primary file is required to have a entry
- The
FOR ATTACH_REBUILD_LOG
option is limited to read/write databases - When attaching a read-only database, you must specify the log file or files in the FOR ATTACH clause
Backing up and restoring a database
The database doesn’t need to come offline at all because the backup is a completely online operation
Understanding compatibility levels
Compatibility levels
110 (for SQL Server 2012), 100 (for SQL Server 2008), or 90 (for SQL Server 2005)
Check compatibility level
1 |
|

Change compatibility level
1 |
|