Showing posts with label key. Show all posts
Showing posts with label key. Show all posts

Wednesday, March 28, 2012

Padding an auto-incremented Int column to ten zeros

Good day gentleman,
Is it possible to pad columns of Int data type with zeros?

I currently have a primary key column of data type Int which is auto-incremented by 1. My problem is I need to pad the auto-incremented ID's with ten zeros so, 0000000009 instead of 9.

I can pad the ID's in the c# dataaccess layer, but i need to generate/store them padded in the database to allow direct reporting.

I am using Enterprise Manager and i can see a Formula field when the table is in design view, can i use this? i am just guessing here.

Any comments or pointers would be a big help.What you are discussing is a display issue. An int is, by definition, isolated from any particular display formatting. It is just a number. You could add an additional column (nvarchar) and have a trigger on inserts that populates that nvarchar() column with a string value with 0 padding.

PAD_INDEX : activating or not ?

Hi all,

On SQL Server 7 SP4.

3 tables with about 100 000 rows each one, frequently updated.
1 clustered index on primary key, and 2 non clustered index on columns used by JOIN and by queries.

Fillfactor for index : 90.
PAD_INDEX : not activated.

I have some deadlocks problems, probably due to index pages and key locking (as precised in error log). Is it possible that the cause of that is splitting index pages while UPDATE ?

May I activate PAD_INDEX to solve part of this problem ?

Thanks for any helpIt's a performance issue thing, I don't think that's your problem

http://www.sql-server-performance.com/oltp_performance.asp|||Well, the nature of deadlocks has NOTHING to do with performance, thus cannot be fixed by recommendations given in the link. The only applicable advice which is not even in the section implied is to keep your transactions short. This will minimize the possibility of deadlocks, but will not eliminate them. As you all know, it deals with object access order, and should be addressed accordingly, by examining all possible transactions involving the same set of objects in reverse order.|||Thanks all, I'm going to have a look at it.|||pad index is really not neccesary. i have chased this guy down many roads
i even had extended conversations with kimberly tripp and kalen delaney on this and everyone i have talked to has told me that pad index is just not gonna provide any real advantage.

Wednesday, March 7, 2012

Overwriting entry in table

Task:

To insert entries into a table. The table has a primary key based on a field 'ID'. When inserting into the destination table, I want to make sure that the new entry will overwrite the old entry.

What's the quickest/cleaniest way to do this ?

thanks,
ClaytonUPDATE <table>
SET <column>=<value>, etc...
WHERE field_id = <field_id>

Saturday, February 25, 2012

Override a foreign key constraint

A website that I'm working on has users sign in and keeps a log of the pages they go to. The log table has a foreign key in it that links to the username is the users table. I need to update the username for one of the users but the foreign key is preventing me from doing so. What is the benefit of having a foreign key like this? Can I delete it to update the username or is there a better way?

Foreign Key constraints ensure the integrity of the data is maintained. If there was no FK in place. then it would be easy to get orphaned and inconsistent data. In your example, if you just update a username to be Bob from Terry then all the records associated with Terry in the log table will now have no link back to the users record ie the Terry records will be orphaned.

Soooooo, in answer to your question, you can drop it make your update but it will throw an error when you try and recreate the FK unless you update the log records too.

You can create these constraints with a cascading updates/deletes which will filter your changes down to the child tables but in this case, it looks like its turned off.

I'd suggest just doing the following

Psuedo code:

UPDATE Log set Username = Bob WHERE username = Terry

UPDATE user set username = Bob WHERE username = Terry.

HTH!