Tuesday, May 25, 2010

Database Standards Part III: Indexes, Constraints & Primary/Foreign Keys

This is Part 3 of 6 blogs about database standards we use at my current employer. Repeating myself, though we do not enforce these with an iron fist, they are advised for clarity and consistency. The important point, in my opinion, is to establish a set of standards and stick to them.


Indexes
The naming of indexes is not unanimous. The standard is for a prefix of idx plus the table name then the field names with an underscore between the table and field name – idxPatient_LocationCode. This will create some very long names. I prefer abbreviation of the table and fields names, or like another DBA a reference name of where the index will be used.

A reminder to developers and contractors is that a primary key clustered index is an index. We have seen duplicate indexes created because of this. I kid not. Also, clustered indexes are included in non-clustered indexes, so a multi-column clustered index has to be seriously reviewed before implemented.

Understanding covering indexes and how they are used is important to reduce the columns used in some indexes. Fields that are regular updated probably should not be in a clustered index.

A reindex maintenance plan needs to be implemented. I will try to cover the custom one we use in a later blog or webcast.

Constraints
A unique constraint has its own index, so do not create an index on a unique constraint.

Use prefix chk (chkDigits) for check constraint and uc (ucPatientNumber) for unique constraint naming.

Default definitions should be in line with the create table statement and default objects have been deprecated, so do not use. Do not use data type conversion in constraint definitions.

Primary Key and Foreign keys
All tables have to have a primary key. When an identity field is used, a unique constraint must be created from a separate field(s) to uniquely identify a row. Every table needs to be able to be queried based on the contents, not an identity field. The normalization talks I have been privileged to give, explains more about this with the 1st normal form portion.

All foreign keys have to be indexes and should be the primary or unique constraint key from the parent table.

Naming convention is prefixed with pk for primary key (pkPatientNumber). Foreign Key naming is prefix fk then parent table name underscore column(s) underscore child table name and last underscore column(s) – fkPatient_ID_Admit_PatientID

Repeating Again, and again…
Though my opinion is not the same as some of these standards (I say you can abbrev. anything), I do believe standards should be discussed and established, then followed. IF a deviation is needed, a group discussion should be voted on to change.

Next week I will talk about Stored Procedures.

Thomas LeBlanc

Thursday, May 13, 2010

Database Standards Part II: Schemas, Tables & View and Columns

This is Part 2 of 6 blogs about database standards we use at my current employer. Repeating myself, though we do not enforce these with an iron fist, they are advised for clarity and consistency. The important point, in my opinion, is establish a set of standards and stick to them.

Schema

The seperation of schmea name and logins was a great addition in SQL Server 2005. Schema names should be used in place of dbo. This makes viewing in SSMS great with filters. It also categorizes the objects so a new DBA or developer can get a good view of the organization in a database. We suggest a Common schema for objects that do not fit in a group, then names that are specific to functionality - Payroll, HR, Billing, etc.

Tables
Tables names should reflect its functionality and they cannot start with a number, contain spaces, underscores or be reserved words. ANSI_NULL (OFF is deprecated in a future version) should be SET ON. Pascal Casing should be used like ClientBill and singular like Admit not Admits.

There are 3 different kinds of Lookup tables we see - developer populated (workflow), small lists (state) and known list that are updattable (RevenueCode). Small lookup tables can use a character primary key. If the developer insists on ID (identity), then a character code/description column created with a unique contraint must be included. The code should be used in the transaction table. (I will clarify this is a seperate blog). Lookup tables use camel case - lkState.

The transaction table(s) should use a ID (indentity or sequential int) as the primary, but should have a unique contraint for uniquely identifying a row. Third Key Normal Form and higher is required. The ID field will be the foreign key in the child table, and named ID (PatientID in an Episode table).

Views
Camel casing should be used in naming views - vwPatientEpisode. Future thinking should be used if the view will eventually become an indexed view, then schema binging and ansi settings need to be looked at.

Columns
Column names should describe the data placed in them, using Pascal Casing - LastName and not start with a number/special character, no spaces, dashes or underscores. Beware of reserve words or key words as object names. Do not repeat table name - use LastName not PatientLastName unless a foreign key to another table - PatientID. No data type in the name - intPatientCount, but Date is acceptable in a date name - AdmitDate.

Be aware of new features in 2008 like a seperate DATE and TIME data types. Before, you had to use midnight as the time in a date column type in order to not specify a time.

Repeating
Though my opinion is not the same as some of these standards (I say you can abbrev. anything), I do believe standards should be discussed and established, then followed. IF a deviation is needed, a group discussion should be voted on to change.


Next week I will talk about Indexes, constraints and Primary/Foreigh Keys.


Thomas LeBlanc

Thursday, May 6, 2010

Database Standards Part I: Defs, Abbreviation & Data Types

This is Part 1 or 6 blogs about database standards we use at my current employer. Though we do not enforce these with an iron fist, they are advised for clarity and consistency. The important point, in my opinion, is establish a set of standards and stick to them.

Definitions

We list some definitions are the beginning of our document. Included are Camel casing (vwVisitOrders) and Pascal casing (MedicalData)

Abbreviations

Though most developers like to spell everything out because they can use IntelliSense, abbreviations are ok for some common and industry standard abbreviations that can be used. Examples are Id (identification), SSN, DOB or Rpt (Report).

Data Types

Now, the standards for these are good because of the consistency factor, comparisons (WHERE clause) and SQL Server features. Some features of SQL Server get deprecated, and others are improvements. A good example is using varchar (max) instead of text data type.
   Yes/No: Use bit data type. We also suggest naming the field like a question: Isdeleted
   Flag/Status: use tinyint (1-Active, 0-Inactive, other numbers can be used for future status)
   Use Unicode only if necessary: nvarchar(100)
   Integers: Be sure to properly size up front, if not sure use int or bigint
   Fixed field character lengths: State - char(2)
   Variable character length: LastName varchar(25)
   Money/Decimal – use over float/real. Price decimal(10,2)
   Null/Not Null – won’t even discuss
   Max – use with varchar, nvarchar, vbinary instead of text and binary.

Though my opinion is not the same as some of these standards (I say you can abbrev. anything), I do believe standards should be discussed and established, then followed. It a deviation is needed, a group discussion should be voted on to change.

God Bless,
Thomas LeBlanc

Thursday, April 29, 2010

SQLSaturday New York: Smiling all day

SQLSaturday is catching on all over the place. It is amazing how many people are thirsting for SQL Server knowledge. One of the best experiences I have when going to a session, is the validation that what I am doing at work is on the right track.

First, I would like to thank the sponsors for funding the event. Without them the event is not possible.

Confio (Ignite) even invited the speakers and volunteer to a Friday evening welcoming. It was just what was needed the night before the event. Thank you Matt Larson and David Waugh.

Other sponsors included Quest, Idera, Expressor, Redgate, Pearl, XLeratorDB, PASS, Pragmatic Works, TwentySix, SetFocus, Agile Technologies, Microsoft, CozyRoc and Apress.

Biggest thank you goes out to the NJ SQL Server User Group and volunteers, who did a top notch job. Thanks Alex for helping the speakers and SQLDiva for leading the group.

Breakfast was plentiful, coffee, orange juice, milk, donuts, bagels, etc.

The first session I attended with Andy Leonard SSIS Design Patterns. Andy did a great job with 2-3 patterns involving variables and events in  SSIS.

Next was Matt Larson(Confio), showing how to use sysprocesses and DMV/DMFs to get performance information from SQL Server. A gentleman from the audience encourage all in attendance to install the trial version on Ignite and we would be amazed with the results from this tool. I am trying to schedule a day or 2 for a trial run.

Lunch was in the speakers room chatting with other speakers and volunteers. Next to me was Thomas LaRock, Andy Leonard, Grant Fitchey, Slava Kokaev, Adam Jorgensen and others. Lunch was box style and worked great.

Grant was up next with a great presentation on More Unnecessary Tuning. Basically, some do's and do not's up front when designing data access.

Andy Leonard continued in the afternoon with Incremental Loads in SSIS, but I had to leave before the end to prepare for my session. The Hash compare for changes interested me.

To close the day, I presented 3rd Key Normal Form: That's Crazy Talk!!! The room was a conference room with a table in the middle and chairs all arounnd with somewhere betwee 20-30 attendees(1 or 2 had to sit on the floor). The session was directed at good and bad DB designs, but I probably only got through the first 10-12 slides before the discussion went to Visio diagrams and SSMS scripting.

The participation from the attendees was great, I just had to keep the focus on the topic - kinda of like leading a meeting at work. It turned out better than I imagined and had 5-6 attendees stay a little while afterwards to thank me and offer some great advice and comments.

One open question was about Attributes on an Entity that vary based on a product type. I have never used XML as a data type, but this seemed to be a good case. Another peson suggested looking into EAVs design. I displayed a design in Visio that got a couple of people saying, 'That is not normal form.' OUCH!!!

It seems like a good session topic for 4th and 5th key normal form, and some industry standards or examples.

Plenty of prizes were given away by the sponsors and the conference.

My next Blog post will be a series on the DB Standards document from my current employer. I could not place this with my session because it belongs to Amedisys.




God Bless,
Thomas

Friday, April 16, 2010

Maintenance Weekend...

This Saturday is the third Saturday of the month, which means I will be working. We have a shutdown were the networking, DBA and security teams can update the systems while it is offline to end users.

These functions used to have to be done on unscheduled evenings when ever possible, but now it has been on Saturday for over a year. The time has gone from 6-10 hours to 2-3 if no problems occur. Also, because of failover systems, the work has been lighter.

LSU plays Alabama in baseball and I will be able to go to Sat and Sun. They beat Tulane on Wednesday night 10-4. Always enjoyable to see use take down the greenies.

God Bless,
ThomBeaux

Thursday, April 8, 2010

Passed 70-431, now MCITP DB 2005

I assumed by reading the title of 70-447, that all I needed to upgrade MCDBA to MCITP was to take this test. Wrong!!!

You need to pass 70-431 and become a MCT first, then take 70-447. Well, I did it in reverse and now can say I am a MCITP. Next, 2008 certification.

The test was 35 multiple choice questions, which did not seemed to hard. What I did not realize, was an interactive 12 questions was coming down the pipe.

You actually would be given a scenario, then have to use a stripped down verison of SSMS (Management Studio) to solve the problem. I have to say, I was not prepared for this, but still finished and Passed with 880. Not bad.

Certifications might not be what they were years ago, but the experience in my opinion is good. There is alot of studying required, plus it questioned some of the pratices we DBAs do at our site.

Some questions about CLRs, security, Serice Broker were areas we do not use and I had to learn from reading. Others like database mirror, replication, log shipping, network protocols were areas we use and were easy.

God Bless,
Thomas

Thursday, April 1, 2010

SQLSaturday Baton Rouge

Great lunch with the planning group. Looks like we will have 9 total tracks, 5 SQL Server and 4 development.

The submitted sessions have us about 75% full with addtional sessions and alternates available.

I spoke last year, but was not involved with the planning. Alot of good people helping.

This week at work required some DBA reviews of developers code. I am also reading an 2008 Analysis Service book which has been great. Microsoft gave us a copy and I am on chapter 12, with 4-5 to go.

It seems the tool is great, but the design has to follow some best pratices in order to work well as designed. The are many properties to change in a dimension or cube to get the output just right. Another case of if you do not start using it, you will forget where to adjust output.

God Bless,
ThomBeaux