« first day (29 days earlier)      last day (928 days later) » 

8:21 AM
@JohnRennie Hi
are u free
 
Hi :-)
Yes I'm free
 
so i was trying to change my topping table like u said
it looks like this now-
create table TOPPING (topping_name varchar(30) not null,topping_id not null, price int not null, primary key (topping_id));
problem is topping name and topping id both are primary keys
is it ok if i just use topping id as primary key
 
The idea of a primary key is that the very fastest way to find data in the table is using the primary key.
When you are joining tables the join is fastest if you use a primary key to be the join.
 
oh
that makes sense
 
Any field that is unique, i.e. no duplicates, can be the primary key, but it is common to create an int field to be the primary key because this is simple and fast.
That's why I suggested you add a new int field topping_id to act as the primary key.
 
8:25 AM
ahh ok
can a primary key consist of multiple attributes by any chance
or is it single attributes always
 
Yes, a primary key can be a combination of columns as long as that combination is unique. e.g. you could write:
 
ahh i checked online and it says it can have multiple attributes
 
create table TOPPING (topping_name varchar(30) not null,topping_id not null, price int not null, primary key (topping_id, topping_name));
(although there is nothing to be gained from doing this in this particular case)
 
ok
 
 
1 hour later…
9:37 AM
@JohnRennie I had a doubt
 
@Aladdin Hi :-)
 
so when we use join
select sum(t.price)
from topping t join pizza_topping pt on t.topping_id = pt.topping_id
so does the table look like this
:
let me edit the picture
 
OK ...
 
all these attributes will be merged in one table
satisfying the condition t.topping_id = pt.topping_id
 
Yes. Try select * from topping t join pizza_topping pt on t.topping_id = pt.topping_id and you'll see it is just the two tables combined into a single table with all the fields.
 
9:44 AM
ok
 
And if you then join the pizza table you'll find you have a single table with all the fields from all three tables.
 
ahh
got it
now i think i finally understand how join works
 
Cool :-)
 
10:19 AM
@JohnRennie is there a way to get pizzas_id and their cost and topping cost in another table
 
Can you clarify what you mean?
 
i think joining all tables is first thing
like each pizza has a cost
then topping cost
 
Do you mean you want a new table that contains the cost of the toppings for each pizza type?
@Aladdin Yes, this is done using a grouping query.
 
pizza cost +topping cost
 
Suppose we have the joins:
pizza p join pizza_topping pt on p.pizza_id = pt.pizza_id join topping t on pt.topping_id = t.topping_id
and now I do the query:
select p.pname, p.price, t.price
from pizza p join pizza_topping pt on p.pizza_id = pt.pizza_id
join topping t on pt.topping_id = t.topping_id
order by p.name, p.price
This will create a recordset with three columns. There will be a record for every combination of pizza and topping.
Is it clear what this means, or shall we take a specific example?
 
10:30 AM
sorry i went away
let me go through this quickly
 
OK ...
 
yes its clear
 
OK. But we don't want a separate record for every combination of pizza and topping, we want a single record for each pizza, with the sum of the prices for all the toppings used in that pizza. Yes?
 
yes
 
The way we do this is:
select p.pname, p.price, sum(t.price)
from pizza p join pizza_topping pt on p.pizza_id = pt.pizza_id
join topping t on pt.topping_id = t.topping_id
group by p.name, p.price
order by p.name, p.price
Note that there are two new things.
1. in the select part we have sum(t.price)
The sum() function sums the values in a set of records.
2. we have group by p.name, p.price
 
10:35 AM
ok
 
What this does is take every combination of p.name and p.price, and for that combination it sums up all the topping prices for that combination.
Do you want to go through an example to see how this works?
 
let me execute it
pizza_management-# order by p.name, p.price;
name | price | sum
---------------------+-------+-----
Chicken Supreme | 570 | 70
Malai Chicken Tikka | 560 | 70
Veg Kebab Surprise | 330 | 70
Veg Kebab Surprise | 529 | 140
Veggie Supreme | 330 | 70
Veggie Supreme | 529 | 70
(6 rows)
@JohnRennie It shows me this
can we go through the example for understanding how this works
The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
i got this definition from online but i dont see how this works
 
I'm just answering another question but I should only be five minutes.
 
no worries.i will wait till u are free
 
@Aladdin Hi
Let's suppose the pizza table contains:
 
10:49 AM
hello
 
ID Name                 Price
1  Chicken Supreme      570
2  Malai Chicken Tikka  560
3  Veg Kebab Surprise   330
And the toppings table contains:
101  Grilled chicken  100
102  Chicken tikka    150
103  Chick peas       50
104  Paneer           50
Now suppose the Chicken supreme pizza has just one topping Grilled chicken. When we do the query (without the sum) we get:
Chicken Supreme      570   100
 
yes
 
Now suppose the Malai chick contains two toppings, Grilled chicken and Chicken tikka. When we do the join we get two records:
Malai Chicken Tikka  560  100
Malai Chicken Tikka  560  150
The first two fields are the same in both rows, because they are for the same pizza, but the topping price fields are different because they are the prices for the two different toppings used.
OK so far?
 
ok
 
And just for completeness assume the veg pizza has two toppings chick peas and paneer. Then for this pizza we get:
Veg Kebab Surprise   330   50
Veg Kebab Surprise   330   50
For the two toppings. So our complete result set is:
Pizza name          price  topping price
Chicken Supreme      570   100
Malai Chicken Tikka  560   100
Malai Chicken Tikka  560   150
Veg Kebab Surprise   330    50
Veg Kebab Surprise   330    50
@Aladdin OK so far?
 
10:59 AM
ok
 
Now, in the grouping query we have group by p.name, p.price
And this splits the result set into groups that have the same values for name and price. So we get this:
Pizza name          price  topping price
Chicken Supreme      570   100

Malai Chicken Tikka  560   100
Malai Chicken Tikka  560   150

Veg Kebab Surprise   330    50
Veg Kebab Surprise   330    50
i.e. we get three groups. Yes?
 
ahh
 
Each group has the same values for name and price.
 
yes
 
And you can probably see what happens now. The sum(t.price) function sums up the t.price values in each group.
 
11:03 AM
yea
that explains ir
 
Pizza name          price  topping price
Chicken Supreme      570   100
                           ---
Chicken Supreme      570   100

Malai Chicken Tikka  560   100
Malai Chicken Tikka  560   150
                           ---
Malai Chicken Tikka  560   250

Veg Kebab Surprise   330    50
Veg Kebab Surprise   330    50
                           ---
Veg Kebab Surprise   330   100
And the final result is just the records with the sum i.e.
Pizza name          price  sum(topping price)
Chicken Supreme      570   100
Malai Chicken Tikka  560   250
Veg Kebab Surprise   330   100
 
got it
i had one doubt
in your query
 
You can do other things with grouping queries.
For example suppose we did:
select p.pname, p.price, count(t.price)
from pizza p join pizza_topping pt on p.pizza_id = pt.pizza_id
join topping t on pt.topping_id = t.topping_id
group by p.name, p.price
Can you guess what this does?
 
find the number of toppings for pizz
pizza
 
Exactly, yes :-)
Or you could do:
select p.pname, p.price, sum(t.price), count(t.price)
from pizza p join pizza_topping pt on p.pizza_id = pt.pizza_id
join topping t on pt.topping_id = t.topping_id
group by p.name, p.price
And this returns both the total cost and the number of the toppings
 
11:09 AM
nice
i will go through some more examples on my own
thanks a lot!
 
OK :-)
 

« first day (29 days earlier)      last day (928 days later) »