Delete Particular Id From Column using mysql - php

Two tables named are image and product the structure of the tables are as below
Table : Image
image_id image_path image
1 ./images/ aaa
2 ./images/ bbb
3 ./images/ ccc
Table : Product
product_id product_gallery
1 1,2
2 3,1
3 1,2,3
Now I want to remove the image from image table image_id = 3
While I'm deleting image id 3 from image table, at the same time product table product gallery column containing the id 3 will be deleted.
For example
Expected output :
product_id product_gallery
1 1,2
2 1
3 1,2
I don't have any idea for this but I can delete the images as of now, still I want to delete image id from the product table.

It's better to have a 3-rd table , some thing like :
Table : product-gallery
product_id image_id
1 1
1 2
2 1
3 1
3 2
In this way, working with database would be as easy as abc :)
But if you want to go your way, you have to deal with some sql or php processing functions.

i think it is better if you change the way your product table was constructed.. it would give you a hard time retrieving and manipulating it.. try using this format:
id product_id product_gallery
1 1 1
2 1 2
3 2 1
etc etc...

Related

Re assign foreign ID without conflict

I have a main table with a primary key (ÍD) and a linked one.
Existing Data
Main
ID | name
=========
1 | foo
2 | bar
3 | loo
4 | zoo
Linked
main_id
=======
1
1
2
2
There are connections to the first entries of main (1,2) in the linked table.
Now new data gets imported from the same structure:
Import Data
ID | name
=========
1 | new_foo
2 | new_bar
3 | new_loo
4 | new_zoo
and
main_id
=======
3
4
3
1
During the import process the IDs of the main table will get new ids (done by a script)
ID | name
=========
1 | foo
2 | bar
3 | loo
4 | zoo
5 | new_foo
6 | new_bar
7 | new_loo
8 | new_zoo
but the main_id will still have the ids from the imported data:
main_id
=======
1
1
2
2
3 => should be 7
4 => should be 8
3 => should be 7
1 => should be 5 => * comment below
*I cannot simple update linked like:
UPDATE linked SET main_id = 5 WHERE main_id = 1
as it would update the first two rows as well.
So how can I map these field with the new primary ID of main?
I could add a high number to the main_id before import like
UPDATE linked SET main_id = main_id + 10000000
do the import
apply the real ID
UPDATE linked SET main_id = %realID WHERE main_id = %importedID
return my temporary ID back to the original.
UPDATE linked SET main_id = main_id - 10000000 WHERE main_id > 10000000
The problem is pretty obvious: This doesn't work well (or at all) if the IDs are higher than 10.000.000 or the temporary id is higher than BIGINT (9223372036854775807).
It could work with a clone of the table but this may cause a problem in memory consumption as the linked table can get pretty big.
I'm sure there's a "best practice" way of doing this.
The steps are:
Pick last ID in Main table: select #lastId := max(ID) from Main;
Just shift all IDs, that are to be written in Linked table, by #lastId.
Something like this: insert into Linked select ID + #lastId from Imported_linked_data
No extra steps needed.
You can add the new entries in the Main table and let the AUTO_INCREMENT column generate the Id of the new row (as it should). For each row you cache the generated Id in an associative array, which maps the old Id to the new generated Id.
After that you insert the entries for the linked table. Before adding the new row you replace the old foreign key Id to the new generated id based on your created associative array.

better way to handle multiple tables relationship

Suppose i have some tables like...
countries
id name
1 Cyprus
2 India
states
id name country_id
1 a 1
2 b 2
3 c 2
cities
id name state_id
1 x 1
2 y 2
3 z 3
4 p 2
pages
id name slug status
1 ab a-b 1
2 pq p-q 0
3 abc a-b-c 1
mode_of_training
id name
1 Virtual
2 Classroom
items
id name description
1 a something
2 b something
prices
id price currency_code
1 200 USD
2 300 AUD
3 4000 INR
offers
id name discount
1 xyz-1 20%
2 abc-2 30%
3 pqr-3 10%
Creating table structure in that way is correct?? so that i can reuse them
using there ids.
For example
items_relation_table
id country_id state_id city_id page_id item_id price_id offer_id status
1 0 0 0 1 1 1 0 1
2 0 0 0 1 1 1 1 0
3 1 0 1 2 2 1 2 1
4 1 0 1 3 3 2 1 1
Now i don't need to use field values of tables
If i want to change price. i'll change price in one place only
I'm saving number of bytes.
database table size is less
But my problem is
To fetch data i need to use Joins
Or Creating View is better idea
Or There is any Better way to create table structure
Your entity-tables look fine at the first glance. But I don't understand your relation table. Looks like you try to relate everything with everything, which in some cases probably doesn't make sense (do offers really belong to countries?) in in some cases it seems redundant: When a page is linked to (many) cities, there is no need to link it to the countries too, because the countries are determined by the cities.
You should add relation-tables only between those entities that really need to be in a direct many-to-many relation. And for each of those relations you need a separate table.
e.g. for a relation between pages and cities:
cities(id, name, state_id) <--> cities_pages(city_id, page_id) <--> pages(id, name, slug, status)
And yes, you have to join tables to fetch data. Thats one of the basic ideas behind relational databases. Don't be afraid of joins, if your tables are properly indexed thats not an expensive operation at all (assuming that performance is your concern). And of course you could add some views if that makes sense for your application, but that will include the same JOINS you would just abstract them behind CREATE VIEW statements.

How to synchronize pivot table in plain PHP

Assume we have these tables
Users table
-----------
id name
-----------
1 xxx
2 yyy
3 ccc
4 bbb
5 aaa
Location table
-------------
id name
-------------
6 Spain
7 Russia
8 Germany
9 USA
Pivot table
------------------------
id user_id location_id
------------------------
1 1 6
2 2 8
3 1 8
4 1 9
5 3 8
What I want to achieve is to sync the data in the pivot table.
So exactly I have post request with array of user ids = [1,5,4] and location_id = 8. So I would get the following result
[updated] Pivot table
-------------------------
id user_id location_id
-------------------------
1 1 6 <-- This one stays
3 1 8
4 1 9
6 5 8 <-- Added
7 4 8 <-- Added
...we deleted the row with location_id=8 and user_id=2 and user=3 because those are not in the users array
How can I do add the new ones, delete the ones that are not in the request, and leave the one that already exists with some functionality, just like Laravel has done it in sync function.
I know that the easyest way is to get all the users that has that specific location_id, delete all, and than insert once again. Is there some workaround or should I do the newbie way :D
Thank you
Not sure if this is the best way to go (actually deleting everything and then inserting the values seems easier and cleaner), but here's one way to do it, if for some reason you want to keep the pivot table ID:
Define a unique index for the table, considering both pivot fields (user_id,location_id).
Insert all data using ON DUPLICATE KEY UPDATE user_id = user_id and location_id = location_id, so that if there's duplicate values, mysql will keep them without modifications and will not throw error.
Implode the Array IDs from the Request and execute a delete from the table, where data is not in the specified values.

Mysql find a table in from another table record and find a specific record in this table

I'm a new in Mysql and I have a complicated problem:
I have a table with "Shops" name in this table there is a ShopID column. The records look like this:
Shop_001
Shop_002...
Every "shopID" refer to a new table with this name, for example there is a table with Shop_0001 name. In this table there is "partnumber" column which mean the parts which are available in this shop.
I send a specific part number to sql server and I want to check all shops in the "Shops" table and return a rows in the "Shop_xxxx" tables which has this specific partnumber.
Unfortunately I have no idea how do I get start on this. Can anybody help me give some instruction or anything on this?
you're looking for a many to many relationship. so you just need 3 tables
1 table is the list of shops
1 table is the list of products
and 1 table is the list of which shops have which products. like this
table1
id|shops
------
1 shop1
2 shop2
3 shop3
table2
id|products
------
1 prod1
2 prod2
3 prod3
4 prod4
5 prod5
table3
id|shop_id|prod_id
-------------------
1 2 3
2 2 1
3 2 2
4 1 3
5 1 4
6 1 5
7 3 2
So for every time a product is added to a shop, an entry is added in table3. This will allow you to query by shops or by products, and you will only ever need 3 tables.
google querying many to many relationships for how to get the list of products for shop1 or the list of shops that have product4 etc.

How to assign comment id?

I am making comment system and now I want to insert comments to database and I am confusing that on what basis to assign specific comment_id.
Suppose we have multiple dives of images with comment system.if someone comment on image then how can we assign a specific comment id on that specific image.and if other user comment on same image then how can he found that image comment_id so the comment save in right direction.
we have many images and comment system for that image.
My english is bad may be you understand what i want to say.
You need to create a number of different tables in the database
table: comments
|comment_id | userID | name | comment | (for example)
1 50 James test
2 50 James test
3 50 James test
table: images
|image_id | link |
1 example.com/images/image1.png
2 example.com/images/image2.png
3 example.com/images/image3.png
table: comments_on_images (to make the table's purpose clear)
|id | comment_id | image_id
1 1 2
2 2 2
3 3 1
Using this method you can assosciate any number of comments to any images. You have to query the database using JOINS to get all the information you need.

Categories