primary-key

Sql Server Indexes Include Primary Key?

眉间皱痕 提交于 2019-11-27 16:09:12
问题 One of my co workers is under the impression that when adding an index to a table in SQL Server 2008 that the PK's index is added to that index as well. Therefore if you are using a wider primary key then that key will also be included in the new index vastly increasing the disk space used above and beyond the penalty already paid for the index on the PK. I hadn't heard that before and my searching so far is coming up empty. Hopefully someone here can point me at relevant docs to confirm or

I need to auto_increment a field in MySQL that is not primary key

我是研究僧i 提交于 2019-11-27 15:37:17
问题 Right now, I have a table whose primary key is an auto_increment field. However, I need to set the primary key as username , date (to ensure that there cannot be a duplicate username with a date). I need the auto_increment field, however, in order to make changes to row information (adding and deleting). What is normally done with this situation? Thanks! 回答1: Just set a unique index on composite of (username, date). ALTER TABLE `table` ADD UNIQUE INDEX `name` (`username`, `date`);

@OneToOne bidirectional mapping with @JoinColumn

好久不见. 提交于 2019-11-27 15:23:13
Let's say I have Person class Person{ @Id Integer id; @OneToOne @JoinColumn(name = "person_id") Job myJob; } and Job class Job{ @Id Integer id; Integer person_id; @OneToOne @PrimaryKeyJoinColumn(name = "person_id") Person currentWorker; } I'm not able to map the Person and Job to other Entity, when fetching. What mistake am I doing ? Guaido79 Your code should be: @Entity public class Person implements Serializable { @Id Integer id; @OneToOne @JoinColumn(name = "id") Job myJob; } @Entity public class Job implements Serializable { @Id Integer id; @OneToOne(mappedBy = "myJob") Person

DynamoDB: When to use what PK type?

倖福魔咒の 提交于 2019-11-27 15:18:06
I am trying to read up on best practices on DynamoDB. I saw that DynamoDB has two PK types: Hash Key Hash and Range Key From what I read, it appears the latter is like the former but supports sorting and indexing of a finite set of columns. So my question is why ever use only a hash key without a range key? Is it a viable choice only when the table is not searched? It'd also be great to have some general guidelines on when to use what key type. I've read several guides (including Amazon's own documentation on DynamoDB) but none of them appear to directly address this question. Thanks The

Retrieve Oracle last inserted IDENTITY

和自甴很熟 提交于 2019-11-27 15:08:11
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)? 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 created with this identity column. This sequence name is a default value in your table. Select TABLE_NAME,

Mysql Innodb: Autoincrement non-Primary Key

自作多情 提交于 2019-11-27 14:57:00
Is it possible to auto-increment a non-Primary Key? Table "book_comments" book_id medium_int timestamp medium_int user_id medium_int vote_up small_int vote_down small_int comment text comment_id medium_int Primary key -> (book_id, timestamp, user_id) There will be no other indexes on this table. However, I would like to make the comment_id column autoincrement so that I can easily create another table: Table "book_comments_votes" comment_id (medium_int) user_id (medium_int) Primary key -> (comment_id, user_id) Users would be able to vote only once per book comment. This table enforces this

MySQL: Determine Table's Primary Key Dynamically

回眸只為那壹抹淺笑 提交于 2019-11-27 14:38:23
I'm, generating a SQL query like this in PHP: $sql = sprintf("UPDATE %s SET %s = %s WHERE %s = %s", ...); Since almost every part of this query is dynamic I need a way to determine the table's primary key dynamically, so that I'd have a query like this: $sql = sprintf("UPDATE %s SET %s=%s WHERE PRIMARY_KEY = %s", ...); Is there a MySQL keyword for a table's primary key, or a way to get it? I've used the information_schema DB before to find information like this, but it'd be nice if I didn't have to resort to that. SHOW INDEX FROM <tablename> You want the row where Key_name = PRIMARY http://dev

SQL - Inserting a row and returning primary key

扶醉桌前 提交于 2019-11-27 14:35:37
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. For MS SQL Server: SCOPE_IDENTITY() will return you the last generated identity value within your current scope: SELECT SCOPE_IDENTITY() AS NewID For SQL Server 2005 and up, and regardless of what type your primary key is, you could always use the OUTPUT clause to return the values inserted: INSERT INTO dbo.YourTable(col1, col2, ....,

Multi-Column Primary Key in MySQL 5

陌路散爱 提交于 2019-11-27 14:19:52
I'm trying to learn how to use keys and to break the habit of necessarily having SERIAL type IDs for all rows in all my tables. At the same time, I'm also doing many-to-many relationships, and so requiring unique values on either column of the tables that coordinate the relationships would hamper that. How can I define a primary key on a table such that any given value can be repeated in any column, so long as the combination of values across all columns is never repeated exactly? Adriaan Stander Quoted from the CREATE TABLE Syntax page: A PRIMARY KEY can be a multiple-column index. However,

Primary Key Type: int vs long

不羁岁月 提交于 2019-11-27 13:33:19
问题 I know some software shops have been burned by using the int type for the primary key of a persistent class. That being said, not all tables grow past 2 billions. As a matter of fact, most don't. So, do you guys use the long type only for those classes that are mapped to potentially large tables OR for every persistent class just to be consistent? What's the industry concensus? I'll leave this question open for a while so that you can share with us your success/horror stories. 回答1: Long can