For example, I have two tables, one of them I have the product description, in another I have data like "price", "date of registration", among others. I would like to know how I can delete the description that does not have id in the product table.
Something like:
delete from ProductDescription
where productId not in (
select productId from Product
);
Consider if we have two tables
Product table
Id-----Description
---------------------------------
1------It is abcd
2------It is Efg
3------It is Xyz
Product table
Id------Name------price---date
-----------------------
1-------Abcd------10-----1/1/2007
2-------Efg------20-----2/2/2007
We need to delete decription 'Its xyz' which doesnot have id in product table.
So use this query
delete from Description
where Id not in (
select Id from Product
);
Related
I have a Table A with customers and a Table P with products.
I also have a table C which represents the purchased product by customers.
The table fields are as follow:
A (Customers)
user_id
Name
Surname
P (Products)
product_id
price
product name
C (Purchased products)
id
product_id
user_id
quantity
date
C is the many-to-many link between A and P.
Assume that today a customer named "Bob Wright" has bought a product called "Beautiful_magazine". Assume that both customer and product are already in the database.
How do I make the entry in the C table?
I know that I should use the insert into select statement but I am facing hard time because I should retrieve the user id form A and the product id form P and then placing them into a new line in C along with the function NOW() and a numerical values representing the price.
I cannot do that because I am a newbie.
Any hint?
EDIT
I would like to make it manually, using the INSERT statement without relying on the software.
Assume:
user_id: 890
Name: Bob
Surname: Wright
product_id: 4897
price: 5.90
product_name: Beautiful_magazine
I need to create a new line in C table as follow:
id (autoincremental)
product_id: 4897
user_id: 890
quantity: 1
date: '2017-2-20'
Obviously, the product_id and user_id from P and A should be retrieve through a select statement using the where statement.
If you are passing the data in from an application, use a procedure (assuming that the id auto increments)
CREATE PROCEDURE `AddSale` (IN ProdID INT, IN UserID INT, IN Qty INT)
BEGIN
insert into TableC (Product_ID, User_ID, Quantity, Date)
values (ProdID, UserID, Qty, Now());
END
In your application, call the procedure and passin the parameters
I have two tables products(name varchar2(200)) table which contains 7 000 records and description (fulldescription text) table which contains 10 million records. I want to search each name from products table in each and every row of fulldescription field. How to do that?
I will never recommend somthing like this buit still if you want then here you go:-
SELECT fulldescription FROM description
WHERE fulldescription IN (SELECT name FROM products);
Could someone give me an idea how to create this database structure.
Here is an example:
Table "countries":
id, countryname
1, "US"
2, "DE"
3, "FR"
4, "IT"
Now I have another table "products" and in there I would like to store all countries where this product is available:
Table "products":
id,productname,countries
1,"product1",(1,2,4) // available in countries US, DE, IT.
2,"product2",(2,3,4) // available in countries DE, FR, IT.
My question:
How do I design the table structure in "products" to be able to store multiple countries?
My best idea is to put a comma-separated string in there (i.e. "1,2,4"), then split that string to look up each entry. But I doubt that this the best way to do this?
EDIT: Thank you all for your help, amazing! It was difficult to choose the right answer,
I finally chose Gregs because he pointed me to a JOIN explanation and gave an example how to use it.
You need an intersection table for that many-to-many relationship.
Table Country
CountryID, CountryName
Table CountryProduct
CountryID, ProductID
Table Product
ProductID, ProductName
You then Inner Join all 3 tables to get your list of Countries & Products.
Select * From Country
Inner Join CountryProduct On Country.CountryID = CountryProduct.CountryID
Inner Join Product On CountryProduct.ProductID = Product.ProductID
Without denormalizing, you'll need to add an extra table
Table Product countries
ProductID CountryID
1 1
1 2
1 4...
What you're talking about is normalisation. You have a many-to-many structure, so you should create another table to link the two. You should never (ok, pretty much never) use delimited strings to store a list of values in a relational database.
Here's an example of the setup:
product_countries table
productid | countryid
----------+-----------
1 | 1
1 | 2
1 | 4
2 | 2
2 | 3
2 | 4
You can use a foreign key to each other table, then make them both into a composite primary key.
You can then get a list of supported products for a country ID like this:
SELECT * FROM products, product_countries
WHERE products.id = product_countries.productid
AND product_countries.countryid = $cid
You could also make a third table countries_products with fields country_id and product_id.
the best approach for relational databases is the following :
One table for coutries, let's say
country_id, country_desc (country_id is primary)
one table for products, let's say
product_id, product_desc and as many columns as you want (product_id is primary)
if you had only one country for sure, it'd be enough to have a foreign key pointing to country_id in each product row. Having a foreign key asserts that there is an actual country behing a country_id referring to country table.
In your case you have several countries for a product, so add a separate association table
product_id, country_id
both keys primary and both foreign as well.
What is the best approach to display the summery of DETAILED.Fields in its master table?
E.g. I have a master table called 'BILL' with all the bill related data and a detailed table ('BILL_DETAIL') with the bill detailed related data, like NAME, PRICE, TAX, ... Now I want to list all BILLS, without the details, but with the sum of the PRICE and TAX stored in the detail table.
Here is a simplified schema of that tables:
TABLE BILL
----------
- ID
- NAME
- ADDRESS
- ...
TABLE BILL_DETAIL
-----------------
- ID
- BILLID
- PORDUCT_NAME
- PRICE
- TAX
- ...
The retrieved table row should look like this:
BILL.CUSTOMER_NAME, BILL.CUSTOMER_ADDRESS, sum(BILL_DETAIL.PRICE), sum(BILL.DETAIL.TAX), ...
Any sugguestions?
A simple GROUP BY and a LEFT JOIN should get you what you want:
Select
bill.customer_name,
bill.customer_address,
Sum(bill_detail.price),
Sum(bill_detail.tax)
From bill
Left Join bill_detail On ( bill_detail.billid = bill.id )
Group By bill.id, bill.customer_name, bill.customer_address
Make sure to group by all columns of table bill that you have in the column list of your select.
I have two tables: 'Category' and 'Product'. In Category I have catid and cat name. In product table I have item-id, item-name, catid.
What I need to do is show a result that will have item name and category name. But the category name will be multiple. So that one product can have multiple categories on it.
You might want to create three tables, because of a join table to would allow each line to have multiple lines corresponding in the other table:
Category : catid catname
Product : itemid itemname
CategoryProduct : catid itemid
So a product can have 0, 1 or more Categories.
Example content for the join table, for two products having the same two categories:
catid itemid in CategoryProduct
1 3
1 4
2 3
2 4
If your schema has a category ID in the product table, then no, one produce can't have multiple categories. If you have another M:N table to link products to categories, you should update your question.
In reply to your comment:
The category id of the product table
is like 2,3,4 so 1 product can have
multiple category
If you're allowed to change the table structure, by all means follow KLE's advice. That's the sane and maintainable approach.
If you can't change the table structure, you can query the categories with a hack like this:
select *
from product p
inner join category c
on ',' + p.catid + ','
like '%,' + cast(c.catid as varchar) + ',%'