Class XII Ch 5
Class XII Ch 5
Revision Tour
Relation - A table storing logically related data; data must be relating in a cell.
Domain - This is a pool of values from which the actual values appearing in a given
column .
Tuple - A row of a relation.
Attribute - A column of a relation.
Degree - This refers to the number of attributes in a relation.
Cardinality - Number of tuples (rows) in a relation.
Primary Key - This refers to a set of one or more attributes that tcan uniquely
identify tuples within
the relation.
Candidate Key - That can serve as primary key.
Alternate Key - A candidate key that is not primary key, is called an alternate
key.
Foreign key - A non-key attribute, whose values are derived from the primary key of
some other table,
known as foreign key in its current table. It is used to link to table.
MySQL database System - MySQL database system referss to the combination of a MySQL
server and
a MySQL database. It operates using client/ server architecture in
which the servers runs on
the machine containing the database and clients connect to the server over a
network.
The server - listens for client requests coming in over the network and accesses
databases.
The clients - are programs that connect to the databse server and issue queries in
a pre-specified format.
MySQL and SQL - MySQL is a datbase and SQL is the set of commands that is
recognised by nearly all
RDBMSs.
mysql> create table INVENTORY(Car_ID int primary key, CarName Varchar(20) not null,
Price decimal(2),
Model char(6), Year_Manufacture date, Fule_Type varchar(10));
A function is a special type of predefined command set that performs some operation
and returns a
single value.
Functions in MySQL can be either single row functions or multiple row functions.
Single row functions - Work oln eaach individual row of a table and return result
for each row.
These are also known as Scalar functions. Numeric (Math), String, Date and Time.
(ii) ROUND(N,D)
SELECT ROUND(2912.564, 1);
Output: 2912.6
(iii) MOD(A, B)
SELECT MOD(21, 2);
Output: 1
(C) Date and Time Functions - functions that are used to perform operations on
date and time data.
Some of the operations include displaying the current date, extracting each element
of a date
(day, month and year), displaying day of the week and so on.
(ii) DATE() - It returns the date part from the given date/ time expression.
mysql> select date(now());
(iii) MONTH(date) - It returns the month in numeric form from the date.
mysql> select month(now());
(iv) MONTHNAME(date) - It returns the month name from the specified date.
mysql> select date(now());
(vii) DAYNAME(date) - It returns the name of the day from the date.
mysql> SELECT DAYNAME(�2019-07-11�);
Aggregate Functions
Aggregate functions are also called multiple row functions. These functions work on
a set of
records as a whole, and return a single value for each column of the records on
which the
function is applied.
SUM(column) - Returns the sum of the values for the specified column.
mysql> SELECT SUM(Price) FROM INVENTORY;
COUNT(column) - Returns the number of values in the specified column ignoring the
NULL values.
mysql> SELECT * from Employee;
GROUP BY in SQL
At times we need to fetch a group of rows on the basis of common values in a
column. This can
be done using a GROUP BY clause. It groups the rows together that contain the same
values in
a specified column.
mysql> SELECT CustID, COUNT(*) "Number of Cars" FROM SALE GROUP BY CustID;
mysql> SELECT CustID, COUNT(*) FROM SALE GROUP BY CustID HAVING Count(*)>1;
OPERATION ON RELATIONS
We can perform certain operations on relations like Union, Intersection, and Set
Difference to
merge the tuples of two tables. These three operations are binary operations as
they work
upon two tables.
Note here, that these operations can only be applied if both the relations have the
same number
of attributes, and corresponding attributes in both tables have the same domain.
UNION (U) - This operation is used to combine the selected rows of two tables at a
time.
If some rows are the same in both the tables, then the result of the Union
operation will show
those rows only once.