<<

MTAT.03.105 (Andmebaasid)

Lab # 1 Hands-On Installation of MySQL and Type of Queries (DDL, DML, DCL, TCL) Institute of , University of Tartu, Estonia

Part A: Demo by Instructor in Lab a. Introduction to MySQL b. MySQL Environment Overview (Schema, Tables, Columns, Checks, so on..) . Installation of MySQL: d. Type of Queries/Categories of SQL command (DDL, DML, DCL, TCL) with Examples Part B: Hands-on by Students in Lab

Part A: Demo by Instructor in Lab

a. Introduction to MySQL MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. MySQL is developed, marketed, and supported by MySQL AB, which is a Swedish company. MySQL is becoming so popular because of many good reasons:  MySQL is released under an open-source license. So you have nothing to pay to use it.  MySQL is a very powerful program in its own right. It handles a large subset of the functionality of the most expensive and powerful packages.  MySQL uses a standard form of the well-known SQL language.  MySQL works on many operating systems and with many languages including PHP, , C, C++, JAVA, etc.  MySQL works very quickly and works well even with large data sets.  MySQL is very friendly to PHP, the most appreciated language for web development.  MySQL supports large databases, up to 50 million rows or more in a . The default file size limit for a table is 4GB, but you can increase this (if your can handle it) to a theoretical limit of 8 million terabytes (TB).  MySQL is customizable. The open-source GPL license allows to modify the MySQL software to fit their own specific environments.

b. MySQL Environment Overview (Schema, Tables, , Checks, so on..) Schema World: It is available in MySQL Tables: City, Country, CountryLanguage

Page 1 of 5

MTAT.03.105 Databases (Andmebaasid)

c. Installation: a. Please make sure to install following version because in this course predefined MySQL schema will be followed. “-installer-community-5.7.10.0.msi”

b. In lab (004), the required version is accessible just follow the following steps to access MySQL i. Login with your username and password ii. My Computer > M drive > Copy the mysql folder iii. My Computer > C drive > TEMP folder > Past iv. Unzip mysql folder in same folder (Right Click on mysql > 7-Zip > Extract Here) v. Open “Oracle VM VirtualBox” from Program menu vi. Oracle VM VirtualBox Manager is opened > Click on New vii. Enter Name “MySQL” > Next > Next > Select “Use an existing virtual hard disk file” > Browse, path “C:\TEMP\VirtualBox VMs\New group\MySQL Windows 10 English”, Select “MySQL Windows 10 English.vdi” and press (Ava/OK) > Click Create > Click Start viii. Open Program menu > Select “MySQL Workbench 6.3 CE” ix. Click on Local Instance MySQL57 and Enter password “MySQL”

Type of Queries/Categories of SQL command The SQL (Structured ) commands are divided in to the following categories:  Data Delimitation language (DDL)  Data Manipulation Language (DML)  (DCL)  Transaction Control Language (TCL)

Like: SQL Statements (Create database, table, , select, , drop, alter, so on…)

Basic Rules for SQL Statements

 Queries are instructions/commands  SQL is not case sensitive language  For readability write MySQL keyword on separate line  It is good practice type SQL keywords in capital letters

Page 2 of 5

MTAT.03.105 Databases (Andmebaasid)

DDL () Commands DDL is a standard for commands that define the different structures in a database. DDL statements create, modify, and remove database objects such as tables, indexes, and users. It is used to communicate with database. DDL is used to:

 Create an object  Alter the structure of an object  To drop the object created.  The commands are used: CREATE, ALTER, DROP, TRUNCATE Example:

First of all, create a “myDatabase” database: Command: CREATE database myDatabase Create “users” table Command: CREATE TABLE users ( username VARCHAR (30), password VARCHAR (20) )

Show the “users” table columns and structure. Command: SHOW COLUMNS FROM users

DML (Data Manipulation Language) COMMAND A popular data manipulation language is SQL, which is used to retrieve and manipulate data in a database. DML commands are the most frequently used in SQL commands and it used to query and manipulate the existing database objects. Some of the commands are Insert, Select, Update, Delete.

Insert Command This is used to add one or more rows to a table. The values are separated by commas. The values must be entered in the same order as they are defined and change sequences of columns and enter corresponding value. Example: Command: INSERT INTO users (username, password) VALUES ('Marko', 'marko123')

Enter multiple record at same time: Command: INSERT INTO users (username, password) VALUES ('Mart', 'Mart44'), ('Anna', 'anna22'), ('Riivo', '55Riivo')

Page 3 of 5

MTAT.03.105 Databases (Andmebaasid)

Change the sequence of columns: Command: INSERT INTO users (password, username) VALUES ('Marlon12', 'Marlon'), ('Jaak221', 'Jaak')

Enter record without specify columns names but you should have to follow the defined sequence of columns: Command: INSERT INTO users VALUES ('Toomas', 'myPassword123')

Select Commands It is used to retrieve information from the table. It is generally referred to as querying the table. We can either display all columns in a table or only specify column from the table.

Example

Select all columns from users table Command: SELECT * FROM users Select specify column(s) from users table: Command: SELECT username FROM users See the current system’s date and time Command: SELECT sysdate() from dual

TRUNCATE Commands (DDL Command) Delete all records from “users” table Command: TRUNCATE users DCL (Data Control Language) The DCL language is used for controlling the access to the table and hence securing the database. DCL is used to provide certain privileges to a particular . Privileges are rights to be allocated. DCL commands include: GRANT to allow specified users to perform specified tasks.  The privilege commands are namely, Grant and Revoke  The various privileges that can be granted or revoked are, o Select Insert Delete Update References Execute All GRANT COMMAND: It is used to create users and grant access to the database. It requires (DBA) privilege, except that a user can change their password. A user can grant access to their database objects to other users.

REVOKE COMMAND: Using this command, the DBA can revoke the granted database privileges from the user.

Page 4 of 5

MTAT.03.105 Databases (Andmebaasid)

TCL (Transaction Control Language) Command TCL is used to control transactional processing in a database. A transaction is logical unit of work that comprises one or more SQL statements, usually a group of Data Manipulation Language (DML) statements.

COMMIT: command is used to save the Records. ROLL BACK: command is used to undo the Records. SAVE POINT command is used to undo the Records in a particular transaction.

Part B: Hands-on by Students in Lab a. Create a database, name it as “StudentDatabase” b. Create a “StudentTable” table having columns like ID, Name, Degree, Address, Email c. Add at least 10 records in “StudentTable” d. Display all added records from table “StudentTable” and show to lab instructor. e. Display only name, address and email of the students from “StudentTable” and and show to lab instructor. f. Delete all records from the “StudentTable” and show to lab instructor.

Page 5 of 5