Category: Uncategorized

  • Is the primary key always clustered in SQL Server?

    As I was playing in SSMS (SQL Server Management Studio), I created a table using the plain-old T-SQL, and then I decided to create a primary key, the “wizard” way, so to speak.

    Let’s have a repro here:

    CREATE TABLE [dbo].[Employees]
        (
          [empID] [int] NOT NULL ,
          [firstname] [varchar](30) NOT NULL ,
          [lastname] [varchar](30) NOT NULL ,
          [salary] [money] NOT NULL,
        );

    Okay, now, in order to create a primary key on the empID column, I can either:

    Use the following T-SQL:

    ALTER TABLE dbo.Employees ADD CONSTRAINT
    	PK_Employees PRIMARY KEY
    	(
    	empID
    	)

    Or use the wizard:

    • Expand your database node (testdb, in my case), navigate to your table, right-click it and select Design from the menu. Do not worry about the extra menus and buttons that you see, they are installed when you install SQL Toolbelt, a wonderful gift that I have from our friends at Redgate.

    • Select the column header that you wish to be your primary key (that is, in the Designer tab) and then click the Set Primary Key button (should look like this )
    • Click Generate Change Script (right to the left of Set Primary Key). The Save Change Script dialog box is displayed. Right-click and select Select All.

    • Copy the text and close the window by selecting No. Paste the text in a new query window:
    /* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
    BEGIN TRANSACTION
    SET QUOTED_IDENTIFIER ON
    SET ARITHABORT ON
    SET NUMERIC_ROUNDABORT OFF
    SET CONCAT_NULL_YIELDS_NULL ON
    SET ANSI_NULLS ON
    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
    COMMIT
    BEGIN TRANSACTION
    GO
    ALTER TABLE dbo.Employees ADD CONSTRAINT
    PK_Employees PRIMARY KEY CLUSTERED
    (
    empID
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    
    GO
    ALTER TABLE dbo.Employees SET (LOCK_ESCALATION = TABLE
    )
    GO
    COMMIT


    I can see some problems already with this approach:

    • The first part is that there’s a BEGIN TRANSACTION for all the database properties. Why is this there? I have no idea, but presumably it looks cool, even if useless.
    • The second part is that there’s the ADD CONSTRAINT that goes with PRIMARY KEY CLUSTERED. Clustered? When did I say that? J
    • Lastly, the LOCK_ESCALATION = TABLE might not be the best idea, at least not always

    OK, now that we spotted some issues, I presume it’s safer to go with the script instead of the wizard:

    ALTER TABLE dbo.Employees ADD CONSTRAINT
    	PK_Employees PRIMARY KEY
    	(
    	empID
    	)

    Now I have successfully created my primary key, so let’s go check it:

    • Expand the Employees table, and then expand the Indexes node.

    Shoot, the primary key is still clustered!

    The answer is this: if you don’t have a clustered index on the table and you don’t define your primary key as being non-clustered, it will assume on your behalf that there should be a clustered index on your primary key.

    I hope this helps.

    Best regards,
    io

  • How to Disable NetBIOS on the SQL Server Box

    Today I’ll post a quick one, since a simple Google search (yeah, Bing, sorry) did not yield too many useful results.

    Just as a quick note, make sure you have a chat with your sys admins; they can tell you whether the protocols are needed or not anymore.

    Okay, on with the tip:

    • Go to Device Manager (either right-click My Computer and select Manage from the contextual menu, or go to Administrative Tools > Computer Management. The Server Management dialog box is displayed, as shown below

    • Expand Diagnostics, and then select Device Manager. You will get to see a screen similar with the one shown below:

    • Go to View menu and select Show Hidden Devices – we’re not getting closer to what we need. Expand the Non-Plug and Play Drivers node (that was not there previously).
      Right-click NETBT (if I remember correctly, it’s called NetBIOS over TCP/IP in Windows 2003) and select Uninstall.

    • Pray that you’ll not get a call in the next few days saying: “what did you do, my app stopped working!”

    Okay, now what you’re actually finding by searching Google and Bing:

    • Open Network and Sharing Center and click the Change Adapter Settings. You should see something similar with the dialog box below:

    • Right-click the net adapter (in my case there’s only one, but you need to change the one that is subject to have its properties changed) and select Properties from the contextual menu.

    • Ah, almost forgot, if you need to disable the SMB as well, select File and Printer Sharing for Microsoft Networks and select Uninstall. Moving further, select Internet Protocol Version 4 and select Properties.

    • Click Advanced and then click the WINS tab.

    • Click the Disable NetBIOS over TCP/IP radio button.

    Now you know how to completely disable NetBIOS, or to “suspend” it from the Advanced TCP/IP settings. I would go for the latter, since most likely somebody will call either you or the sys admins telling that there is an app that needs that particular “protocol“.

    I hope this helps.

    Best regards,
    io

  • To 32 or not to 32 on Windows 7

    Finally I got 8 GB of RAM on my laptop. That’s a great thing, especially that I have free licenses to pretty much whatever Microsoft has as x64, except that I’m married and this is not quite my laptop, but my wife’s. If you ask anyone on the planet, you are to find out that she has all the brains and the beauty in our home; after all, those who can do, will do, and those who cannot do, will teach.

    To cut it short: I have Windows 7 32-bit installed on my laptop and I want to access all the RAM, without installing Windows 7 x64. How hard can it be? Just run the following at the command prompt:

    BCDEdit /set PAE forceenable 

     

    Of course, the command executes successfully, and after the reboot I’ll be able to access all the memory, right? Wrong.

    Okay, Google is my friend and I search how to access all the memory on a x86 platform. So I get some interesting posts:

    http://msdn.microsoft.com/en-us/library/aa366778.aspx – this article tells me that the absolute maximum memory that I can access is 4 GB in my case. Of course, they must be wrong, so let’s try another post that actually helps me. After all, they are Microsoft, what do they know about Windows?

    http://blogs.technet.com/b/markrussinovich/archive/2008/07/21/3092070.aspx – there’s this guy, Mark Russinovich that explains at a very technical level why I cannot access more than 4 GB of memory on my machine. Very misleading title of the article, and of no help for my caseJ.

    http://www.unawave.de/windows-7-tipps/32-bit-ram-barrier.html?lang=EN – finally! So I can actually do itJ.

    Key takeaways:

    • You can enable access to more than 4 GB; it’s a licensing issue, not an architectural issue. But because you can does not mean that you should
    • If you do it, chances are that your apps are not aware of how smart and sexy you are – this is definitely true for SQL Server. So, if you want to use more than 4 GB for SQL Server (say, 6 GB, as in my case), you need to do the following:

    Enable the Use AWE to allocate memory checkbox, and specify the memory to use (6X1024 MB = 6144 MB) in the Maximum Server Memory text field. That’s it.

    One word of caution: I did not test this, so I have no idea whether is actually working or not; I advise you to do the same thing I do: simply switch to x64 architecture, especially that we’re talking servers and licensing issues here.

    Best regards,
    io

    Quick edit: you do the same by using sp_configure sproc. You simply neeed to enable the advanced options, set the AWE Enabled to 1, and set the memory for Max Server Memory.

  • SQL Server installation tips… NOT

    I have to admit that I shamelessly “steal” any info available on the net when it comes to SQL Server. Just to give you a quick example: Brent Ozar and Jonathan Kehayas both have some neat articles about installing SQL Server. Of course, I use the Next > Next > Finish feature, just as anybody else, but every now and then some wise IT managers are asking me “why did you do this”. Then I get to tell him or her that is because some guys said that this is good practiceJ.

    Anyway, moving on, I want to point out how to change the default installation of SQL Server.

    Change the default installation path

    Truth be told, I have no idea how to change the default installation path for SQL Server per se, but rather to where the Program Files folder will reside. To change the default location:

    1. Go to Registry Editor (regedit); on my local machine (Windows 7), the sequence is Start > type regedit in the Search Programs and Files text box, and click regedit.exe.
    2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion

    You should see something similar to the window (ok, dialog box; stop it, you technical writers) below:

    As you can see, the CommonFilesDir entry holds the default path of Program Files. Simply change the value data in the Edit String dialog box (double-click the entry to get there) and you’re done.

    I hope this helps.

    Best regards,
    io

  • Strange error in SQL Server 2005… when aplying SP3

    Before going any further, let’s get this straight: I’m not sure whether it’s a bug or not, whether can be reproduced or not, but I care to have this documented.

    A machine was re-installed (Win 2003, SQL 2005), having 3 named instances (no default, just named instances). SP3 was applied… and nothing was like before:-). You could start only one instance (any instance, for that matter), but not all of them.

    Apparently the solution was to install SP3 on all the instances at the same time, and not one after another.

    I know, it’s generic, it might not be relevant because it is that generic, but again, if you ever encounter this, you might want to check this among other things.

    best regards,
    calin


    PS > still couldn’t replicate it, so it must be something else:-)

    PS 2 > now ladies and gentlemen… for the solution: NEVER replicate everything from production to each of the instances (that would mean also the system databases).

    see here and here for additional details.

    as for me, I’m still laughing in tears:-)

  • SQL 2008 DBA now

    I made myself an early Christmas present: since I had a free exam voucher from Microsoft, I said to myself heck, since I’m not paying for the exam, it’s kinda time to upgrade my DBA skills from 2005 to 2008. That is, 70-453.

    OK, I passed, but I want to tell you some impressions:

    • tough exam
    • difficult exam
    • not piece of cake exam

    Having said that, the main idea is this: it’s actually a combination of 70-432 and 70-450 exams, namely you’ll get around 25 questions from each exam – at least this is what I got from my paper, reporting that I passed both MCTS and MCITP exams:-). In addition, make sure you know the stuff, because they are gonna test your skills, ranging from DMVs all the way to disk subsystem configuration.

    The only thing left to be said is the fact that yes, finally, there’s no chance for you to have a single choice question and more than one correct answer. It might seem like, but trust me, it’s not the case. I learned this on my own, as I screwed up some answers to apparently simple questions.

    Final word on preparation materials: there’s a little hidden gem on the net, called SQL Server 2008 Jumpstart, which has been turned into free e-clinics on Microsoft e-learning site by now. Believe me, if you already have some SQL Server knowledge and you’re not taking this learning thing from scractch, there’s nothing much that you need besides these videos.
    The little much that you need, though, are the (famous by now) “Inside SQL Server 2008” series for SQL Server Development, and the SQL Server 2008 Architecture and Internals, all from MS Press. Of course, you can also read the auhtor’s blogs, which are by all measures far more interesting than mine, but I’m working in that direction:-).

    I’ll close with an anecdotical story: I took the test at the same center where I teach SQL Server classes. The guy said: OK, if you fail the exam, at least that was for free. I told him: if I fail this exam, then it means that I did not pay much attention to what the teacher had explained last week, when teaching the administration course.
    You can pretty much guess who was the teacher:-)

  • talking about customer care:-)

    I just received (as an MCT I get all kinds of goodies) a free licence of PerfectDisk 11. So far, so good… but: “the official release isn’t until tomorow, but I wanted to make sure our MCTs had it in their hands a little early”.
    This is what I call “yes, they care about me”.

    best regards,
    io

  • Simple lesson learned

    KISS (Keep it stupid simple) 🙂

    “System.Data.OleDb.OleDbException: [DBNETLIB][ConnectionOpen (PreLoginHandshake()).]General network error. Check your network documentation.” – that was the wife having an issue.

    I told her: okay, do you have SQLNCLI? Obviously, she had it installed.

    Me: well then, check here:
    http://blogs.msdn.com/sql_protocols/archive/2005/10/22/483684.aspx
    http://blogs.msdn.com/sql_protocols/archive/2005/10/29/486861.aspx

    It turned out that SQL Browser service was not started. Again, KISS or be KISS-ed.

  • "Dreadful" i/o errors

    Okay, now I have to admit that this one is not quite cert related, but still:

    Friend: I tango with a suspect database.
    Me: suspect database? why?
    Friend: well, at some point I got an error telling me that it took too much to write something to the database.
    Me: that would be a premiere… sure it ain’t something else?
    Friend: well, I was lucky, no errors reported by DBCC, and I could bring the database online (yes, ALTER DATABASE stuff).

    Long story short: it was a soft i/o error, 823 (although initially reported by the 1117 error). And it was thrown because they were running out of space in one of their virtual machines. So you can encounter i/o errors in virtualized environments as well, so please bear this in mind.
    Also, mind that i/o error 823 is what we call “soft i/o”, while i/o error 824 is what we call “hard i/o”. While 823 is a reason to worry (but no so much, as I’ve shown you in this post), a 824 error is an indication that your disk subsystem is about to give you some unpleasant surprises.

    I hope this helps.

  • First blog

    Well, for whatever reason I decided to run this blog (it’s just a first step of many more to come); it will actually contain all the bells and whistles I am hearing during my SQL Server 2005 courses, which I’m currently teaching.

    First, a bit about me: MCT in SQL Server 2005 – DBA, Windows 2000/2003, married, one kid, another one on the way. Should be more than enough.
    Purpose of this blog: help you get certified – now, there’s a catch, though: get certified through knowledge (which I’m willing to share) and not just cramming the exam. Reason: just go to a job interview and you’ll get the point.
    Area of (above average knowledge): well, SQL Server 2005 Administration :-).

    Let’s have some fun (in the near future).

    best regards,
    calin