foreign-keys

MySQL cleanup table from duplicated entries AND relink FK in depending table

浪子不回头ぞ 提交于 2019-12-20 04:58:07
问题 Here is my situation: I have 2 tables, patient and study . Each table has its own PK using autoincrement. In my case, the pat_id should be unique. It's not declared as unique at database level since it could be non unique is some uses (it's not a home made system). I found out how to configure the system to consider the pat_id as unique, but I need now to cleanup the database for duplicated patients AND relink duplicated patients in study table to remaining unique patient , before deleting

Unable to determine the principle end of an association

隐身守侯 提交于 2019-12-20 04:33:13
问题 Using EF5 Code first, I have two classes: [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } [ForeignKey("AddressId")] public virtual Address Address { get; set; } } [Table("Address")] public class Address : IEntity { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int

Foreign keys referring other foreign keys in PostgreSQL

与世无争的帅哥 提交于 2019-12-20 04:32:42
问题 In PostgreSQL I have a database, which I intend to make the following table declaration: CREATE TABLE canvas_user ( id INTEGER, login_id VARCHAR(50) UNIQUE NOT NULL, email VARCHAR(355) UNIQUE NOT NULL, name_given VARCHAR(30), name_family VARCHAR(30), name_full VARCHAR(50), role canvas_role, last_login TIMESTAMP, PRIMARY KEY (id) ); CREATE TABLE problem ( id SERIAL, title VARCHAR(50), author VARCHAR(50), path TEXT, compiler VARCHAR(20), PRIMARY KEY (id) ); CREATE TABLE assignment ( id INTEGER,

Foreign keys referring other foreign keys in PostgreSQL

谁说胖子不能爱 提交于 2019-12-20 04:32:21
问题 In PostgreSQL I have a database, which I intend to make the following table declaration: CREATE TABLE canvas_user ( id INTEGER, login_id VARCHAR(50) UNIQUE NOT NULL, email VARCHAR(355) UNIQUE NOT NULL, name_given VARCHAR(30), name_family VARCHAR(30), name_full VARCHAR(50), role canvas_role, last_login TIMESTAMP, PRIMARY KEY (id) ); CREATE TABLE problem ( id SERIAL, title VARCHAR(50), author VARCHAR(50), path TEXT, compiler VARCHAR(20), PRIMARY KEY (id) ); CREATE TABLE assignment ( id INTEGER,

Three level database - foreign keys

纵饮孤独 提交于 2019-12-20 04:27:17
问题 I have a three level database with the following structure (simplified to only show the primary keys): Table A: a_id Table B: a_id, b_id Table C: a_id, b_id, c_id So possible values for table C would be something like this: a_id b_id c_id 1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 2 1 1 2 2 1 2 2 2 ... I am now unsure, how foreign keys should be set; or if they should be set for the primary keys at all. My idea was to have a foreign key on table B B.a_id -> A.a_id , and two foreign key on C C.a_id -> A.a

Include other field as choices to foreign key, Django

杀马特。学长 韩版系。学妹 提交于 2019-12-20 02:58:09
问题 I have two models as follows : class FlightSchedule(models.Model): tail_number = models.ForeignKey(TailNumber, null=False, blank=False) flight_number = models.CharField(max_length=30, null=False, blank=False) flight_group_code = models.ForeignKey(FlightGroup, null=False, blank=False) origin_port_code = models.ForeignKey(Port, null=False, related_name="Origin", blank=False) destination_port_code = models.ForeignKey(Port, null=False, related_name="Destination", blank=False) flight_departure

Does it ever make sense to have more than one foreign keys between two tables

北城以北 提交于 2019-12-20 02:37:13
问题 I was learning Sqlalchemy and noticed that a foreign key relation was defined by relating the table names without identifying the actual foreign key connecting the two tables, so I was thinking, if there are more than one foreign keys between the two tables(if it ever makes sense), sqlalchemy would fail to determine the key used to link the two tables. Anyway, my question is: does it ever make sense to have more than one foreign keys between two tables 回答1: Yes, there are certainly cases

SQL Server: drop a FK constraint, which is not foreign key

吃可爱长大的小学妹 提交于 2019-12-20 02:28:24
问题 I am using SQL Server 2008 and its Management Studio. I am doing a web project, which has a tool to automate the tables/relationships creation. My web project reveals this error: Unsuccessful: alter table Tester add constraint FK_c6c4bf4s2rvp56a32nnruww2b foreign key (game) references Game Column 'Game.id' is not the same data type as referencing column 'Tester.game' in foreign key 'FK_c6c4bf4s2rvp56a32nnruww2b' However, when I ran the following in the management studio: ALTER TABLE dbo

Values controlled by foreign keys

こ雲淡風輕ζ 提交于 2019-12-20 01:44:34
问题 I have very simple database in SQL Server, with these three tables: Theater ( ID, is3D, othervalues...) Show ( ID, Theater_ID, Movie_ID, date, time, othervalues...) Movie ( ID, is3D, othervalues...) I would like to ensure that 3D movies can be only played in 3D theaters. 2D movies only in 2D theaters , and doing it ONLY BY foreign keys (no triggers etc.). 回答1: To do this through foreign keys alone you need to add an is3D column to Show as well and a couple of logically redundant UNIQUE

Should I make a foreign key that can be null or make a new table?

删除回忆录丶 提交于 2019-12-20 01:13:20
问题 I have a small question concerning with how I should design my database. I have a table dogs for an animal shelter and I have a table owners. In the table dogs all dogs that are and once were in the shelter are being put. Now I want to make a relation between the table dogs and the table owners. The problem is, in this example not all dogs have an owner, and since an owner can have more than one dog, a possible foreign key should be put in the table dogs (a dog can't have more than one owner,