Chapter 8 MySQL Revision 1
Chapter 8 MySQL Revision 1
8:
MySQL – Revision
Tour
Informatics Practices
Class XII (CBSE Board)
What is the Database?
A database is an organized collection of
interrelated data stored together to serve
applications. It work like a container which may
contains the various database objects.
Most of the databases stores data in the form of
Relations (also called Tables). Such Database are
known as Relational Database.
A Software used to manage Relational database
is called RDBMS (Relational Database
System).
Management •Tables
Database
•Queries
Some Commonly used RDBMS •Views
software are- Oracle, MySQL, •Index
MS SQL Server, SyBase and
Ingress etc.
Why Database System is used? (Advantages)
Databases reduces Redundancy
It removes duplication of data because data are kept at one
place and all the application refers to the centrally
maintained database.
Database controls Inconsistency
When two copies of the same data do not agree to each other,
then it is called Inconsistency. By controlling redundancy, the
inconsistency is also controlled.
Database facilitate Sharing of Data
Data stored in the database can be shared among several
users.
Database ensures Security
Data are protected against accidental or intentional
disclosure to unauthorized person or unauthorized
modification.
Database maintains Integrity
It enforces certain
correctness integrity
of data. For ex.rules to can’t
A date insurebe
the validity
like or
25/25/2000.
Data Model
•Data model describes ‘How data is organized or stored’
in the database. It may be-
Relational Data Model
•In this model data is organized into Relations or Tables (i.e.
Rows and Columns). A row in a table represents a relationship of
data to each other and also called a Tuple or Record. A column is
called Attribute or Field.
Network Data Model
• In this model, data is represented by collection of records and
relationship among data is shown by Links.
Hierarchical Data Model
•In this model, Records are organized as Trees. Records at top
level is called Root record and this may contains multiple
directly linked children records.
Object Oriented Data Model
• In this model, records are represented as a objects. The
collection
of similar types of object is called class.
Data Models
Attribute (Field) Table
(Relation) Entity
Name Address DOB City Phone (Record)
Amar 2/3 01.04.1990 Kanpur 12345
Chowk 23.10.1992 67890
Kailash 22 Katra Lucknow
Relational Model
R1
R1
E1 E2 E3 E1 E2 E3
Network Hierarchical
Model Model
Creating a Database.
The following command will create School database in
MySQL. mysql> CREATE DATABASE School;
Opening a database
To open an existing database, following command is
used. mysql> USE school ;
Getting listings of database and tables
mysql> SHOW DATABASES;
mysql> SHOW TABLES;
Deleting a Database and Table
mysql> DROP DATABASE
School; mysql> DROP TABLE
Student;
Viewing Table Structure
Select database();
mysql> DESCRIBE Shows the name of
Student; currently open
database
Data type in MySQL
Numeric Data Types:
INTEGER or INT – up to 11 digit number without decimal.
SMALLINT – up to 5 digit number without decimal.
FLOAT (M,D) or DECIMAL(M,D) or NUMERIC(M,D)
Stores Real numbers upto M digit length (including .) with D
decimal places.
e.g. Float (10,2) can store 1234567.89
Date & Time Data Types:
DATE - Stores date in YYYY-MM-DD format.
TIME - Stores time in HH:MM:SS format.
String or Text Data Type:
CHAR(Size)
A fixed length string up to 255 characters. (default is 1)
VARCHAR(Size)
A variable length string up to 255 characters.
Char, Varchar, Date and Time values should be enclosed with single (‘ ‘) or
double ( “”) quotes in MySQL.
Creating Tables
Creating Simple Tables:
CREATE TABLE < Table Name>
(<Col name1><data type>[(size)][Constraints],….);
Data types- INTEGER, NUMERIC(P,D), CHAR(n), VARCHAR(n), DATE etc.
MySQL will display the all records with all columns in the Student
table.
* Is used to represent all columns.
StID Name Fname DOB City Class
S1 Amitabh Harivansh Rai 1948-11-10 Allahabad 12
S2 Sharukh Firoz 1970-05-10 Delhi 11
S3 Irphan Akbar 1970-10-05 Jaipur 11
S4 Salman Salim Javed 1972-04-10 Mumbai 10
S5 Abhishek Amitabh 1975-03-12 Mumbai 10
Making Simple Queries – Cont..
Selecting columns
If you want to view only Name and City columns of the student table
mysql> SELECT Name, City FROM Student ;
Name City
Amitabh Allahabad
Sharukh Delhi
Irphan Jaipur
Salman Mumbai
Abhishek Mumbai
City
Mumbai is repeated
Allahabad
Delhi
Jaipur
Mumbai
Mumbai
Relational Operators
We can use the following Relational operators in condition.
=, > , < , >=, <=, <>, IS , LIKE, IN,
BETWEEN
Logical Operators
We can use the following Logical Operators to connect two conditions.
OR , AND , NOT (!)
What is Function?
A function is a special types of command that
performs some operation and returns a single
value as a result.
It is similar to method or function in JAVA, which
can be called by giving some argument.
Types of Functions:
Numeric Functions
String Functions
Date & Time Function
Aggregate Functions
Numeric Functions
These functions may accept some numeric values and
performing required operation, returns numeric values as result.