primary-key

Creating composite primary key in SQL Server

夙愿已清 提交于 2019-12-17 18:35:07
问题 How to add composite primary keys in SQL Server 2008? I have a table as follows. testRequest (wardNo nchar(5) , BHTNo nchar(5) , testID nchar(5) , reqDateTime datetime); I need wardNo, BHTNo and testID to be a composite primary key. How can I do this in SQL Server Management Studio? 回答1: If you use management studio, simply select the wardNo, BHTNo, testID columns and click on the key mark in the toolbar. Command for this is, ALTER TABLE dbo.testRequest ADD CONSTRAINT PK_TestRequest PRIMARY

Clustered primary key on unique identifier ID column in SQL Server

耗尽温柔 提交于 2019-12-17 16:50:57
问题 If your ID column on a table is a unique identifier (Guid), is there any point creating a clustered primary key on the ID column? Given that they are globally unique, how would the sorting work? 回答1: GUIDs as they are are terrible for performance since they are effectively random values (this "breaks" clustered index), and they are awful for indexes, since less entries fit on a single page/extent (SQL Server terms). SQL Server 2005 introduces newsequentialid() which helps solving first

Entity Framework Code First Migrations: Set Primary Key Value

前提是你 提交于 2019-12-17 16:30:13
问题 I have a table that stores some extra data for some rows of a table like: public class QuoteExtra { [Key] public int QuoteId { get; set; } // More fields here } I'd like to be able to add rows to this table where I explicitly set the PK. If I simply leave it as above, setting a value and submitting the row causes the value to be discarded and replaced with the auto-generated value from the database (and the column is defined as an Identity column in the actual schema). This appears to be the

Retrieve Oracle last inserted IDENTITY

泪湿孤枕 提交于 2019-12-17 12:34:25
问题 Since Oracle 12c we can use IDENTITY fields. Is there a way to retrieve the last inserted identity (i.e. select @@identity or select LAST_INSERTED_ID() and so on)? 回答1: Well. Oracle uses sequences and default values for IDENTITY functionality in 12c. Therefore you need to know about sequences for your question. First create a test identity table. CREATE TABLE IDENTITY_TEST_TABLE ( ID NUMBER GENERATED ALWAYS AS IDENTITY , NAME VARCHAR2(30 BYTE) ); First, lets find your sequence name that is

Should a database table always have primary keys?

…衆ロ難τιáo~ 提交于 2019-12-17 10:42:36
问题 Should I always have a primary key in my database tables? Let's take the SO tagging. You can see the tag in any revision, its likely to be in a tag_rev table with the postID and revision number. Would I need a PK for that? Also since it is in a rev table and not currently use the tags should be a blob of tagIDs instead of multiple entries of multiple post_id tagid pair? 回答1: You should strive to have a primary key in any non-trivial table where you're likely to want to access (or update or

Should I have a dedicated primary key field?

怎甘沉沦 提交于 2019-12-17 07:34:18
问题 I'm designing a small SQL database to be used by a web application. Let's say a particular table has a Name field for which no two rows will be allowed to have the same value. However, users will be able to change the Name field at any time. The primary key from this table will be used as a foreign key in other tables. So if the Name field was used as the primary key, any changes would need to be propagated to those other tables. On the other hand, the uniqueness requirement would be handled

Should I have a dedicated primary key field?

五迷三道 提交于 2019-12-17 07:34:02
问题 I'm designing a small SQL database to be used by a web application. Let's say a particular table has a Name field for which no two rows will be allowed to have the same value. However, users will be able to change the Name field at any time. The primary key from this table will be used as a foreign key in other tables. So if the Name field was used as the primary key, any changes would need to be propagated to those other tables. On the other hand, the uniqueness requirement would be handled

How to retrieve the last autoincremented ID from a SQLite table?

风格不统一 提交于 2019-12-17 02:59:27
问题 I have a table Messages with columns ID (primary key, autoincrement) and Content (text). I have a table Users with columns username (primary key, text) and Hash. A message is sent by one Sender (user) to many recipients (user) and a recipient (user) can have many messages. I created a table Messages_Recipients with two columns: MessageID (referring to the ID column of the Messages table and Recipient (referring to the username column in the Users table). This table represents the many to many

Entity Framework Compound Key (Many-To-Many Relationship with a Payload)

杀马特。学长 韩版系。学妹 提交于 2019-12-14 04:27:15
问题 I've got database tables like this: A person may be a member of many teams. A team may have many members. Each person may have a position (think job title) within the team. I've tried to set this up with ADO.NET Entity Framework and get errors: Error 3021: Problem in mapping fragments starting at line ... Each of the following columns in table Membership is mapped to multiple conceptual side properties: Membership.PersonId is mapped to <MembershipPerson.Membership.PersonId, MembershipPerson

Changing GUID Primary Keys to Integer Primary Keys

烈酒焚心 提交于 2019-12-14 03:59:53
问题 I took over an application a few months ago that used Guids for primary keys on the main tables. We've been having some index related database problems lately and I've just been reading up on the use of Guids as primary keys and I've learnt how much of a bad idea they can be, and I thought it might pay to look into changing them before the database gets too large. I'm wondering if there's an easy way to change them to Ints? Is there some amazing software that will do this for me? Or is it all