0% found this document useful (0 votes)
77 views25 pages

Practical MySQL 1

This document provides an overview of MySQL and databases. It discusses what a database is, the components of databases like tables and columns, and how MySQL is a database management system that uses SQL. It also covers starting MySQL using XAMPP, creating and using databases, listing tables, creating tables with different data types, and describing and viewing table structures. The document includes an exercise to create an "Employee" table with various data types like varchar, int, date, and double.

Uploaded by

Sachin Sul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
77 views25 pages

Practical MySQL 1

This document provides an overview of MySQL and databases. It discusses what a database is, the components of databases like tables and columns, and how MySQL is a database management system that uses SQL. It also covers starting MySQL using XAMPP, creating and using databases, listing tables, creating tables with different data types, and describing and viewing table structures. The document includes an exercise to create an "Employee" table with various data types like varchar, int, date, and double.

Uploaded by

Sachin Sul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

MySQL

Practical 1

1
What is a Database ?

• A database is a collection of tables, with related data

• It may be anything from a simple shopping list to a picture


gallery or the vast amounts of information in a corporate
network.

2
What is a Database ? cont...
• Table: A table is a matrix with data. A table in a database
looks like a simple spread sheet

• Column: One column (data element) contains data of one


and the same kind, for example the column postcode

• Row: A row (= tuple, entry or record) is a group of related


data, for example the data of one subscription.
3
Introduction

• MySQL is a database management system

• SQL stands for the Structured Query Language.

• It defines how to insert, retrieve, modify and delete data

4
Introduction cont…

• MySQL software is Open Source


• Open Source means that it is possible for anyone to use and
modify the software
• Anybody can download the MySQL software from the Internet
and use it without paying anything
• The MySQL Database Server is very fast, reliable, and easy
to use
5
Starting MySQL with XAMPP

• Open XAMPP control panel


• Start MySQL

6
Starting MySQL with XAMPP cont…

• Click “Shell”

• Navigate to mysql/bin
• Type cd mysql/bin

7
Starting MySQL with XAMPP cont…

• Log in to MySQL
• Type mysql -h localhost -u root

8
MySQL is a RDBMS

• Relational database management systems (RDBMS) is


used to store and manager huge volume of data
• This is called relational database because all the data is
stored into different tables and relations are established
using primary keys or other keys known as foreign keys

9
Creating and Using a Database
• mysql> SHOW DATABASES;
• SHOW statement can be used to find out the databases currently
existing on the server
• mysql> CREATE DATABASE example;
• mysql> USE example
• Example is a database name.
• USE command does not need a semi colon and must be given in a
single line
10
MySQL Command Line
Prompt Meaning
mysql> Ready for new command
-> Waiting for next line of multiple-line command.
‘> Waiting for next line, collecting a string that
begins with a single quote ( ’ ).
“> Waiting for next line, collecting a string that
begins with a double quote ( ” )

11
MySQL Command Line
mysql> SELECT *
-> FROM my_table
-> WHERE name = “Smith”;

Or

mysql> SELECT * FROM my_table WHERE name =


"Smith ;
12
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>

13
Exit MySQL
• To exit the MySQL Shell, just type QUIT or EXIT:

mysql> quit;
or
mysql> exit;

14
Listing Tables
• 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 have not created any tables yet

15
Creating a Table
• The CREATE TABLE statement is used to create a table in
MySQL
• Syntax
mysql> CREATE TABLE table_name(
-> column_name1 data_type,
-> column_name2 data_type,
-> column_name3 data_type);

16
Data types
• MySQL supports a number of data types in several
categories: numeric types, date and time types, and string
(Text) types

17
Data types – Date Types
DATE YYYY-MM-DD.
DATETIME YYYY-MM-DD HH:MM:SS.
TIMESTAMP YYYYMMDDHHMMSS.
TIME HH:MM:SS.

18
Data types – Text Types
CHAR( ) A fixed section from 0 to 255 characters long
VARCHAR( ) A variable section from 0 to 255 characters long
TINYTEXT A string with a maximum length of 255 characters
TEXT A string with a maximum length of 65535 characters
BLOB A string with a maximum length of 65535 characters
MEDIUMTEXT A string with a maximum length of 16777215 characters.
MEDIUMBLOB A string with a maximum length of 16777215 characters
LONGTEXT A string with a maximum length of 4294967295 characters.
LONGBLOB A string with a maximum length of 4294967295 characters.

• The ( ) brackets allow you to enter a maximum


number of characters will be used in the column.
VARCHAR(20)
19
Data types – Number Types
-128 to 127 normal
TINYINT( )
0 to 255 UNSIGNED
-32768 to 32767 normal
SMALLINT( )
0 to 65535 UNSIGNED.
-8388608 to 8388607 normal
MEDIUMINT( )
0 to 16777215 UNSIGNED.
-2147483648 to 2147483647 normal
INT( )
0 to 4294967295 UNSIGNED.
-9223372036854775808 to 9223372036854775807 normal
BIGINT( )
0 to 18446744073709551615 UNSIGNED.
FLOAT A small number with a floating decimal point.
DOUBLE( , ) A large number with a floating decimal point.
DECIMAL( , ) A DOUBLE stored as a string , allowing for a fixed decimal point.

• The integer types have an extra option called UNSIGNED. Normally, the
integer goes from an negative to positive value. Using an UNSIGNED
command will move that range up so it starts at zero instead of a negative
number. 20
Example Create Table

21
Describing Tables
• To view a table structure, use the DESCRIBE command:

mysql> desc Persons;

22
Exercise
• Table: Employee
Name Age DateOfBirth City Salary Time

Column Data type


Name Varchar
Age Int
DateOfBirth Date
City Varchar
Salary Double (with 2 decimal point)
Time Date

23
Exercise
Name Age DateOfBirth City Salary Time

Create table employee(


Name varchar(10),
Age int,
DateOfBirth date,
City varchar(45),
Salary double(5 ,2 ),
Time time
);
24
END

25

You might also like