Practical MySQL 1
Practical MySQL 1
Practical 1
1
What is a Database ?
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
4
Introduction cont…
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
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
-> 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:
• 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 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:
22
Exercise
• Table: Employee
Name Age DateOfBirth City Salary Time
23
Exercise
Name Age DateOfBirth City Salary Time
25