Wednesday, January 22, 2014

Contained Databases in SQL Server 2012

Contained Databases in SQL Server 2012


Contained
databases are a new feature in SQL Server 2012 and are defined on MSDN Library as ”a database that is isolated from other databases and from the instance of SQL Server that hosts the database”.

The
containment setting of a database can be NONE, PARTIAL or FULL. But only NONE and PARTIAL are supported on SQL Server 2012.

There
are three types of containment hinted at in SQL Server 2012:
  • NONE: This default containment mode is what exists prior to SQL Server 2012. There are no boundaries defined or visible.
  • PARTIAL: With partially contained databases, you have the ability to define clearer boundaries between databases and the server, making it easier for metadata to be hosted within the databases. This, in turn, makes SQL Server databases more portable and less dependent on underlying hosts.
  • FULL: Fully contained databases are only alluded to in SQL Server 2012 BOL in a few locations and aren't currently available as an option. It's assumed, however, that full containment will enable greater database portability and potentially allow for strict enforcement of containment boundaries as a means of fully isolating databases from each other and from the underlying host.

Benefits and characteristics.


The
following are some of the benefits and characteristics that contained databases have:

1.
They make easier to migrate databases from one server to another. Errors related to orphan users are no longer an issue with contained databases, since a contained database user can now be created without an associated login.

2.
Authentication can now occur at the database level.

3.
Contained database users can be Windows and SQL Server authentication users.

4.
A contained database user can access only contained database objects. They cannot access system databases and cannot access server objects.

5.
Metadata is stored on the contained database and not stored on system databases. This makes contained databases more portable than the databases we know.

 
Disadvantages and limitations.


Some
disadvantages and limitations are the following:

1.
There are some security concerns. A database owner can create contained database users without the permission of a DBA. The possibility of denial of service attacks exist with contained databases using AUTO_CLOSE option. For security best practices about contained databases, please see the references shown at the end of the article.2. Partially contained databases cannot use replication, change data capture, change tracking, numbered procedures, schema-bound objects that depend on built-in functions with collation changes. You may find more limitations on the references shown at the end of this article.Requirements of contained databases.
1 It is required to enable contained databases on the instance.2. The contained database needs to be added to the connection string or specified when connecting via SQL Server Management Studio.Step-by-step instructions on how to create a contained database.

To
be able to create contained databases on a SQL Server 2012 instance, we need to enable the contained database authentication option on the instance.

Open
SQL Server 2012 Management Studio, connect to the instance, make a right click on the name of the instance on Object Explorer, select the Advanced page on the "Select a page" panel, and set to true the "Enable Contained Databases" option.

Alternatively,
you can use sp_configure system stored procedure to enable contained databases on the instance, as shown below.
EXEC sp_configure 'show advanced', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'contained database authentication', 1GO
RECONFIGURE
GO





When
a database is created the "Containment type" should be set to "partial" to make the database a contained database, as shown below.

This
can be done using T-SQL too, as shown below.


A
contained database allows the creation of a database user that is not associated to an instance login. A contained database user can be created expanding the Security folder on the contained database, making a right click on the Users folder and selecting the "New User" option.



Next,
set the user type to "SQL user with password", assign a user name, set the password for the database user and specify the default schema for the user.


Specify
the roles for this user on the database and click OK.



If
you would like to create the contained database user using T-SQL, please see the example below.CREATE USER [MorilloCD2User]
    WITH PASSWORD=N'p@ssw0rd123',
   
DEFAULT_SCHEMA=[dbo]GO





To
connect a contained database user to a contained database, the database name should be specified on the connection string. If you are using SQL Server Management Studio, on the Connect to Server dialog specify the authentication, specify the user name, provide the password, click on the Options button and specify the database name on the "Connect to database" combo box.

Thursday, June 20, 2013

Display the size of all tables in a database



Below is the query to get the list of tables with sizes in a database:
 

CREATE PROCEDURE getAllTablesSize
AS
BEGIN
      DBCC UPDATEUSAGE (0) WITH NO_INFOMSGS;
      CREATE TABLE
            #temp (
                         [name] varchar(250),
                         [rows] varchar(50),
                         [reserved] varchar(50),
                         [data] varchar(50),
                         [index_size] varchar(50),
                         [unused] varchar(50)
                       );
      INSERT #temp EXEC ('sp_msforeachtable ''sp_spaceused ''''?''''''');
      UPDATE  #temp
      SET
            [rows] = LTRIM(RTRIM(REPLACE(t.rows,'KB',''))),
            [reserved] = LTRIM(RTRIM(REPLACE(t.reserved,'KB',''))),
            [data] = LTRIM(RTRIM(REPLACE(t.data,'KB',''))),
            [index_size] = LTRIM(RTRIM(REPLACE(t.index_size,'KB',''))),
            [unused] = LTRIM(RTRIM(REPLACE(t.unused,'KB','')))
      FROM #temp AS t
      SELECT
            SUM(CAST([reserved] as decimal))/1024 AS 'Total reserved MB',
            SUM(CAST([data] as decimal))/1024 AS 'Total data MB',
            SUM(CAST([index_size] as decimal))/1024 AS 'Total index_size MB',
            SUM(CAST([unused] as decimal))/1024 AS 'Total unused MB'
      FROM
            #temp
      SELECT
            [name] ,
            CAST([rows] as INT)'rows' ,CAST([reserved] as INT)/1024 'reserved MB',
            CAST([data] as INT)/1024 'data MB' ,
            CAST([index_size]/1024 as INT)'index_size MB',
            CAST([unused] as INT)/1024 'unused MB'
      FROM
            #temp
      ORDER BY  name
      DROP  TABLE #temp;
END
GO
--EXECUTE getAllTablesSize

Thursday, May 30, 2013

SQL DBA reference websites

Hi Viewers,

Below are the sites for SQL DBA reference which i am using:

http://www.careerride.com/SQL-DBA-different-types-of-BACKUPs.aspx
http://www.sqldbadiaries.com/
http://www.sql-server-helper.com/
http://strictlysql.blogspot.in/2010/06/finding-cpu-utilization-in-sql-server.html
http://www.sqlmag.com/articles/sqlserverdenali/sql-server-denali-alwayon-140199
http://beyondrelational.com/blogs/community/archive/2011/12/21/sql-server-2012-denali-alwayson.aspx
http://insqlserver.com/
http://www.a2zmenu.com/Blogs/SQL/store%20procedure%20to%20take%20database%20backup.aspx
http://www.jamesserra.com/archive/2012/02/sql-server-2012-denali-t-sql-enhancements/
http://www.mytechmantra.com
http://sqlwithmanoj.wordpress.com/tag/t-sql-interview-questions/
http://www.sql-server-citation.com/2007/08/resource-for-new-sql-server-dba-and.html
http://www.sqlcourse.com/intro.html
http://sqlserver2008tutorial.com/member.html
http://dba.fyicenter.com/faq/sql_server/
http://www.careerride.com/SQLServer-Interview-Questions.aspx
http://www.mssqltips.com/sql-server-tip-category/53/dba-best-practices/
http://learnsqlwithbru.com/sql-server-interview-questions-and-answers/sql-server-dba-q-and-a/
http://www.sqlmonkeys.com
http://www.bradmcgehee.com/2010/03/an-introduction-to-sql-server-2008-audit/
http://sqlblog.com/blogs/jonathan_kehayias/archive/2009/06/06/getting-log-space-usage-without-using-dbcc-sqlperf.aspx
http://sqldbachronicles.blogspot.in/2011/05/check-log-space-dbcc-sqlperflogspace.html
http://dbaspot.com/microsoft-sql-server/
http://sql-school.blogspot.in/
http://arshadali.blogspot.in/
http://www.blackwasp.co.uk/SQLProgrammingFundamentals.aspx
http://vblvbl.brinkster.net/SQL7DI/ch12c.htm
http://www.sqlserver-training.com/sql-server-2008-interview-questions/-
http://www.sqlvillage.com/Interviews/Inerview%20Questions%20-%20Part%202.asp
http://www.slideshare.net
http://www.mssqltips.com/sqlservertip/1253/sql-server-dba-concurrency-and-locking-interview-questions/
http://sqlserverplanet.com/
http://sqlserverpedia.com/wiki/Database_Administration
http://www.mssqlfix.com/2011/02/mcitp-70-450-exam-sample-question-and.html
http://sql-server-2005.com/
http://www.sql-dba.com/sql_server_scripts.html
http://www.databasetimes.com/2009/05/keep-up-indexes-in-sql-server-2005.html
http://www.sqllion.com/2009/07/sql-server-indexes-pros-and-cons-part-2/
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=37037#top
http://www.sqlserverblogforum.com/
http://www.databasejournal.com/features/mssql/archives/
http://www.reference.com/motif/computers/difference-between-a-database-instance-and-schema


Backup and Restore
http://www.sqlbackuprestore.com/speedinguprestores.htm


upgrade sql2000 to sql2005:
http://www.sqlservercentral.com/articles/Administration/2988/   
http://www.sqlservercentral.com/articles/News/3036/         
http://gouravverma.blogspot.in/2008/04/top-10-new-features-in-sql-server-2005.html

SQL Server 2008:
http://sqlcat.com/sqlcat/b/top10lists/archive/2009/01/30/top-10-sql-server-2008-features-for-the-database-administrator-dba.aspx
http://blog.sqlauthority.com/sql-server-interview-questions-and-answers/

SQL Programming:
http://beginner-sql-tutorial.com/sql.htm
www.sqlserver2005tutorial.com/
http://dba.fyicenter.com/faq/sql_server/


Cluster:
http://clearinglogfiles.blogspot.in/2012/03/cluster-problems_3942.html

SSRS:
http://www.katieandemil.com/ssrs-create-reports-a-simple-example-of-ssrs-report

Certification:
http://www.ucertify.com/certifications/Microsoft/mcitp-database-admin-sql-server-2008.html
http://braindumps.org/
Exam: 70-432
Exam: 70-433
Exam: 70-450
Exam: 70-451
http://www.sql-training.net/

SQL SERVER 2012:
http://www.mssqltips.com/sql-server-tip-category/117/sql-server-denali/
http://www.mssqltips.com/sql_server_dba_tips.asp
http://social.technet.microsoft.com/wiki/contents/articles/3783.what-s-new-in-sql-server-2012-en-us.aspx
http://www.infosysblogs.com/microsoft/2012/03/sql_server_2012_-_overview.html#more
http://www.sqlskills.com/T_MCMVideos.asp
http://www.sqlserver-training.com/what-is-contained-database-in-sql-server/-
http://www.devproconnections.com/article/sqlserverdenali/sql-server-denalis-security-enhancements-140479
http://www.yourdba.com/?p=123
http://sql-server-tuning.com/2011/11/06/sql-server-2012-availability-enhancements-part-ii/
http://www.simple-talk.com/sql/database-administration/sql-server-2012-alwayson/

Tutorial:
http://www.infotechguyz.com/SQLServer2012Tutorial.html

Ebook:
http://www.andreavb.com/forum/viewtopic_9981.html

Interview:
http://www.mssqltips.com/sqlservertip/1472/sql-server-dba-phone-interview-questions/
http://www.sqlserver-expert.com/search/label/TempDB
http://interviewquestions4u.com/database/sql-server/sql-server-dba
http://www.mssqlfix.com/2010/11/sql-dba-interview-questions-with.html
https://www.logicsmeet.com/forum/35808-sql-server-interview-questions-and-answers-for-experienced.aspx
http://www.mssqltips.com/sql-server-tip-category/46/interview-questions-dba/
http://www.careerride.com/SQLServer-Interview-Questions.aspx#sql16
http://www.careerride.com/Online-practice-test.aspx

Websites for SQL DBA:
http://www.mssqltips.com/sqlservertip/948/sql-server-websites/
www.dba-24x7.com/sql-server-services.php


Indexes:
http://msdn.microsoft.com/en-us/library/ms175049.aspx
http://www.simple-talk.com/sql/learn-sql-server/sql-server-index-basics/
http://www.codeproject.com/Articles/39006/Overview-of-SQL-Server-2005-2008-Table-Indexing-Pa
http://www.sqllion.com/2009/07/sql-server-indexes-pros-and-cons-part-2/


Wednesday, May 22, 2013

How to Recover Database from Suspect Mode in SQL Server

Database in Suspect Mode reasons:

1. When records are inserted (it could be bulk inserts through Stored Procedure) in Application Database from application web form, if power failure on the Database Server Machine occurs during at that time , will there be chances of data corruption for those specific database records.

2. When records are inserted (it could be bulk inserts through Stored Procedure) in Application Database from application web form, if someone stops the SQL SERVER Database Engine Service, will there be chances of data corruption for those specific database records.


3. When the data files are not available. 

Solution:
1. Restore the database backup file.

2. If no backup is available, then run the following commands

   ALTER DATABASE Databasename SET EMERGENCY
   DBCC CHECKDB 'Databasename'
   ALTER DATABASE Databasename SET SINGLE USER with ROLLBACK IMMEDIATE
   DBCC CHECKDB 'Databasename, REPAIR_ALLOW_DATA_LOSS'
   ALTER DATABASE Databasename SET MULTIUSER

Sunday, May 19, 2013

Cannot execute a program. The command being executed was "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe" /noconfig /fullpaths

Hi Team,

While performing patching SQL server 2008 R2 SP2 CU6 on an Active/Active cluster having two nodes e.g. Node1 and Node2, I encountered an unusal error which indicates:

SQL Server Setup has encountered the following error:

Cannot execute a program. The command being executed was "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe" /noconfig /fullpaths @"C:\Users\uswal.dba.va_c\AppData\Local\Temp\-euzaneo.cmdline".

Error code 0x84B10001.



Solution:

1. Start installing the SQL Server 2008 R2 Cumulative Update, by clicking on setup. It will start    
    the extraction of files to a shared disk where there is large amount of Disk space.
2. Once extraction is complete, manually copy the extracted files (check the latest timestamp) to 

    the C drive ( Non clustered disk).
3. Cancel the current installation which is in progress.
4. Start installation again by clicking the setup.exe from C drive , where you have manually 

    copied the extracted files. 

Thursday, May 2, 2013

SQL Server: Database Basics

System Databases
  1. Master: composed of system tables that keep track of server installation as a whole and all other databases that are eventually created. Master DB has system catalogs that keep info about disk space, file allocations and usage, configuration settings, endpoints, logins, etc.
  2. Model: template database. Gets cloned when a new database is created. Any changes that one would like be applied by default to a new database should be made here
  3. Tempdb: re-created every time SQL Server instance is restarted. Holds intermediate results created internally by SQL Server during query processing and sorting, maintaining row versions, etc. Recreated from the model database. Sizing and configuration of tempdb is critical for SQL Server performance.
  4. Resource [hidden database]: stores executable system objects such as stored system procedures and functions. Allows for very fast and safe upgrades.
  5. MSDB: used by the SQL Server Agent service and other companion services. Used for backups, replication tasks, Service Broker, supports jobs, alerts, log shipping, policies, database mail and recovery of damaged pages.
Database Files
  1. Primary data files: every database must have at least one primary data file that keeps track of all the rest of the files in the database. Has the extension .mdf.
  2. Secondary data files: a database may have zero or more secondary data files. Has the extension .ndf.
  3. Log files: every database has at least one log file that contains information necessary to recover all transactions in a database. Has the extension .ldf.
Creating a Database
  1. New user database files must be at least 3 MB or larger including the transaction log
  2. The default size of the data file is the size of the primary data file of the model database (2 MB) and the default size of the log file is 0.5 MB
  3. If LOG ON is not specified but data files are specified during a create database, the size of the log file is 25% of the sum of the sizes of all the data files.
Expanding or Shrinking a Database
  1. Automatic File Expansion:
  1. The file property FILEGROWTH determines how automatic expansion happens
  2. File property MAXSIZE sets the upper limit on the size
  • Manual File Expansion: use the ALTER DATABASE command with the MODIFY FILE option to change the SIZE property to increase the database file size
  • Fast File Initialization: adds space to the data file without filling the newly added space with zeros. New disk content is overwritten as new data is written to the files. Security is managed through Windows security setting SE_MANAGE_VOLUME_NAME
  • Automatic Shrinkage:
    1. Same as doing DBCC SHRINKDATABASE (dbname, 25). Leave 25 % free space in the database after the shrink
    2. Thread performs autoshrink as often as 30 minutes, very resource intensive
  • Manual Shrinkage: use DBCC SHRINKDATABASE if you want to shrink.
  • I highly recommend not to shrink the database.

  • Filegroups
    1. Can group data files for a database into filegroups for allocation and administration purposes.
    2. Improves performance by controlling the placement of data and indexes into specific filegroups on specific drives or volumes.
    3. Filegroup containing the primary data file is called the primary filegroup, there is only one primary filegroup.
    4. Default filegroup: there is at least one filegroup with the property of DEFAULT, can be changed by DBA.
    5. Use cases when -not- to use filegroups:
    1. DBA might decide to spread out the I/O for a database: easiest way is to create a database file on a RAID device.
    2. DBA might want multiple files, perhaps to create a database that uses more space than is available on a single drive: can be accomplished by doing CREATE DATABASE with a list of files on separate drives
  • Use cases when you want to use filegroups:
    1. DBA might want to have different tables assigned to different drives or to use the table and index partitioning feature in SQL Server.
  • Benefits:
    1. Allows backup of parts of the database.
    2. Table is created on a single filegroup, allows for backup of critical tables by backing up selected filegroups.
    3. Same for restoration. Database can be online as soon as primary filegroup is restored, but only objects on the restored filegroups will be available.


    Wednesday, May 1, 2013

    During the SQL Server Service Pack installation, "Consistency validation for SQL Server registry keys" failed.

    Description:

    Issue: Installation of SQL Express 2008 fail with the error: "Consistency validation for SQL Server registry keys" failed

    Error:
    Rule "Consistency validation for SQL Server registry keys" failed
    The SQL Server registry keys from a prior installation cannot be modified. To Continue; see SQL Server Setup documentation about how to fix registry keys.

    Solution-1:

    Navigate to:
    %ProgramFiles%\Microsoft SQL Server\100\Setup Bootstrap\Log

    Search (in Detail_GlobalRules.txt) for lines containing:
    "could not fix registry key"

    Run regedit

    Set full control permissions for the appropriate registry keys mentioned in "Detail_GlobalRules.txt" file.

    Re- run the installation.


    Solution-2

    Steps
    1. Located HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server in registry
    2. Right click and go to Permission
    3. Click on Advance
    4. Tick on both check box
         I. Inherit from parent the permission...
        II. Replace permission entries on all child objects..., click OK
    5. Click OK again