You are on page 1of 27

MySQL ASSIGNMENT

Install Mysql Workbench following the tutorial on the link


https://www.youtube.com/watch?v=7S_tz1z_5bA&t=598s
And do practice sql commands starting from slide 7 of Mysql
slides to slide 27.
Deadline is today at 9pm. Ensure to submit your sql file on V
class.
WHAT IS MySQL
• Free SQL (Structured Query Language) database
server.

• licensed with the GNU General public license


http://www.gnu.org/

• MySQL is a relational database management system


(RDMS).

• MySQL is Open Source Software.


Reading assignment

• Read about the advantages and disadvantages of


using MySQL
MySQL Data types
• MySQL uses many different data types which are
broken into 3 categories i.e.
▫ Numeric
▫ Date and Time
▫ string types.
Starting MySQL
• Without using passwords
(when the password for the specified user
is empty)
▫ mysql -u <user> -h <Host>

• Using passwords
▫ mysql -u <user> -h <Host> -p
E.g: mysql -u root -h localhost

• Exiting with the command quit or exit.


Data Management
• CREATE DATABASE databaseName;
• DROP DATABASE databaseName;
• SHOW DATABASES;
• USE databaseName;
• SHOW TABLES;
• CREATE TABLE tableName(name1 type1, name2 type2, ...);
• DROP TABLE tableName;
• DESCRIBE table;
• SELECT * FROM table;
• INSERT INTO TABLE VALUES( value1, value2, ...);
DATABASE commands
• CREATE DATABASE database_name;
• CREATE DATABASE pat;

• SHOW DATABASES;

• DROP DATABASE database_name;


• DROP DATABASE pat;

• USE database_name;
• USE pat;
CREATE table
• CREATE TABLE table_name
(Column_name,Column_type);

• Create table patientd ( pno int not null primary key


auto_increment, patientname varchar (100) not null,
phoneno varchar (10) not null, residence varchar(50)
not null, diseases varchar(50) not null, gender
varchar(6) not null, comments varchar(50) not null);

• Note: Auto_Increment is a table modifier/constraint that requests


MySQL to add the next available number to the recordno field for
you
ALTER table
• Allows users to add, delete or modify columns in existing
tables
▫ ALTER TABLE table_name ADD column_name datatype AFTER
column_name
 ALTER TABLE patientd ADD DateofBirth date AFTER residence;
▫ ALTER TABLE table_name DROP COLUMN column_name
 ALTER TABLE patientd DROP COLUMN phoneno;
▫ ALTER TABLE table_name ALTER COLUMN column_name
datatype
 ALTER TABLE patientd ALTER COLUMN comments text;
 ALTER TABLE patientd CHANGE comments comments text;


DESCRIBE command
• Allows users to view the structure of a table

▫ DESCRIBE table_name;
 DESCRIBE patientd;
INSERT command
• MySQL INSERT Command adds new records to the table.

• INSERT INTO table_name (column list) values (Column values);

▫ E.g. Insert into patientd (pno,


patientname,residence,dob,nationality,diseases,gender,com
ments) values (NULL, ‘Mukasa’,’kikoni’,’1978-06-12’,
‘Ugandan’,’malaria’,’female’,’Aids Victim');

▫ OR: Insert into patientd values (NULL, ‘Mukasa’,


’kikoni’,’1978-06-12’, ‘Ugandan’,’malaria’,’female’,’Aids
Victim’);
SELECT command
• SELECT is the MySQL command used to retrieve records.

• SELECT expressions_and_columns from table_name


[WHERE some condition is true][ORDER BY
column_name [ ASC | DESC]]

• The * symbol stands for ‘everything’.


▫ E.g. SELECT * from patientd;
▫ OR SELECT pno, patientname from patientd;
SELECT DISTINCT command
• The SELECT DISTINCT command enables users to list only
different(distinct) values in a table.

• SELECT DISTINCT column_names(s) from table_name

▫ E.g. SELECT DISTINCT nationality from patientd;


WHERE Clause

• The WHERE clause specifies a particular


condition.

Select expressions_and_column_names
FROM table_name
WHERE [Some_condition_is_true]

Select * FROM patientd WHERE Patientname


= ‘Mukasa’;
Using operators in Where clause
• Basic comparison operators and meaning:
= Equal to
!= Not Equal to
<= Less than or equal to
< Less than
>= Greater than or equal to
> Greater than

• LIKE – Searches for a specified pattern in a column

SELECT column_names(s) FROM table_name WHERE


column_name LIKE pattern

Select * FROM patientd WHERE Patientname LIKE ‘M%’; OR


Select * FROM patientd WHERE Patientname LIKE ‘%A’;
Using operators in Where clause
• BETWEEN – Selects range of data between
two values

SELECT column_names(s) FROM table_name


WHERE column_name BETWEEN value1
AND Value 2

▫ SELECT * from patientd WHERE nationality


BETWEEN ‘Kenyan’ AND ‘Ugandan’

• NOT BETWEEN
ORDER BY command
• If you want to order results in a specific way,
requirements using the “ORDER BY” clause.

SELECT column_name(s) FROM table_name


ORDER BY column_name(s) ASC|DESC;

▫SELECT * FROM patientd ORDER BY patientname


DESC;
Update command
• Used to update existing records in a table

• UPDATE table_name SET column1 = “new value”,


Column2 = “new value” Where some_column =
some_Value;

▫ UPDATE patientd SET patientname=‘Nanyonjo’ WHERE


pno=2 AND patientname = ‘Mukasa’;
Delete command
The DELETE statement deletes records in a table without
deleting the table

• DELETE FROM table_name WHERE some_column =


some_value;
• DELETE FROM patientd WHERE patientname = ‘Mukasa’;

To delete all rows, use:

• DELETE * FROM table_name OR DELETE FROM


table_name
• DELETE FROM patientd;
TRUNCATE command

• Truncate command deletes the data


inside the table but not the table itself.

• TRUNCATE TABLE table_name;

▫ E.g. TRUNCATE TABLE patientd;


MySQL JOIN command
• The JOIN keyword queries data from teo or more tables based on
a relationship between certain columns in these tables.

• Tables are related to each other using keys

• JOIN OR INNER JOIN: Returns rows when there is at least one


match in both tables

• LEFT JOIN: Return all rows from the left table, even f there are
no matches in the right table.

• RIGHT JOIN: Return all rows from the right table, even if there
are no matches in the left table

• FULL JOIN: Return rows when there is a match in one of the


tables.
MySQL JOIN command
• The JOIN keyword queries data from two or more tables based
on a relationship between certain columns in these tables.

• Tables are related to each other using keys

• JOIN OR INNER JOIN: Returns rows when there is at least one


match in both tables

• LEFT JOIN: Return all rows from the left table, even f there are
no matches in the right table.

• RIGHT JOIN: Return all rows from the right table, even if there
are no matches in the left table

• FULL JOIN: Return rows when there is a match in one of the


tables.
MySQL JOIN command
• Class exercise:

Create a doctors table with the following columns:


1.docno int not null primary key auto_increment
2.docname varchar(50) not null
3.pno int(20) not null

4.Insert values into this table (null, ‘timothy’, 1);


5.Insert 3 other records into your table.
INNER JOIN OR JOIN Keyword
• JOIN OR INNER JOIN: Returns rows when
there is at least one match in both tables

• SELECT column_name(s) FROM table_name1


INNER JOIN table_name2 ON
table_name1.column_name=table_name2.col
umn_name;

• SELECT patientd.pno, patientd.patientname,


doctor.docname FROM patientd INNER JOIN
doctor ON patientd.pno=doctor.pno ORDER
BY patientd.patientname;
LEFT JOIN Keyword
• LEFT JOIN: Return all rows from the left table
(table_name1), even if there are no matches in the right
table(table_name2). Also called: LEFT OUTER JOIN

• SELECT column_name(s) FROM table_name1 LEFT JOIN


table_name2 ON
table_name1.column_name=table_name2.column_name;

• SELECT patientd.pno, patientd.patientname,


doctor.docname FROM patientd LEFT JOIN doctor ON
patientd.pno=doctor.pno ORDER BY patientd.patientname;
RIGHT JOIN Keyword
• RIGHT JOIN: Return all rows from the right table
(table_name2), even if there are no matches in the left
table (table_name1). Also called: RIGHT OUTER JOIN

• SELECT column_name(s) FROM table_name1 RIGHT JOIN


table_name2 ON
table_name1.column_name=table_name2.column_name;

• SELECT patientd.pno, patientd.patientname,


doctor.docname FROM patientd RIGHT JOIN doctor ON
patientd.pno=doctor.pno ORDER BY patientd.patientname;
FULL JOIN Keyword
• FULL JOIN: Return rows when there is a
match in one of the tables.

• SELECT column_name(s) FROM table_name1


FULL JOIN table_name2 ON
table_name1.column_name=table_name2.col
umn_name;

• SELECT patientd.pno, patientd.patientname,


doctor.docname FROM patientd FULL JOIN
doctor ON patientd.pno=doctor.pno ORDER
BY patientd.patientname;

You might also like