Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Imagine I have a table with only 2 columns (FKs to other tables). I want to define "the primary key of this table is the combination of the 2 values".
What happens if I don't have a PK in this kind of table?
Without a UNIQUE constraint or unique index defined on the two columns, the table could have duplicate rows.
Also, a primary key is a clustered index by default: you would need to separately index the table for expected query performance.
Refer to another SO question and yet another SO question declared as a duplicate of it regarding the differences between primary key & unique constraints and unique indexes.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
In order to control the duplicate rows in a SQL Server table, which approach will have the better performance for insert times in high loads?
Create a primary key constraint on a column that should have a unique value in the table (type of column is varchar(100) and the possible value is like g_12546987456_13-9. It means a composite primary key and no specific character orders)
Create a numeric and auto-incremented primary key and set a non-clustered index with uniqueness constraint on the string column (g_12546987456_13-9)
One thing you have to be vigilant about, when creating these Alpha-Numeric Primary keys, you have to make sure that your Alpha-Numeric values are Incremental.
The first option you have mentioned with Random values, will most certainly impact the performance massively. Because of the random Primary Key, it will end up inserting new rows on the existing data pages, thus pushing records down and ending up with Page splits - Very bad for SQL Server Performance. (One of the main reason why GUID is not a good candidate for a Primary Key and MS had to introduced sequential GUID).
I would suggest make use of SQL Server Sequence Object to Auto-Increment values and with your desired Alphabets but still make sure the new values are sequential and incremental.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a question about designing a table for events.
Which one is better using a multi-column primary key, or using a sequential primary key with multi-column unique index?
Columns of this table are like this:
Generally in SQL Server, PRIMARY KEY is created as unique clustered index in the background.
So, it is good practice to keep clustered index key as:
Unique (avoids effort to add uniquifier to make the value unique)
Narrow (does not occupy lot of space)
Incremental (avoids fragmentation)
So, in your case , it is better to go for
Sequential Primary key & multi column unique index
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want create a instead of insert trigger where It should not allow any record with same VersionNo or having VersionNo as NULL
You don't need a trigger for this. The right way to do this is using constraints:
alter table t alter column version int not null;
Then mandate that it be unique:
alter table add constraint unq_t_version unique (version);
If you want a combination of columns to be unique, such as (SGID, Version), then use that for the unique constraint instead of a single column.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a hierarchical table - it would suit a self referencing style, like this example I found.
CREATE TABLE OurStuff
(
StuffID INT NOT NULL PRIMARY KEY,
StuffSubID INT NULL,
StuffName VARCHAR(10) NOT NULL,
CONSTRAINT fk_StuffID FOREIGN KEY (StuffSubID)
REFERENCES OurStuff(StuffID)
)
I have a similar table - however when I try to replicate using Visual Studio Server Explorer, I get an error message:
"The columns in table 'abc' do not match an existing primary key or
unique constraint"
I can understand why, but I don't know why the above which is cited as an example would work and mine does not.
UPDATE: Here the link to the page in the example “How do I create a self-referencing foreign key?”
UPDATE:
I think you've got the relationship reversed. The ProductTypeIDcolumn should be on the left under the "Primary key table" and the ProductTypeParentID column should be on the right under the "Foreign key table."
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What are the good example columns for which I should never create an index? As per my understanding the clustered index should often be done on primary keys (default) as it represents base data as a whole. But on which columns I should never create a non-clustered index?
You cannot say for sure. The fact is: you cannot create an index on any column (or combination of columns) that has a max size of more than 900 bytes - so any columns like VARCHAR(1000) or VARCHAR(MAX) cannot be indexed.
Other than that - it reallly depends on what your system does! There's no magic rule what columns to index - or which to avoid.
In general: fewer indexes are better than too many. Most DB developers tend to over-index their databases - but as I said - this is really heavily dependent on the exact situation of your system - there's no simple, general rules to follow here.