relational-database

One-to-Many relationship in MySQL - how to build model?

你说的曾经没有我的故事 提交于 2019-11-30 08:43:49
I have got two tables: 1) Area 2) Map Each Area shall have at least 1 Map, but can also have more than one Map. One Map can only belong to one Area. How to build this in MySQL? Jenius Add a Foreign key in Map that references the Area's Primary Key. That will enforce a one-to-many relationship between Maps and Areas. As for enforcing a minimum of one map per area (if that is necessary) there are some ideas in this post here . One of the simpler solutions would be to create a view that only displays areas which have maps: CREATE VIEW viewAreas AS SELECT * FROM Areas, Maps WHERE Areas.ID = Maps

How to perform a LEFT JOIN in SQL Server between two SELECT statements? [closed]

送分小仙女□ 提交于 2019-11-30 08:22:34
I have two SELECT statements in SQL Server like these: (SELECT [UserID] FROM [User]) (SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043) I want to perform a LEFT JOIN between these two SELECT statements on [UserID] attribute and [TailUser] attribute. I want to join existent records in second query with the corresponding records in first query and NULL value for absent records. How can I do this? SELECT * FROM (SELECT [UserID] FROM [User]) a LEFT JOIN (SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043) b ON a.UserId = b.TailUser SELECT [UserID] FROM [User] u LEFT

what are the advantages of defining a foreign key

此生再无相见时 提交于 2019-11-30 07:57:56
What is the advantage of defining a foreign key when working with an MVC framework that handles the relation? I'm using a relational database with a framework that allows model definitions with relations. Because the foreign keys are defined through the models, it seems like foreign keys are redundant. When it comes to managing the database of an application in development, editing/deleting tables that are using foreign keys is a hassle. Is there any advantage to using foreign keys that I'm forgoing by dropping the use of them altogether? Foreign keys with constraints(in some DB engines) give

Insert multiple rows in one table based on number in another table

本秂侑毒 提交于 2019-11-30 06:01:02
问题 I am creating a database for the first time using Postgres 9.3 on MacOSX. Let's say I have table A and B . Table A starts off as empty and Table B as filled. I would like the number of entries in column all_names in table B to equal the number for each names in table A like table B below. Thus names should contain each unique entry from all_names and number its count. I am not used to the syntax yet so I do not really know how to go about it. The birthday column is redundant. A names | number

Data migration between different DBMS's

让人想犯罪 __ 提交于 2019-11-30 05:53:33
As i couldnt get any satisfying answer to my Question it seems we have to write our own program for that, we are in the design phase and we are thinking which format shall we use to backup the data. The program will be written in Delphi. Needed is Exporting/Importing data between Oracle/Informix/Msserver, very important here is the Performance issue, as this program will run on a 1-2 GB Databases. Beside the normal data there are Blobs in the Database which have to be backuped. We thought of Xml-Data or comma-separated data as both are transparent (which is nice to have), but Blobs must be

6NF and historical attribute data

百般思念 提交于 2019-11-30 04:51:23
问题 When using a database normalized accoring to 6NF principles, how would you store historical attribute data? Let say for example we take this example from @PerformanceDBA but with the following extra requirement: We need to store historical data for all our products, we should be able to just enter a date and get a snapshot of the attributes of the product at that particular time. A more practical example : Suppose the disks and CPU's from the example above are virtual and a user can change

How to store bidirectional relationships in a RDBMS like MySQL?

左心房为你撑大大i 提交于 2019-11-30 03:38:54
Suppose I want to store relationships among the users of my application, similar to Facebook, per se. That means if A is a friend(or some relation) of B , then B is also a friend of A . To store this relationships I am currently planning to store them in a table for relations as follows UID FriendID ------ -------- user1 user2 user1 user3 user2 user1 However I am facing two options here: The typical case, where I will store both user1 -> user2 and user2->user1 . This will take more space, but (at least in my head) require just one pass over the rows to display the friends of a particular user.

Rails: violates foreign key constraint

廉价感情. 提交于 2019-11-30 03:01:49
问题 I have three models: Book , genre , BookGenre , and here are relationships: class BookGenre < ActiveRecord::Base belongs_to :book belongs_to :genre end class Book < ActiveRecord::Base has_many :book_genres has_many :genres, through: :book_genres end class Genre < ActiveRecord::Base has_many :book_genres has_many :books, through: :book_genres end And then I use seed file to put data into these tables. But when I want to do rake db:seed again, it showed this error ActiveRecord:

Where Result set is stored while working with jdbc and oracle driver

亡梦爱人 提交于 2019-11-30 02:41:44
问题 Once I use jdbc with oracle driver and run select query is the result of the query is stored in the server of oracle memory or file system or temp table ? and once I run the next method by getting the next row is it loaded from the oracle server memory to the jvm memory ? And in case I define the the number of fetch size on the result set to be 1000 is this mean that the 1000 rows are loaded from the oracle to the JDBC driver on the JVM? 回答1: A default number of rows (not the entire result

has_many :through multiple has_one relationships?

懵懂的女人 提交于 2019-11-30 02:25:45
I'm writing a mentorship program for our church in rails (im still farily new to rails).. And i need to model this.. contact has_one :father, :class_name => "Contact" has_one :mother, :class_name => "Contact" has_many :children, :class_name => "Contact" has_many :siblings, :through <Mother and Father>, :source => :children So basically an objects "siblings" needs to map all the children from both the father and mother not including the object itself.. Is this possible? Thanks Daniel It's funny how questions that appear simple can have complex answers. In this case, implementing the reflexive