primary-key

get auto generated key, on save, using hibernate + spring mvc

浪尽此生 提交于 2019-12-19 09:07:53
问题 I am using Spring MVC + Hibernate @Resource(name = "sessionFactory") private SessionFactory sessionFactory; // save public <T> int save(T entity) throws DataAccessException { Session session = sessionFactory.getCurrentSession(); session.save(entity); } As New Record Save , New Primary Key generated which in auto increment (db.MySQL). I want to get and return the new auto incremented value with respect to the above method. Update me ! 回答1: Save method should return generated ID: http://docs

Auto increment a non-primary key field in Ruby on Rails

…衆ロ難τιáo~ 提交于 2019-12-19 05:45:14
问题 In a RoR migration, how do I auto increment a non-primary-key field? I'd like to do this in the db definition, and not in the model. 回答1: You need to execute an SQL statement. statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT" ActiveRecord::Base.connection.execute(statement) you can entry manually in your migration Note this is just an example. The final SQL statement syntax depends on the database. 来源: https://stackoverflow.com/questions/3220473

How to get the primary key of an Ms Access table in C#

只愿长相守 提交于 2019-12-19 04:58:11
问题 I need the field or fields (just the name of the field will do) that form the primary key of a Microsoft Access Table, given a connection and a tableName. 回答1: ok, I guess I found it. It should work for all oledb and is sth. like : public static List<string> getKeyNames(String tableName, DbConnection conn) { var returnList = new List<string>(); DataTable mySchema = (conn as OleDbConnection). GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, new Object[] {null, null, tableName}); // following

How to get the primary key of an Ms Access table in C#

匆匆过客 提交于 2019-12-19 04:57:05
问题 I need the field or fields (just the name of the field will do) that form the primary key of a Microsoft Access Table, given a connection and a tableName. 回答1: ok, I guess I found it. It should work for all oledb and is sth. like : public static List<string> getKeyNames(String tableName, DbConnection conn) { var returnList = new List<string>(); DataTable mySchema = (conn as OleDbConnection). GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, new Object[] {null, null, tableName}); // following

Rails ActiveRecord handle an id column that is not the primary key

梦想与她 提交于 2019-12-19 04:07:45
问题 Struggling with ActiveRecord auto assigning the :id attribute as the primary key even though it is a separate column. Table - legacy-table id - int pk_id - int (primary key) name - varchar2 info - varchar2 Model class LegacyModel < ActiveRecord::Base self.table_name = 'legacy-table' self.primary_key = 'pk_id' default_scope {order(:name => :asc)} alias_attribute :other_id, :id end I don't care that ActiveRecord automatically assigns the primary key (pk_id) to the :id attribute however I lose

Should all database tables have a primary key?

我的未来我决定 提交于 2019-12-19 02:27:10
问题 Is it good practice to give every database table a primary key? It seems to me that if the primary key is not explicitly needed, then it would just be extra clutter in my database. 回答1: When you probably WOULD: In an OLTP database you'd almost always (in my case always) have a primary key of some sort. Sometimes Guid, sometimes autonumber/identity fields, sometimes set by the application or the customer. Sometimes even a combination of more than one field. This is because you'll typically

Can foreign key references contain NULL values in PostgreSQL?

元气小坏坏 提交于 2019-12-18 20:32:47
问题 As an example create table indexing_table ( id SERIAL PRIMARY KEY, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), ); Is there a difference between the following tables? Table 1: create table referencing_table ( indexing_table_id INTEGER references indexing_table ); Table 2: create table referencing_table ( indexing_table_id INTEGER references indexing_table NOT NULL ); Alternatively, in the case of Table 1, where there is no NOT NULL constraint, are we allowed to insert records containing

How to discover the underlying primary (or unique) key columns from an Oracle view

非 Y 不嫁゛ 提交于 2019-12-18 17:21:28
问题 I was wondering whether there is a possibility for me to discover the underlying primary (or unique) key columns for all tables involved in an Oracle view. Here's an example to show what I mean: CREATE TABLE t_a ( id number(7), primary key(id) ); CREATE VIEW v_a AS SELECT * FROM t_a; So by naming convention, I know that v_a.id is actually the primary key column of the underlying t_a table. Is there any way of formally discovering this information by using system views, such as SYS.ALL

create foreign key without a primary key

一笑奈何 提交于 2019-12-18 15:12:17
问题 Why is it necessary to have a primary key on a column of one table to which a column of the other table having foreign key references. create table D(Did int) create table E(Eid int foreign key references D(Did)) The above query gives error: There are no primary or candidate keys in the referenced table 'D' that match the referencing column list in the foreign key 'FK__E__Eid__79C80F94'. 回答1: Easy. If you have 2 values the same in the parent table, how do you know which one to associate child

create foreign key without a primary key

为君一笑 提交于 2019-12-18 15:11:28
问题 Why is it necessary to have a primary key on a column of one table to which a column of the other table having foreign key references. create table D(Did int) create table E(Eid int foreign key references D(Did)) The above query gives error: There are no primary or candidate keys in the referenced table 'D' that match the referencing column list in the foreign key 'FK__E__Eid__79C80F94'. 回答1: Easy. If you have 2 values the same in the parent table, how do you know which one to associate child