What is a Database, MySQL Specifics Trần Anh Tuấn Edit from Telerik Software Academy

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
Welcome to the JSON-stores world Telerik Software Academy Databases.
NoSQL Concepts, Redis, MongoDB, CouchDB
Telerik Software Academy Telerik School Academy Creating E/R Diagrams with SQL Server.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Transaction Management and Concurrency Control Telerik Software Academy Databases.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Accessing SQL Server and MySQL – Live Demo Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Creating E/R Diagrams with SQL Server Management Studio and MySQL Workbench Svetlin Nakov Telerik Software Academy Manager Technical.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Nikolay Kostov Telerik Software Academy Senior Software Developer and Trainer
Telerik Software Academy End-to-end JavaScript Applications.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Nikolay Kostov Telerik Corporation
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
SQL Intro (MS SQL Server and MySQL) Telerik Software Academy Databases.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
With MSSQL Server Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Presentation transcript:

What is a Database, MySQL Specifics Trần Anh Tuấn Edit from Telerik Software Academy

1. What is Database? 2. Keys and Table Relations 3. Data Manipulation Language  Select  Insert  Update  Delete 4. Aggregate Functions 5. MySQL Specifics

 Relational database is set of tables with defined relations between them  Each table has columns (fields) and rows  Some fields are called primary and foreign keys and define relation 100StevenKing NeenahKochhar LexDe Haan(null)90 103HunoldAlexander ErnstBruce FieldField RowRow

 Relational databases are manipulated using Structure Query Language (SQL)  Language for describing operations on structure and content of the database  Easy and straightforward to learn  Most databases follow the SQL standard 99 with little exceptions and additions  Uses English phrases and words: SELECT department_name FROM departments

DB The query is sent to the server Enter SQL query SELECT department_name FROM departments DEPARTMENT_NAME Administration Marketing Shipping The DB returns result (usually a table)

 SQL (Structured Query Language)  Language for describing and modifying database structure and data  Consists of DDL and DML  Data Definition Language (DDL) – defines the database structure – tables, fields and relations  Data Manipulation Language (DML) – modifies the data, stored in the tables – insert, delete, update or fetch rows

 Tables relations are defined by primary and foreign keys  Special properties of tables  Pair is formed by primary key in one table and linked foreign key in another  The values in a primary key field must be unique across the rows in the table  In a table there can be only one primary key but multiple foreign keys, pointing to other tables

 Example of two tables with primary and foreign key  In table Employees we put the department id instead of all the information for the department  Data is not duplicated, less storage space required LAST_NAMEDEPARTMENT_ID King1 Kochhar1 Fay2 Toto3 Jack2 IDNAME 1Executive 2Marketing 3Administration DEPARTMENTS EMPLOYEES Primary key Foreign key to field ID in table Departments

 There are three types of relations between two tables  One-to-one – one row in the first table corresponds to single row in the other  One-to-many – one row in the first table corresponds to many rows in the other  Many-to-many – many rows in one table correspond to many rows in the other  Third table is needed to be achieved  Sum of two one-to-many relations

 There are additional properties of the fields that change their behavior  Unique – requires the values in that field to be unique  Inserting or modifying value that already exists raises error  Index – modifies the internal work of the storage engine – speeds up searching for value in that field  Requires storage space

 Autoincrement – usually used for primary key fields; if the inserted value is NULL a new value is generated and used instead  Not null fields – require the inserted value to be distinct from NULL  Raises error otherwise  All primary keys are not null  MySQL supports also full text index – index for string fields

Table 1 Table 2 Table 1 Filtering Choosing set of rows Projection Choosing set of columns Joining Combining data from two or more tables

 Example select query:  EMPLOYEE_ID, FIRST_NAME, SALARY – fields we are selecting  as sets name of the field in the result table  From defines the tables we are gathering the data from  Where filters the rows SELECT EMPLOYEE_ID, FIRST_NAME as NAME, SALARY FROM EMPLOYEES WHERE EMPLOYEE_ID > 180 SELECT EMPLOYEE_ID, FIRST_NAME as NAME, SALARY FROM EMPLOYEES WHERE EMPLOYEE_ID > 180

 Instead of list of fields to select * can be used to specify all fields  Example: table employees : Is similar to query: SELECT * FROM EMPLOYEES EMPL_IDFIRST_NAMELAST_NAMESALARY 10LarryKing900 20JohnKochhar800 30PapaDe Haan850 50MimiTochkova1200 SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, salary FROM EMPLOYEES

 To select from the employees table all employees with salary less than 1000: Produces result: SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES WHERE SALARY < 1000 SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES WHERE SALARY < 1000 LAST_NAMEFIRST_NAMESALARY KingLarry900 KochharJohn800 De HaanPapa850

 The special value null means there is no value  Similar to PHP null  Different from zero or empty string  All operations with null produce null  Including comparison!

 Strings are enclosed in quotes  Some RDBMS support strings, enclosed in double- quotes  Example: selecting string Produces result: Produces result: SELECT LAST_NAME, 'foo' AS FOO FROM EMPLOYEES LAST_NAMEFOO Kingfoo Kochharfoo De Haanfoo Mimifoo

 The keyword distinct sets the database engine to return only distinct rows as result SELECT MANAGER_ID, SALARY FROM EMPLOYEES SELECT MANAGER_ID, SALARY FROM EMPLOYEES MANAGER_IDSALARY SELECT DISTINCT MANAGER_ID,SALARY FROM EMPLOYEES SELECT DISTINCT MANAGER_ID,SALARY FROM EMPLOYEES MANAGER_IDSALARY

 Arithmetic operations: - + * / ( )  Example using in select query: SELECT LAST_NAME, SALARY, SALARY + 300, 2*(SALARY + 300) AS BIG_SALARY FROM EMPLOYEES WHERE SALARY < 1000 SELECT LAST_NAME, SALARY, SALARY + 300, 2*(SALARY + 300) AS BIG_SALARY FROM EMPLOYEES WHERE SALARY < 1000 LAST_NAMESALARYSALARY + 300BIG_SALARY King Kochhar De Haan

 Concatenation (joining) of strings is done by CONCAT() SELECT concat(FIRST_NAME,' ',LAST_NAME) AS Employees, SALARY FROM EMPLOYEES SELECT concat(FIRST_NAME,' ',LAST_NAME) AS Employees, SALARY FROM EMPLOYEES EmployeesSALARY Larry King900 John Kochhar800 Papa De Haan850 Mimi Tochkova1200

 Used in the where clause  Comparisons -, =, <>  BETWEEN value AND value – similar to combination of comparisons  IN (value, …) – specifying if value is in a list  LIKE, RLIKE – simple and extended string comparison with regular expressions  IS NULL, IS NOT NULL – check if value is (not) null

 Used in where clauses  Logical operations – or, and, xor, not  Used to build complex filters for select query SELECTMANAGER_ID,DEPARTMENT_NAME FROM DEPARTMENTS WHERE MANAGER_ID < 200 AND NOT (DEPARTMENT_NAME = 'SALES') SELECTMANAGER_ID,DEPARTMENT_NAME FROM DEPARTMENTS WHERE MANAGER_ID < 200 AND NOT (DEPARTMENT_NAME = 'SALES')

 Result of select query can be sorted via the ORDER BY clause  Syntax is: order by {column [asc|desc],…}  The asc and desc modifiers sort in ascending and descending order, respectively  By default sorting is ascending SELECT LAST_NAME, HIRE_DATE FROM EMPLOYEES ORDER BY HIRE_DATE, SALARY ASC SELECT LAST_NAME, HIRE_DATE FROM EMPLOYEES ORDER BY HIRE_DATE, SALARY ASC

 The insert query has multiple forms:  Insert into values ( ) INSERT INTO COUNTRIES VALUES ('BG', 'Bulgaria', '1') INSERT INTO COUNTRIES (COUNTRY_ID,COUNTRY_NAME,REGION_ID) VALUES ('BG', 'Bulgaria', '1') INSERT INTO COUNTRIES VALUES ('BG', 'Bulgaria', '1') INSERT INTO COUNTRIES (COUNTRY_ID,COUNTRY_NAME,REGION_ID) VALUES ('BG', 'Bulgaria', '1')

 The update query modifies single or multiple rows in a table  The syntax is update set =,… where  The syntax is update set =,… where UPDATE EMPLOYEES SET FIRST_NAME = 'Updated Name', DEPARTMENT_ID = 90 WHERE EMPLOYEE_ID = 100 UPDATE EMPLOYEES SET FIRST_NAME = 'Updated Name', DEPARTMENT_ID = 90 WHERE EMPLOYEE_ID = 100

 The delete query deletes single or multiple rows from a table  Syntax is delete from where  Syntax is delete from where  The truncate query empties table DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = 1 DELETE FROM EMPLOYEES WHERE FIRST_NAME LIKE 'S%' DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = 1 DELETE FROM EMPLOYEES WHERE FIRST_NAME LIKE 'S%' TRUNCATE TABLE EMPLOYEES

 Aggregate functions operate on multiple rows and return single row as result  Usually used on numeric fields EMPLOYEE_IDSALARY MAX(SALARY) 24000

 Count(*) – returns count of rows  Sum (field) – returns the sum of the values in the column  Avg (field) – returns the average of the values in the column  Max (field) - return the maximum value in the column  Can be used for string values  Min (field) – returns the minimum value in the column  Can be used for string values

 Selecting minimum, maximum, average and total salary for all representatives SELECT AVG(SALARY), MAX(SALARY), MIN(SALARY), SUM(SALARY) MIN(SALARY), SUM(SALARY) FROM EMPLOYEES WHERE JOB_ID LIKE '%ACC%' SELECT AVG(SALARY), MAX(SALARY), MIN(SALARY), SUM(SALARY) MIN(SALARY), SUM(SALARY) FROM EMPLOYEES WHERE JOB_ID LIKE '%ACC%' AVG(SALARY)MAX(SALARY)MIN(SALARY)SUM(SALARY)

 Selecting earliest and latest date of hiring of employee  Dates are stored as numbers so the numeric aggregate functions can be applied to them SELECT MIN(HIRE_DATE), MAX(HIRE_DATE) FROM EMPLOYEES SELECT MIN(HIRE_DATE), MAX(HIRE_DATE) FROM EMPLOYEES MIN(HIRE_DATE)MAX(HIRE_DATE) :00: :00:00

 Counting the employees in department with id 50  Count only counts the values, different from null SELECT COUNT(*) FROM EMPLOYEES WHERE DEPARTMENT_ID = 50 SELECT COUNT(*) FROM EMPLOYEES WHERE DEPARTMENT_ID = 50 COUNT(*) 45

 The aggregate functions ignore the null values SELECT AVG(COMMISSION_PCT) FROM EMPLOYEES AVG(COMMISSION) EMPLOYEE_IDCOMMISION (null)

 Select queries can be used in other queries  Join result of select query instead of table  Result of select query, as value or list of values in comparison SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEES) SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID IN (SELECT DEPARTMENT_ID FROM DEPARTMENTS WHERE DEPARTMENT_NAME='Accounting') SELECT D.DEPARTMENT_NAME, E.FIRST_NAME, E.SALARY FROM DEPARTMENTS D LEFT JOIN (SELECT EMPLOYEE_ID, FIRST_NAME, DEPARTMENT_ID, SALARY FROM EMPLOYEES WHERE SALARY > 10000) E ON E.DEPARTMENT_ID = D.DEPARTMENT_ID SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEES) SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID IN (SELECT DEPARTMENT_ID FROM DEPARTMENTS WHERE DEPARTMENT_NAME='Accounting') SELECT D.DEPARTMENT_NAME, E.FIRST_NAME, E.SALARY FROM DEPARTMENTS D LEFT JOIN (SELECT EMPLOYEE_ID, FIRST_NAME, DEPARTMENT_ID, SALARY FROM EMPLOYEES WHERE SALARY > 10000) E ON E.DEPARTMENT_ID = D.DEPARTMENT_ID

 Exists operator returns true if the select query returns results  Example: selecting all employees that worked in department ID 110 SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEES E WHERE EXISTS (SELECT EMPLOYEE_ID FROM JOB_HISTORY JH (SELECT EMPLOYEE_ID FROM JOB_HISTORY JH WHERE DEPARTMENT_ID = 110 AND WHERE DEPARTMENT_ID = 110 AND JH.EMPLOYEE_ID=E.EMPLOYEE_ID) JH.EMPLOYEE_ID=E.EMPLOYEE_ID) SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEES E WHERE EXISTS (SELECT EMPLOYEE_ID FROM JOB_HISTORY JH (SELECT EMPLOYEE_ID FROM JOB_HISTORY JH WHERE DEPARTMENT_ID = 110 AND WHERE DEPARTMENT_ID = 110 AND JH.EMPLOYEE_ID=E.EMPLOYEE_ID) JH.EMPLOYEE_ID=E.EMPLOYEE_ID)

 MySQL is the most common database for use with PHP  Very light and fast  Authentication is very fast  Doesn't slow page loading times  High scalability  Can work with millions of rows  Open source, free  Designed for Linux

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране