foreign-keys

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

廉价感情. 提交于 2019-11-29 13:59:59
问题 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. 回答1: 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,

Changing MySQL primary key when foreign key contraints exist

三世轮回 提交于 2019-11-29 13:34:16
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE parent ( old_pk CHAR(8) NOT NULL PRIMARY KEY ) ENGINE=InnoDB; CREATE TABLE child ( parent_key CHAR(8), FOREIGN KEY (parent_key) REFERENCES parent(old_pk) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB; I want to add a new auto-incrementing integer id column to parent and use it as the primary key instead, while still keeping old_pk as a unique key and allowing other tables like child to reference it in foreign key contraints. Unfortunately, simply saying ALTER TABLE parent DROP PRIMARY KEY doesn't work

Referencing a composite primary key

冷暖自知 提交于 2019-11-29 13:08:11
问题 I have two tables, with each table having a composite primary key. One attribute is in both composite primary keys. How am i supposed to reference the common attribute?? Do i just reference it as a FK in both tables as below? The cust_id and flight_id below are each part of the composite key as well and reference primary keys in other tables. (Ignore the third attribute in the erd for the br_flight table as I choose to use a composite key in the end). CREATE TABLE BOOKING_REFERENCE (

SQL : one foreign key references primary key in one of several tables

核能气质少年 提交于 2019-11-29 13:03:00
I am working on an application that will be used as an extensible framework for other applications. One of the fundamental classes is called Node, and Nodes have Content. The SQL tables look like this: TABLE Node ( NodeId int, .... etc ) TABLE NodeContentRelationship ( NodeId int, ContentType string, ContentId int) It will be up to the developers extending the application to create their own content types. Clearly this is bad from a relationship-database point of view as it is not possible to add a foreign key relationship to NodeContentRelationship.ContentId, even though it is a foreign key

How to have a foreign key pointing to two primary keys?

≯℡__Kan透↙ 提交于 2019-11-29 12:27:58
I'm trying to simplify a database structure, and I have two tables matches and team_statistics : Here in the team_statistics table the team_statistics.team_id should be a foreign key that references matches.teams_id and matches.teams_id1 and similarly team_statistics.group_id should be a foreign key referencing matches.groups_id and matches.groups_id1 How to do this in PostgreSQL? If there are other ways of doing this by having another table between matches and team_statistics I'm open for suggestion, but I would still like to know how to have one foreign key referencing two primary keys.

How can I query using a foreign key in MySQL?

a 夏天 提交于 2019-11-29 11:59:23
问题 Right now I have a small database with two tables that look something like this: users table ==================== id name status_id 1 Bobby 3 2 James 2 and statuses table ============= id value 1 Waiting 2 Approved 3 Other status_id is setup as a foreign key constraint to id from the statuses table. My query looks something like this: SELECT * FROM `users` WHERE `status_id` = 2"; When I display $row['status_id'] it outputs 2 but I would like it to display as Approved instead, what is the best

laravel migration best way to add foreign key

痴心易碎 提交于 2019-11-29 11:45:01
问题 Simple question: I'm new to Laravel. I have this migration file: Schema::create('lists', function(Blueprint $table) { $table->increments('id'); $table->string('title', 255); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); $table->timestamps(); }); I want to update it to add onDelete('cascade') . What's the best way to do this? 回答1: Firstly you have to make your user_id field an index: $table->index('user_id'); After that you can create a

what are the advantages of defining a foreign key

泪湿孤枕 提交于 2019-11-29 11:20:30
问题 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

Mysql: adding foreign key does not give warning/error on MyISAM tables

。_饼干妹妹 提交于 2019-11-29 11:14:39
Here is a table I made: mysql> show create table notes; +-------+----------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------+ | notes | CREATE TABLE `notes` ( `id` int(11) NOT NULL auto_increment, `note` text NOT NULL, `status` enum('active','hidden','deleted','followup','starred') default NULL, `created` datetime NOT NULL, `last_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +-------+-----------------------------------

Should I use foreign keys? [duplicate]

人走茶凉 提交于 2019-11-29 11:01:23
问题 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? 回答1: Foreign key's don't actually improve performance, in fact they incur a small performance penalty on all write operations, to ensure that the