primary-key

Naming primary keys “id” vs “something_id” in SQL [closed]

痞子三分冷 提交于 2019-12-12 08:06:23
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm wondering what the best practice is for naming primary/unique keys in a database with many tables. Should you always just call

How to manage non-autoincrement primary key in Rails?

。_饼干妹妹 提交于 2019-12-12 07:38:36
问题 I have lots of situations where I'd like to have a non-autoincrement primary key when using Rails. Example: I have one-to-one relationship between A and B. B describes some specific features added to A thus can't exist without A. So we have: A has one B B belongs to A The natural thinking would be having B.A_id as primary key. So I tried create_table b, :id=>false in migration and set_primary_key :a_id in B's model, but it doesn't create actual primary keys in the database. And I want them

SQL Index Performance - ASC vs DESC

大憨熊 提交于 2019-12-12 07:17:21
问题 I've got a user table keyed on an auto-increment int column that looks something like this: CREATE TABLE `user_def` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(20) NOT NULL, `date_created` datetime NOT NULL, PRIMARY KEY (`user_id`), UNIQUE KEY `user_name_UNIQUE` (`user_name`), ) ENGINE=MyISAM Are there any practical performance advantages to using a DESC index (primary key) rather than the default ASC? My suspicion / reasoning is as follows: I'm assuming that more recent

Hibernate: How does native identifier generation strategy work

被刻印的时光 ゝ 提交于 2019-12-12 04:46:17
问题 Hibernate has an identifier generation strategy called native that selects identity , sequence or hilo depending upon the capabilities of the underlying database. I used MySQL with hibernate.hbm2ddl.auto=update which generated id BIGINT(20) NOT NULL AUTO_INCREMENT for id property of Long Java data type. I am trying to understand how did Hibernate choose AUTO_INCREMENT when it used SchemaExport tool. Is AUTO_INCREMENT the default primary key generation strategy for MySQL? Could somebody help

Entity Framework Get Entity By Primary Key

ⅰ亾dé卋堺 提交于 2019-12-12 03:36:03
问题 Some time ago, I wanted to implement a method that was able to determine whether do an insert or an update on a given entity, so I didn't have to expose "Insert" and "Update" methods, but just a simple "InsertOrUpdate". The part of code that finds out if the entity is new or not, is this: public virtual T GetEntityByPrimaryKey<T>(T entity) where T : class { var entityType = entity.GetType(); var objectSet = ((IObjectContextAdapter)this.DatabaseContext).ObjectContext.CreateObjectSet<T>(); var

Should one generate a unique id for each row in a database table that otherwise has no unique keys?

て烟熏妆下的殇ゞ 提交于 2019-12-12 03:22:04
问题 I'm designing a MySQL database for a startup working with patient medical records. I have several tables that can have no unique keys: medical conditions, allergies, family history, etc. For example the medications table might have fields patient_id , med_name , usage_dates , dosage , reason , etc. The only guaranteed unique key is all of the fields together. A web application can easily retrieve this data for a given patient by selecting for patient_id , but unless it is possible to save

How do I reference a composite primary key with a foreign key using MySQL

…衆ロ難τιáo~ 提交于 2019-12-12 03:16:15
问题 I'm attempting to setup a foreign key in table cell_lines that will reference the topographic_region column of the composite primary key in table topographic_regions . Each time I run the last three lines of code trying to add the foreign key, I receive Error Code 1215: cannot add foreign key constraint. Now, the foreign key column name ( topographic_region ) in cell_lines only matches one of the composite primary key column names in topographic_regions , the other composite primary key

mysql int field always returning the same value

白昼怎懂夜的黑 提交于 2019-12-12 02:26:39
问题 Recently MySQL 5.1.49 (OSX) started inserting the same value '2147483647' for ALL integer fields named id and set as primary key. This is happening for all databases and all tables. Here's the a quick example: mysql> INSERT INTO packages(id,name,rate) VALUES ('37364428662',"Testing","300"); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> SELECT * FROM packages; +------------+---------+------+ | id | name | rate | +------------+---------+------+ | 2147483647 | Testing | 300 | +----------

Using one of the columns in a composite key as a foreign key

感情迁移 提交于 2019-12-12 02:15:46
问题 I was trying to see whether I can use one of the columns in a composite key as a foreign key. And I got strange result. CREATE TABLE TESTPARENT( PK1 INT, PK2 INT, PRIMARY KEY(PK1,PK2) ); Query OK, 0 rows affected (0.01 sec) CREATE TABLE TESTCHILD1( FK1 INT, FOREIGN KEY (FK1) REFERENCES TESTPARENT(PK1) ); Query OK, 0 rows affected (0.01 sec) CREATE TABLE TESTCHILD2( FK2 INT, FOREIGN KEY (FK2) REFERENCES TESTPARENT(PK2) ); ERROR 1005 (HY000): Can't create table 'test.TESTCHILD2' (errno: 150)

Datanucleus JDO Retrieve newly generated primary key

守給你的承諾、 提交于 2019-12-12 01:35:43
问题 I am using datanucleus and jdo in a GWT project. How can I retrieve the generated primary key after adding an element to the database with makePersistent() Edit We use annotations, here is the primary key: @PrimaryKey @Column(name = "id_bla", allowsNull = "false") @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, extensions = { @Extension(vendorName = "datanucleus", key = "strategy-when-notnull", value = "false") }) private Long idBla; I am not the one who did the mapping and I don't