Presentation 1
Presentation 1
4. What is RDBMS?
• RDBMS stands for Relational Database Management System.
• RDBMS is a program used to maintain a relational database.
• RDBMS is the basis for all modern database systems such as MySQL, Microsoft SQL Server,
Oracle, and Microsoft Access.
• RDBMS uses SQL queries to access the data in the database.
5. What is a Relational Database?
A relational database defines database relationships in the form of tables. The tables are related
to each other - based on data common to each.
Example:- Customers Table
2 Ana Trujillo Emparedados y Ana Trujillo Avda. de la Constitución México D.F. 05021 Mexico
helados 2222
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
10278 5 8 1996-08-12 2
10280 5 2 1996-08-14 1
10308 2 7 1996-09-18 3
10355 4 6 1996-11-15 1
10365 3 3 1996-11-27 2
10383 4 8 1996-12-16 3
10384 5 3 1996-12-16 3
The relationship between the "Customers" table and the "Orders" table is the CustomerID
column
Shippers Table
The relationship between the "Orders" table and the "Shippers" table is the ShipperID
column
6. What is SQL?
•SQL is the standard language for dealing with Relational Databases.
•SQL is used to insert, search, update, and delete database records.
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
•SQL keywords are NOT case sensitive: select is the same as SELECT
WHERE Syntax:-
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Text Fields vs. Numeric Fields:-
• SQL requires single quotes around text values (most database systems will also allow
double quotes).
SELECT * FROM Customers
WHERE Country = 'Mexico';
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
•The AND operator displays a record if all the conditions separated by AND are TRUE.
•The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
• AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
• AND Example
The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city is "Berlin":
Example
SELECT * FROM Customers
WHERE Country = 'Germany' AND City = 'Berlin';
• OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
OR Example
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR
"Stuttgart":
Example
SELECT * FROM Customers
WHERE City = 'Berlin' OR City = 'Stuttgart';
• NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
NOT Example
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany":
Example
SELECT * FROM Customers
WHERE NOT Country = 'Germany';
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
• ORDER BY Syntax
• ORDER BY Example
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" column:
Example
SELECT * FROM Customers
ORDER BY Country;
CustomerID CustomerName ContactName Address City PostalCode Country
12 Cactus Comidas para llevar Patricio Simpson Cerrito 333 Buenos Aires 1010 Argentina
54 Océano Atlántico Ltda. Yvonne Moncada Ing. Gustavo Moncada 8585 Piso 20- Buenos Aires 1010 Argentina
A
64 Rancho grande Sergio Gutiérrez Av. del Libertador 900 Buenos Aires 1010 Argentina
50 Maison Dewey Catherine Dewey Rue Joseph-Bens 532 Bruxelles B-1180 Belgium
76 Suprêmes délices Pascale Cartrain Boulevard Tirou, 255 Charleroi B-6000 Belgium
The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
Example
SELECT * FROM Customers
ORDER BY Country DESC;
• ORDER BY Several Columns Example
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column. This means that it orders by Country, but if some
rows have the same Country, it orders them by CustomerName:
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
The following SQL statement selects all customers from the "Customers" table, sorted ascending
by the "Country" and descending by the "CustomerName" column:
Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
12. The MySQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
2. If you are adding values for all the columns of the table, you do not need to specify the
column names in the SQL query. However, make sure the order of the values is in the same
order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
The selection from the "Customers" table will now look like this:
CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Karl Jablonski 305 - 14th Ave. S. Seattle 98128 USA
Markets Suite 3B
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
• The selection from the "Customers" table will now look like this:
CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Karl Jablonski 305 - 14th Ave. Seattle 98128 USA
Markets S. Suite 3B
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.We will
have to use the IS NULL and IS NOT NULL operators instead.
• IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
• The following SQL lists all customers with a NULL value in the "Address" field:
Example
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
• The following SQL lists all customers with a value in the "Address" field:
Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
CustomerName ContactName Address
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
CustomerID CustomerName ContactName Address City PostalCode Country
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1) with a new contact
person and a new city.
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
CustomerID CustomerName ContactName Address City PostalCode Country
Update Warning!
Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
Example
UPDATE Customers
SET PostalCode = 00000;
The selection from the "Customers" table will now look like this:
CustomerID CustomerName ContactName Address City PostalCode Country
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 00000 Mexico
Taquería
15. The MySQL DELETE Statement
DELETE Syntax
DELETE FROM table_name WHERE condition;
Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement.
The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause,
all records in the table will be deleted!
2 Ana Trujillo Emparedados Ana Trujillo Avda. de la Constitución México 05021 Mexico
y helados 2222 D.F.
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México 05023 Mexico
D.F.
• SQL DELETE Example
The following SQL statement deletes the customer "Alfreds Futterkiste" from the "Customers"
table:
Example
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
LIMIT Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
• ADD a WHERE CLAUSE
The following SQL statement selects the first three records from the "Customers" table, where
the country is "Germany":
Example
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
•MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MIN() Example
The following SQL statement finds the price of the cheapest product:
Example
2.50
• MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
• MAX() Example
The following SQL statement finds the price of the most expensive product:
Example
LargestPrice
263.50
I. The COUNT() function returns the number of rows that matches a specified criterion.
• COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
• COUNT() Example
The following SQL statement finds the number of products:
Example
SELECT COUNT(ProductID)
FROM Products;
COUNT(ProductID)
77
II. The AVG() function returns the average value of a numeric column.
• AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Example
SELECT AVG(Price)
FROM Products;
AVG(Price)
28.866364
III. The SUM() function returns the total sum of a numeric column.
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Example
SELECT SUM(Quantity)
FROM OrderDetails;
SUM(Quantity)
51317
You can also combine any number of conditions using AND or OR operators.
LIKE Syntax:-
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
20. The MySQL IN Operator
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
IN Operator Examples:-
The following SQL statement selects all customers that are located in "Germany", "France" or
"UK":
Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
CustomerID CustomerName ContactName Address City PostalCode Country
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
7 Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
• The following SQL statement selects all customers that are NOT located in "Germany",
"France" or "UK":
Example
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
CustomerID CustomerName ContactName Address City PostalCode Country
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
Example
SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
CustomerID CustomerName ContactName Address City PostalCode Country
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
BETWEEN Syntax:-
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
BETWEEN Example:-
The following SQL statement selects all products with a price between 10 and 20:
Example:-
To display the products outside the range of the previous example, use NOT BETWEEN:
Example:-
The following SQL statement selects all products with a price between 10 and 20. In addition; do
not show products with a CategoryID of 1,2, or 3:-
Example:-
The following SQL statement creates two aliases, one for the CustomerID column and one for
the CustomerName column:
Example:-
SELECT CustomerName AS Customer, ContactName AS "Contact Person"
FROM Customers;
Single or double quotation marks are required if the alias name contains spaces
The following SQL statement creates an alias named "Address" that combine four columns
(Address, PostalCode, City and Country):
Example:-
SELECT CustomerName, CONCAT_WS(', ', Address, PostalCode, City, Country) AS Address
FROM Customers;
Example:-
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
The following SQL statement is the same as above, but without aliases:
Example:-
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName='Around the
Horn' AND Customers.CustomerID=Orders.CustomerID;
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
UNION Syntax:-
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Example:-
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
If some customers or suppliers have the same city, each city will only be listed once,
because UNION selects only distinct values. Use UNION ALL to also select duplicate values!
City
Aachen
Albuquerque
Barcelona
Barquisimeto
Example:-
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
City
Aachen
Albuquerque
Anchorage
Bergamo
Berlin
Berlin
The following SQL statement returns the German cities (only distinct values) from both the
"Customers" and the "Suppliers" table:
Example:-
Berlin Germany
Brandenburg Germany
Cunewalde Germany
Cuxhaven Germany
24. MySQL UNIQUE Constraint
• The UNIQUE constraint ensures that all values in a column are different.
• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
UNIQUE (ID)
); LastName varchar(255) NO
• Primary keys must contain UNIQUE values, and cannot contain NULL values.
• A table can have only ONE primary key; and in the table, this primary key can consist of single
or multiple columns (fields).
To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use
the following SQL:
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
Persons Table
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20
Orders Table
1 77895 3
2 44678 3
3 22456 2
4 24562 1
•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.
A SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them. Different types of Joins are:
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
StudentCourse
1. INNER JOIN:
The INNER JOIN keyword selects all rows from both the tables as long as the condition satisfies.
This keyword will create the result-set by combining all rows from both the tables where the
condition satisfies i.e value of the common field will be same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Example Queries(INNER JOIN):-
This query will show the names and age of students enrolled in different courses.
Output:
2.LEFT JOIN:
This join returns all the rows of the table on the left side of the join and matching rows for the
table on the right side of join. The rows for which there is no matching row on right side, the
result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Example Queries(LEFT JOIN):
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:-
3. RIGHT JOIN:
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the right side of
the join and matching rows for the table on the left side of join. The rows for which there is no
matching row on left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT
OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Example Queries(RIGHT JOIN):
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:-
4. FULL JOIN:-
FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The
result-set will contain all the rows from both the tables. The rows for which there is no
matching, the result-set will contain NULL values.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Example Queries(FULL JOIN):
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
28. What is a Stored Procedure?
• A stored procedure is a prepared SQL code that you can save, so the code can be reused over
and over again.
• So if you have an SQL query that you write over and over again, save it as a stored procedure,
and then just call it to execute it.
• You can also pass parameters to a stored procedure, so that the stored procedure can act
based on the parameter value(s) that is passed.
Example:-
Execution:-
call pcm;