Anda di halaman 1dari 64

MySQL SQL Commands

HTTP 5105 Database Development and Design

HTTP 5105 Database Development and Design 1 November 2014


Introduction

This set of notes will investigate SQL commands with


the emphasis on MySQL

HTTP 5105 Database Development and Design 2 November 2014


Create Tables

MySQL does allow the creation of a database


In our configuration at Humber you can only work
with predefined databases
These are created by the administrator
You have two
abcd0000_a
abcd0000_b
You will be directed which one to use
HTTP 5105 Database Development and Design 3 November 2014
Creating Tables

The biggest change when comparing to Oracle is the


data types used
Last we you were provided with a chart outlining the
various datatypes permitted in MySQL, please make
sure you are aware of the differences and the most
commonly used datatypes

HTTP 5105 Database Development and Design 4 November 2014


Creating Tables Datatypes
Data Type Description
NUMERIC or DECIMAL DECIMAL can also be DEC, fixed point
INTEGER and variations INT most common
FLOAT Represent floating point numbers
CHAR Fixed-length strings, usually followed by a
number CHAR(20), defaults to 1 if no size
VARCHAR Stores variable length strings up to 255,
same length as a CHAR
DATE Stores a date value in format of YYYY-MM-
DD
TIME Stores a time value in the format of
HH:MM:SS

HTTP 5105 Database Development and Design 5 November 2014


Creating Tables - Engines

MySQL has various storage engines


The MySQL engines are:
MyISAM (default engine)
InnoDB (supports transactions and row-level locking
and foreign keys)
BerkeleyDB
There are other engines than can be used

HTTP 5105 Database Development and Design 6 November 2014


Creating Tables

Department_id datatype int, also not null so you have to enter a value, auto_increment
so it will assign a value for you, and finally a primary key for the table

Name datatype varchar with a length of 30

The engine this table is created with is the InnoDB engine


HTTP 5105 Database Development and Design 7 November 2014
Creating Tables

MySQL responds with the


message SQL query has
been executed
successfully
A table is now created, if
you expand your
database in the left pane
you will see the new table
listed
HTTP 5105 Database Development and Design 8 November 2014
Creating Tables

HTTP 5105 Database Development and Design 9 November 2014


Creating Tables

After the second table is


created the same
confirmation message is
shown
If you expand the tree
under the database name
you will now see two
tables defined

HTTP 5105 Database Development and Design 10 November 2014


Creating Tables

After selecting the database from the tree in the left panel you will notice you
see a more detailed description of your tables in the right panel
HTTP 5105 Database Development and Design 11 November 2014
Creating Tables

What is different?
Datatypes are different
Auto_increment for sequence number generation
The defining of an engine or type
What is the same?
The actual command CREATE TABLE
The open and close parentheses
The column list inside the parentheses
NOT NULL and PRIMARY KEY
FOREIGN KEY definition

HTTP 5105 Database Development and Design 12 November 2014


Inserting, Updating and Deleting Data

The biggest difference with the INSERT in MySQL is the ability to


insert multiple rows at one time with a single INSERT INTO command

HTTP 5105 Database Development and Design 13 November 2014


Inserting, Updating and Deleting Data

You will notice you


receive confirmation of
how many rows were
inserted to the table
You may wonder about
the two values that were
left as NULL for the
primary key, what
happened?
HTTP 5105 Database Development and Design 14 November 2014
Inserting, Updating and Deleting Data

Let us see what happened to the rows of data we entered


Enter the above command in the SQL window

HTTP 5105 Database Development and Design 15 November 2014


Inserting, Updating and Deleting Data

You will notice the entries for


Human Resources and Marketing
both have a primary key value
This is courtesy of the
auto_increment that was applied
to the table when it was created
Notice it also starts at the next
value above the highest value
inserted already, we supplied 128
so it started at 129

HTTP 5105 Database Development and Design 16 November 2014


Inserting, Updating and Deleting Data

The SQL DELETE statement allows us to delete rows


from a table
The WHERE clause is optional
For the most part it behaves the same as Oracle
MySQL does have a couple of other variations that we
will not go into

HTTP 5105 Database Development and Design 17 November 2014


Inserting, Updating and Deleting Data

There is no WHERE clause specified, what will happen?

HTTP 5105 Database Development and Design 18 November 2014


Inserting, Updating and Deleting Data

Does this give you any indication?

If I click OK I loose all the rows in the table

If I click Cancel nothing will happen, all the rows will remain

HTTP 5105 Database Development and Design 19 November 2014


Inserting, Updating and Deleting Data

As with Oracle the WHERE clause can be used to indicate which rows
are to be deleted
HTTP 5105 Database Development and Design 20 November 2014
Inserting, Updating and Deleting Data

Same as before I am prompted before the deletion takes place

If I click OK I loose all the row specified in the command

If I click Cancel nothing will happen, and the row will remain

HTTP 5105 Database Development and Design 21 November 2014


Inserting, Updating and Deleting Data

The WHERE clause is optional, so if I do not use the WHERE clause all
of the rows would have the update applied to them
HTTP 5105 Database Development and Design 22 November 2014
Inserting, Updating and Deleting Data

In this case I have an


indication 1 row was
updated
The change is applied to
the table right away
Notice there is no
COMMIT or ROLLBACK
commands

HTTP 5105 Database Development and Design 23 November 2014


Inserting, Updating and Deleting Data

You will notice the


employee_id 6651 now
has a job of DBA

HTTP 5105 Database Development and Design 24 November 2014


SELECT Statement

Should look familiar, there is no difference from what we used


previously in Oracle
HTTP 5105 Database Development and Design 25 November 2014
SELECT Statement

All of the rows in the


DEPARTMENT table are
displayed
There is a limit of 30 rows
of output imposed on the
query.

HTTP 5105 Database Development and Design 26 November 2014


SELECT Statement

Can also specify the column names I wish to see in the output

HTTP 5105 Database Development and Design 27 November 2014


SELECT Statement

Can also use the column


names instead of the *
character
I could also qualify the
table with the column
name, department.name

HTTP 5105 Database Development and Design 28 November 2014


SELECT Statement

Here only the name


column is now displayed

HTTP 5105 Database Development and Design 29 November 2014


SELECT Statement

HTTP 5105 Database Development and Design 30 November 2014


SELECT Statement

You will now notice the


column heading for the
data now reads:
employeeName

HTTP 5105 Database Development and Design 31 November 2014


SELECT Statement

HTTP 5105 Database Development and Design 32 November 2014


SELECT Statement

You will notice the AS


keyword is optional, as
with Oracle
Any differences so far
with the SELECT?

HTTP 5105 Database Development and Design 33 November 2014


SELECT Statement

The WHERE clause can be used to restrict the rows to a specific


criteria
HTTP 5105 Database Development and Design 34 November 2014
SELECT Statement

The WHERE clause has


limited the output to that
specific criteria
The JOB is Programmer

HTTP 5105 Database Development and Design 35 November 2014


SELECT Statement

The same basic operators we used in Oracle apply here


in MySQL
=, <, >, >=, <=, !=, <>
IS NULL and IS NOT NULL
AND
OR
NOT
LIKE
BETWEEN AND
HTTP 5105 Database Development and Design 36 November 2014
SELECT Statement

The DISTINCT keyword will suppress any duplicates in the result set

HTTP 5105 Database Development and Design 37 November 2014


SELECT Statement

The DISTINCT keyword


suppresses any duplicate
values in the result set

HTTP 5105 Database Development and Design 38 November 2014


SELECT Statement

The ORDER BY clause will sort the results on the columns and the
sequence specified, in this case it is ascending sequence on the JOB
and then pm descending sequence on the name

HTTP 5105 Database Development and Design 39 November 2014


SELECT Statement

The ORDER BY behaves


the same as with Oracle
ASC for ascending
sequence
DESC for descending for
descending sequence

HTTP 5105 Database Development and Design 40 November 2014


SELECT Statement

As with Oracle multiple tables can be used in a SELECT


statement
We can run NATURAL, INNER, and CROSS joins
LEFT and RIGHT joins can also be performed

HTTP 5105 Database Development and Design 41 November 2014


SELECT Statement

HTTP 5105 Database Development and Design 42 November 2014


SELECT Statement

The two columns being


displayed had to be
qualified since the column
NAME appears in both
tables

HTTP 5105 Database Development and Design 43 November 2014


SELECT Statement

This format corresponds to the Oracle ANSI syntax


HTTP 5105 Database Development and Design 44 November 2014
SELECT Statement

The JOIN keyword can


also be used as shown it
this output

HTTP 5105 Database Development and Design 45 November 2014


SELECT Statement

In this screen capture you will notice there is no join condition being
specified
HTTP 5105 Database Development and Design 46 November 2014
SELECT Statement

There are 16 rows of


output, every row of the
EMPLOYEE table is joined
with every row on the
DEPARTMENT table
4 rows * 4 rows is 16 rows

HTTP 5105 Database Development and Design 47 November 2014


SELECT Statement

In this screen capture you will notice there is the cross join condition
being specified
HTTP 5105 Database Development and Design 48 November 2014
SELECT Statement

The CROSS JOIN


keywords also work with
MySQL
Indicates there is no join
condition being used
between the two tables

HTTP 5105 Database Development and Design 49 November 2014


SELECT Statement

The GROUP BY function can also be used to group data in the result
set together
HTTP 5105 Database Development and Design 50 November 2014
SELECT Statement

The COUNT function has


counted the occurrences
of the job and displayed
the result in the output

HTTP 5105 Database Development and Design 51 November 2014


SELECT Statement

Here the entire table is treated as one group


HTTP 5105 Database Development and Design 52 November 2014
SELECT Statement

The result says there are


three distinct job values in
the table

HTTP 5105 Database Development and Design 53 November 2014


SELECT Statement

Not the use of the HAVING clause to restrict the groups being
displayed
HTTP 5105 Database Development and Design 54 November 2014
SELECT Statement

The HAVING clause is


being used here display
the rows returned by the
group by that are equal to
only a value of 1

HTTP 5105 Database Development and Design 55 November 2014


Transactions

We made a note before that the COMMIT and


ROLLBACK statements were not being used
In the format we used previously value for Autocommit
is set to being active
Set autocommit=0;
This is the default behaviour
Set autocommit=1 will set it for your session, if you log
off it is gone and has to be reset next time you connect

HTTP 5105 Database Development and Design 56 November 2014


Transactions

The above table has been created to look at transactions


HTTP 5105 Database Development and Design 57 November 2014
Transactions

We will use this table to


look at transactions in
MySQL
We do have to insert
some data rows to the
table

HTTP 5105 Database Development and Design 58 November 2014


Transactions

The above rows are being inserted into the ACCOUNTS table
HTTP 5105 Database Development and Design 59 November 2014
Transactions

Confirmation the three


rows were inserted into
the table

HTTP 5105 Database Development and Design 60 November 2014


Transactions

The transaction begins with the keywords START TRANSACTION

The COMMIT keyword will make the change permanent in the table ,
a ROLLBACK could also be used
HTTP 5105 Database Development and Design 61 November 2014
GROUP BY Revisited

Since there was a table now with a numeric value, I decided to add
to the various group functions So here are the ADD, SUM, MIN, and
MAX function in use

HTTP 5105 Database Development and Design 62 November 2014


Transactions

The various group


function results are
shown

HTTP 5105 Database Development and Design 63 November 2014


Conclusions

I think you can agree there are far more similarities


between the SQL flavours then there are differences
This is the beauty of having a SQL standard
Most database vendors do adhere quite close to the
standard
There are a few places where the syntax is not
identical

HTTP 5105 Database Development and Design 64 November 2014

Anda mungkin juga menyukai