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 a team_name column but I want to instead store the team_IDs of the same teams from column (team_ID) in a different table to use as a foreign key and then drop team_names. Here are some screenshots
The table I have (players):
The table I want to get the values of team_ID from (teams_in_league):
For each row where team_ID is null, I want that to be the ID of the team_name contained in the next column. What query can I use to achieve this? Team_ID is meant to be a foreign key btw
EDIT:
Extra info: there should be two tables, I'm going to add more columns to the second table. That's why I'm not joining the two tables.
You need to join the tables in an UPDATE statement:
UPDATE players p
INNER JOIN teams_in_league t
ON t.team_name = p.team_name
SET p.team_id = t.team_id
After you do the update, you can remove the column team_name from the table players because it is redundant:
ALTER TABLE players
DROP COLUMN team_name;
and make the column team_id of players a foreign key that references team_id of teams_in_league:
ALTER TABLE players
ADD FOREIGN KEY (team_id) REFERENCES teams_in_league(team_id);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Here is the statement I have now:
INSERT INTO jobs
SELECT *
FROM proposals
JOIN commissions
ON proposals.commission_id = commissions.job_id
WHERE proposals.proposal_id = '123'
It's working fine but is creating some primary key conflicts. So, what I would like to do when the new row is entered, is this:
new primary key + commissions table data + proposals table data
Any ideas?
then if you have auto increment pk , do this :
INSERT INTO jobs ( [list of columns except the auto increment column])
SELECT ( [list of columns accordingly])
FROM proposals
JOIN commissions
ON proposals.commission_id = commissions.job_id
WHERE proposals.proposal_id = '123'
Like p.Salmon said in the comments, I would add a new column to the jobs table
ALTER TABLE Jobs add autoid INT(10)AUTO_INCREMENT PRIMARY KEY;
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 2 years ago.
Improve this question
lets say I have two MySQL tables, table A and table B. Each table has a primary key called id and a name column. I also have a third table, table C, that contains relationships between table A and table B. Each row contains two foreign keys called a_id and b_id, which, as you might expect, correspond to ids in tables A and B.
What I want to do is select a random set of 10 table A rows, but only select rows that have a relationship with specific entries in table B. I don't know which entries I'm looking for ahead of time, and I will start with their names. The names will be provided via query parameters.
I understand I should probably start with this:
SELECT * FROM `A`
ORDER BY RAND()
LIMIT 10
But I don't know how to structure the where clause.
You need something like this:
SELECT *
FROM `A` a
INNER JOIN `C` c ON
a.ID = c.a_id AND
c.b_id in (1,2,3,4) -- your entries here
-- order and limit as you wish
ORDER BY RAND()
LIMIT 10
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 4 years ago.
Improve this question
Hey so i have been doing alot of reading and i am finding many conflicting ways to link my tables. hoping someone can help me put the foreign keys in the correct places.
tables -
customers: customer_id(primary), customer_name ;
employee: employee_id(primary), employee_name ;
appointments: appointment_id(primary), appointment_date ;
inventory: inventory_id(primary), item ;
so i have primary key for each table but i need to link 1 customer to the appointments table and multiple employees to the appointments tables.
the inventory table must link to the appointments tables.
Here is an example, customer sets an appointment then employee(s) fills appointment. during that appointment the customer buys an item(s).
it is sufficent enough to just show the item with the appointment. the item does not need to link to the customer.
thank you for help. i was thinking i needed to make a 5th table to fill with all the keys but im really unsure and the tables have alot more information in them than i posted above. would be alot of trial an error to then see if not work one of the ways i need.
You should add customer_id foreign key to the appointments table
You should create table appointment _employee:id (primary key),appointment_id(foreign key), employee_id(foreign key)
You should create table appointment_inventory:appointment_id, inventory_id
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 5 years ago.
Improve this question
I’m currently trying to design my table structures for a made up database. The database will have three separate tables and track the populations of cities for 10 years. After every year, population figures will be added for each city in the database. Here is how I’ve laid out my three tables so far:
Cities (city_id, city_name, city_abbrv)
Year (year_id, year)
Stats (year_id, city_id, population)
I’m worried about not having a unique identifier in my Stats table. With 10 cities, the year data will be the same for 10 entries. Once I enter multiple years of data, the city_id will be reused. In my research on this site I’ve read that having a unique ID for every table is not required but the book I’m using to learn database design (while brief) never mentions that this is okay. I would like to know the best way to design a database that receivers data entries for the same group of things on a daily/weekly/monthly/yearly schedule. Should I add in a unique_id column to my Stats table? Or would this be a wasted column? Thanks for the help!
First of all you need each of those tables to have the column id as primary key.
A primary key is used to uniquely identify a table row. A primary key cannot be NULL since NULL is not a value. So basically those columns will be unique yes.
Question: Why you need primary keys in your tables?
Answer:
If you are looking to select data from different tables you are opting for join so you need keys to use for that join.
If you want your
table to be clustered, you need some kind of a primary key.
Side Note: You may need to get familiar with indexing columns, see advantages of indexing.
Cities (id, city_name, city_abbrv) //with id as primary key
Year (id, year) //with id as primary key
Stats (id, year_id, city_id, population) //with id as primary key
//And year_id and city_id as foregin key connected by their respective ids
If you are still beginner with MYSQL see the W3school tutorial for SQL primary keys.
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 a table with two attributes
Staffnumber Name
Now I want to add a new staff member into this table, for example: Christian, and Staffnumber is the current max Staffnumber + 1. (Assume I have til now 20 staff members, then Christian would be number 21)
How could i do that, without knowing how many staff members I had? Is there any way to make SQL numbers the Christian as 21?
Making a call to the table to count the records should achieve sort of what you're looking for. Forgive my syntax but it should be something along these lines...
INSERT INTO TABLE_NAME (STAFF_NUMBER, NAME) VALUES ((SELECT COUNT(*) FROM TABLE_NAME) + 1, "Christian");
Keep in mind if you delete staff members and add more later this won't work because the count will be off.
If you make your STAFF_NUMBER column a primary key, you shouldn't have to specify this value at all when entering new values, it should auto-increment and assign a random ID to the staff member.
INSERT INTO TABLE_NAME (NAME) VALUES ("Christian");
If there is no auto-increment on your column, then you would just do a simple INSERT statement:
INSERT INTO table_name (Staffnumber, Name)
VALUES (21, 'Christian')
Also here is a link to set AUTO_INCREMENT for your column for MySQL:
http://www.w3schools.com/sql/sql_autoincrement.asp
Increment
So if you don't have auto increment. You will want to select the COUNT() from Staffnumber.
SELECT COUNT(*) + 1 From Table;
Inserting
Now you also want to insert a new record into the table.
INSERT INTO Customers VALUES(1,"Christian");
But the problem with this is that it will only Staffnumber of 1. So you need to combine both of these.
Final
INSERT INTO Table VALUES((SELECT COUNT(*)+1 FROM Table),"Christian");
INSERT INTO Table VALUES((SELECT COUNT(*) FROM Table) + 1,"Christian");