Monday, June 16, 2008
An easy way to track the growth of your database
AS
Select @DBName as [Growth Track of database]
Select BackupDate = convert(varchar(10),backup_start_date, 111) ,SizeInGigs=floor( backup_size/1024000000) from msdb..backupset where database_name = @DBName and type = 'd' order by backup_start_date desc
--EXEC GTrack 'PUBS'
Thursday, June 12, 2008
What is an execution plan? When would you use it? How would you view the execution plan?
Virtual Tables a Trigger uses
What is the basic functions for master, msdb, model, tempdb databases?
The msdb database stores information regarding database backups, SQL Agent information, DTS packages, SQL Server jobs, and some replication information such as for log shipping.
The tempdb holds temporary objects such as global and local temporary tables and stored procedures.
The model is essentially a template database used in the creation of any new user database created in the instance.
What is the basic functions for master, msdb, model, tempdb databases?
The msdb database stores information regarding database backups, SQL Agent information, DTS packages, SQL Server jobs, and some replication information such as for log shipping.
The tempdb holds temporary objects such as global and local temporary tables and stored procedures.
The model is essentially a template database used in the creation of any new user database created in the instance.
Rebuild Master Database
In the Rebuild Master dialog box, click Browse.
In the Browse for Folder dialog box, select the \Data folder on the SQL Server 2000 compact disc or in the shared network directory from which SQL Server 2000 was installed, and then click OK. Click Settings. In the Collation Settings dialog box, verify or change settings used for the master database and all other databases.Initially, the default collation settings are shown, but these may not match the collation selected during setup. You can select the same settings used during setup or select new collation settings. When done, click OK.
In the Rebuild Master dialog box, click Rebuild to start the process. The Rebuild Master utility reinstalls the master database. To continue, you may need to stop a server that is running.
What is the STUFF function and how does it differ from the REPLACE function?
What are the OS services that the SQL Server installation adds?
- MS SQL SERVER SERVICE
- SQL AGENT SERVICE
- DTC (Distribution transac co-ordinator)
What are the different types of replication?
- Transactional
- Snapshot
- Merge
Snapshot replication distributes data exactly as it appears at a specific moment in time and does not monitor for updates to the data. Snapshot replication is best used as a method for replicating data that changes infrequently or where the most up-to-date values (low latency) are not a requirement. Whensynchronization occurs, the entire snapshot is generated and sent to Subscribers.
Transactional replication, an initial snapshot of data is applied at Subscribers, and then when data modifications are made at the Publisher, the individual transactions are captured and propagated to Subscribers.
Merge replication is the process of distributing data from Publisher to Subscribers, allowing the Publisher and Subscribers to make updates while connected or disconnected, and then merging the updates between sites when they are connected.
What is sp_configure commands and set commands?
What command do we use to rename a db?
What is the difference between a local and a global variable?
A global temporary table remains in the database permanently, but the rows exist only within a given connection. When connection are closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.
What is log shipping?
What is Raiseerror?
doesn't change the flow of a procedure; it merely displays an error message, sets the @@ERROR
automatic variable, and optionally writes the message to the SQL Server error log and the NT
application event log.
What is @@ERROR?
What is SQL server agent?
Which command using Query Analyzer will give you the version of SQL server and operating system?
SERVERPROPERTY('productlevel'),
SERVERPROPERTY('edition')
Where are SQL server users names and passwords are stored in sql server?
What are the authentication modes in SQL Server? How can it be changed?
To change authentication mode in SQL Server click Start, Programs, Microsoft SQL Server and click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group.Select the server then from the Tools menu select SQL Server Configuration Properties, and choose the Security page.
Which TCP/IP port does SQL Server run on? How can it be changed?
What is SQL Profiler?
What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
Difference between Function and Stored Procedure?
What is difference between DELETE & TRUNCATE commands?
TRUNCATE
TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.TRUNCATE removes the data by deallocating the data pages used to store the table’s data, and only thepage deallocations are recorded in the transaction log.TRUNCATE removes all rows from a table, but the table structure and its columns, constraints, indexesand so on remain. The counter used by an identity for new rows is reset to the seed for the column.You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint.Because TRUNCATE TABLE is not logged, it cannot activate a trigger.TRUNCATE can not be Rolled back.TRUNCATE is DDL Command.TRUNCATE Resets identity of the table.
DELETE
DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.If you want to retain the identity counter, use DELETE instead. If you want to remove table definitionand its data, use the DROP TABLE statement.DELETE Can be used with or without a WHERE clauseDELETE Activates Triggers.DELETE Can be Rolled back.DELETE is DML Command.DELETE does not reset identity of the table.
How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?
One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.
Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table.
What's the difference between a primary key and a unique key?
What is Collation?
What is a Linked Server?
What is the use of DBCC commands?
E.g.
DBCC CHECKDB - Ensures that tables in the db and the indexes are correctly linked.DBCC CHECKALLOC - To check that all pages in a db are correctly allocated.DBCC CHECKFILEGROUP - Checks all tables file group for any damage.
What is cursors?
In order to work with a cursor we need to perform some steps in the following order:
- Declare cursor
- Open cursor
- Fetch row from the cursor
- Process fetched row
- Close cursor
- Deallocate cursor
What are the different index configurations a table can have?
- No indexes
- A clustered index
- A clustered index and many nonclustered indexes
- A nonclustered index
- Many nonclustered indexes
What is the difference between clustered and a non-clustered index?
A nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows
What is Index?
Clustered indexes define the physical sorting of a database table’s rows in the storage media. For this reason, each database table may have only one clustered index.
Non-clustered indexes are created outside of the database table and contain a sorted list of referencesto the table itself.
What is View?
What is Trigger?
Nested Trigger: A trigger can also contain INSERT, UPDATE and DELETE logic within itself, so when thetrigger is fired because of data modification it can also cause another data modification, thereby firinganother trigger. A trigger that contains data modification logic within itself is called a nested trigger.
What is Stored Procedure?
e.g. sp_helpdb, sp_renamedb, sp_depends etc.
What are different normalization forms?
Make a separate table for each set of related attributes, and give each table a primary key. Each field contains at most one value from its attribute domain.
2NF: Eliminate Redundant Data
If an attribute depends on only part of a multi-valued key, remove it to a separate table.
3NF: Eliminate Columns Not Dependent On Key
If attributes do not contribute to a description of the key, remove them to a separate table. All attributes must be directly dependent on the primary key
BCNF: Boyce-Codd Normal Form
If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables
What is normalization?
What is RDBMS?
Saturday, June 7, 2008
Stored Proc to list all object dependencies
Go
sp_depends displays information about database object dependencies.
sp_depends displays two result sets. First One shows the objects on which the 'given
object' depends and the second shows the objects that depend on the 'given object'
Friday, June 6, 2008
Query to Check if a table is system table or User table
0 --> Indicates User Table
1 --> Indicates System Table
Query to mark a user table as a system table
dtproperties Table in SQL Server 2000
gets updated automatically.
Basically this is a system table but when we run the below query we will get the name of
dtproperties table in the result set, indicating that this is user table.Microsoft has
confirmed that this is a bug in the Microsoft SQL Server.
Select * from sysobjects where xtype='u'
Thursday, June 5, 2008
Finding Difference between Tables in SQL 2005
TableDiff.exe.
TableDiff.exe is a comparison tool that is provided with the sql server.
It will be found in the following location:
"C:\Program Files\Microsoft SQL Server\90\COM\TableDiff.exe"
The Drive name depends on where the SQL is Installed.
This tool compares 2 tables of the same database on the same server and creates a new
table called new Table that holds the differences.
"C:\Program Files\Microsoft SQL Server\90\COM\tablediff.exe"
-sourceserver ServerName
-sourcedatabase DatabaseName
-sourcetable TableName
-destinationserver ServerName
-destinationdatabase DatabaseName
-destinationtable TableName
-et NewTableName
For more information on this utility check out the microsoft website:
http://msdn2.microsoft.com/en-us/library/ms162843.aspx
Undocumented Stored Procedure to get SQL Version
Different ways of finding SQL Version
1. EXEC master..sp_MSgetversion
2. SELECT SERVERPROPERTY('productversion'),SERVERPROPERTY ('productlevel')
3. SELECT @@version
4. EXEC master..xp_msver
The sp_MSgetversion is an Undocumented Stored Proc to find the SQL Version.
Where are Temp tables found???
Select * from tempdb..sysobjects where type = 'U'
Status of the Database
1 = autoclose (ALTER DATABASE)
4 = select into/bulkcopy (ALTER DATABASE using SET RECOVERY)
8 = trunc. log on chkpt (ALTER DATABASE using SET RECOVERY)
16 = torn page detection (ALTER DATABASE)
32 = loading
64 = pre recovery
128 = recovering
256 = not recovered
512 = offline (ALTER DATABASE)
1024 = read only (ALTER DATABASE)
2048 = dbo use only (ALTER DATABASE using SET RESTRICTED_USER)
4096 = single user (ALTER DATABASE)
32768 = emergency mode
4194304 = autoshrink (ALTER DATABASE)
1073741824 = cleanly shutdown