Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MySQL for beginners

2,669 views

Published on

A presentation about MySQL for beginners. It includes the following topics:
- Introduction
- Installation
- Executing SQL statements
- SQL Language Syntax
- The most important SQL commands
- MySQL Data Types
- Operators
- Basic Syntax
- SQL Joins
- Some Exercise

Published in: Technology

MySQL for beginners

  1. 1. Saeid Zebardast @saeid http://about.me/saeid saeid.zebardast@gmail.com 1
  2. 2. Please Please Please Ask Questions As Much As You Like • This is not a lecture! - But an opportunity to learn from each other. - If you haven’t seen some of these frameworks, methods, etc. It is OK! - Let we know if you know ‣ Better ways ‣ Best practices ‣ My mistakes!
  3. 3. Introduction • What’s MySQL? - Since 1995 - Written in C/C++ - RDMBS (Relational Database Management System) 3
  4. 4. Installation • Just enter the following command: - $ sudo apt-get install mysql-server mysql-client • Check MySQL CLI: - $ mysql -u root -p - mysql> SHOW DATABASES; 4
  5. 5. Execute SQL Statements • Interactively - $ mysql [database] ‣ mysql> stmt; • Command Line - $ mysql [database] -e ‘stmt’ • A file or a pipe - $ mysql [database] < stmt_file - $ cat stmt_file | mysql [database] 5
  6. 6. SQL Language • SQL (Structured Query Language) - Provided by RDBMS - Data Definition (DDL) ‣ CREATE TABLE, DROP DATABASE - Data Manipulation (DDL) ‣ SELECT, INSERT, UPDATE, DELETE - Data Control (DCL) ‣ GRANT, REVOKE 6
  7. 7. Some of The Most Important SQL Commands • SELECT - extracts data from a database • UPDATE - updates data in a database • DELETE - deletes data from a database • INSERT INTO - inserts new data into a database • CREATE DATABASE - creates a new database • ALTER DATABASE - modifies a database • USE DATABASE - choose a database to execute a statement • CREATE TABLE - creates a new table • ALTER TABLE - modifies a table • DROP TABLE - deletes a table • CREATE INDEX - creates an index (search key) • DROP INDEX - deletes an index 7
  8. 8. SQL Syntax • SQL is NOT case sensitive: select is the same as SELECT • Semicolon (;) after SQL Statements - or G 8
  9. 9. Data Types Integer Type Min Max 2^ TINYINT -128 127 2^7 SMALLINT -32,768 32,767 2^15 MEDIUMINT -8,388,608 8,388,607 2^23 INT -2,147,483,648 2,147,483,647 2^31 BIGINT -9,223,372,036,854,775,808 9,223,372,036,854,775,807 2^63 Note: If unsigned, the allowable range is from 0 to twice the Max.
  10. 10. Data Types Floating-point Type Description FLOAT(M,D) A small number with a floating decimal point. Size or Display length (M), Decimals (D) 0, 23 DOUBLE(M,D) A large number with a floating decimal point. 0, 53 DECIMAL(M,D) 0, 65
  11. 11. Data Types Date and Time Type Description DATE YYYY-MM-DD format. between 1000-01-01 and 9999-12-31. DATETIME YYYY-MM-DD HH:MM:SS format. between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. TIMESTAMP stored as the number of seconds since the Unix epoch 1970-01-01 between 1970-01-01 00:00:01 and 2038-01-09 03:14:07. TIME HH:MM:SS YEAR 1901 to 2155
  12. 12. Data Types String Type Description CHAR A fixed-length string between 1 and 255 characters. right-padded with spaces. VARCHAR A variable-length string between 1 and 255 characters. 0 to 65,535 in 5.0.3 TINYTEXT TINYBLOB up to 255 characters TEXT BLOB up to 65,535 characters MEDIUMTEXT MEDIUMBLOB up to 16,777,215 characters LONGTEXT LONGBLOB up to 4,294,967,295 characters ENUM List of items. For example: ENUM ('A', 'B', 'C') Note: case sensitive on BLOBs and are not case sensitive in TEXT fields.
  13. 13. Operators (Most Used) Operator Description = (A = B) is not true. != (A != B) is true. <> > (A > B) is not true. < (A < B) is true. >= (A >= B) is not true. <= (A <= B) is true. LIKE Simple pattern matching BETWEEN ... AND ... Check whether a value is within a range of values
  14. 14. Basic Syntax • Create Database - CREATE DATABASE db_name • Create Table - CREATE TABLE table_name (column_name column_type, …); • Insert Data - INSERT INTO table_name (field1, field2,...fieldN) VALUES (value1, value2, …valueN); • Select Data - SELECT field1, field2,...fieldN table_name1, table_name2... [WHERE condition1 [AND -OR condition2]….] • Update Data - UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] • Delete Data - DELETE FROM table_name [WHERE Clause] • Sorting Result - SELECT field1, field2,...fieldN table_name1, table_name2… ORDER BY field1, [field2...] [ASC [DESC]] 14
  15. 15. 15
  16. 16. Exercise 1 Data Definition, Data Control • Create database `workshop` - mysql> CREATE DATABASE `workshop`; mysql> USE `workshop`; • Grant all privileges to the database user `worker` - mysql> GRANT ALL PRIVILEGES on `workshop`.* to `worker`@localhost identified by ‘123456'; - mysql> exit; • Create table `workshops_list` - mysql> CREATE TABLE `workshops_list` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `description` MEDIUMTEXT DEFAULT NULL, PRIMARY KEY(`id`) ); - DESC `workshops_list`; 16
  17. 17. Exercise 2 Data Manipulation • Show data - mysql> SELECT * FROM `workshops_list`; • Insert data - mysql> INSERT INTO `workshops_list`(`title`) VALUES ('MySQL Workshop'), ('Java Workshop'), ('Ubuntu'), ('Windows 10’); - mysql> SELECT * FROM `workshops_list` G • Update data - mysql> UPDATE `workshops_list` SET `description` = 'Introduce MySQL database' WHERE id = 1; - … - mysql> SELECT * FROM `workshops_list` ORDER BY `id` DESC; • Remove data - mysql> DELETE FROM `workshops_list` WHERE title = 'Windows 10’; - mysql> SELECT * FROM `workshops_list` ORDER BY `title` ASC; 17
  18. 18. Exercise 3 Export and Import • Export database dump. you might want to take a look at the contents. - $ mysqldump -u worker -p workshop > workshop.sql - $ less workshop.sql • Delete database `workshop` - mysql> DROP DATABASE `workshop`; • Import database dump file. - $ mysql -u worker -p < workshop.sql 18
  19. 19. Read The F* Manual • RTFM - https://dev.mysql.com/doc/ • Help - mysql> HELP; - mysql> HELP CONTENTS; - mysql> HELP SELECT; 19
  20. 20. Thank You

×