0% found this document useful (0 votes)
16 views

Class XII Ch 5

Uploaded by

asayushsingh638
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Class XII Ch 5

Uploaded by

asayushsingh638
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

My SQL SQL

Revision Tour

Introduction:- A database system is basically a compuer based record keeping


system. The collection of data ,
usually referred to as the database, contans infromation about one particular
enterprise.

Advantages of database system.


1. Database systems reduce data redundancy (data duplication) to a large extent.
2. Database systems control data incocnsistency / Confliccting to a large extent.
3. Database facilitate sharing of data.
4. Databases enforce standards.
5. Centralized databases can ensure data security.
6. Ingegrity/ Truthfulness can be maintained through databases.

Relational database model:- A data model refers to a set of concepts to describe


the structure of a database,
and certain constraints (restrictions) that the database should obey.

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.

Classificaiton of SQL statements


1. Data Definition Language(DDL) commands. That allow us to perform tasks related
to the data defination.
eg. (1) create, altering and dropping.
(2) granting and revoking privileges and roles.
(3) maintenance commands.
2. Data Manipulation Language (DML) commands - That allow us sto perform data
manipulation.
eg. retrieval, insertion, deleting and modification of data stored in a
database.
3. Transaction Contral Language(TCL) commands - That allow us to manage and
control the transaction.
eg. making changes to database permanent
undoing changes to database, permanent
creating savepoints.
setting properties for current transaction.

A constraint is a condition or check applicable on a field or set of fields.


SQL Constraints
1. NOT NULL - Ensurs that a column cannot have NULL value.
create table a(rollno int not null, name char(10));

2. DEFAULT - Provides a default value for a column when none is


specified.
create table b (student_ID int(2) Unique, name varchar(30), MaxM float(5) default
'100');
3. UNIQUE - Ensure that all values in a column are different.
4. CHECK - Makes sure that tall values in a column satisfy
certain criteria.
5. Primary Key - Used to uniquely identify a row in the table.
6. Forign Key - Used to ensure referential integrity of the data.

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));

Accessing Database in MySQL


To view databases stored on MySQL, we can give following command:
mysql>show databases;
To open the database for use
mysql>use <databasename>;
mysql> use students; # students is the name of database.
Creating tables in MySQL
Tables are defined with the CREATE TABLE command.
mysql>create table stud (ecode integer, ename char(20), Tot_marks float,
percentage float);
To view the tables in open database, we can give the command as
mysql>SHOW TABLES;
Inserting Data into Table
mysql> insert into stud values (1001,'Ravi', 85.5, 85.5);
Inserting Null Values
mysql> insert into stud(ecode,ename) values (1002, 'Kavi');

Modify in table stracture


mysql> alter table sale change PaymentMode Paymode varchar(10);
data update
update sale set saledate ='2017-10-10' where invoiceno = 1021;

To delete table from database


mysql> drop table inventory;

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.

Multiple row(Aggregate) funcitons - Work on multiple rows together and return a


summary result
for a group of rows.

Single Row Functions


Single row functions are applied on a single value and return a single value. Math
functions accept
numeric value as input, and return a numeric value as a result. String functions
accept character
value as input, and return either character or numeric values as output. Date and
time functions
accept date and time values as input, and return numeric or string, or date and
time values
as output.
(A) Numeric Functions - Three commonly used numeric functions are POWER(), ROUND()
and MOD().
(i) POWER(X,Y)
mysql> SELECT POWER(2,3);
Output: 8

(ii) ROUND(N,D)
SELECT ROUND(2912.564, 1);
Output: 2912.6

(iii) MOD(A, B)
SELECT MOD(21, 2);
Output: 1

(B) String Functions - String functions can perform various operations on


alphanumeric data which
are stored in a table. They can be used to change the case (uppercase to lowercase
or vice-versa),
extract a substring, calculate the length of a string and so on.

(i) UCASE(string) OR UPPER(string) - Convert string into upper case.


mysql> select ucase("Informatics Practices");

(ii) LOWER(string) OR LCASE(string) - Convert string into lower case.


mysql> select lower("INFORMATICS PRACTICES");

(iii) MID(string, pos, n) OR SUBSTRING(string, pos, n) OR SUBSTR(string, pos, n)


Returns a substring of size n starting from the specified position (pos) of the
string. If n is not
specified, it returns the substring from the position pos till end of the string.
mysql> select mid("informatics",3,4);
output - form
mysql> select mid("informatics", 7);

(iv) LENGTH(string) - Return the number of characters in the specified string.


mysql> select length("gyankunjacademy");
output - 15
(v) LEFT(string, N) - Returns N number of characters from the left side of the
string.
mysql> select left("gyankunj",4);
output - gyan
(vi) RIGHT(string, N) - Returns N number of characters from the right side of the
string.

mysql> select right("gyankunj",4);


output - kunj

(vii) INSTR(string, substring) - Returns the position of the first occurrence of


the substring in the
given string. Returns 0, if the substring is not present in the string.
mysql>SELECT INSTR("Informaticsma", "ma");
output - 6
(viii) LTRIM(string) - Returns the given string after removing leading white
space characters.
mysql> SELECT LENGTH(� DELHI�), LENGTH(LTRIM(� DELHI�));
Output: 7 5
(ix) RTRIM(string) - Returns the given string after removing trailing white space
characters.
mysql> SELECT LENGTH("PEN "), LENGTH(RTRIM("PEN "));
output - LENGTH("PEN ") LENGTH(RTRIM("PEN ")) |
5 3
(x) TRIM(string) - Returns the given string after removing both leading and
trailing white space
characters.
mysql>select length("madam"), length(trim(" madam "));
output - length("madam") length(trim(" madam ")) |
9 5

(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.

(i) NOW() - It returns the current system date and time.


mysql> select now();

(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());

(v) YEAR(date) - It returns the year from the date.


mysql> SELECT YEAR(�2003-10-03�);

(vi) DAY(date) - It returns the day part from the date.


mysql>SELECT DAY(�2003-03-24�);

(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.

Differences between Single row and Multiple row Functions


Single row function
1. It operates on a single row at a time.
2. It returns one result per row.
3. It can be used in Select, Where, and Order by clause.
4. Math, String and Date functions are examples of single row functions.
Multi row function
1. It operates on groups of rows.
2. It returns one result for a group of rows.
3. It can be used in the select clause only.
4. Max(), Min(), Avg(), Sum(), Count() and Count(*) are examples of multiple row
functions.

Aggregate Functions in SQL


MAX(column) - Returns the largest value from the specified column.
mysql> SELECT MAX(Price) FROM INVENTORY;

MIN(column) - Returns the smallest value from the specified column.


mysql> SELECT MIN(Price) FROM INVENTORY;

AVG(column) - Returns the average of the values in the specified column.


mysql> SELECT AVG(Price) FROM INVENTORY;

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;

COUNT(*) - Returns the number of records in a table.


mysql> SELECT COUNT(*) 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.

You might also like