Unit 3 DBMS Keys Join Aggregate View Clauses
Unit 3 DBMS Keys Join Aggregate View Clauses
A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a relation(table).
They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a
combination of one or more columns in that table.
Example:
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
In the above-given example, employee ID is a primary key because it uniquely identifies an employee
record. In this table, no other employee can have the same employee ID.
Keys help you to identify any row of data in a table. In a real-world application, a table could contain
thousands of records. Moreover, the records could be duplicated. Keys ensure that you can
uniquely identify a table record despite these challenges.
Allows you to establish a relationship between and identify the relation between tables
Help you to enforce identity and integrity in the relationship.
DBMS has folwing seven types of Keys each have their different functionality:
Super Key
Primary Key
Candidate Key
1
Alternate Key
Foreign Key
Compound Key
Composite Key
Surrogate Key
A superkey is a group of single or multiple keys which identifies rows in a table. A Super key may have
additional attributes that are not needed for unique identification.
Example:
A column or group of columns in a table which helps us to uniquely identifies every row in that table is
called a primary key. This DBMS can't be a duplicate. The same value can't appear more than once in the
table.
Example:
2
StudID Roll No First Name LastName Email
All the keys which are not primary key are called an alternate key. It is a candidate key which is currently
not the primary key. However, A table may have single or multiple choices for the primary key.
StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary key, Roll No,
Email becomes the alternative key.
The Primary key should be selected from the candidate keys. Every table must have at least a single
candidate key.
3
Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to uniquely
identify the student record in the table.
A foreign key is a column which is added to create a relationship with another table. Foreign keys help us to
maintain data integrity and also allows navigation between two different instances of an entity. Every
relationship in the model needs to be supported by a foreign key.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
4
In this example, we have two table, teach and department in a school. However, there is no way to see
which search work in which department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship
between the two tables.
Compound key has many fields which allow you to uniquely recognize a specific record. It is possible that
each column may be not unique by itself within the database. However, when combined with the other
column or columns the combination of composite keys become unique.
Example:
In this example, OrderNo and ProductID can't be a primary key as it does not uniquely identify a record.
However, a compound key of Order ID and Product ID could be used as it uniquely identified each record.
5
A key which has multiple attributes to uniquely identify rows in a table is called a composite key. The
difference between compound and the composite key is that any part of the compound key can be a foreign
key, but the composite key may or maybe not a part of the foreign key.
An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key are
unique because they are created when you don't have any natural primary key. They do not lend any
meaning to the data in the table. Surrogate key is usually an integer.
Above, given example, shown shift timings of the different employee. In this example, a surrogate key is
needed to uniquely identify each employee.
Helps you to uniquely identify a It is a field in the table that is the primary key
record in the table. of another table.
Primary Key never accept null values. A foreign key may accept multiple null
values.
Primary key is a clustered index and A foreign key cannot automatically create an
data in the DBMS table are physically index, clustered or non-clustered. However,
organized in the sequence of the you can manually create an index on the
6
clustered index. foreign key.
You can have the single Primary key You can have multiple foreign keys in a
in a table. table.
Summary
A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a
relation(table)
DBMS keys allow you to establish a relationship between and identify the relation between tables
Seven Types of DBMS keys are Super, Primary, Candidate, Alternate, Foreign, Compound,
Composite, and Surrogate Key.
A super key is a group of single or multiple keys which identifies rows in a table.
A column or group of columns in a table which helps us to uniquely identifies every row in that table
is called a primary key
All the keys which are not primary key are called an alternate key
A super key with no repeated attribute is called candidate key
A compound key is a key which has many fields which allow you to uniquely recognize a specific
record
A key which has multiple attributes to uniquely identify rows in a table is called a composite key
An artificial key which aims to uniquely identify each record is called a surrogate key
Primary Key never accept null values while a foreign key may accept multiple null values.
SQL Clauses
1. GROUP BY
o SQL GROUP BY statement is used to arrange identical data into groups. The
GROUP BY statement is used with the SQL SELECT statement.
o The GROUP BY statement follows the WHERE clause in a SELECT statement and
precedes the ORDER BY clause.
o The GROUP BY statement is used with aggregation function.
Syntax
7
SELECT column FROM table_name WHERE conditions GROUP BY column
ORDER BY column
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example:
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY;
Output:
Com1 5
Com2 3
Com3 2
8
2. HAVING
Syntax:
Example:
3. ORDER BY
Syntax:
Where
9
DESC: It sorts the result set in descending order by expression.
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
10
Example: Sorting Results in Descending Order
1. SELECT *
2. FROM CUSTOMER
3. ORDER BY NAME DESC;
Output:
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
Views
N A view is nothing more than a SQL statement that is stored in the database with an associated
name. A view is actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view can be created from one
or many tables which depends on the written SQL query to create a view.
Views, which are a type of virtual tables allow users to do the following −
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data in such a way that a user can see and (sometimes)
modify exactly what they need and no more.
Summarize data from various tables which can be used to generate reports.
11
Creating Views
Database views are created using the CREATE VIEW statement. Views can be created from a
single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the specific
implementation.
The basic CREATE VIEW syntax is as follows −
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
You can include multiple tables in your SELECT statement in a similar way as you use them in a
normal SQL SELECT query.
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example to create a view from the CUSTOMERS table. This view would be used
to have customer name and age from the CUSTOMERS table.
SQL > CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table.
Following is an example for the same.
SQL > SELECT * FROM CUSTOMERS_VIEW;
Now, let us join these two tables in our SELECT statement as shown below.
SQL> SELECT ID, NAME, AGE, AMOUNT
FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
13
SQL Index
o Indexes are special lookup tables. It is used to retrieve data from the database
very fast.
o An Index is used to speed up select queries and where clauses. But it shows
down the data input with insert and update statements. Indexes can be created
or dropped without affecting the data.
o An index in a database is just like an index in the back of a book.
o For example: When you reference all pages in a book that discusses a certain
topic, you first have to refer to the index, which alphabetically lists all the topics
and then referred to one or more specific page numbers.
Syntax
Example
Syntax
Example
14
3. Drop Index Statement
It is used to delete an index in a table.
Syntax
Example
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means
"to combine two or more tables".
In SQL, JOIN clause is used to combine the records from two or more tables in a
database.
Sample Table
EMPLOYEE
15
3 Christian Denver 100000 42
PROJECT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as long
as the condition is satisfied. It returns the combination of all rows from both the
tables where the condition satisfies.
Syntax
1. SELECT table1.column1, table1.column2, table2.column1,....
2. FROM table1
3. INNER JOIN table2
4. ON table1.matching_column = table2.matching_column;
Query
1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
2. FROM EMPLOYEE
3. INNER JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
16
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values from
the right table. If there is no matching join value, it will return NULL.
Syntax
1. SELECT table1.column1, table1.column2, table2.column1,....
2. FROM table1
3. LEFT JOIN table2
4. ON table1.matching_column = table2.matching_column;
Query
1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
2. FROM EMPLOYEE
3. LEFT JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
17
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right
table and the matched values from the left table. If there is no matching in both
tables, it will return NULL.
Syntax
1. SELECT table1.column1, table1.column2, table2.column1,....
2. FROM table1
3. RIGHT JOIN table2
4. ON table1.matching_column = table2.matching_column;
Query
1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
2. FROM EMPLOYEE
3. RIGHT JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join
tables have all the records from both tables. It puts NULL on the place of matches not
found.
Syntax
18
1. SELECT table1.column1, table1.column2, table2.column1,....
2. FROM table1
3. FULL JOIN table2
4. ON table1.matching_column = table2.matching_column;
Query
1. SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
2. FROM EMPLOYEE
3. FULL JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
1. COUNT FUNCTION
19
o COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.
Syntax
1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT()
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:
10
20
Example: COUNT with WHERE
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;
Output:
7
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.
Syntax
1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )
Example: SUM()
21
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Output:
670
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG
function returns the average of all non-Null values.
Syntax
1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
Example:
22
1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
Syntax
1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )
Example:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax
1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Example:
1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;
Output:
10
23