You are on page 1of 11

MySQL Tutorial

What is DBMS
What is DBMS – DBMS or Database Management System is a
collection of programs which enable user to access database,
manipulate data and represent data. A technology to store and
retrieve data with utmost efficiency along with appropriate
security measures.

SQL means structured query language. This is a computer language


that use to communicate with relational database platforms such
as Oracle, Microsoft SQL, MySQL, Postgre, etc.

SQL Command Category


Data Definition Language (DDL) – Consists of command used to
define the database schema, such as CRATE, DROP, ALTER,
TRUNCATE, COMMENT, RENAME.
Data Manipulation Language (DML) – SQL commands which the
manipulation of data, such as SELECT, INSERT, UPDATE, DELETE
Data Control Language (DCL) – commands which mainly deals with
the rights, permission and other controls such as GRANT, INVOKE
Transaction Control Language (TCL) – includes the command which
mainly deals with the transactions of the database, such as
COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION.

Entity- Relationship Diagram


This is a type of diagram that display the relationship of one
entity to another.
Boxes such as EMPLOYEE, DEPARTMENT, PROJECTS, are so called
“main entities”. Spherical objects are the attributes of each
entities
Weak entities or Dependent Entities: This is a type of
relationship between the main entities and the weak entities such
as Name, Sex, Relationship, Birthdate.
Strong Entities: This is the relationship between two main
entities such as Employee and Department
Cardinality: N is to 1; 1 is to 1; or Many is to 1; Many to Many.
Example, 1 employee MANAGES 1 department. Number (N) of employees
WORKS for 1 department. Or 1 employee SUPERVISE number (N) of
employees.
Partial Participation and Total Participation: These are both
represented by single or double line in the diagram. If 1
Employee (not all) MANAGES 1 Department, then EMPLOYEE entity may
have just a partial participation with the department in terms of
MANAGING. If the all Employees WORKS ON the Project, then it
should be total participation, and it is represented by double
line in terms of WORKING ON.

Attributes
Composite Attributes:
COMPOSITE VS SIMPLE: Composite
Attributes are attributes that can
be divided into several sub-
attributes. Simple Attributes are
those attributes that cannot be
divided into sub parts.
SINGLE VALUED VS MULTI-VALUED:
Single value like Number and Name
under the DEPARTMENT entity,
describes that the department has only one name and one number.
Location is a multi-valued attribute because department can be
located into several areas.
COMPLEX ATTRIBUTE: This is a group of composite attributes such
as Phone (area code + phone number) and Address (number + street
+ town/city).

Type of Keys
PRIMARY KEY AND FOREIGN KEY
DATABASE NORMALIZATION IS USED TO REDUCE REDUNDANCY OR DATA
ANOMALIES
Data Types

Constraints

Data Definition Commands

Data Manipulation
Create Database
 Go to MySQL Workbench
 Create New Connection and give it a name
 Click Store in Vault and enter your MySQL Password
 Check Connection
 Click OK
 Select your newly created Connection, until directed to the
GUI
 Click Create new schema (scheme literally means database)

 Name your schema or database, then click Apply


 Once applied, there will be a code or “query”, this will
create your database. This query is very important so save a
copy of this line of code for future reference. In our
sample, we have the query line: CREATE SCHEMA `DBYee` ;
 Click apply, and the new database will appear on the
Workbench
Note, we can also create a new database by hitting the “create
new SQL Tab”

We can copy the query code generated from our first database
and paste it on the new SQL File and rename it with a new
database name. In our case, we renamed it as CREATE SCHEMA
`DBYee2` ;
Execute the new query by hitting the thunder bolt sign on the
GUI. A check indicator will appear on the console signifies
that a new database has been create. Refresh the scheme on the
Object Browser, and a new database will appear.
Create Table
To create Table,
 Click the arrow beside the database name, and right click on
Table.
 Name the table
Create Column
 Click the double arrow at the top right corner of the window
to reduce the display
 Double click the empty box under the Column Name Tab, and it
will automatically generate a column name.

Primary Key
The first column is normally the “primary key” (PK), and what is
primary key? This is the column of unique identities of a data.
The data inside the primary key column doesn’t have any duplicate
or similarities, thus it serves as the unique identification of a
person included in the database.
Data Type
We have plenty of data types in MySQL and can be shown in the
dropdown:

For strings, we can use the data type VARCHAR and the default
number of character is 45, but for any numerical data we have to
use the INT type (integer).
Once we clicked the Apply button after we set up all the details
of the columns of our first table, the system will generate a new
set of codes or queries. This is the SQL code for the entire
first table.
:
CREATE TABLE `dbyee`.`EmployeeInfo` (

`idEmployeeInfo` INT NOT NULL ,

`FName` VARCHAR(45) NULL ,

`SName` VARCHAR(45) NULL ,

`Age` INT NULL ,

PRIMARY KEY (`idEmployeeInfo`) );

Again, Refresh the Object Browser and check if a new table has
been created under the database.
Right click the table icon and select Edit Table to check the
table structure on the GUI
Note, to create a new table, you can also use the query code
above. Just edit the input data and apply the query. Then execute
using the thunderbolt icon.

Show Command
To show all the list of the databases in the connection, type:
show databases;

To show the tables of the database, type the command:


show table;

Use Command
To switch into or to select the database you want to use, use the
use command. For example:
use `dbyee`;

Drop Command
To leave the database, use the command “drop database
`NameofTheDatabase`;
Example:
drop database `dbyee`;

Insert Command
To insert data into the database, use the follow sample syntax:
insert into `db_yee`.`studentinfo`
(`studentID`, `studentFN`, `studentLN`, `Age`)
VALUES
('0002','RUBEN','SARMIENTO','19');

We can also insert multiple data like the sample below:


INSERT INTO `db_yee`.`studentinfo` (`studentID`, `studentFN`, `studentLN`, `age`)
VALUES ('4', 'SARAH', 'LOPEZ', '20');
INSERT INTO `db_yee`.`studentinfo` (`studentID`, `studentFN`, `studentLN`, `age`)
VALUES ('5', 'JENNY', 'NUNEZ', '20');

To input some data that into the table (without completing all
the columns)

INSERT INTO `db_yee`.`studentinfo` (`studentID`, `studentFN`, `studentLN`)


VALUES ('6', 'MON', 'LUSTRE');

INSERT INTO `db_yee`.`studentinfo` (`studentID`, `studentLN`, `age`) VALUES


('7', 'BERMUDEZ', '19');

Notice that we did not complete all the necessary information


within the table:
To reduce typing same column names, we can just type the values
after the insert command. For example:

insert into `db_yee`.`studentinfo`


VALUES
('0009','MELVIN','BUENO','19');

Select Command
This command displays the content of a table. The command to use
if we want to see the entire content of a table is:
select * from studentinfo; // where studentinfo is the name of the
table.

To select a single column of a table, just use the command;

Select nameofthecolumn from nameofthetable;

Or if we are going to select two or many columns we can use the


command:

Select nameofthecolumns from nameofthetable;

We can also display the columns according to the order that we


wanted to be displayed. For example, we want to display the 2nd
column then followed by the 1st column. So to do that, we can
execute the command:

select column2, column1 from nameofthetable;

Mathematical Operation using Select Command


To perform Mathematical operation using the select statement in
MYsql, we can use the dual statement, like the examples below:
Distinct Command
The Distinct command provides one detail from a table field
having similar name. Let say, one field or table consist of
duplicate information or a name that has duplicate, the DISTINCT
statement will select only one information from this duplicates.
This table displays
duplicate surnames in
the studentLN field, but
by using the “select
distinct” statement, our
query will display only
one data from this
similar surnames. The
syntax would be:

select distinct studentLN from studentinfo;

https://www.youtube.com/watch?
v=cfY9BkYdAh0&list=PLS1QulWo1RIY4auvfxAHS9m_fZJ2wxSse&index=10
youtube.com/watch?
v=Iaj3jm2pC0Q&list=PLS1QulWo1RIahlYDqHWZb81qsKgEvPiHn&index=3

You might also like