foreign-keys

Is it possible to have a MySQL column containing multiple values as foreign keys?

♀尐吖头ヾ 提交于 2019-12-06 15:35:42
I am learning MySQL and have MariaDB installed in Fedora 19. I have a scenario where I require a column to contain multiple values in order to reduce possible redundancy of column allocation. In the example below, is it possible to have each value in the tags column of the log table reference the tag_id column in the tags table? users user_id | 1 | activities activitity_id | 1 log user_id | activity_id | tags 1 | 1 | 1,3,5 # multiple foreign keys? tags tag_id | 1 | 2 | 3 | 4 | 5 | If it is not possible, could anyone provide the logic for the most feasible solution based on the data scenario

Only one key from composite primary key as foreign key

倾然丶 夕夏残阳落幕 提交于 2019-12-06 14:35:29
问题 In this database, key1 & key2 make up the composite primary key of table4 , but i'm able to add a foreign key to table3 that comprise just key1 . Why MySQL allows this? Does the above database design make no sense at all? 回答1: TL;DR It's not standard SQL. MySQL documentation itself advises not doing it. In SQL a FOREIGN KEY declaration must reference a column list declared PRIMARY KEY or UNIQUE NOT NULL. (PRIMARY KEY creates a UNIQUE NOT NULL constraint.) (The constraint has to be explicit,

How to use a foreign key in sqlite?

限于喜欢 提交于 2019-12-06 14:11:17
I have two tables in sqlite, which are "connected" with an id. The app using this tables is running on Android OS. Table 1;: |id| entry 1| entry2| |1 | aaaaaa | aaaaa | |2 | bbbbbb | bbbbb | Table 2: |id| entryx| constant| |1 | aaaaa | aaaaaaaa| |1 | baaaa | baaaaaaa| |1 | caaaa | caaaaaaa| |2 | ababa | baabaaba| At the moment I delete entries with the following query, by using a loop: do{ db.delete("Table 1","id='"+cid+"'",null); db.delete("Table 2","id='"+cid+"'",null); } while(getNextID()); I want to use a foreign key, which allows me to delete the entry in table 1 and all entries in table

How to make two foreign keys to same model unique together?

穿精又带淫゛_ 提交于 2019-12-06 13:41:37
问题 Let's say I have a relationship class such as: class Friendship(models.Model): person1 = models.ForeignKey(Person, related_name='person1') person2 = models.ForeignKey(Person, related_name='person2') so I want to make this object unique for a pair of Person s. If I simply do unique_together = (("person1", "person2"),) then I can end up with two Friendship objects where FS1.person1 = A, FS1.person2 = B FS2.person1 = B, FS2.person2 = A I do not want this. I want a unique friendship object

Comment system design

五迷三道 提交于 2019-12-06 13:20:13
问题 Here is my current comment system design: I'm developing it for a website that has lots of areas, blogs, tutorials, manuals etc etc. As supposed to developing a separate comment table for each ( tblBlogComments , tblTutorialComments ) etc etc, I'm trying to go for a one structure fits all approach. This way, I can turn the comment system into a web control, and just drop it on any page that I want comments for. It means I only have one set of rules, one set of code files to maintain. The only

Example of when you should use a foreign key that points to a candidate key, not a primary key?

我的梦境 提交于 2019-12-06 12:52:46
问题 From my reading, I understand what makes a good primary key, what a foreign key is and what a candidate key is. I've read in several different books and sources that: A foreign key must point to a candidate key (or primary) A foreign key almost always points to a primary key The authors of the sources always say something along the lines of, "while foreign keys can point at a candidate key (not primary) they seem to". Are there any examples of why you might choose a candidate key and not the

Best practice for inserting objects with foreign keys in symfony2

丶灬走出姿态 提交于 2019-12-06 11:35:51
问题 What should be the best practice to insert a foreign key in an object in symfony2, if i already have the id of the foreign key? If i have an action: /** * @Route("assignCategory/{category}/product/{product}") */ public function assignCategory($category, $product) { ... // here i should set the category to the existing product ... } I don't like the idea of retrieving the category from the database. This also applies if you have ids in session and you want to store them to the object but

Get all foreign keys using JDBC

a 夏天 提交于 2019-12-06 11:26:21
问题 I am using postgreSQL. I am trying to get all of the foreign keys from a table. This is the method that I am currently using. public String getFKeyData(String tableName, int i) throws SQLException { DatabaseMetaData dm = connection.getMetaData(); ResultSet rs = dm.getImportedKeys(null, null, tableName); while (rs.next()) { fkTableData = rs.getString(i); } return fkTableData; } This code works but it only gets me the last foreign key which is fine if there is only one in the table but this

JPA/hibernate - Cannot add or update a child row: a foreign key constraint fails - BUT record exists

☆樱花仙子☆ 提交于 2019-12-06 11:20:18
I'm having a strange problem. I have some records in the database: Company id = 1, Name = Microsoft id = 2, Name = Sun Now I have another entity, Event which has a foreign key reference to Company: @Entity public class Event { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @ManyToOne private Company company; } In my web service layer, I create an Event using a company ID passed as a URL param: @GET @Path("addEvent") public String addEvent(@QueryParam("cid") long companyId) { Company alreadyInDB = new Company(); alreadyInDB.setId(companyId); Event event = new Event(); event

foreign key and index issue

倖福魔咒の 提交于 2019-12-06 11:18:44
I am using SQL Server 2008 Enterprise. I have a table and one of its column is referring to another column in another table (in the same database) as foreign key, here is the related SQL statement, in more details, column [AnotherID] in table [Foo] refers to another table [Goo]'s column [GID] as foreign key. [GID] is primary key and clustered index on table [Goo]. My question is, in this way, if I do not create index on [AnotherID] column on [Foo] explicitly, will there be an index created automatically for [AnotherID] column on [Foo] -- because its foreign key reference column [GID] on table