I want to joint multiple ID'S in my SQL table - sql

I have a table when my database Users and which one has an ID, and i want to say like ID 1 and 2 belong to the same Project, i cannot do that, what i can do i say that ID 1 bellong to this project but i really need to add multiple ID'S to the project, if anyone could help me please.

Related

Table structure for reporting SQL Performance

I am not sure if this is the best structure for what I am trying to do.
I have 2 tables.
First table called Team which contains 3 columns, Team ID, Team Name and Kit Colour. This table contains data about the team only.
Second table called player name contains 3 columns, Player ID, Player name and Team ID. More columns will be added soon. Player table is about the player only. Team ID is the foreign key which links both tables together. A team can only have 15 players max so there can be 15 player entries for each team.
There can be thousands of teams with a maximum of 15 players each.
I have a horizontal Report that I need to fill that looks like this:
Team Name | Player 1 Name | Player 2 Name | Player 3 Name....| Player Name 15
My question is, I this best table structure set up for the report? How would I get data of a certain team with performance in mind? For example I want information for Team A, and all its players. There can be 15 players. If I can display the information in one select statement from left to right, I can easily fill in the report but this can t be done without using multiple selects which can be negative towards performance.
The other table structure that was suggested was having join both tables together instead of 2 table. A column for each player and their properties would have a column as well but this does not look correct as table would be massive and more player properties can be added.
I am using SQL 2008
You need to have two separate tables one for team and one for players, since team attributes can be common for multiple players it's better to have them separated rather having all together and duplicating, in your case TeamID serves as the link between both team and player table. so two table with a one to many relation ship is your ideal choice in my opinion.
To retrieve the information of a specific team you can use a simple join and a select statement
SELECT *
FROM TEAM T
INNER JOIN Player P
on T.TeamID=P.TeamID
WHERE T.TeamID=''

Not Equal / Exist in Access Database

So I am unable to wrap my brain around this.
I have 2 Tables in my database with matching fields in it
Table1: ComplaintsLogged ( This table contains all the data and all records)
Table2: ComplaintsClosed (This table only contains the complaints that have been closed)
Now I am trying to run a query which can be represented something like this.
ComplaintsLogged.status - ComplaintsClosed.taskStatus
Subtract the closed status in ComplaintsClosed from ComplaintsLogged and show me the rest of the records in complaintsLogged
both have a common field of complaint No. in them.
This is one way of doing what you asked
select ComplaintsLogged.id from ComplaintsLogged
where not exists (select 1 from ComplaintsClosed
where ComplaintsClosed.id = ComplaintsLogged.id)

Locking records in back-end using relationship in frontend

I am trying to figure out how to protect records in access back-end with a relationship in access front-end.
I have the following table in back-end:
tblSit(linked from back-end)
tblSitID(autonumber) ProductID LocationID
1 1 2
2 5 1
3 8 3
temp_tblToMove(table in front-end)
temp_tblToMoveID(autonumber) tblSitID
1 1
2 3
what I want to do is move the product from one location to another. The idea is:
I select the record in tblSit that stores locations for each product. Then I insert that ID in temp_tblToMove local table. Then I have a form that in the end will delete the selected records from tblSit and insert them again in tblSit changing LocationID.
I want record locking so that if two users try to move the same product then they get error when trying to delete the record from tblSit.
if I would have temp_tblToMove in back-end then the relationship would prevent record deleting. But I'd like to keep temp_tblToMove in front-end, but here the ralationship doesn't include "Enforce referential integrity".
Thanks for the help.
PS: sorry if I didn't do a good job at explaining what I want.
Any reason you can't just update the existing records?
UPDATE tblSit SET Location = NewLocationID WHERE ID = WhicheverID;

How to merge two identical database data to one?

Two customers are going to merge. They are both using my application, with their own database. About a few weeks they are merging (they become one organisation). So they want to have all the data in 1 database.
So the two database structures are identical. The problem is with the data. For example, I have Table Locations and persons (these are just two tables of 50):
Database 1:
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
1 Location A
2 Location B
Persons:
Id LocationId Name etc...
1 1 Mark
2 2 Ashley
3 1 Ben
We see that person is related to location (column locationId). Note that I have more tables that is referring to the location table and persons table.
The databases contains their own locations and persons, but the Id's can be the same. In case, when I want to import everything to DB2 then the locations of DB1 should be inserted to DB2 with the ids 3 and 4. The the persons from DB1 should have new Id 4,5,6 and the locations in the person table also has to be changed to the ids 4,5,6.
My solution for this problem is to write a query which handle everything, but I don't know where to begin.
What is the best way (in a query) to renumber the Id fields also having a cascade to the childs? The databases does not containing referential integrity and foreign keys (foreign keys are NOT defined in the database). Creating FKeys and Cascading is not an option.
I'm using sql server 2005.
You say that both customers are using your application, so I assume that it's some kind of "shrink-wrap" software that is used by more customers than just these two, correct?
If yes, adding special columns to the tables or anything like this probably will cause pain in the future, because you either would have to maintain a special version for these two customers that can deal with the additional columns. Or you would have to introduce these columns to your main codebase, which means that all your other customers would get them as well.
I can think of an easier way to do this without changing any of your tables or adding any columns.
In order for this to work, you need to find out the largest ID that exists in both databases together (no matter in which table or in which database it is).
This may require some copy & paste to get a lot of queries that look like this:
select max(id) as maxlocationid from locations
select max(id) as maxpersonid from persons
-- and so on... (one query for each table)
When you find the largest ID after running the query in both databases, take a number that's larger than that ID, and add it to all IDs in all tables in the second database.
It's very important that the number needs to be larger than the largest ID that already exists in both databases!
It's a bit difficult to explain, so here's an example:
Let's say that the largest ID in any table in both databases is 8000.
Then you run some SQL that adds 10000 to every ID in every table in the second database:
update Locations set Id = Id + 10000
update Persons set Id = Id + 10000, LocationId = LocationId + 10000
-- and so on, for each table
The queries are relatively simple, but this is the most work because you have to build a query like this manually for each table in the database, with the correct names of all the ID columns.
After running the query on the second database, the example data from your question will look like this:
Database 1: (exactly like before)
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
10001 Location A
10002 Location B
Persons:
Id LocationId Name etc...
10001 10001 Mark
10002 10002 Ashley
10003 10001 Ben
And that's it! Now you can import the data from one database into the other, without getting any primary key violations at all.
If this were my problem, I would probably add some columns to the tables in the database I was going to keep. These would be used to store the pk values from the other db. Then I would insert records from the other tables. For the ones with foreign keys, I would use a known value. Then I would update as required and drop the columns I added.

How to duplicate data in relation table?

Currently, I have 2 tables:
Table A (master) with 3 columns: tblA (tblA_ID, name, desc) (tblA_ID is identity key)
Table B (detail) with 4 columns: tblB (tblB_ID, tblA_ID, name, desc)
Table A has 100 records, every record has 10 records at table B in relationship.
What I want is to add 1.000.000 records to Table 2 for each of these 100 tblA_IDs. Or rather, add 999.990 records for each tblA_IDs, since Table B already has 10 records for each of those IDs.
My solution is using the cursors, get one-by-one in table A, with each tblA_ID, get it, find its data at table B and insert into both tables then.
So, is it possible? Do you have any suggestion to solve this case ?
From what i'm trying to understand, It seems like you are trying to insert dummy data for testing. There are some software available to accomplish what you are after. One which I found to be extremely good is:
RedGate SQL Data Generator: http://www.red-gate.com/products/sql-development/sql-data-generator/
This has a 14 day trial to test it out.
There are some free options available but not as good, since redgate's software handles table relationships on it's own.
One of the free generators is: http://www.generatedata.com
You can download a csv file and add it to your tables by right clicking on the database, hover on Tools, and click on Import data