relational-database

How to create relationships in MySQL

眉间皱痕 提交于 2019-11-26 18:22:08
In class, we are all 'studying' databases, and everyone is using Access. Bored with this, I am trying to do what the rest of the class is doing, but with raw SQL commands with MySQL instead of using Access. I have managed to create databases and tables, but now how do I make a relationship between two tables? If I have my two tables like this: CREATE TABLE accounts( account_id INT NOT NULL AUTO_INCREMENT, customer_id INT( 4 ) NOT NULL , account_type ENUM( 'savings', 'credit' ) NOT NULL, balance FLOAT( 9 ) NOT NULL, PRIMARY KEY ( account_id ) ) and CREATE TABLE customers( customer_id INT NOT

Composite Primary Keys : is it good or bad

大憨熊 提交于 2019-11-26 18:01:16
问题 I've been designing a database for an online store system. The question that I've come across by reading some posts in this website is that although I can use composite primary keys in the case I'm gonna explain below, is it really a bad practice (according to the posts I read in this respect over stackoveflow, many says it is a bad practice so that's why I'm asking). I want to store payments for the orders in a separate table. The reason is that, an order can have many items which are

What Kind of Relationship is Between These Tables?

送分小仙女□ 提交于 2019-11-26 17:51:56
问题 I have two tables that have foreign keys to each other's primary key. This DB is in French. I will translate the two tables that I want to you to understand. Atelier Cuisine ==> Kitchen Cuisinier == > Cooking chef So in this picture we see that in the Kitchen table we have a PK referenced by the FK from the Cooking chef table; in the Cooking chef table we have a PK referenced by the FK from the Kitchen table. So I am confused. I don't understand this kind of relationship between these tables.

Relational database design multiple user types

删除回忆录丶 提交于 2019-11-26 17:48:40
I have a 4 types of users and each have specific data, but they also share commun data, like username , password .. My first thought is to create a main users table with user_type column. Then when querying user data i can just first select their user_type and then depending on the output run a different query to grab "user type" specific data. I am not fond of this as i wish i could grab all user related data with one query and ideally using Foreign Keys. Second idea is to not have a user_type column in the users table and instead use foreign key that from a specific user type table will

What are the known ways to store a tree structure in a relational DB? [closed]

拜拜、爱过 提交于 2019-11-26 17:29:54
问题 There is the "put a FK to your parent" method, i.e. each records points to it's parent. Which is a hard for read actions but very easy to maintain. And then there is a "directory structure key" method: 0001.0000.0000.0000 main branch 1 0001.0001.0000.0000 child of main branch one etc Which is super easy to read, but hard to maintain. What are the other ways and their cons/pros? 回答1: As always: there is no best solution. Each solution makes different things easier or harder. The right solution

Database Normalization chaining

扶醉桌前 提交于 2019-11-26 17:22:28
问题 I have multiple table chaining like so: Table1 product_id SERIAL NOT NULL, name varchar, Table2 ( kept separate from table1 because same product name but can be different color ) table2_id product_id integer, color varchar, FOREIGN KEY (product_id) REFERENCES table1 (product_id) ON DELETE CASCADE table3 ( kept separate from table2 because same product color but can be different size) table3_id table2_id integer, size varchar, FOREIGN KEY (table2_id) REFERENCES table2 (table2_id) ON DELETE

Inventory management with stock options

跟風遠走 提交于 2019-11-26 16:57:03
问题 I'm trying to create an inventory management schema where I can track the stock of various options related to products. A product may have any number of options, but for this example I'll use "size" and "color" options. I've come up with three tables: CREATE TABLE shop_options ( option_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(40) NOT NULL, PRIMARY KEY (option_id) ); INSERT INTO shop_options (option_id, option_name) VALUES (1, 'Size'); INSERT INTO shop_options (option

Setting up a parent-child relationship in Core Data

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:29:46
问题 I'm trying to set up a relationship in Core Data. I have a list of Trees, and each Tree will have a list of Fruits. So I have a Tree entity and a Fruit entity. In code, I will want to list the Trees, in a table view for example. When you click on a Tree, it should then display a list of fruits that growing on that Tree. How do I set up this relationship? Do I need to give the Fruit an attribute called tree? And how do I set the relationship in code, for example when I create a Fruit how do I

How to store a one to many relation in my sql database ? (MySQL)

五迷三道 提交于 2019-11-26 16:08:48
问题 I'm making a website and I need to store a random number of data in my database. for example: User john may have one phone number where jack can have 3. I need to be able so store an infinite number of values per user. I couldn't find how to do this anywhere, Hope you can help me! :) I am a novice in Relational databases. 回答1: You create a separate table for phone numbers (i.e. a 1:M relationship). create table `users` ( `id` int unsigned not null auto_increment, `name` varchar(100) not null,

Laravel - Eloquent “Has”, “With”, “WhereHas” - What do they mean?

为君一笑 提交于 2019-11-26 14:49:08
I've found the concept and meaning behind these methods to be a little confusing, is it possible for somebody to explain to me what the difference between has and with is, in the context of an example (if possible)? lukasgeiter With with() is for eager loading . That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection. Example: User