foreign-keys

MySQL table with a varchar column as foreign key

北城以北 提交于 2019-11-28 02:41:14
问题 I am trying to create a table with a varchar column as foreign key but MySql gives me an error while creating the table. My query is like this: CREATE TABLE network_classes ( id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT, category VARCHAR(80) NOT NULL, PRIMARY KEY(id), KEY `key_1` (`id`,`category`) ) ENGINE=InnoDB; CREATE TABLE networks ( id TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, category VARCHAR(80) NOT NULL, director_id TINYINT(3) UNSIGNED NULL, director

Foreign key constraints: When to use ON UPDATE and ON DELETE

微笑、不失礼 提交于 2019-11-28 02:30:58
I'm designing my database schema using MySQL Workbench, which is pretty cool because you can do diagrams and it converts them :P Anyways, I've decided to use InnoDB because of it's Foreign Key support. One thing I noticed though is that it allows you to set On Update and on Delete options for foreign keys. Can someone explain where "Restrict", "Cascade" and set null could be used in a simple example? For example, say I have a user table which includes a userID . And say I have a message table message which is a many-to-many which has 2 foreign keys (which reference the same primary key, userID

Is there a way to maintain a db relationship (pk/fk) in the following scenario

ⅰ亾dé卋堺 提交于 2019-11-28 02:27:48
My current db design is in the screenshot. It seems flawed because there is no enforced relationship (pk/fk) between the t_criteria_director and the t_criteria_a b c d e f... tables. Is there a better way to design this one to many scenario to keep referential integrity? Your design doesn't "seem" anything because we can't read your mind. You have given some aspects of a design but not the business "scenario" it represents/implements/describes or how it does so. SQL NULL, UNIQUE, PKs & FKs are kinds of constraints. A constraint is a limitation on what database values can appear. An SQL FK says

Find referenced field(s) of foreign key constraint

ぃ、小莉子 提交于 2019-11-28 02:20:51
I have a table Users with a field called org_id which is a foreign key to a table organisation , with primary key field organisation_id . Knowing the table name (users) and the field name ( users.org_id ), is there a query that can tell me the name and field that org_id references? I've found a Stackoverflow post similar to this where a query was provided to determine the referenced table name, but I also need to know the field name that is referenced: SELECT c.confrelid::regclass::text AS referenced_table ,c.conname AS fk_name ,pg_get_constraintdef(c.oid) AS fk_definition FROM pg_attribute a

Creating entity relationship with renamed fields and non-primary key in primary table

瘦欲@ 提交于 2019-11-28 01:50:55
The following are two partial tables in which I am trying to define a foreign key relationship. public class Form { [Key, Column("FormID")] public System.Guid FormGUID { get; set; } [Column("PatGUID")] public Nullable<System.Guid> PatientGUID { get; set; } } public class Patient { [Column("PatGUID")] public System.Guid PatientGUID { get; set; } [Key, Column("PatID")] public int PatientID { get; set; } } I've eliminated all but the relevant information, fields, navigations, etc. for this example; hopefully not too much. We have a table Form, with a FK of PatGUID to a Patient table with field

Many-to-Many Relationships in MySQL

僤鯓⒐⒋嵵緔 提交于 2019-11-28 01:13:40
问题 I've been reading up on foreign keys and joins recently, and have been pleasantly surprised that many of the basic concepts are things I'm already putting into practice. For example, with one project I'm currently working on, I'm organizing word lists, and have a table for the sets, like so: `words` Table `word_id` `headword` `category_id` `categories` Table `category_id` `category_name` Now, generally speaking this would be a one-to-many relationship, with several words being placed under a

EF4 Independent Associations - Why avoid them?

匆匆过客 提交于 2019-11-28 01:07:58
问题 I saw this comment on MSDN (link and link): "Note that Independent Associations should often be avoided since things like N-Tier and concurrency becomes more difficult." I'm new to EF4 and I'm building an n-Tier web app. This sounds like an important pitfall. Can someone explain to me what this means? 回答1: I think it's personal preference. Originally, EF was created to only use indep. associations and aligned with a more classic ERM approach. However, most of us devs are so dependent on FKs

PostgreSQL foreign key not existing, issue of inheritance?

别来无恙 提交于 2019-11-27 23:50:24
I am struggling with foreign keys in my DB, possibly it has something to do with inheritance? So here's the basic setup: -- table address CREATE TABLE address ( pk_address serial NOT NULL, fk_gadmid_0 integer NOT NULL, -- this table already exists, no problem here street character varying(100), zip character varying(10), city character varying(50), public boolean, CONSTRAINT address_primarykey PRIMARY KEY (pk_address), CONSTRAINT gadmid_0_primarykey FOREIGN KEY (fk_gadmid_0) REFERENCES adm0 (gadmid_0) MATCH SIMPLE ON UPDATE CASCADE ON DELETE NO ACTION ) WITH ( OIDS=FALSE ); ALTER TABLE address

Entity Framework relationships between different DbContext and different schemas

喜你入骨 提交于 2019-11-27 23:36:45
问题 So, I have two main objects, Member and Guild. One Member can own a Guild and one Guild can have multiple Members. I have the Members class in a separate DbContext and separate class library. I plan to reuse this class library in multiple projects and to help differentiate, I set the database schema to be "acc". I have tested this library extensively and can add, delete, and update Members in the acc.Members table. The Guild class is as such: public class Guild { public Guild() { Members =

Adding constraints in phpMyAdmin

家住魔仙堡 提交于 2019-11-27 23:16:27
问题 I feel like I'm being stupid, but I can't find anywhere on the phpMyAdmin interface to add constraints to foreign keys e.g. CASCADE ON DELETE I've looked for similar questions on here and on the phpMyAdmin wiki but I can't find anything about it. I realise I could do this via the query interface, but I'd like to know how to do it through the graphical interface. 回答1: First, you should have your storage engine as InnoDB. Then select a table and go to 'Structure' tab. Under the table you will