Showing posts with label keys. Show all posts
Showing posts with label keys. Show all posts

Friday, March 30, 2012

relationships, primary key sql2000 question.

Hello,

I have 2 tables with a field called userid which is uniqueidentifier and they are both primary keys.

When I add a record to table1 and the userid field is filled, what is the best way to update table 2 with the same record.

Is there an sql function that will update automatically or do I have to write code in vb.net to select the record form table1 and insert into table2.

Thanks

Peter

If you wanted to do it relationship, one of the keys would have to be a foreign key and the other the primary key, probably using a 1 to 1 relationship.

If you want to use two primary keys, you would either need to do it by T-SQL or on the .NET Server end, ex. with VB.NET.

|||

What you are looking for is called a DRI(declarative referential integrity) constraint. You create it with the enable relationship dialog box at the top of Management Studio and look for option Cascade on Update. The two links below one explains DRI and the second is a walkthrough to enable it. Hope this helps.

http://msdn2.microsoft.com/en-us/library/ms177288.aspx

http://msdn2.microsoft.com/en-us/library/ms186973.aspx

|||

Thanks guys, working through links now.

Peter

sql

Relationships, keys, UniqueIDs - why... really?!

I've done my database studies and know about normalizing, relationships, primary keys and so forth. However, when it comes to actual developing of web sites, I've always skipped setting up relationships, and at times, the primary and (especially) foreign keys.

Now I'm designing my first db in Asp.Net 2, and the thought struck me - Why should I do this?

I guess the reason for setting up relationships is that the website should not get any trouble if rules are violated or poorly written, that is, data is input in an Orders table but not the Oredered_products table or the like. The only reason I can think of for using primary keys (when not defining relationships anyway) is to get the Insert, Delete, and Update statements of GridViews etc to work.

Or am I missing something? For many years, I've been told that the keys plus relationships taken together speed up the SQL queries, but, honestly, I can't see why they should. And I guess millions of web developers do like I do - skip the relationship thing altogether.

Please enlighten me, someone! (Or let's have an interesting discussion on this topic - if there is something to discuss, that is!)

Pettrer

pettrer:

Or am I missing something? For many years, I've been told that the keys plus relationships taken together speed up the SQL queries, but, honestly, I can't see why they should. And I guess millions of web developers do like I do - skip the relationship thing altogether.

Actaully indexes speed up SQL queries, keys are only constraints used to enforce the integrity of database (but keep in mind in SQL Server a unique,cluster index is automatically created when a PRIMARY KEY) . And I guess relationships you mentioned should be about data tables in application, or reference integrity between database tables. Here are some links that may help you understand:

Constraints:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/architec/8_ar_da_0777.asp

Indexes:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_05_30s5.asp

Creating an Index:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_05_8185.asp

|||

Thanks a lot for the links!

P

Monday, March 26, 2012

Relations between tables - contraints diagram

Hi all,
I have a big problem. I have many tables with constraints, with foreign keys. I need to create a ordered list of tables, on the top must be the basic table what has no parents, then the second level tables (those depends on the first level) the the names of third level etc.

for example:
Table A[id]
Table B[id, idc]
Table C[id, ida]
Table D[id, ida]
Table E[id, idc]

I tried it by using information_scheme but I was unsuccesfull.

The result should be:
A
C
D
B
E

Thank you,
Tom.There isn't always a relational solution to this problem, because users sometimes create non-linear (aka graphical) relations. I know... Bad user! Naughty user! No donut!

I worked up an iterative solution, but I don't have it handy. If no one else posts a solution (you still have a copy of that one Rudy?), I'll dig it up and post it, but it might be a day or three.

-PatP|||thank Pat,
I apreciate your help, I am also working now on a iterative solution, but it does not work. So if you will have a time I will be very glad to see your solution and help, three days or five, does not matter.
Thanks again.

Tom.

Friday, March 23, 2012

Related tables with different publications

My database contains tables related together by foreign keys (Not for
Replication), and I want to design a transactional replication with queued
updating option between them, can I put each related table on a different
publication?
Saeed,
the general recommendation is to have the related articles in the same
publication, to ensure the same processing order is applied at the
subscriber. Is there any particular reason you want to split the publication
into two?
Rgds,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
|||Hi Paul,
our database is too huge, we have tables with millions of records, so if any
problem occured to any table or adding new column(s), we just want to start
reinitilize the effected table. not all related tables to minimize the
efforts on both the database and the network.
Thanks
"Paul Ibison" wrote:

> Saeed,
> the general recommendation is to have the related articles in the same
> publication, to ensure the same processing order is applied at the
> subscriber. Is there any particular reason you want to split the publication
> into two?
> Rgds,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
> (recommended sql server 2000 replication book:
> http://www.nwsu.com/0974973602p.html)
>
>
|||OK - then you could have different publications and ensure
that they synchronize in the correct order.
Rgds,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)

Friday, March 9, 2012

regular primary keys vs autonumber primary keys

What is the best way to handle primary key selection in general? It would seem that autonumbered primary keys would result in faster query times, but what if there is another field in the table which is a PK canidate and must be unique? For example lets say I have a product table with an autonumbered PK. This table also has a product ID which must be unique. Would it be best to keep the autonumbered ID as the PK and put a unique constraint on the product ID maybe? OR would it be best to change the PK to the product ID and nix the autonumber all-together? All thoughts on this would be appreciated.Originally posted by Chuckt
What is the best way to handle primary key selection in general? It would seem that autonumbered primary keys would result in faster query times, but what if there is another field in the table which is a PK canidate and must be unique? For example lets say I have a product table with an autonumbered PK. This table also has a product ID which must be unique. Would it be best to keep the autonumbered ID as the PK and put a unique constraint on the product ID maybe? OR would it be best to change the PK to the product ID and nix the autonumber all-together? All thoughts on this would be appreciated.

If table does not have natural key like state abbreviation (KY - Kentucky - I was living there almost three years) you have to create fake primary key. Usually it is integer (SQL2000 does have bigint) field. Should you use IDENTITY? It depends on what kind of table (lookup or not), what kind of database, will you use replication or not, etc. Identity does have some pluses and exactly the same quantity of minuses. If you are going to change data frequently in tables by 'hands' (not from application) - do not use IDENTITY. You can use SET IDENTITY_INSERT for inserting into the identity column of a table.|||Thanks for the reply snail. My dilemma is this... I have a table that relates two other tables(its primary key is the primary keys of the other two tables it is relating). It has over 2 million rows. The primary keys of all three tables are clustered indexes and the data types of the fields are varchar 20. The query times are a little slow and I was trying to figure out ways to increase the speed. Would it be faster to use an autonumber for the three tables? It would seem to me that it would be faster to join in a query based on an index comprised of less characters. How much faster and if it is worth it I am not sure.|||Couple things to think about.

(1) Joins are better utilized on integer fields and are faster.

(2) What type of inserts do you have, the nice thing about having an identity as a PK and clustered index is all inserts fall to the last leaf level thereby reducing page splits. If your data is solely for querying data, this is a waste though.

(3) Are you running certain queries the majority of the time that only consist of a few of the columns? If so, you might think about a composite non-clustered index to have applicable columns in. Having a non-clustered covering index on the columns you need is faster than having to conduct table scans, or even clustered index key locks.

HTH|||"(2) What type of inserts do you have, the nice thing about having an identity as a PK and clustered index is all inserts fall to the last leaf level thereby reducing page splits."

Yes, but unless you reindex won't your query efficiency be reduced because the average number of pages traversed will increase. In effect, the data structure will be lopsided toward the latest inserted values:

/\
../\
.../\
...../\
....../\

instead of:

../\
./\/\
/\/\/\

blindman|||An int as a foreign key will be faster than a wider key because it takes up less space and less page reads.
It can cause the system to be slower though because to obtain any data from the other table you will need to join to it. Using the natural key the data in that key is available on the referred table.
Not so bad with just two tables but

t1 (col1, col2, col3)
t2 (t1_col1, t1_co2,t1_col3)
t3 (t1_col3)

This is quite common. With the natural keys you can join t2 and t3. If you replace with artificial keys you would have to join to t1 as well so causing more page reads and using up more memory.

It's a subject that people get very passionate about. I disagree with those that say that all tables should have an artificial key and this should always be used for the join field but wouldn't disagree that it can sometimes be useful to create one for efficiency or ease of coding.|||well, that's why after going through structural normalization process designers revise the design with DE-normalization steps. it still is an evidence of consistency and is easy to follow. btw, you don't have to join t1 in the example you give while using "artificial keys".|||The B-tree still maintains ranges but yes it is a trade-off, if you are heavy reads, low inserts, you should not use this method, but if you are heavy inserts with medium to heavy reads, this is good for minimizing page splits without having to play with fill factors.

Nigel, I'm one of those that recommends a surrogate key on every table, just seen too many times a company changes their primary key algorithm and it makes things easier in my opinion when you push to a warehouse. But I won't argue that passionately for it, to each his own :)

Originally posted by blindman
"(2) What type of inserts do you have, the nice thing about having an identity as a PK and clustered index is all inserts fall to the last leaf level thereby reducing page splits."

Yes, but unless you reindex won't your query efficiency be reduced because the average number of pages traversed will increase. In effect, the data structure will be lopsided toward the latest inserted values:

/\
../\
.../\
...../\
....../\

instead of:

../\
./\/\
/\/\/\

blindman|||I used to use natural keys a lot, but I found that the lower-level tables in schemas designed with natural keys tended to have multi-column indexes with multiple joins to related tables. I still consider using natural keys for higher-level tables, especially look-up tables.

Consider the needs of your application as well. By standardizing the key you simplify things for developers who always know what type of field to submit for searches, etc. If you use a natural key and then decide you want to change it (length or type) you may force changes in your middle tier.

...and one other cool thing I was able to do with GUIDs. We had developers who wanted to be able to lookup information based on the ID of an employee record, but they also wanted the same functionality by submitting the session-specific security token the employee was using. Since both were GUIDs, I was able to create a single procedure which required only a single parameter @.EmployeeIDorToken, and determine within the procedure which one was submitted. I could count on the fact that the same GUID would never exist in both tables.

blindman

Wednesday, March 7, 2012

Registry Keys

hi,
does anyone know the registry location of
1. sqlservr.exe
2. which controls switching between 6.5 and 7.0 or 2000.
thanx.
sajal.Hi,
1. sqlservr.exe
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSSQLServer]
Key is "ImagePath" - Value will be the path and the SQLSERVR.exe
2. I am not sure about that key...Search the registry for "vswitch.exe"
Thanks
Hari
MCDBA
"Sajal Kumar" <skumar3@.metlife.com> wrote in message
news:1af8201c41eb6$15a4f5b0$a101280a@.phx.gbl...
> hi,
> does anyone know the registry location of
> 1. sqlservr.exe
> 2. which controls switching between 6.5 and 7.0 or 2000.
> thanx.
> sajal.

Registry Keys

hi,
does anyone know the registry location of
1. sqlservr.exe
2. which controls switching between 6.5 and 7.0 or 2000.
thanx.
sajal.
Hi,
1. sqlservr.exe
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servi ces\MSSQLServer]
Key is "ImagePath" - Value will be the path and the SQLSERVR.exe
2. I am not sure about that key...Search the registry for "vswitch.exe"
Thanks
Hari
MCDBA
"Sajal Kumar" <skumar3@.metlife.com> wrote in message
news:1af8201c41eb6$15a4f5b0$a101280a@.phx.gbl...
> hi,
> does anyone know the registry location of
> 1. sqlservr.exe
> 2. which controls switching between 6.5 and 7.0 or 2000.
> thanx.
> sajal.
|||hi hari,
i also found it here as well
hkey... > software > microsoft > windows > currentversion > app paths >
sqlservr.exe
looks like it is listed under as an app also.
could not find vswitch.exe when i searched the registry.
thanx v much.
sajal.
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!

Registry Keys

hi,
does anyone know the registry location of
1. sqlservr.exe
2. which controls switching between 6.5 and 7.0 or 2000.
thanx.
sajal.Hi,
1. sqlservr.exe
& #91;HKEY_LOCAL_MACHINE\SYSTEM\CurrentCon
trolSet\Services\MSSQLServer]
Key is "ImagePath" - Value will be the path and the SQLSERVR.exe
2. I am not sure about that key...Search the registry for "vswitch.exe"
Thanks
Hari
MCDBA
"Sajal Kumar" <skumar3@.metlife.com> wrote in message
news:1af8201c41eb6$15a4f5b0$a101280a@.phx
.gbl...
> hi,
> does anyone know the registry location of
> 1. sqlservr.exe
> 2. which controls switching between 6.5 and 7.0 or 2000.
> thanx.
> sajal.|||hi hari,
i also found it here as well
hkey... > software > microsoft > windows > currentversion > app paths >
sqlservr.exe
looks like it is listed under as an app also.
could not find vswitch.exe when i searched the registry.
thanx v much.
sajal.
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!

Saturday, February 25, 2012

Registration

I have a client that is running SQL 2005 Standard edition and they purchased 10 more Cal's, do I have to enter new registration keys, and if so where do you enter them?

Thanks in advance.

Joe

No, you don't have to enter additional reg keys.|||Thank you