Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University

Slides:



Advertisements
Similar presentations
Virtual training week 4 structured query language (SQL)
Advertisements

Introduction to Structured Query Language (SQL)
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
ASP.NET Programming with C# and SQL Server First Edition
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Using MongoDB with.NET Welcome to the JSON-stores world SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
What is a Database, MySQL Specifics Trần Anh Tuấn Edit from Telerik Software Academy
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Conditional Statements Implementing Control-Flow Logic in C# SoftUni Team Technical Trainers Software University
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
SQL (DDL & DML Commands)
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Database APIs and Wrappers
Entity Framework Performance SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Nikolay Kostov Telerik Corporation
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Data Modeling Creating E/R Diagrams SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Joins, Subqueries, CTE and Indices
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Database Design and Rules
EF Relations Object Composition
Data Definition and Data Types
Basic CRUD in SQL Server
Writing Basic SQL SELECT Statements
Entity Framework: Relations
Basic select statement
ATS Application Programming: Java Programming
Introduction to Databases
Data Definition and Data Types
Manipulating Data.
Restricting and Sorting Data
Presentation transcript:

Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University

Table of Contents 1.What is Database? 2.Keys and Table Relations 3.Data Manipulation Language  Select  Insert  Update  Delete 2

What is Database?

What is database?  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 Field Row

What is SQL?  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

Communication 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  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

Keys and Table Relations  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

Keys and Table Relations

 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 Keys and Table Relations (2) IDNAME 1Executive 2Marketing 3Administration DEPARTMENTS EMPLOYEES Primary key Foreign key to field ID in table Departments

Types of Relations  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

Fields Properties  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

Fields Properties (2)  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

Data Manipulation Language

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

Select Query (2)  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

Selecting all Fields  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

Selecting Fields Live Demo

Filtering Rows  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 LAST_NAMEFIRST_NAMESALARY KingLarry900 KochharJohn800 De HaanPapa850

Filtering Rows Live Demo

The null Value  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  Strings are enclosed in quotes  Some RDBMS support strings, enclosed in double-quotes  Example: selecting string Produces result: SELECT LAST_NAME, 'foo' AS FOO FROM EMPLOYEES LAST_NAMEFOO Kingfoo Kochharfoo De Haanfoo Mimifoo

Selecting Only Distinct Rows  The keyword distinct sets the database engine to return only distinct rows as result SELECT MANAGER_ID, SALARY FROM EMPLOYEES MANAGER_IDSALARY SELECT DISTINCT MANAGER_ID,SALARY FROM EMPLOYEES MANAGER_IDSALARY

Selecting Distinct Rows Live Demo

Arithmetic Operations  Arithmetic operations: - + * / ( )  Example using in select query: 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

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

Comparison Operations  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

Boolean Operations  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')

Boolean Operations Live Demo

Sorting the Data  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

Inserting Data Into Table  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')

Inserting Data Into Table Live Demo

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

Modifying Data Live Demo

Deleting Data  The delete query deletes single or multiple rows from a table  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%' TRUNCATE TABLE EMPLOYEES

? ? ? ? ? ? ? ? ? PHP & MySQL

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International  Attribution: this work may contain portions from  "PHP Manual" by The PHP Group under CC-BY licensePHP ManualCC-BY  "PHP and MySQL Web Development" course by Telerik Academy under CC-BY-NC-SA licensePHP and MySQL Web DevelopmentCC-BY-NC-SA 37

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg