Joining tables that don't have a common id [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two tables in a MySQL database. One is "accounts" and one is "posts". I'm at the point where I want to allow users to only edit their posts, which I think means I need to create a table join. The problem is there isn't a common id for each table.
Accounts:
id, first_name, last_name, username, password, email, activation_code, rememberme, role
Posts:
id, title, body, author, tags, status, image, created_at
I'm still searching on how to do this. I know I need to create a common id. That's where I'm getting confused since there will need to be a common link when a post is created. Can someone point me in the right direction for making this happen please?

The "author" column in Posts table should ideally be a foreign key of the "id" column in Accounts table. This would allow you to join the tables based on this column. A foreign key can be added as follows for your case -
ALTER TABLE Posts ADD FOREIGN KEY (author) REFERENCES Accounts(id);
You can have a look at a tutorial about foreign keys here - https://www.w3schools.com/sql/sql_foreignkey.asp

Related

Mysql, retrieve students who haven't received a request from me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently logged in as the first student. I want to retrieve a list of the students I did not send a request to yet. I also want to retrieve a list of students I sent a request to.
Both sender_id and Rec_id are foreign keys referencing to Students.id.
DB schema
Hi Souleiman and welcome to stackoverflow! Your question is not quite clear, you say that sender_id and Rec_id in requestts are both referencing to students, but you can't see to which field exactly. Your requestts table contains different entries in these fields.
Next time you should post the output of SHOW CREATE TABLE students and SHOW CREATE TABLE requestts and a little INSERT statement for the data. Furthermore it would be nice if you would explain the relations and show what you have already tried (see Barmars comment).
I suppose that the relevant foreign key in requestts is Rec_id, so you could try this query:
SELECT s.email FROM students s WHERE s.id NOT IN
(SELECT Rec_id FROM requestts WHERE Rec_id = s.id);
Hope this helps. Have fun learning SQL!

Best practice for setting a selected row in a database [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have one table called addresses in my database which stores all the shipping addresses which belong to the user. A user can select their primary address.
My question is, which is better practice:
To create a separate table for the primary address selection (e.g. a table called primaryaddress with a user_id and address_id foreign keys. If a row exists in this table, it links to the primary address of the user)?
or
To create a value in the addressses table which keeps track of the primary address (a boolean called primary which can either be true or false)?
Personally I would add an AddressType field on the Address table and make a unique constraint against the UserId and the address type so that the user can have many addresses but only one of each type i.e Primary, Secondary, Other etc.
This lets you have the option to have other address types in the future and doesnt lock you down to a boolean flag. Then if you decide a user can have many primary addresses you can remove the constraint and implement logic to handle this.

MySQL user data storage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two tables that should somehow be associated. Let's call them table_a and
table_b. A row in table_a can be associated with multiple rows in table_b, and the same goes the other way around. How could I achieve this? Should I use a pivot table?
Both tables have an auto-incrementing id-column.
What you're looking for is called a many-to-many relationship (a given user has zero or more games, a given game has zero or more users). This is typically handled with a "mapping table", e.g. USER_GAMES which has a user_id and a game_id, uniqueness is on the combination of these. http://www.joinfu.com/2005/12/managing-many-to-many-relationships-in-mysql-part-1/ has some good details.
As it is a many to many relationship, an intersection table with the user ID & game ID would be the best. Otherwise you would have to parse the list of game ID's stored in the user table and that would cause performance issues.

How to tie (connect) to values in mysql? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am newbie in programming and I need some help.
I have table (clubs) in DB with teams names and their id.
Next I want to create match in table "matches" in new table named "matches".
How can I tie team's id from "clubs" with teams (home&away)in table matches. I need that id in next actions.
I thought about foreing key, but I cant us it.
Thanks in advance for ur help
Regardless of if the two tables are linked via. a foreign key relationship (although it is good practice to do so) when selecting you can still use a JOIN to connect the two tables. Since you have both a home and away team you need to join the clubs table twice, once for each team.
SELECT m.*, ch.name AS homeClub
FROM matches m
INNER JOIN clubs ch
ON ch.id = matches.home_id
This query will get the match and the home club name, I leave it to you to figure out how to get the away club's name.
Read http://dev.mysql.com/doc/refman/5.0/en/join.html for more information on using joins.

SQL server 2008 naming standards for tables and columns [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have the following question regarding the SQL server 2008 naming standards.
Should I name a table as singular “Customer” OR “Customers”.
If inside the above table I have an ID column; should I name it “CustomerID”, “Customer_ID”, OR “ID” .
Thanks
I use this notation:
L_TABLE for lookup tables, F_TABLE for fact table.
Plural name if a single row describes, for example, a CUSTOMER. The table contains in fact multiple rows (CUSTOMERS) so it will be meaningful.
Descriptor prefix is useful for join and for readability: if you're not the only one to access to that table it's easier to understand if user query will retrieve CUSTOMER_ID, generic ID, ROW_ID and so on. Think about multiple IDs in the same table referring to different fields (even in other tables).
Personal point: for an ID in a look up table I will always use L_CUSTOMER_ID, it's quite long but totally clear.
Use plural, "Customers", the table will hold many rows.
Use just "ID", its easier to always use the same and easier when writing queries. ("select x where Customers.ID = 5")
On a side note, for other tables referencing the Customers table I would name the referencing column to something that describes the relation. Not just "CustomerID" but "PlacedBy_CustomerID" in the orders table, or "PaidBy_CustomerID" in the invoice table. That way the meaning of the relation becomes a bit more present.