foreign-keys

How to copy structure of one table to another with foreign key constraints in psql?

泄露秘密 提交于 2019-11-30 09:29:24
Foreign key constraints are not copied when using create table table_name ( like source_table INCLUDING ALL)' in Postgres. How can I create a copy of an existing table including all foreign keys. There is no option to automatically create foreign keys in CREATE TABLE ... LIKE ... . For the documentation: LIKE source_table [ like_option ... ] Not-null constraints are always copied to the new table. CHECK constraints will be copied only if INCLUDING CONSTRAINTS is specified [...] Indexes, PRIMARY KEY, and UNIQUE constraints on the original table will be created on the new table only if the

What creates the FOREIGN KEY constraint in Ruby on Rails 3?

一笑奈何 提交于 2019-11-30 09:27:26
I understand that by default the id field is created and also: PRIMARY KEY ( id ) . How about the foreign key ? I have Shops and Products tables and the following associations: Shop: has_many :products Product: belongs_to :shop In Product I have also: t.integer "shop_id" which is meant to be the foreign key, and also: add_index("products", "shop_id") However, if I export the database I see only: KEY `index_products_on_shop_id` (`shop_id`) What should I do in order to add FOREIGN KEY (`shop_id`) REFERENCES Shop(`id`) ? You can use the foreigner gem for adding foreign keys to your application.

Laravel migration self referencing foreign key issue

江枫思渺然 提交于 2019-11-30 09:00:21
Hi I have a problem to create a table using migration schema builder. The problem occure with table with self referencing foreign key. Here is the code which produce error: Schema::create('cb_category', function($table) { $table->integer('id')->primary()->unique()->unsigned(); $table->integer('domain_id')->unsigned(); $table->foreign('domain_id')->references('id')->on('cb_domain'); $table->integer('parent_id')->nullable(); $table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade'); $table->string('name'); $table->integer('level'); }); Here is

Entity Framework - How do I join tables on non-primary key columns in secondary tables?

元气小坏坏 提交于 2019-11-30 08:28:28
问题 I want to join 2 tables using entity framework. I want the join to the second table to be on a non-primary key column. e.g. I have a table Foo with fields Foo.Id (PK) Foo.DbValue and table Bar Bar.Id (PK) Bar.DbValue Bar.Description And I want to join Foo to Bar in EF on the DbValue field. In hibernate/nhibernate one can do this by adding a column parameter to a many-to-one. roughly like this <class name="Foo" table="Foo> <id name="Id" column="Id" /> <many-to-one name="Bar" class="Bar" column

Two foreign keys instead of primary

爱⌒轻易说出口 提交于 2019-11-30 08:23:59
问题 I was wondering, is there any possibility to create a table without a primary key, but with two foreign keys, where the foreign keys pairs are always different? For example, a STOCK table with item_id and warehouse_id as foreign keys from ITEMS and WAREHOUSES tables. So same item can be in different warehouses. The view of the table: item_id warehouse_id quantity 10 200 1000 10 201 3000 10 202 10000 11 200 7000 11 202 2000 12 203 5000 Or do i have to create unused primary key field with auto

ORMlite Android foreign key support

寵の児 提交于 2019-11-30 08:20:34
I am not clever from ORMlite documentation. Is is possible to declare in class, that this parameter is foreign key? e.g. I have table Customer: @DatabaseTable(tableName = "customer") public class Customer { @DatabaseField(id = true) private String customerName; @DatabaseField private String customerSurname; @DatabaseField(foreign = true) private String accountNameHolder; @DatabaseField private int age; public Customer() { } } AccountNameHolder should aim towards DatabaseField name from table Accounts. How to do that? I have found only parameter foreign = true, but there is nothing about, which

Should I use foreign keys? [duplicate]

孤人 提交于 2019-11-30 08:03:55
Possible Duplicate: What’s wrong with foreign keys? I use MS Sql Server with a large database about 4 GB data. I search around the web why I should use foreign keys. by now I only indexed the keys used to join tables. Performance is all fine, dataintegrety is no problem. Should I use foreign keys? Will I get even more performance with foreign keys? Foreign key's don't actually improve performance, in fact they incur a small performance penalty on all write operations, to ensure that the constraint is followed. The reason why you want to use these is to prevent destructive write operations. If

what are the advantages of defining a foreign key

此生再无相见时 提交于 2019-11-30 07:57:56
What is the advantage of defining a foreign key when working with an MVC framework that handles the relation? I'm using a relational database with a framework that allows model definitions with relations. Because the foreign keys are defined through the models, it seems like foreign keys are redundant. When it comes to managing the database of an application in development, editing/deleting tables that are using foreign keys is a hassle. Is there any advantage to using foreign keys that I'm forgoing by dropping the use of them altogether? Foreign keys with constraints(in some DB engines) give

Can someone explain MySQL foreign keys

此生再无相见时 提交于 2019-11-30 07:34:32
I know what they are my question is, how do you link them or are they automatically linked when you have identical names in different tables. Here is an example: Say I have an [orders] table and a [customer] table. Each row in the [orders] table has a customer_id number which is associated with the customer_id in the [customer] table. So how do I get the customer information by referencing the order? What would be the sql query? ... how do you link them or are they automatically linked when you have identical names in different tables. This is not automatic, you have to add a foreign key

Enforce a foreign-key constraint to columns of same table

时间秒杀一切 提交于 2019-11-30 07:27:29
问题 How to enforce a constraint of foreign key on columns of same table in SQL while entering values in the following table: employee : empid number, manager number (must be an existing employee) 回答1: CREATE TABLE TABLE_NAME ( `empid_number` int ( 11) NOT NULL auto_increment, `employee` varchar ( 100) NOT NULL , `manager_number` int ( 11) NOT NULL , PRIMARY KEY (`empid_number`), CONSTRAINT `manager_references_employee` FOREIGN KEY (`manager_number`) REFERENCES (`empid_number`) ) ENGINE=InnoDB