<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Database Archives - Pritam&#039;s Blog</title>
	<atom:link href="https://www.pritambaldota.com/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.pritambaldota.com/category/database/</link>
	<description>cloud technology, enterprise architecture</description>
	<lastBuildDate>Thu, 09 Jan 2020 07:21:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>SQL Server Database Triggers</title>
		<link>https://www.pritambaldota.com/sql-server-database-triggers/</link>
					<comments>https://www.pritambaldota.com/sql-server-database-triggers/#respond</comments>
		
		<dc:creator><![CDATA[Pritam Baldota]]></dc:creator>
		<pubDate>Mon, 16 Aug 2010 15:32:59 +0000</pubDate>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[database triggers]]></category>
		<category><![CDATA[SQL Server Database Triggers]]></category>
		<category><![CDATA[sql triggers]]></category>
		<guid isPermaLink="false">http://www.pritambaldota.com/?p=52</guid>

					<description><![CDATA[<p>A trigger is an object contained within an SQL Server database that is used to execute a batch of SQL code whenever a specific event&#8230;</p>
<p>The post <a href="https://www.pritambaldota.com/sql-server-database-triggers/">SQL Server Database Triggers</a> appeared first on <a href="https://www.pritambaldota.com">Pritam&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<ul>
<li>A trigger is an object contained within an SQL Server database that is used to execute a batch of SQL code whenever a specific event occurs.</li>
<li>A trigger is â€œfiredâ€ whenever an INSERT, UPDATE, or DELETE SQL command is executed against a specific table.</li>
<li>A Trigger is associated with Database Table</li>
</ul>
<p>There are three types of Database Triggers &#8211;</p>
<ol>
<li>INSERT TRIGGER</li>
<li>UPDATE TRIGGER</li>
<li>DELETE TRIGGER</li>
</ol>
<p><strong><span style="text-decoration: underline;">INSERT TRIGGER</span></strong></p>
<ul>
<li>Body of trigger can have any block which a Stored Procedure can have</li>
<li>Body can call SP using Exec</li>
<li>When SQL server processes this &#8220;INSERT&#8221; command, it creates a new virtual table,Â which contains all nine of the fields in the &#8220;INSERT&#8221; command. This table is namedÂ <strong><em>&#8220;Inserted&#8221;</em></strong>, and is passed to the <em>trig_addAuthor </em>trigger.</li>
<li>At any one time, each trigger only deals with one row.</li>
<li>So for INSERT Trigger only hold reference of <em>Inserted virtual Table</em></li>
</ul>
<p><span style="color: #0000ff;">CREATE TRIGGER trig_addAuthor<br />
ON authors<br />
FOR INSERT<br />
AS<br />
&#8212; Get the first and last name of new author<br />
DECLARE @newName VARCHAR(100)<br />
SELECT @newName = (SELECT au_fName + &#8216; &#8216; + au_lName FROM<br />
Inserted)<br />
&#8212; Print the name of the new author<br />
PRINT &#8216;New author &#8220;&#8216; + @newName + &#8216;&#8221; added.&#8217;</span></p>
<p><strong><span style="text-decoration: underline;">UPDATE TRIGGER</span></strong></p>
<ul>
<li>The &#8220;UPDATE&#8221; function is used to check whether or not the &#8220;au_fName&#8221; and &#8220;au_lName&#8221; fields have been updated by the &#8220;UPDATE&#8221; Query</li>
<li>&#8220;UPDATE&#8221; triggers have access to two virtual tables: Deleted (which contains all of the fields and values for the records before they were updated), and Inserted (which contains all of the fields and values for the records after they have been updated)</li>
<li>Update triggers can also be used to check field constraints and relationships</li>
</ul>
<p><span style="color: #0000ff;">CREATE TRIGGER trig_updateAuthor<br />
ON authors<br />
FOR UPDATE<br />
AS<br />
DECLARE @oldName VARCHAR(100)<br />
DECLARE @newName VARCHAR(100)<br />
IF NOT UPDATE(au_fName) AND NOT UPDATE(au_lName)<br />
BEGIN<br />
RETURN<br />
END<br />
SELECT @oldName = (SELECT au_fName + &#8216; &#8216; + au_lName FROM Deleted)<br />
SELECT @newName = (SELECT au_fName + &#8216; &#8216; + au_lName FROM Inserted)<br />
PRINT &#8216;Name changed from &#8220;&#8216; + @oldName + &#8216;&#8221; to &#8220;&#8216; + @newName + &#8216;&#8221;&#8216;</span></p>
<p><strong><span style="text-decoration: underline;">DELETE TRIGGER</span></strong></p>
<ul>
<li>It has only access to the virtual table &#8220;Deleted&#8221;</li>
<li>Same as INSERT, UPDATE</li>
</ul>
<p><span style="text-decoration: underline;"><strong><span style="color: #0000ff;">ADVANTAGES Of TRIGGERS</span></strong></span></p>
<ol>
<li>When triggers are used correctly, they can save a lot of development work. One of the main benefits of using triggers is that they are stored in a central repository (the database), meaning that they are accessible from all client applications / web pages that can connect to the database.</li>
<li>Before triggers came along, if we had a table that needed to be updated and we wanted to perform some actions after that update, then we would have to &#8220;hard code&#8221; the extra SQL into our application. This meant that if we wanted to change the code later down the track, then each client would need the updated version of ourÂ application. This is both annoying and time consuming.</li>
</ol>
<p>Happy Coding <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.pritambaldota.com/sql-server-database-triggers/">SQL Server Database Triggers</a> appeared first on <a href="https://www.pritambaldota.com">Pritam&#039;s Blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.pritambaldota.com/sql-server-database-triggers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Optimizing SQL Server Database Design Tips &#038; Tricks</title>
		<link>https://www.pritambaldota.com/database-design-tips-tricks-part-1/</link>
					<comments>https://www.pritambaldota.com/database-design-tips-tricks-part-1/#respond</comments>
		
		<dc:creator><![CDATA[Pritam Baldota]]></dc:creator>
		<pubDate>Mon, 08 Jun 2009 02:47:39 +0000</pubDate>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[asp.net database performance]]></category>
		<category><![CDATA[database tunning]]></category>
		<category><![CDATA[improve performance of database queries in asp.net]]></category>
		<category><![CDATA[sql server tunning]]></category>
		<guid isPermaLink="false">http://www.pritambaldota.com/?p=13</guid>

					<description><![CDATA[<p>Database design is most crucial part in application development. Database can be anything SQL Server, MySQL, Oracle, etc. I will consider SQL SERVER as a&#8230;</p>
<p>The post <a href="https://www.pritambaldota.com/database-design-tips-tricks-part-1/">Optimizing SQL Server Database Design Tips &#038; Tricks</a> appeared first on <a href="https://www.pritambaldota.com">Pritam&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Database design is most crucial part in application development. Database can be anything SQL Server, MySQL, Oracle, etc. I will consider SQL SERVER as a part of discussion.</p>
<p>Tunning the database depends upon various factors like whether one datatype is better depends on the context of it&#8217;s usage. Following are most common datatypes used in database.</p>
<p><strong><span style="text-decoration: underline;"><span class="Apple-style-span" style="font-size: small;">Numbers Datatype (For storing Primary Keys, Foreign Keys, Status etc)</span></span></strong><span class="Apple-style-span" style="font-size: small;">Â </span></p>
<p>An int requires 4 times the storage of a tinyint. More storage means more data and</p>
<p>index pages to read / write, more I/O and thus potentially more seek time. It also means more memory consumption.</p>
<p>For eg. Desigining User Role and Users Table. Generally application will not have more than 5-10 user roles like Admin, Manager, Employee, etc. Most of developers design the table like -Â </p>
<p><strong>Table &#8211; UserRole</strong></p>
<p><strong>RoleId</strong> â€“ Integer (whole number) data from â€“2^31 (â€“2,147,483,648) through 2^31â€“1 (2,147,483,647). It takes 4 Bytes to store.</p>
<p><strong>RoleName</strong> â€“ Varchar (50)</p>
<p>Which is not suiting to our requirements. Instead of int we can use tinyint &#8211; Integer data from 0 to 255. Storage size is 1 byte.Â </p>
<p>&nbsp;</p>
<p><strong><span style="text-decoration: underline;"><span class="Apple-style-span" style="font-size: small;">DateTime Datatype (For storing dates)</span></span></strong></p>
<p>Most of the times database tables required date to be stored to maintain record creation date.Â </p>
<p><strong>DateTime</strong> data type are stored internally two 4-byte integers. The first 4 bytes store the number of days before or after the base date: January 1, 1900. The base date is the system reference date. The other 4 bytes store the time of day represented as the number of milliseconds after midnight.</p>
<p>The <strong>smalldatetime</strong> data type stores dates and times of day with less precision than datetime. The Database Engine stores smalldatetime values as two 2-byte integers. The first 2 bytes store the number of days after January 1, 1900. The other 2 bytes store the number of minutes since midnight.</p>
<p>So, instead of using datetime as datatype smalldatetime is more efficient in general development.</p>
<p>&nbsp;</p>
<p><strong>STRING DATATYPE (Storing text data)</strong></p>
<p>All string data stored inside varchar, text, ntext, nvarchar, etc.Â </p>
<p>For eg. FirstName data field developers used varchar or nvarchar with default size i.e. 50. We should know the scope of the filed in terms of maximum characters. Generally first name, last name etc.. won&#8217;t take more than 20 â€“ 25 characters. So instead of 50 if we use 20 characters we can save half the storage, index size which will increase performance of queries.Â </p>
<p>Â These are the basics which needs to keep in mind while developing applications.Â </p>
<p>Happy Coding &#8230; <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.pritambaldota.com/database-design-tips-tricks-part-1/">Optimizing SQL Server Database Design Tips &#038; Tricks</a> appeared first on <a href="https://www.pritambaldota.com">Pritam&#039;s Blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.pritambaldota.com/database-design-tips-tricks-part-1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
