primary-key

Django model instances primary keys do not reset to 1 after all instances are deleted

那年仲夏 提交于 2019-11-27 12:46:43
I have been working on an offline version of my Django web app and have frequently deleted model instances for a certain ModelX. I have done this from the admin page and have experienced no issues. The model only has two fields: name and order and no other relationships to other models. New instances are given the next available pk which makes sense, and when I have deleted all instances, adding a new instance yields a pk=1, which I expect. Moving the code online to my actual database I noticed that this is not the case. I needed to change the model instances so I deleted them all but to my

Should a database table always have primary keys?

房东的猫 提交于 2019-11-27 12:27:59
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? You should strive to have a primary key in any non-trivial table where you're likely to want to access (or update or delete) individual records by that key. Primary keys can consist of multiple columns, and formally speaking,

Picking the best primary key + numbering system

牧云@^-^@ 提交于 2019-11-27 11:35:55
We are trying to come up with a numbering system for the asset system that we are creating, there has been a few heated discussions on this topic in the office so I decided to ask the experts of SO. Considering the database design below what would be the better option. Example 1: Using auto surrogate keys. ================= ================== Road_Number(PK) Segment_Number(PK) ================= ================== 1 1 Example 2: Using program generated PK ================= ================== Road_Number(PK) Segment_Number(PK) ================= ================== "RD00000001WCK" "00000001.1"

Primary and Foreign Key at the same time

有些话、适合烂在心里 提交于 2019-11-27 11:28:41
问题 Would it be possible in SQL Server 2008 to have a table created with 2 columns that are at the same time primary and foreign keys? If yes, how would such a code look like? I've searched and came up with nothing. 回答1: Sure, no problem: CREATE TABLE dbo.[User] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [Group] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [UserToGroup] ( UserId int NOT NULL, GroupId int NOT NULL,

Determine a table's primary key using TSQL

五迷三道 提交于 2019-11-27 11:26:58
问题 I'd like to determine the primary key of a table using TSQL (stored procedure or system table is fine). Is there such a mechanism in SQL Server (2005 or 2008)? 回答1: This should get you started: SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name WHERE tc.TABLE_NAME = 'TableName' and tc.CONSTRAINT_TYPE = 'Primary Key' 回答2: How about sp_pkeys 'TableName' 回答3: Here's one based on system tables from

What is the difference between a primary key and a index key

北战南征 提交于 2019-11-27 11:04:03
问题 Can anyone tell me what is the difference between a primary key and index key. And when to use which? 回答1: A primary key is a special kind of index in that: there can be only one; it cannot be nullable; and it must be unique. You tend to use the primary key as the most natural unique identifier for a row (such as social security number, employee ID and so forth, although there is a school of thought that you should always use an artificial surrogate key for this). Indexes, on the other hand,

ADO.NET Entity Framework: Update Wizard will not add tables

北城余情 提交于 2019-11-27 11:01:29
问题 I added a new ADO.Net Entity Data Model into my project and used the Update Wizard to add tables into the model. Five of the selected tables were added to the design surface. Two other tables will not add. I select them in the wizard and click Finish, yet they never show up on the design surface. Is this a bug, or are there some situations where a table cannot be added to the model (by design)? UPDATE: The XML (*.edmx) reveals the problem. <!--Errors Found During Generation: warning 6013: The

MySQL - Meaning of “PRIMARY KEY”, “UNIQUE KEY” and “KEY” when used together while creating a table

て烟熏妆下的殇ゞ 提交于 2019-11-27 11:00:35
Can anyone explain about the purpose of PRIMARY KEY , UNIQUE KEY and KEY , if it is put together in a single CREATE TABLE statement in MySQL? CREATE TABLE IF NOT EXISTS `tmp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `tag` int(1) NOT NULL DEFAULT '0', `description` varchar(255), PRIMARY KEY (`id`), UNIQUE KEY `uid` (`uid`), KEY `name` (`name`), KEY `tag` (`tag`) ) ENGINE=InnoDB AUTO_INCREMENT=1 ; How do I convert this query to MSSQL? A key is just a normal index. A way over simplification is to think of it like a card catalog at a

Using a UUID as a primary key in Django models (generic relations impact)

落花浮王杯 提交于 2019-11-27 10:46:38
For a number of reasons^, I'd like to use a UUID as a primary key in some of my Django models. If I do so, will I still be able to use outside apps like "contrib.comments", "django-voting" or "django-tagging" which use generic relations via ContentType? Using "django-voting" as an example, the Vote model looks like this: class Vote(models.Model): user = models.ForeignKey(User) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() object = generic.GenericForeignKey('content_type', 'object_id') vote = models.SmallIntegerField(choices=SCORES) This app seems to be

SQL Server: how to add new identity column and populate column with ids?

怎甘沉沦 提交于 2019-11-27 10:14:38
问题 I have a table with huge amount of data. I'd like to add extra column id and use it as a primary key. What is the better way to fill this column with values from one 1 to row count Currently I'm using cursor and updating rows one by one. It takes hours. Is there a way to do that quicker? Thank you 回答1: Just do it like this: ALTER TABLE dbo.YourTable ADD ID INT IDENTITY(1,1) and the column will be created and automatically populated with the integer values (as Aaron Bertrand points out in his