foreign-keys

SQL Server 2008 - Multiple Cascading FK's - Do i need a trigger?

浪子不回头ぞ 提交于 2019-12-01 04:11:57
问题 I have a 1..* relationship between User and Post . (one user has many posts) Post has a FK called "UserId", which maps to the "UserId" field on User table. I tried to set this FK as Cascade UPDATE/DELETE, but i get this error: 'Users' table saved successfully 'Posts' table - Unable to create relationship 'FK_Posts_Users'. Introducing FOREIGN KEY constraint 'FK_Posts_Users' on table 'Posts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or

Mysql create table with multiple foreign key on delete set null

随声附和 提交于 2019-12-01 04:11:54
I am trying to create a database with multiple foreign keys with delete/ update constraints, but I got a error code 1005 with following sql scripts: CREATE TABLE Worker ( WorkerID smallint auto_increment, WorkerType varchar(45) NOT NULL, WorkerName varchar(45) NOT NULL, Position varchar(45) NOT NULL, TaxFileNumber int NOT NULL, Address varchar(100) , Phone varchar(20) , SupervisorID smallint , PRIMARY KEY (WorkerID), FOREIGN KEY (SupervisorID) REFERENCES Worker(WorkerID) ON DELETE SET NULL ON UPDATE CASCADE )Engine=InnoDB; CREATE TABLE Grape ( GrapeID smallint NOT NULL, GrapeType varchar(45)

How to put foreign key constraints on a computed fields in sql server?

天涯浪子 提交于 2019-12-01 03:43:02
Table A has a computed field called Computed1. It's persisted and not null. Also, it always computes to an expression which is char(50). It's also unique and has a unique key constraint on it. Table B has a field RefersToComputed1, which should refer to a valid Computed1 value. Trying to create a foreign key constraint on B's RefersToComputed1 that references A' Computed1 leads to the following error: Error SQL01268: .Net SqlClient Data Provider: Msg 1753, Level 16, State 0, Line 1 Column 'B.RefersToComputed1' is not the same length or scale as referencing column 'A.Computed1' in foreign key

Adding foreign key on multiple columns

天大地大妈咪最大 提交于 2019-12-01 03:40:50
I'm trying to create a foreign key on two columns of a table to point to the same column of another table, but I seem to get an error... Here's what I do: CREATE TABLE test2 ( ID INT NOT NULL AUTO_INCREMENT, col1 INT NOT NULL, col2 INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk FOREIGN KEY (col1, col2) REFERENCES test1(ID, ID) ON UPDATE CASCADE ON DELETE RESTRICT ) ENGINE=InnoDB; But I get ERROR 1005 (HY000): Can't create table 'DB.test2' (errno: 150) If I only have one column, however, the table is correctly created. Could someone point out to me where the error is? Thanks n Tried it here and

Entity Framework foreign keys to non-primary key fields

眉间皱痕 提交于 2019-12-01 03:30:18
Is it possible for Entity Framework 4.0 to have an association/navigation property based off of a foreign key to a non-primary key field (it has a unique constraint). No because EF don't understand unique constraint yet and relations in EF must follow same rules as relations in database. Without unique principal relation cannot exist and the only way to get unique principal in EF is using primary key. 来源: https://stackoverflow.com/questions/9217448/entity-framework-foreign-keys-to-non-primary-key-fields

MySQL Syntax in creating Foreign Key

浪尽此生 提交于 2019-12-01 03:25:11
Is this syntax correct in creating a Foreign Key? create table department ( departmentID int not null auto_increment primary key, name varchar(30) ) type=InnoDB; create table employee ( employeeID int not null auto_increment primary key, name varchar(80), job varchar(30), departmentID int not null references department(departmentID) ) type=InnoDB; Doug It looks like MySQL accepts it (doesn't complain about the syntax) but the foreign key is not actually created. To create this foreign key, run this command: ALTER TABLE employee ADD CONSTRAINT fk_department FOREIGN KEY (departmentID) REFERENCES

sqlite3 “foreign key constraint failed”

谁都会走 提交于 2019-12-01 03:10:46
I've set up two tables: CREATE TABLE A ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT ); CREATE TABLE B ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, id2 INTEGER, book TEXT, FOREIGN KEY(id2) REFERENCES A(id) ); After I insert data into A , it looks like this: 1 John 2 Amy 3 Peter After I insert data into B , it looks like this: 1 1 Lord of the Rings 2 1 Catch 22 3 2 Sum of All Fears 4 3 Hunt for Red October I then execute the following statement: delete from a where id=1; I get the following: "Error: foreign key constraint failed" I then restart sqlite3 and try again but this

MySQL - Cannot add or update a child row: a foreign key constraint fails

北慕城南 提交于 2019-12-01 02:24:58
This seems to be a common error, but for the life of me I can't figure this out. I have a set of InnoDB user tables in MySQL that are tied together via foreign key; the parent user table, and a set of child tables that store email addresses, actions, etc. These are all tied to the parent user table by a foreign key, uid , with all of the parent and child keys being int(10) . All of the child tables have a uid value with a foreign key constraint pointing to user.uid , and set to ON DELETE CASCADE and ON UPDATE CASCADE . When I delete a user from user , all of the child constrained entries are

Adding foreign key to existing table gives error 1050 table already exists

雨燕双飞 提交于 2019-12-01 02:16:21
I've a table CustomizationSet with the columns: customization_set_guid (which is a non-nullable guid and also the primary key) creator_account_guid and a few others And a table with existing data Registration with the columns: registration_id (an int and the primary key) customization_set_guid (also a guid (so a char(36)) which is nullable, and all entries are currently null) and a few other columns When I try and run ALTER TABLE Registration ADD FOREIGN KEY ( customization_set_guid ) REFERENCES CustomizationSet ( customization_set_guid ); in MySQL Workbench, it gives the error 1050Table '.

How to delete data from multiple tables?

浪尽此生 提交于 2019-12-01 02:13:34
I have these tables: event (evt_id, evt_code, reg_id) magnitude (mag_id, evt_id, value) trace (trace_id, pt_id) point (pt_id, evt_id) I want to delete all rows from all tables related to evt_id=1139 . How do I do it? Justin Pihony If you have control over your schema, I would make the schema use cascading deletes . From the article (the more pertinent portion translated for your example) CREATE TABLE point ( pt_id integer PRIMARY KEY, evt_id integer REFERENCES event ON DELETE CASCADE ) If you have cascades set up, then you can just delete from the main event table and all the other tables will