relational-database

What's the best way to store an array in a relational database?

陌路散爱 提交于 2019-11-30 19:11:20
I'm currently trying to design a database and I'm not too sure about the best way to approach a dynamically sized array field of one of my objects. My first thought is to use a column in my object to store an array of integers. However the more I read, the more I think this isn't the best option. Concrete example wise, I have a player object that stores 0 to many items, which are represented by an integer. What is the best way to represent this? If that collection of values is atomic , store them together. Meaning, if you always care about the entire group, if you never search for nested

Rails: violates foreign key constraint

旧巷老猫 提交于 2019-11-30 19:07:37
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::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "books" violates foreign key constraint

MySQL Database design. Inserting rows in 1to1 tables.

余生颓废 提交于 2019-11-30 18:57:52
问题 What is the best way to insert rows into tables with references 1 to 1 of each other? I mean, in a MySQL 5.5 and tables InnoDB, I have a database design similar to the following The problem arises when we try to insert rows in table1 and table2. Since there is no multi-table insert in MySQL, I can not insert a row becouse the foreign keys are NOT NULL fields in both tables and should be inserted simultaneously in both. Which is the bes way to solve this problem? I have in mind 3 possible

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

时光总嘲笑我的痴心妄想 提交于 2019-11-30 18:34:52
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? A default number of rows (not the entire result set) will be fetched in your local memory. Once you reach at the last line of the fetched rows (say by doing

Laravel / Eloquent : hasManyThrough WHERE

时光怂恿深爱的人放手 提交于 2019-11-30 18:17:08
问题 In the documentation of Eloquent it is said that I can pass the keys of a desired relationship to hasManyThrough . Lets say I have Models named Country, User, Post. A Country model might have many Posts through a Users model. That said I simply could call: $this->hasManyThrough('Post', 'User', 'country_id', 'user_id'); This is fine so far! But how can I get these posts only for the user with the id of 3 ? Can anybody help here? 回答1: So here it goes: models: Country has many User has many Post

Average posts per hour on MySQL?

谁都会走 提交于 2019-11-30 18:06:26
I have a number of posts saved into a InnoDB table on MySQL. The table has the columns "id", "date", "user", "content". I wanted to make some statistic graphs, so I ended up using the following query to get the amount of posts per hour of yesterday: SELECT HOUR(FROM_UNIXTIME(`date`)) AS `hour`, COUNT(date) from fb_posts WHERE DATE(FROM_UNIXTIME(`date`)) = CURDATE() - INTERVAL 1 DAY GROUP BY hour This outputs the following data: I can edit this query to get any day I want. But what I want now is the AVERAGE of each hour of every day, so that if on Day 1 at 00 hours I have 20 posts and on Day 2

Normalization of an 1:1 or 1:0 relationship

我与影子孤独终老i 提交于 2019-11-30 18:06:23
问题 when using relation databases and you want 3NF (do you call it 3NF in english?), then you pull 1:1 relationsships together into one table. But what happens if the rationship is 1:0/1 (/ meaning or)? Then you keep them separated to avoid blank spaces in tables? Kepping them apart is valid 3NF in ths case? 回答1: Third normal form basically means that an attribute (or column) depends on the key, the whole key and nothing but the key (so help me, Codd). If you have an attribute which is either

Best method to verify multi-level relational dependencies

南楼画角 提交于 2019-11-30 17:55:03
问题 Suppose you have entities A, B, C and D. D relates to C C relates to B B relates to A Furthermore, user is only allowed to operate on D , if user owns A . At a certain state in an application, you include a link to a page, which accesses D. Thus, you includes D's ID as a GET or POST parameter. If user clicks on the link, the application retrieves D's ID and begins to operate on D. Simple apps use URLs like this [modulo URL-rewriting]: http://www.myServer.com/?action=1234&entity=D&ID=23 How to

Any good relational database tutorials? [closed]

喜夏-厌秋 提交于 2019-11-30 17:50:25
I am looking for how to build a relational mysql database, and I would like to follow a tutorial. I need one that shows how to make multiple tables, and link those together using an id. Which I can later use to grab relational data from the different tables. This is probably easy stuff, but I am just learning about databases. Thanks for the help! Here is a really good one for MySQL beginners. Nettus have some really great articles on Internet technologies. They are the new w3school. http://net.tutsplus.com/tutorials/databases/sql-for-beginners/ If you haven't already, I'd start with the MySQL

SQL two tables and creating a link table

倖福魔咒の 提交于 2019-11-30 17:38:53
问题 I have two tables: Employee (ID, Name, Address) and Store(ID,Address) and I would like to record information about people who work in each store. I thought of making a new table called Employee_List table. My questions: 1- Employee_List and Employee has one-to-many relation, right? 2- Employee_list to store has one-to-one relation, right? 3- How to define foreign and primary keys for Employee_List table? 回答1: Employee_list should have: employee_listid (INT PK) employee_id (INT FK) store_id