primary-key

Picking the best primary key + numbering system

岁酱吖の 提交于 2019-11-26 15:38:33
问题 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 ================= ==================

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

一曲冷凌霜 提交于 2019-11-26 15:23:27
问题 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?

When does the JPA set a @GeneratedValue @Id

别等时光非礼了梦想. 提交于 2019-11-26 15:22:39
I have a simple JPA entity that uses a generated long "ID" as its primary key: @Entity public class Player { private long id; protected Player() { // Do nothing; id defaults to 0L } @GeneratedValue @Id public long getId() { return id; } protected void setId(final long id) { this.id = id; } // Other code } At some point in the life-cycle of an object of this type the JPA must call setId() to record the generated ID value. My question is, when does this happen, and where is the documentation that states this . I've looked through the JPA Specification and can not find a clear statement. The JPA

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

ε祈祈猫儿з 提交于 2019-11-26 15:18:58
问题 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

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

删除回忆录丶 提交于 2019-11-26 15:07:30
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 relation between recipients and messages. So, the question I have is this. The ID of a new message

SQL - Inserting a row and returning primary key

孤人 提交于 2019-11-26 14:26:41
问题 I have a little witty problem. Say, I inserted a row with some data in a table where a primary key is present. How would one "SELECT" the primary key of the row one just inserted? I should have been more specific and mentioned that I'm currently using SQLite. 回答1: For MS SQL Server: SCOPE_IDENTITY() will return you the last generated identity value within your current scope: SELECT SCOPE_IDENTITY() AS NewID 回答2: For SQL Server 2005 and up, and regardless of what type your primary key is, you

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

╄→尐↘猪︶ㄣ 提交于 2019-11-26 14:24:23
问题 We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys. With the classes below, how would we configure the one-to-many relationship between Report and RunStat such that Report.RunStats would return all of the RunStat entities where the ReportCode fields are equal? public class Report { [Key] public int ReportKey

adding primary key to sql view

半腔热情 提交于 2019-11-26 14:16:49
问题 Reading that how to do hibernate mapping for table or view without a primary key I am wondering how to add a primary key to my view as it is basically just a stored query...? PS: oracle 10g thx 回答1: We can add a disabled primary key constraint to a view. That is, the constraint does not fire if an insert or update is run against the view. The database expects integrity to be maintained through constraints on the underlying tables. So the constraint exists solely for the purposes of

Difference between Key, Primary Key, Unique Key and Index in MySQL

烂漫一生 提交于 2019-11-26 13:53:37
When should I use KEY , PRIMARY KEY , UNIQUE KEY and INDEX ? KEY and INDEX are synonyms in MySQL. They mean the same thing. In databases you would use indexes to improve the speed of data retrieval. An index is typically created on columns used in JOIN , WHERE , and ORDER BY clauses. Imagine you have a table called users and you want to search for all the users which have the last name 'Smith'. Without an index, the database would have to go through all the records of the table: this is slow, because the more records you have in your database, the more work it has to do to find the result. On

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

随声附和 提交于 2019-11-26 13:51:38
问题 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