MySQL link two tables together implicitly - mysql

Suppose we have two tables
A table called people with people linked to a bank account balances
| id | name | account_id |
--------------------------
| 1 | bob | 11 |
--------------------------
| 2 | sam | 22
A table called accounts with bank account balances
| id | value |
--------------
| 11 | 200 |
--------------
| 22 | 500 |
In order to link the two tables you can do
SELECT a.value as account_balance
FROM people p
WHERE p.name="bob"
LEFT JOIN accounts a ON p.account_id = a.id`
This would return
id => 1
name => bob
account_balance => 200
That's cool - but I am wondering if there is a more implicit way to do this via SQL linkage (foreign keys or otherwise). Can we in MySQL add links in some other way so that when we do a SELECT, it already knows to return value instead of **account_id **?
I'm asking this because I am creating a system where my users can create lookup tables and link them to other tables - but it must be do-able without any programming. The only other way I can think of is to set the name of account_id for example to accounts.value and treat that as a foreign key when doing a SELECT.
I would have to get the column structure and analyze and then determine that there is a foreign key and then return the appropriate foreign column by looking at the column name.

Related

Adding data to table by comparing one table to another

I am working to split apart this one large table into smaller tables for easier management via foreign keys, and have come across a dilemma with trying to bring the data back together. I am new to working with this type of data merging so I am a bit lost.
There are 3 tables: one table with a list of product owners, one table with a list of systems they are responsible for, and the original table with all of the data (examples below):
Product Owners Table:
+----+---------------+
| id | product_owner |
+----+---------------+
| 1 | User1 |
+----+---------------+
PRIMARY KEY: id
System Table:
+----+-----------+---------------+
| id | system | product_owner |
+----+-----------+---------------+
| 6 | Server1 | NULL |
+----+-----------+---------------+
FOREIGN KEY: product_owner(id)
Original Table:
+---------+---------------+
| system | product_owner |
+---------+---------------+
| Server1 | User1 |
+---------+---------------+
I want to take the data from the original table and merge it with the new system table, however I don't want to go the route of multiple UPDATE statements to add what needs to be added. What's the best route to approach on this?
After a bunch more poking around and reading additional terminology, I was able to solve my problem by using a mix of join statements in conjunction with update, set and where statements:
update system_table a
join original_table.system b
on a.system = b.system
join product_owner c
on c.product_owner = b.product_owner
set a.product_owner = c.id
where c.product_owner = b.product_owner;

How to join tables over multiple databases/schema in django orm?

I am new to django, and I have started the process of moving an existing API to Django. The current API has multiple databases.
For example, table account is in database A, table notification is in database B etc.
It is a legacy database on which the app is running, and as such, I cannot change the database.
For explanations sake, the structure of both are -
Account Table
+-----+--------------+
| id | account_name |
+-----+--------------+
| 123 | John |
+-----+--------------+
| 124 | Henry |
+-----+--------------+
| 126 | Vlad |
+-----+--------------+
Notification Table
+----+------------+-----------------------------------+
| id | account_id | notification_body |
+----+------------+-----------------------------------+
| 1 | 123 | Someone sent you a message |
+----+------------+-----------------------------------+
| 2 | 123 | Someone commented on your photo |
+----+------------+-----------------------------------+
| 3 | 126 | Someone sent you a friend request |
+----+------------+-----------------------------------+
In the above tables, notification.account_id is a foreign key with relation to account.id, which is the primary key in the account table. I want to join both the tables, the equivalent of the following SQL query.
SELECT * FROM A.account AS acc
JOIN B.notification ON noti ON noti.account_id = acc.id
Django uses foreign key to join tables using the select_related() function. Now, the problem is, I have foreign keys which are not limited to a single database. But, django as of now does not support foreign keys over multiple databases. So, how do we join those tables that are in another database in Django if foreign key related joins is not feasible?
Remember that both the tables are in separate databases.

SQL Table Relationships - Trying to Avoid Cyclical Relationships

I'm trying to manage table relationships in my database, but every option I mock up ends up creating cyclical relationships, which seem to be a big no-no. Here is my setup:
I have the Group table:
+----+--------+
| id | name |
+----+--------+
| 1 | group1 |
| 2 | group2 |
+----+--------+
I also have a UserToGroup table giving users access (with varying permission level) to groups, with FKs pointing to my Group (many-to-one) and to my User table (many-to-one):
+----+----------+---------+---------------+
| id | group_id | user_id | permission_id |
+----+----------+---------+---------------+
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 |
| 3 | 2 | 1 | 2 |
+----+----------+---------+---------------+
Where I get stuck is that I want to add an owner flag on each instance of the Group table, where creating a FK constraint would block SQL from removing the UserToGroup instance of a group's owner (ultimately the owner of a group cannot lose permission access to its group.
Functionally, owner permission is completely independent from the permissions granted to users (as the owner would have access to certain management pages, while permission access grants various levels of read/write), and I want to always maintain a required UserToGroup instance in the database for the group's owner, stopping the database (through FK constraints) from the possibility of removing the owner from the UserToGroup table, where he manages all users for a given group.
Initially I had Group.owner_id pointing to the given User, but that only create a FK constraint for when the user is deleted.
I have also tried creating the following variations to my database, but they create cyclical relationships:
Where I created a FK in my Group table pointing to a UserToGroup instance labelling it as the owner's instance:
+----+--------+---------------------+
| id | name | owner_user_to_group |
+----+--------+---------------------+
| 1 | group1 | 2 |
| 2 | group2 | 3 |
+----+--------+---------------------+
Inversly, where I created an owner table that would manage the owner check between the Group and UserToGroup tables:
+----+----------+------------------+
| id | group_id | user_to_group_id |
+----+----------+------------------+
| 1 | 1 | 2 |
| 2 | 2 | 3 |
+----+----------+------------------+
But in both cases it creates a cyclical relationship with the Group table, where both cannot be created without each other.
Is there a way I can manage both owner and permissions separately for Group instances, but also create a logical relationship between both in my database so that I can use the FK constraints to avoid SQL from deleting the UserToGroup instance of the Group's owner?
I might have misunderstood but are you looking for this?
If not, try to explain more simply what you need exactly and I'll try again.

How To Design A Database for a "Check In" Social Service

I want to build a "check in" service like FourSquare or Untappd.
How do I design a suitable database schema for storing check-ins?
For example, suppose I'm developing "CheeseSquare" to help people keep track of the delicious cheeses they've tried.
The table for the items into which one can check in is fairly simple and would look like
+----+---------+---------+-------------+--------+
| ID | Name | Country | Style | Colour |
+----+---------+---------+-------------+--------+
| 1 | Brie | France | Soft | White |
| 2 | Cheddar | UK | Traditional | Yellow |
+----+---------+---------+-------------+--------+
I would also have a table for the users, say
+-----+------+---------------+----------------+
| ID | Name | Twitter Token | Facebook Token |
+-----+------+---------------+----------------+
| 345 | Anne | qwerty | poiuyt |
| 678 | Bob | asdfg | mnbvc |
+-----+------+---------------+----------------+
What's the best way of recording that a user has checked in to a particular cheese?
For example, I want to record how many French cheeses Anne has checked-in. Which cheeses Bob has checked into etc. If Cersei has eaten Camembert more than 5 times etc.
Am I best putting this information in the user's table? E.g.
+-----+------+------+--------+------+------+---------+---------+
| ID | Name | Blue | Yellow | Soft | Brie | Cheddar | Stilton |
+-----+------+------+--------+------+------+---------+---------+
| 345 | Anne | 1 | 0 | 2 | 1 | 0 | 5 |
| 678 | Bob | 3 | 1 | 1 | 1 | 1 | 2 |
+-----+------+------+--------+------+------+---------+---------+
That looks rather ungainly and hard to maintain. So should I have separate tables for recordings check in?
No, don't put it into the users table. That information is better stored in a join table which represents a many-to-many relationship between users and cheeses.
The join table (we'll call cheeses_users) must have at least two columns (user_ID, cheese_ID), but a third (a timestamp) would be useful too. If you default the timestamp column to CURRENT_TIMESTAMP, you need only insert the user_ID, cheese_ID into the table to log a checkin.
cheeses (ID) ⇒ (cheese_ID) cheeses_users (user_ID) ⇐ users (ID)
Created as:
CREATE TABLE cheeses_users
cheese_ID INT NOT NULL,
user_ID INT NOT NULL,
-- timestamp defaults to current time
checkin_time DATETIME DEFAULT CURRENT_TIMESTAMP,
-- (add any other column *specific to* this checkin (user+cheese+time))
--The primary key is the combination of all 3
-- It becomes impossible for the same user to log the same cheese
-- at the same second in time...
PRIMARY KEY (cheese_ID, user_ID, checkin_time),
-- FOREIGN KEYs to your other tables
FOREIGN KEY (cheese_ID) REFERENCES cheeses (ID),
FOREIGN KEY (user_ID) REFERENCES users (ID),
) ENGINE=InnoDB; -- InnoDB is necessary for the FK's to be honored and useful
To log a checkin for Bob & Cheddar, insert with:
INSERT INTO cheeses_users (cheese_ID, user_ID) VALUES (2, 678);
To query them, you join through this table. For example, to see the number of each cheese type for each user, you might use:
SELECT
u.Name AS username,
c.Name AS cheesename,
COUNT(*) AS num_checkins
FROM
users u
JOIN cheeses_users cu ON u.ID = cu.user_ID
JOIN cheeses c ON cu.cheese_ID = c.ID
GROUP BY
u.Name,
c.Name
To get the 5 most recent checkins for a given user, something like:
SELECT
c.Name AS cheesename,
cu.checkin_time
FROM
cheeses_users cu
JOIN cheeses c ON cu.cheese_ID = c.ID
WHERE
-- Limit to Anne's checkins...
cu.user_ID = 345
ORDER BY checkin_time DESC
LIMIT 5
Let's define more clearly, so you can tell me if I'm wrong:
Cheese instances exist and aren't divisible ("Cheddar/UK/Traditional/Yellow" is a valid checkinable cheese, but "Cheddar" isn't, nor is "Yellow" or "Cheddar/France/...)
Users check into a single cheese instance at a given time
Users can re-check into the same cheese instance at a later date.
If this is the case, then to store fully normalized data, and to be able to retrieve that data's history, you need a third relational table linking the two existing tables.
+-----+------------+---------------------+
| uid | cheese_id | timestamp |
+----+-------------+---------------------+
| 345 | 1 | 2014-05-04 19:04:38 |
| 345 | 2 | 2014-05-08 19:04:38 |
| 678 | 1 | 2014-05-09 19:04:38 |
+-----+------------+---------------------+
etc. You can add extra columns to correspond to the cheese data, but strictly speaking you don't need to.
By putting all this in a third table, you potentially improve both performance and flexibility. You can always reconstruct the additions to the users table you mooted, using aggregate queries.
If you really decide you don't need the timestamps, then you'd replace them with basically the equivalent of a COUNT(*) field:
+-----+------------+--------------+
| uid | cheese_id | num_checkins |
+----+-------------+--------------+
| 345 | 1 | 15 |
| 345 | 2 | 3 |
| 678 | 1 | 8 |
+-----+------------+--------------+
That would dramatically reduce the size of your joining table, although obviously there's less of a "paper trail", should you need to reconstruct your data (and possibly say to a user "oh, yeah, we forgot to record your checkin on such-a-date.")
The entities 'User' and 'Cheese' have a many-to-many relationship. A user can have multiple cheeses he checked into, and a cheese can have multiple people that checked into it.
The only right way to design this in a relational database is to store it into a separate table. There are many reasons why storing it into the user table for instance, is a very bad idea. Read up on normalizing databases for more info on this.
Your table should look something like this:
CheckIns(CheeseId, UserId, (etc...))
Other useful columns might include date or rating, or whatever you want to store about a particular relationship between a user and a cheese.

Set up Table to store variable number of fields per record?

How should I set up my database / table, if I do not know the number of fields I would populate per record?
For example, if I have a web form that allows a user to enter all the cars he owns, and I don't want to limit him to a certain number, how would I store this in the database end?
The above problem extends to similar situations such as storing a user's order (variable number of items per order) etc.
In Relational Database Management Systems (RDBMS) instead you create child records in a dependent table that relate child entities (cars) with parent entities (users). There is a concept known as database normalization, and the objective is that each table contains data for a single type of entity.
So you have a user table with the user information:
user_id | user_name | email | ...
1234 | User1 | user1#example.com | ...
2356 | User2 | user2#example.com | ...
Then another table for storing the information of each car of a user:
user_car_id | user_id | car_label | make | model | ...
1 | 1234 | MyCar | Ford | 2011 | ...
2 | 2356 | A Car | Chevrolet | 2010 | ...
3 | 1234 | MyOtherCar| BMW | 2000 | ...
So instead of storing the info of the cars in the user table, you have a table for storing car (user_car) information related to each user by way of the user_id column. This is an example of a one-to-many relationship, in which one user can have many related cars.
this is an entire topic: database normalization.
the short answer is you make more than one table.
in your example you would have person table, a car table, and a third that linked person to the car