You are on page 1of 113

Lesson 7

Intro to MySQL and Basic Commands


Objectives

▪ Know the Top 5 Relational Database Management


Systems
▪ Understand the SQL and its parts
▪ Starting MySQL console
▪ Understand and explain the SQL datatypes
▪ Execute simple SQL commands
What is SQL?

▪ SQL stands for the structured query language.


▪ SQL is the standardized language used to access the
database.
▪ ANSI/SQL defines the SQL standard. The current
version of SQL is SQL:2016. Whenever we refer to the
SQL standard, we mean the current SQL version.
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond
F. Boyce after learning about the relational model from Edgar F. Codd in the
early 1970s. It is used in programming and managing data held in relational
database management systems such as MySql, MS SQL Server, oracle
Sybase, etc as a medium (instructions) for accessing and interacting with
data. It enables performing several operations such as creating, deleting,
modifying, and fetching entries in the database and some other advanced
statistical, arithmetic, and mathematical operations.
▪ MySQL is a database management system that allows you to
manage relational databases. It is open source software backed
by Oracle. It means you can use MySQL without paying a dime.
Also, if you want, you can change its source code to suit your
needs.
▪ Even though MySQL is open source software, you can buy a
commercial license version from Oracle to get premium support
services.
▪ MySQL is pretty easy to master in comparison with other database
software like Oracle Database, or Microsoft SQL Server.
▪ MySQL can run on various platforms UNIX, Linux, Windows, etc.
You can install it on a server or even in a desktop. Besides,
MySQL is reliable, scalable, and fast.
Type of SQL Commands
•DDL(Data Definition Language): To make/perform changes to the
physical structure of any table residing inside a database, DDL is used.
These commands when executed are auto-commit in nature and all the
changes in the table are reflected and saved immediately.

•DML(Data Manipulation Language): Once the tables are created and the
database is generated using DDL commands, manipulation inside those
tables and databases is done using DML commands. The advantage of
using DML commands is, that if in case any wrong changes or values are
made, they can be changed and rolled back easily.
5
Type of SQL Commands
•DQL(Data Query Language): Data query language consists of only one
command upon which data selection in SQL relies. The SELECT command
in combination with other SQL clauses is used to retrieve and fetch data
from databases/tables based on certain conditions applied by the user.

•DCL(Data Control Language): DCL commands as the name suggests


manage the matters and issues related to the data controller in any
database. DCL includes commands such as GRANT and REVOKE which
mainly deal with the rights, permissions, and other controls of the
database system.
5
Type of SQL Commands
•TCL(Transaction Control Language): Transaction Control Language as the
name suggests manages the issues and matters related to the
transactions in any database. They are used to roll back or commit the
changes in the database.
Trivia
▪ My is the daughter’s name of the MySQL’s co-
founder, Monty Widenius.
▪ The name of MySQL is the combination of My and
SQL, MySQL.
▪ The official way to pronounce MySQL is My Ess Que
Ell, not My Sequel.
Download SQL
To download MySQL installer, go to the following
link http://dev.mysql.com/downloads/installer/.

There are two installer files:


•If you are connecting to the internet while installing
MySQL, you can choose the online installation
version mysql-installer-web-community-
<version>.exe .

• In case you want to install MySQL offline, you can


download the mysql-installer-community-
<version>.exe file.
To start in MySQL Command Line Client

▪ From the Start Menu,


select on the MySQL folder
▪ Select MySQL 8.0
Command Line Client
To start in MySQL Command Line Client

▪ To access SQL via the command line we simply


navigate to the xampp folder. the folder is usually on
the c:\ drive. type: cd c:\xampp\mysql\bin then press
enter. type: in mysql -u root -p then press enter
▪ XAMPP is an acronym that stands for Cross-Platform, Apache, MySQL,
PHP, and Perl, with the Ps standing for PHP and Perl, respectively. It's an
open-source web-solutions kit that provides Apache delivery for a variety
of servers and command-line executables, as well as Apache api,
MariaDB, PHP, and Perl modules.
To start in MySQL Command Line Client in XAMPP
• Make sure the
MySQL Module is
running.
• Click the Shell
button.
• Type the command:
mysql –u root -p

23
24
Connect to MySQL Using mysql command-line client
mysql is a command-line client program that allows you to interact with MySQL in the interactive and
non-interactive mode.

The mysql command-line client is typically located in the bin directory of the MySQL’s installation
folder.
Entering Queries
A query normally consists of an SQL statement followed by a semicolon.
When you issue a query, mysql sends it to the server for execution and displays the
results, then prints another mysql>prompt to indicate that it is ready for another
query.

Mysql displays query output in tabular form (rows and columns).


The first row contains labels for the columns. The rows following are the query
results. Normally, column labels are the names of the columns you fetch from
database tables.
Mysql shows how many rows were returned and how long the query took to
execute, which gives you a rough idea of server performance.
• A query need not be given all on a single line, so
lengthy queries that require several lines are not a
problem.
• MySQL determines where your statement ends by
looking for the terminating semicolon, not by
looking for the end of the input line.

Notice how the prompt changes from mysql>to - > after you
enter the first line of a multiple-line query. This is how mysql
indicates that it has not yet seen a complete statement and is
waiting for the rest.
If you decide you do not want to execute a query that
you are in the process of entering, cancel it by typing \c:

Here, too, notice the prompt. It switches back to


mysql>after you type \c, providing feedback to indicate
that mysql is ready for a new query
• The standard SQL commands to interact with
relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE and DROP.
• These commands can be classified into groups
based on their nature.
• Use the SHOW statement to find out what databases
currently exist on the server:
• Type: mysql> SHOW DATABASES;
Use SQL Alias
• Creating a database does not select it for use; you must
explicitly do that by typing: USE <database name>

• Your database needs to be created only once, but you must select it for use each
time you begin a mySQL session. You can do this by issuing a USE statement.
SHOW Databases;
Statement that displays all created databases

SHOW Tables;
Statement that enables you to find out what
tables are in a given database.
Display existing Selecting database Display the tables for
databases for use dbcongzon database
• Creating the database is the easy part, but at this point it is empty, as
SHOW TABLES tells you:
• SQL data type is an attribute that specifies type
of data of any object. Each column, variable
and expression has related data type in SQL.
• You would use these data types while creating
your tables. You would choose a particular data
type for a table column based on your
requirement.
• SQL Server offers six categories of data types
for your use.
Textual Datatype

• Textual information, also known as strings,


are commonly stored using the CHAR or
VARCHAR data type in MySQL. They can
contain letters, numbers or special
characters.
Textual Datatype
Numerical Datatype
• Numbers in MySQL are commonly stored
using INT, FLOAT, DOUBLE or DECIMAL.
Numerical Datatype
Numerical Datatype
Numerical Datatype
Date and Time Datatype
Date and Time Datatype
Date and Time Datatype
Other resources on data types

▪ https://www.w3schools.com/sql/sql_datatypes.asp
▪ https://www.tutorialspoint.com/mysql/mysql-data-types.htm
The DESCRIBE
statement is used to
display a table
structure in a
database.
DROP DATABASE

The DROP DATABASE statement is used to drop an existing


SQL database.
The CREATETABLE statement is used to create a table in a database.
Tables are organized into rows and columns; and each table must
have a name.

• The column_name parameters specify the names of the columns of the table.
• The data_type parameter specifies what type of data the column can hold (e.g.
varchar, integer, decimal, date, etc.).
• The size parameter specifies the maximum length of the column of the table.
• SQL constraints are used to specify rules for the
data in a table.
• If there is any violation between the constraint and
the data action, the action is aborted by the
constraint.
• Constraints can be specified when the table is
created (inside the CREATE TABLE statement) or
after the table is created (inside the ALTER TABLE
statement).
• NOT NULL - Indicates that a column cannot store NULL value.
• UNIQUE - Ensures that each row for a column must have a
unique value.
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Ensures that a column (or combination of two or more columns)
have a unique identity which helps to find a particular record in a
table more easily and quickly.
• FOREIGN KEY - Ensure the referential integrity of the data in one
table to match values in another table.
• CHECK - Ensures that the value in a column meets a specific
condition.
• DEFAULT - Specifies a default value for a column
• AUTO_INCREMENT - Specifies that the values for this column
should be automatically increased by 1 for each new record. This
feature is often used to generate a primary key for the table. By
default, the starting value for an auto increment column is 1.
The NOT NULL constraint enforces a column to NOT accept
NULL values.
The NOT NULL constraint enforces a field to always contain a
value. This means that you cannot insert a new record, or
update a record without adding a value to this field.
The following SQL enforces the "P_Id" column and the
"LastName" column to not accept NULL values:
The UNIQUE constraint uniquely identifies each
record in a database table.
The UNIQUE and PRIMARY KEY constraints both
provide a guarantee for uniqueness for a column or
set of columns.
A PRIMARY KEY constraint automatically has a
UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints
per table, but only one PRIMARY KEY constraint per
table.
The following SQL creates a UNIQUE constraint on the "P_Id" column
when the "Persons" table is created:

SQLServer / Oracle / MS
MySQL:
Access:
To allow naming of a UNIQUE constraint, and for defining a
UNIQUE constraint on multiple columns, use the following SQL
syntax:
• The PRIMARY KEY constraint uniquely
identifies each record in a database table.
• Primary keys must contain UNIQUE values. A
primary key column cannot contain NULL
values.
• Most tables should have a primary key, and
each table can have only ONE primary key.
Note: In the example above there is only ONE PRIMARY KEY
(pk_PersonID). However, the VALUE of the primary key is made
up of TWO COLUMNS (P_Id + LastName).
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
• The CHECK constraint is used to limit the
value range that can be placed in a column.
• If you define a CHECK constraint on a single
column it allows only certain values for this
column.
• If you define a CHECK constraint on a table it
can limit the values in certain columns based
on values in other columns in the row.
• The DEFAULT constraint is used to insert a default value into
a column.
• The default value will be added to all new records, if no
other value is specified
• The DEFAULT constraint can also be used to insert
system values, by using functions like GETDATE():
Create a table Display the created table
Display the table structure

43
Drop Database

• The DROP DATABASE statement is used


to drop an existing SQL database.

Syntax: DROP DATABASE databasename;

Note: Be careful before dropping a database. Deleting a


database will result in loss of complete information
stored in the database!

43
Drop Table

The DROP TABLE statement is used to drop an existing


table in a database.

Syntax: DROP TABLE table_name;

Note: Be careful before dropping a table. Deleting a table will


result in loss of complete information stored in the table!

43
Truncate Table

• The TRUNCATE TABLE statement is used to


delete the data inside a table, but not the
table itself.

Syntax: TRUNCATE TABLE table_name;

43
Data Definition Language (DDL)
• DDL is a set of SQL commands used to create, modify,
and delete database structures but not data.
Data Definition Language (DDL)
Data Definition Language (DDL)

Option 2:
ALTER TABLE tableName
CHANGE COLUMN originalName
newname datatype()
Data Definition Language (DDL)
Data Definition Language (DDL)
Data Definition Language (DDL)
Data Definition Language (DDL)
Data Manipulation Language (DML)
• DML is a category of SQL commands that are used to
manipulate and modify data within a database.
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Query Language (DQL)
• DQL is a category of SQL commands that are used to
retrieve an manipulate data from a database.
Data Query Language (DQL)
• DQL is a category of SQL commands that are used to
retrieve an manipulate data from a database.
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
End of
Lesson 7
44

You might also like