Date post: | 11-Jul-2015 |
Category: |
Technology |
Upload: | saeid-zebardast |
View: | 1,336 times |
Download: | 0 times |
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!
Introduction• What’s MySQL?
- Since 1995
- Written in C/C++
- RDMBS (Relational Database Management System)
3
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
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
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
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
SQL Syntax• SQL is NOT case sensitive: select is the same as
SELECT
• Semicolon (;) after SQL Statements
- or \G
8
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.
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
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
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
TINYTEXTTINYBLOB up to 255 characters
TEXTBLOB up to 65,535 characters
MEDIUMTEXTMEDIUMBLOB up to 16,777,215 characters
LONGTEXTLONGBLOB 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.
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
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
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
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
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
Read The F* Manual• RTFM
- https://dev.mysql.com/doc/
• Help
- mysql> HELP;
- mysql> HELP CONTENTS;
- mysql> HELP SELECT;
19
Thank You