foreign-keys

PostgreSQL array of elements that each are a foreign key

坚强是说给别人听的谎言 提交于 2019-12-17 08:55:55
问题 I am attempting to create a DB for my app and one thing I'd like to find the best way of doing is creating a one-to-many relationship between my Users and Items tables. I know I can make a third table, ReviewedItems , and have the columns be a User id and an Item id, but I'd like to know if it's possible to make a column in Users , let's say reviewedItems , which is an integer array containing foreign keys to Items that the User has reviewed. If PostgreSQL can do this, please let me know! If

Foreign Key Not Populating with Primary Key Values

孤者浪人 提交于 2019-12-17 07:54:34
问题 I have searched for an answer but am not finding it. I have 2 tables. Both have an auto-generated PK. The PK from table 2 is an FK in table 1. Since they are both auto-generated I assumed the FK in table 1 would populate with the value that is auto-generated from table 2 but it is not working. The FK in table 1 ends up null. Here is my SQL code for creating table 1: CREATE TABLE Employee_tbl ( EmployeeID int PRIMARY KEY IDENTITY, LastName varchar(20) not null, FirstName varchar(20) not null,

How do I add a foreign key to an existing SQLite table?

前提是你 提交于 2019-12-17 07:07:41
问题 I have the following table: CREATE TABLE child( id INTEGER PRIMARY KEY, parent_id INTEGER, description TEXT); How do I add a foreign key constraint on parent_id ? Assume foreign keys are enabled. Most examples assume you're creating the table - I'd like to add the constraint to an existing one. 回答1: You can't. Although the SQL-92 syntax to add a foreign key to your table would be as follows: ALTER TABLE child ADD CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES parent(id); SQLite

How to insert values in table with foreign key using MySQL?

一笑奈何 提交于 2019-12-17 06:38:14
问题 I have these two tables just for example: TAB_TEACHER - id_teacher // primary key, autoincrement - name_teacher // a varchar TAB_STUDENT - id_student // primary key, autoincrement - name_student // a varchar - id_teacher_fk // foreign key reference to a teacher (TAB_TEACHER) I want to know how to insert in these two cases: CASE 1 - INSERT a new Student with an pre-existing TEACHER, so I have to get the foreign key with a teacher name CASE 2 - INSERT a new Student with a new TEACHER (the

Understanding ForeignKey attribute in entity framework code first

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 06:36:51
问题 See the following post for some background: Entity framework one to zero or one relationship without navigation property I had always thought that ForeignKey was used to show which property in a class held the ForeignKey that determined the navigation property e.g. public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int? DeferredDataId { get; set; } [ForeignKey("DeferredDataId")] public virtual DeferredData

Defining multiple Foreign Key for the Same table in Entity Framework Code First

拟墨画扇 提交于 2019-12-17 06:27:32
问题 I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance. Student: public class Student { public int ID { get; set; } public string Name { get; set; }

Force InnoDB to recheck foreign keys on a table/tables?

旧城冷巷雨未停 提交于 2019-12-17 06:23:07
问题 I have a set of InnoDB tables that I periodically need to maintain by removing some rows and inserting others. Several of the tables have foreign key constraints referencing other tables, so this means that the table loading order is important. To insert the new rows without worrying about the order of the tables, I use: SET FOREIGN_KEY_CHECKS=0; before, and then: SET FOREIGN_KEY_CHECKS=1; after. When the loading is complete, I'd like to check that the data in the updated tables still hold

How to change the foreign key referential action? (behavior)

南笙酒味 提交于 2019-12-17 04:47:59
问题 I have set up a table that contains a column with a foreign key, set to ON DELETE CASCADE (delete child when parent is deleted) What would the SQL command be to change this to ON DELETE RESTRICT ? (can't delete parent if it has children) 回答1: Old question but adding answer so that one can get help Its two step process: Suppose, a table1 has a foreign key with column name fk_table2_id , with constraint name fk_name and table2 is referred table with key t2 ( something like below in my diagram )

How to change the foreign key referential action? (behavior)

不想你离开。 提交于 2019-12-17 04:47:14
问题 I have set up a table that contains a column with a foreign key, set to ON DELETE CASCADE (delete child when parent is deleted) What would the SQL command be to change this to ON DELETE RESTRICT ? (can't delete parent if it has children) 回答1: Old question but adding answer so that one can get help Its two step process: Suppose, a table1 has a foreign key with column name fk_table2_id , with constraint name fk_name and table2 is referred table with key t2 ( something like below in my diagram )

How can I INSERT data into two tables simultaneously in SQL Server?

随声附和 提交于 2019-12-17 04:19:19
问题 Let's say my table structure looks something like this: CREATE TABLE [dbo].[table1] ( [id] [int] IDENTITY(1,1) NOT NULL, [data] [varchar](255) NOT NULL, CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED ([id] ASC) ) CREATE TABLE [dbo].[table2] ( [id] [int] IDENTITY(1,1) NOT NULL, [table1_id] [int] NOT NULL, [data] [varchar](255) NOT NULL, CONSTRAINT [PK_table2] PRIMARY KEY CLUSTERED ([id] ASC) ) The [id] field of the first table corresponds to the [table1_id] field of the second. What I would like