SlideShare a Scribd company logo
1 of 25
Discuss the terms:
(i) Cartesian Product –Cross Join,
(ii) Equi-Join,
(iii) Referential Integrity,
(iv) Foreign Key.
To differentiate between Equi-Join, and Cross join in Mysql
To construct MySql query to join rows of different tables
based on constraints.
Aggregate Functions
•To find the highest cost of any shoe of type 'School'.
•To find the average cost from the shoes table.
•To count the different types of shoes that the factory produces
Starter Activity
•To find the highest cost of any shoe of type 'School'.
SELECT MAX(cost) FROM shoes WHERE type = 'School';
•To find the average cost from the shoes table.
SELECT AVG(cost) FROM shoes;
•To count the different types of shoes that the factory produces
SELECT COUNT(distinct type) FROM shoes;
Starter Activity
Foreign Key :
It is a column of a table which is the primary key of another table in the same
database. It is used to enforce referential integrity of the data.
In a join the data is retrieved from the Cartesian product of two tables by giving
a condition of equality of two corresponding columns - one from each table.
Generally, this column is the Primary Key of one table. In the other table this
column is the Foreign key. Such a join which is obtained by putting a condition
of equality on cross join is called an ‘Equi-join'.
In these tables there is a common column between Product and
Order_table tables (Code and P_Code respectively) . Code is the
Primary Key of Product table and in Order_table table it is not so (we
can place more than one orders for the same product). In the
order_table, P_Code is a Foreign Key. A foreign key in a table is used to
ensure referential integrity and to get Equi-Join of two tables.
Referential Integrity:
The property of a relational database which ensures that no entry in a foreign key
column of a table can be made unless it matches a primary key value in the
corresponding related table is called Referential Integrity.
Suppose while entering data in Order_table we enter a P_Code that does not exist in
the Product table. It means we have placed an order for an item that does not exist!
We should and can always avoid such human errors. Such errors are avoided by
explicitly making P_Code a foreign key of Order_table table which always references
the Product table to make sure that a non-existing product code is not entered in the
Order_table table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.
Look at the following two tables: Persons Table
PersonID LastName FirstName Age
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20
OrderID OrderNumber PersonID
1 77895 3
2 44678 3
3 22456 2
4 24562 1
SQL FOREIGN KEY on CREATE TABLE
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
SQL FOREIGN KEY on ALTER TABLE
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL:
ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
DROP a FOREIGN KEY Constraint
To drop a FOREIGN KEY constraint, use the following SQL:
ALTER TABLE Orders
DROP FOREIGN KEY PersonID;
Create table empnew
( Id int(3) not null primary key,
Name varchar(20),
Eno int(5),
Foreign key(eno) references emp(eno));
Alter table flight add foreign key(fare) references emp(eno);
Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons"
table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign
key column, because it has to be one of the values contained in the parent table.
1. Identify the candidate key of Table Customer.
2. How many rows and columns will be there in the Cartesian product of
the above given tables. Also mention the degree and cardinality of the
Cartesian product of the above given table.
3. Which column can be considered as Foreign Key column in Transaction
table.
4. Select * from Customer, Transaction.
Each row from the first table (Customer )will be paired
with each table in the second table(Transaction)
Equi- Join of tables :
Equi-Join: An equi join of two tables is obtained by putting an equality
condition on the Cartesian product of two tables. This equality condition is
put on the common column of the tables. This common column is,
generally, primary key of one table and foreign key of the other.
We can extract meaningful information from the Cartesian product by placing some
conditions in the statement. To find out the product details corresponding to each
Order details
SELECT * FROM order_table, product WHERE P_code = Code;
|
Two table names are specified in the FROM clause of this statement, therefore MySQL
creates a Cartesian product of the tables. From this Cartesian product MySQL selects
only those records for which P_Code (Product code specified in
the Order_table table) matches Code (Product code in the Product
table). These selected records are then displayed.
Example of Equi-join.
Select Order_no, Product.name as Product, Supplier.Name as Supplier
From order_table, Product, Supplier WHERE order_table.Sup_Code =
Supplier.Sup_Code and P_Code = Code;
Natural join
The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in
such a way that, columns with the same name of associated tables will appear once only.
Natural Join: Guidelines
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
Main Activity
Group 1 Group 2,3 Group 4
• Identify data type of
column I_code in
table Items
 Identify the Primary
Key of
Items Table.
 Which column can be
considered as
Foreign Key column
in Bills Table.
•Display the total quantity sold
for each item.
•Display the details of bill
records along with Name of
each corresponding item.
•Display the bill records for
each Italian item sold.
•Display total quantity of
each item sold but don't
display this data for the
items whose total quantity
sold is less than 3.
•Display the details of the bill
records for which the item is
'Dosa'.
•Display the total value of
items sold for each bill.
MIP
EmpId Name TelNo DOJ DptID Salary
100 Steve 05883452 1987-06-17 20 25000
101 Neena 05224897 1989-06-18 30 34000
102 Lex 05991234 1990-08-12 60 12000
103 Alexa 05881278 1996-03-12 60 55000
104 Bruce 05534879 1999-04-12 30 40000
Create two table
GASCO_Emp
(EmpId, Fname,LName,Email,TelNo, DOJ, DeptID, Salary)
GASCO_Dept(DeptID,DeptName,MgrID,LocID)
DeptID DeptName MgrID LocID
20 Administration 200 1700
30 Marketing 130 1800
40 Purchasing 114 2200
50 HumanResource 220 1600
60 IT 136 2300
Display the Empid, Name, Telno. Dept and Deptname of all the
employees working in GASCO
PLENARY
 Difference between Foreign Key and Primary Key
 Identify the candidate key of Customer table.
 Which column can be considered as Foreign Key column in
Transaction table.
 Identify Primary key column of Transaction table.
 What is the Cartesian product of two table? Is it same as an Equi-
join?
1. Display details of the students of Cricket team.
2. Display the name and phone number of the students of class 12 who are
play some game.
3. Display the Number of students with each coach.
4. Display the names and phone numbers of the students whose grade is 'A'
and whose coach is ‘Narendra’.
5. Identify the Foreign Keys (if any) of these tables. Justify your choices.
6. SELECT game, name, address FROM students, Sports WHERE
students.admno = sports.admno AND grade = 'A';
7. SELECT Game FROM students, Sports WHERE students.admno
= sports.admno AND Students.AdmNo = 1434;
8. There are two table T1 and T2 in a database. Cardinality and
degree of T1 are 3 and 8 respectively. Cardinality and degree of
T2 are 4 and 5 respectively. What will be the degree and Cardinality
of their Cartesian product?

More Related Content

Similar to MQSL JOINING OF TABLES.pptx

oracle Sql constraint
oracle  Sql constraint oracle  Sql constraint
oracle Sql constraint home
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive TechandMate
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of dataDimara Hakim
 
Constraints
ConstraintsConstraints
Constraintspunu_82
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraintsuman kumar
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsAshwin Dinoriya
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01sagaroceanic11
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set Mohd Tousif
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic ConceptsTony Wong
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)Muhammad T Q Nafis
 

Similar to MQSL JOINING OF TABLES.pptx (20)

Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
 
SQL report
SQL reportSQL report
SQL report
 
oracle Sql constraint
oracle  Sql constraint oracle  Sql constraint
oracle Sql constraint
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
 
1 introduction to my sql
1 introduction to my sql1 introduction to my sql
1 introduction to my sql
 
Constraints
ConstraintsConstraints
Constraints
 
MY SQL
MY SQLMY SQL
MY SQL
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
SQL & PLSQL
SQL & PLSQLSQL & PLSQL
SQL & PLSQL
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)
 

Recently uploaded

Introduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxIntroduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxAniqa Zai
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...nirzagarg
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?RemarkSemacio
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...gajnagarg
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...Elaine Werffeli
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...kumargunjan9515
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...kumargunjan9515
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...ThinkInnovation
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubaikojalkojal131
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNKTimothy Spann
 
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...HyderabadDolls
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...gajnagarg
 
Statistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbersStatistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numberssuginr1
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowgargpaaro
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...HyderabadDolls
 

Recently uploaded (20)

Introduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxIntroduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptx
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Call Girls in G.T.B. Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in G.T.B. Nagar  (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in G.T.B. Nagar  (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in G.T.B. Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
 
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get CytotecAbortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Statistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbersStatistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbers
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
 

MQSL JOINING OF TABLES.pptx

  • 1. Discuss the terms: (i) Cartesian Product –Cross Join, (ii) Equi-Join, (iii) Referential Integrity, (iv) Foreign Key. To differentiate between Equi-Join, and Cross join in Mysql To construct MySql query to join rows of different tables based on constraints.
  • 3. •To find the highest cost of any shoe of type 'School'. •To find the average cost from the shoes table. •To count the different types of shoes that the factory produces Starter Activity
  • 4. •To find the highest cost of any shoe of type 'School'. SELECT MAX(cost) FROM shoes WHERE type = 'School'; •To find the average cost from the shoes table. SELECT AVG(cost) FROM shoes; •To count the different types of shoes that the factory produces SELECT COUNT(distinct type) FROM shoes; Starter Activity
  • 5.
  • 6.
  • 7.
  • 8. Foreign Key : It is a column of a table which is the primary key of another table in the same database. It is used to enforce referential integrity of the data. In a join the data is retrieved from the Cartesian product of two tables by giving a condition of equality of two corresponding columns - one from each table. Generally, this column is the Primary Key of one table. In the other table this column is the Foreign key. Such a join which is obtained by putting a condition of equality on cross join is called an ‘Equi-join'. In these tables there is a common column between Product and Order_table tables (Code and P_Code respectively) . Code is the Primary Key of Product table and in Order_table table it is not so (we can place more than one orders for the same product). In the order_table, P_Code is a Foreign Key. A foreign key in a table is used to ensure referential integrity and to get Equi-Join of two tables.
  • 9. Referential Integrity: The property of a relational database which ensures that no entry in a foreign key column of a table can be made unless it matches a primary key value in the corresponding related table is called Referential Integrity. Suppose while entering data in Order_table we enter a P_Code that does not exist in the Product table. It means we have placed an order for an item that does not exist! We should and can always avoid such human errors. Such errors are avoided by explicitly making P_Code a foreign key of Order_table table which always references the Product table to make sure that a non-existing product code is not entered in the Order_table table.
  • 10. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table. Look at the following two tables: Persons Table PersonID LastName FirstName Age 1 Hansen Ola 30 2 Svendson Tove 23 3 Pettersen Kari 20 OrderID OrderNumber PersonID 1 77895 3 2 44678 3 3 22456 2 4 24562 1
  • 11. SQL FOREIGN KEY on CREATE TABLE The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created: CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) ); SQL FOREIGN KEY on ALTER TABLE To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL: ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID); DROP a FOREIGN KEY Constraint To drop a FOREIGN KEY constraint, use the following SQL: ALTER TABLE Orders DROP FOREIGN KEY PersonID;
  • 12. Create table empnew ( Id int(3) not null primary key, Name varchar(20), Eno int(5), Foreign key(eno) references emp(eno)); Alter table flight add foreign key(fare) references emp(eno); Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the "Persons" table. The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table. The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the parent table.
  • 13. 1. Identify the candidate key of Table Customer. 2. How many rows and columns will be there in the Cartesian product of the above given tables. Also mention the degree and cardinality of the Cartesian product of the above given table. 3. Which column can be considered as Foreign Key column in Transaction table. 4. Select * from Customer, Transaction. Each row from the first table (Customer )will be paired with each table in the second table(Transaction)
  • 14. Equi- Join of tables :
  • 15. Equi-Join: An equi join of two tables is obtained by putting an equality condition on the Cartesian product of two tables. This equality condition is put on the common column of the tables. This common column is, generally, primary key of one table and foreign key of the other. We can extract meaningful information from the Cartesian product by placing some conditions in the statement. To find out the product details corresponding to each Order details SELECT * FROM order_table, product WHERE P_code = Code; | Two table names are specified in the FROM clause of this statement, therefore MySQL creates a Cartesian product of the tables. From this Cartesian product MySQL selects only those records for which P_Code (Product code specified in the Order_table table) matches Code (Product code in the Product table). These selected records are then displayed.
  • 16. Example of Equi-join. Select Order_no, Product.name as Product, Supplier.Name as Supplier From order_table, Product, Supplier WHERE order_table.Sup_Code = Supplier.Sup_Code and P_Code = Code;
  • 17. Natural join The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that, columns with the same name of associated tables will appear once only.
  • 18. Natural Join: Guidelines - The associated tables have one or more pairs of identically named columns. - The columns must be the same data type. - Don’t use ON clause in a natural join.
  • 19.
  • 20.
  • 21. Main Activity Group 1 Group 2,3 Group 4 • Identify data type of column I_code in table Items  Identify the Primary Key of Items Table.  Which column can be considered as Foreign Key column in Bills Table. •Display the total quantity sold for each item. •Display the details of bill records along with Name of each corresponding item. •Display the bill records for each Italian item sold. •Display total quantity of each item sold but don't display this data for the items whose total quantity sold is less than 3. •Display the details of the bill records for which the item is 'Dosa'. •Display the total value of items sold for each bill.
  • 22. MIP EmpId Name TelNo DOJ DptID Salary 100 Steve 05883452 1987-06-17 20 25000 101 Neena 05224897 1989-06-18 30 34000 102 Lex 05991234 1990-08-12 60 12000 103 Alexa 05881278 1996-03-12 60 55000 104 Bruce 05534879 1999-04-12 30 40000 Create two table GASCO_Emp (EmpId, Fname,LName,Email,TelNo, DOJ, DeptID, Salary) GASCO_Dept(DeptID,DeptName,MgrID,LocID) DeptID DeptName MgrID LocID 20 Administration 200 1700 30 Marketing 130 1800 40 Purchasing 114 2200 50 HumanResource 220 1600 60 IT 136 2300 Display the Empid, Name, Telno. Dept and Deptname of all the employees working in GASCO
  • 23. PLENARY  Difference between Foreign Key and Primary Key  Identify the candidate key of Customer table.  Which column can be considered as Foreign Key column in Transaction table.  Identify Primary key column of Transaction table.  What is the Cartesian product of two table? Is it same as an Equi- join?
  • 24.
  • 25. 1. Display details of the students of Cricket team. 2. Display the name and phone number of the students of class 12 who are play some game. 3. Display the Number of students with each coach. 4. Display the names and phone numbers of the students whose grade is 'A' and whose coach is ‘Narendra’. 5. Identify the Foreign Keys (if any) of these tables. Justify your choices. 6. SELECT game, name, address FROM students, Sports WHERE students.admno = sports.admno AND grade = 'A'; 7. SELECT Game FROM students, Sports WHERE students.admno = sports.admno AND Students.AdmNo = 1434; 8. There are two table T1 and T2 in a database. Cardinality and degree of T1 are 3 and 8 respectively. Cardinality and degree of T2 are 4 and 5 respectively. What will be the degree and Cardinality of their Cartesian product?