Database and
MySQL
MODULE II
Database Server:
A database that is implemented and interfaced on a
computer is called a database server.
One of the fastest SQL database server currently in the
market is the MySQL server, which is developed by
Swedish company TcX in 1994.
http://dev.mysql.com/downloads
Available free for general public for private and
commercial use.
Database and Database
Management System
Student_id Name Grade Database is simply a collection of
data. In relational database, data is
101 Asha A organized into tables.
Database Management System
102 Jatin B (DBMS) is software to maintain and
utilize the collections of data (Oracle,
103 Avi A MySQL, etc.)
Database and Database
Management System
Column (Field): Particular information is called a column. In the above
example, there is one column for Student_id, one column for Name,
and so on.
Row(Record): Collection of related columns makes a row (record). In
the example student_id, name, grade of a student together will make
a row.
Table( File): Collection of related rows makes a table. In the example
above, all the rows together is called as a table.
Database: Collection of related tables makes a database. i.e. a
database is a set of related information.
Generally, a Database contains tables, table contains rows and
columns.
Example of Database
Student_id Name Grade Student_id DOB Address
101 Asha A 101 15/12/1998 Andheri
102 01/12/1999 Khar
102 Jatin B
103 08/03/1998 Bandra
103 Avi A
Rows
MySQL Introduction
MySQL is a database management
system
SQL stands for the Structured Query
Language. It defines how to insert,
retrieve, modify and delete data.
Basic MySQL Operations
Create table Modify table
Insert records Join table
Load data Drop table
Retrieve records Count, Like, Order by, Group by
Update records
Delete records
8
MySQL
MySQL is a very popular, open source
database.
Officially pronounced “my Ess Que Ell” (not my
sequel).
Handles very large databases; very fast
performance.
SQL: Non-Procedural Query Language
A procedural language defines both the desired results and
the mechanism, or process, by which the results are
generated.
Non-procedural languages also define the desired result, but
not the process; the process by which the results are
generated is left to database engine.
SQL statements define the necessary inputs and outputs, but
not the process; the database engine will look at SQL
statements and taking into account all the aspects, it will
decide the most efficient execution path.
10
Connecting to MySQL
MySQL provides an interactive shell for
creating tables, inserting data, etc.
On Windows, just go to c:\mysql\bin, and
type:
mysql
Or, click on the Windows icon
How MySQL stores data (by default)
A MySQL server can store several databases
Databases are stored as directories
Default is at /usr/local/mysql/var/
Tables are stored as files inside each database
(directory)
Sample Session
For example:
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 241 to server version: 3.23.49
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
To exit the MySQL Shell, just type QUIT or EXIT:
mysql> QUIT
mysql> exit
12
Basic Queries
Once logged in, you can try some simple queries.
For example:
mysql> SELECT VERSION(), CURRENT_DATE; Command in MySQL
+-----------+--------------+
| VERSION() | CURRENT_DATE | Heading of the answer
+-----------+--------------+
| 3.23.49 | 2016-06-29 | Answer
+-----------+--------------+
1 row in set (0.00 sec) Number of rows in the
answer and time taken to
display the answer.
Note that most MySQL commands end with a semicolon (;)
MySQL returns the total number of rows found, and the total time to execute the query. 15
Basic Queries
Keywords may be entered in any lettercase.
The following queries are equivalent:
mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;
16
Basic Queries
You can also enter multiple statements on a single line. Just end
each one with a semicolon:
mysql> SELECT VERSION(); SELECT NOW();
+--------------+
| VERSION() |
+--------------+
| 3.22.20a-log |
+--------------+
+---------------------+
| NOW() |
+---------------------+
| 2004 00:15:33 |
+---------------------+
17
Basic Queries
Here's another query. It demonstrates that you can
use mysql as a simple calculator:
mysql> SELECT SIN(PI()/4), (4+1)*5;
+-------------+---------+
| SIN(PI()/4) | (4+1)*5 |
+-------------+---------+
| 0.707107 | 25 |
+-------------+---------+
18
Multi-Line Commands
mysql determines where your statement ends by looking for
the terminating semicolon, not by looking for the end of the
input line.
Here's a simple multiple-line statement:
mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
+--------------------+--------------+
| USER() | CURRENT_DATE |
+--------------------+--------------+
| joesmith@localhost | 1999-03-18 |
+--------------------+--------------+
19
Canceling a Command
If you decide you don't want to execute a command that you are in the
process of entering, cancel it by typing \c
mysql> SELECT
-> USER()
-> \c
mysql>
20
Using a Database
To get started on your own database, first check which databases currently
exist.
Use the SHOW statement to find out which databases currently exist on the
server:
mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.01 sec)
21
Using a Database
To create a new database, issue the “create database” command:
mysql> create database webdb;
To the select a database, issue the “use” command:
mysql> use webdb;
22
Creating a Table
Once you have selected a database, you can view all
database tables:
mysql> show tables;
Empty set (0.02 sec)
An empty set indicates that I have not created any
tables yet.
23
Creating a Table
Let’s create a table for storing pets.
Table: pets
➢ name: VARCHAR(20)
➢ owner: VARCHAR(20)
➢ species: VARCHAR(20)
➢ sex: CHAR(1)
VARCHAR is
➢ birth: DATE
➢ death: DATE
usually used
to store string
data.
24
Creating a Table
To create a table, use the CREATE TABLE command:
mysql> CREATE TABLE pet (
-> name VARCHAR(20),
-> owner VARCHAR(20),
-> species VARCHAR(20),
-> sex CHAR(1),
-> birth DATE, death DATE);
Query OK, 0 rows affected (0.04 sec)
25
Showing Tables
To verify that the table has been created:
mysql> show tables;
+------------------+
| Tables_in_test |
+------------------+
| pet |
+------------------+
1 row in set (0.01 sec)
26
Describing Tables
To view a table structure, use the DESCRIBE
command:
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)
27
Loading Data
Use the INSERT statement to enter data into a table.
For example:
INSERT INTO pet VALUES
('Fluffy','Harold','cat','f',
'1999-02-04',NULL);
The next slide shows a full set of sample data.
28
More data… 29
name owner species sex birth death
Fluffy Harold cat f 1993-02-04
Claws Gwen cat m 1994-03-17
Buffy Harold dog f 1989-05-13
Fang Benny dog m 1990-08-27
Bowser Diane dog m 1998-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11
Whistler Gwen bird 1997-12-09
Slim Benny snake m 1996-04-29
Selecting All Data
The simplest form of SELECT retrieves everything from a
table
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1999-02-04 | NULL |
| Claws | Gwen | cat | f | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1999-08-27 | NULL |
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+ 30
8 rows in set (0.00 sec)
Deleting a Table
To delete an entire table, use the DROP TABLE command:
mysql> drop table pet;
Query OK, 0 rows affected (0.02 sec)
31