SQL Server for Developers Using SQL Server, DB Design, SQL SELECT, INSERT, UPDATE, DELETE SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Entity Framework Performance SoftUni Team Technical Trainers Software University
Microsoft Azure SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Web Fundamentals (HTML and CSS) Course Introduction 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
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
Introduction to SQL Server  Working with MS SQL Server and SQL Server Management Studio.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
Software Technologies Course Overview SoftUni Team Technical Trainers Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
ASP.NET MVC Course Program, Trainers, Evaluation, Exams, Resources SoftUni Team Technical Trainers Software University
Big Data Yuan Xue CS 292 Special topics on.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Entity Framework (EF) ORM and Entity Framework Code First. CRUD Operations SoftUni Team Technical Trainers Software University
Version Control Systems
Querying with Transact-SQL
Aga Private computer Institute Prepared by: Srwa Mohammad
Introduction to Entity framework
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
WordPress Introduction
Introduction to Entity Framework
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
Database Design and Rules
EF Relations Object Composition
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
The Basics of Data Manipulation
Entity Framework: Relations
MVC Architecture, Symfony Framework for PHP Web Apps
C# Web Development Basics
Introduction to Databases
Data Definition and Data Types
Version Control Systems
The Basics of Data Manipulation
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
Chapter 2: Creating And Modifying Database Tables
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

SQL Server for Developers Using SQL Server, DB Design, SQL SELECT, INSERT, UPDATE, DELETE SoftUni Team Technical Trainers Software University

Table of Contents  SQL Server Overview  Installing LocalDB and SSMS  Creating DB Diagrams in SSMS  Creating Blog DB: Users, Posts, Comments, Tags  Introduction to SQL  SELECT, WHERE, JOIN  Aggregate Functions and GROUP BY  INSERT, UPDATE, DELETE 2

3 sli.do #8575 Have a Question?

MS SQL Server – Overview

5  Microsoft SQL Server  SQL Server is enterprise DB server (RDBMS)  Widely used in the Microsoft ecosystem  For Web apps and enterprise apps  Free editions  SQL Express and SQL LocalDB  Paid editions  SQL Standard, SQL Enterprise What is SQL Server?

6  SQL Server LocalDB is free lightweight version of SQL Server  It starts automatically on connect request (no service start / stop)  Install MS SQL Server 2016 Express LocalDB  us/download/confirmation.aspx?id= us/download/confirmation.aspx?id=52679  Install SQL Server Management Studio (SSMS)  Use June 2016 version (July 2016 is broken!)  Installing SQL Server LocalDB and SSMS

7  Check whether you already have SQL Server LocalDB installed  Start Command Prompt  List all LocalDB versions:  List LocalDB instances: Checking for SQL Server LocalDB sqllocaldb versions sqllocaldb info sqllocaldb info MSSQLLocalDB

8 Installing SQL Server 2016 LocalDB

9 Installing SQL Server Management Studio

10 Connecting to SQL Server LocalDB 2016

11 SQL Server Management Studio (SSMS)

12  Install SQL Server Data Tools for Visual Studio (SSDT):  Use the SQL Server Object Explorer SQL Server Data Tools for Visual Studio (SSDT)

13  SQL Server Express 2016 requires Windows 8 or later  In Windows 7 use SQL Server 2014 instead  Installing SQL Server 2014 LocalDB (and SSMS 2014)  en-us/download/details.aspx ?id= en-us/download/details.aspx ?id=42299  SSMS 2016 works on Win7 and supports SQL 2014 Emergency Setup: SQL Server 2014

Install and Connect to SQL Server Live Exercise in Class (Lab)

15 Creating Database Diagrams in SQL Server

16 Creating DB Diagrams in SSMS

17  Numeric types  bit, int, float, money, numeric( scale, precision )  Text types  char( size ), varchar( size ) / nvarchar( size ), text / ntext  Date and time  datetime, smalldatetime  Binary data  varbinary( size ), image (large binary object) Data Types in SQL Server

18  Users table holds blog users (authors)  ID + Username + PasswordHash (binary data) + FullName  ID is primary key and identity (auto-increment)  Unique index UK_Users_Username(Username) Users Table

19 Create Unique Index: Users(Username)

20  Posts table holds blog posts (publications)  ID + Title + Body + Date + AuthorID (optional)  The AuthorID is a foreign key to Users(ID)  The Date column is auto-filled by the current date and time Posts Table

21 Creating Foreign Key: Users to Posts

22  Comments table holds post comments  ID + Text + PostID + AuthorID or AuthorName + Date  AuthorID is a foreign key to Users(ID)  PostID is foreign key to Posts(ID)  Either AuthorID or AuthorName is used  The Date column is auto-filled by the current date and time Comments Table

23  Tags table holds post tags  ID (primary key, identity)  Name (Unicode text)  Posts_Tags table connects post to tags  PostID – foreign key to posts  TagID – foreign key to tags  Composite primary key ( PostID and TagID are unique together) Tags and Posts_Tags Tables

24 Configure the Management Studio

25 Fill Sample Data

26 Export Database as SQL Script

27 1.Connect to the database server 2.Open SQL query window 3.Paste the SQL script code 4.Click the [Execute] button Import Database from SQL Script

Create BlogDB Database Schema Live Exercise in Class (Lab)

29  SQL is standardized language for relational databases  Data manipulation language (DML)  Search / modify table data rows  SELECT, INSERT, UPDATE, DELETE  Data definition language (DDL)  Define / modify database schema  CREATE, ALTER, DROP  Permission commands  GRANT, DENY, REVOKE SQL Language

30  SELECT  WHERE  ORDER BY SQL SELECT, WHERE, ORDER BY SELECT * FROM Posts SELECT Username, FullName FROM Users SELECT * FROM Users WHERE ID = 2 SELECT * FROM Users WHERE FullName >= 'M' SELECT * FROM Users ORDER BY Username SELECT TOP 3 * FROM Users ORDER BY FullName DESC

31  Join Posts with Comments tables Join Tables SELECT p.ID as PostID, p.Title, p.ID as PostID, p.Title, c.ID as CommentID, c.Text, c.Date c.ID as CommentID, c.Text, c.Date FROM Posts p JOIN Comments c ON p.ID = c.PostID JOIN Comments c ON p.ID = c.PostID

32  Join Comments with Users tables in order to correctly display the comment author name Join Tables with Filtering and NULL Check SELECT c.ID AS CommentID, c.Text AS CommentText, c.ID AS CommentID, c.Text AS CommentText, ISNULL(u.FullName, c.AuthorName) AS Author ISNULL(u.FullName, c.AuthorName) AS Author FROM Comments c LEFT JOIN Users u ON c.AuthorID = u.ID LEFT JOIN Users u ON c.AuthorID = u.ID WHERE PostID = 1

33  Join the Posts with Tags (many-to-many relationship) Join Multiple Tables SELECT p.ID AS PostID, p.Title, p.ID AS PostID, p.Title, t.ID AS TagID, t.Name t.ID AS TagID, t.Name FROM Posts p JOIN Posts_Tags pt ON p.ID = pt.PostID JOIN Posts_Tags pt ON p.ID = pt.PostID JOIN Tags t ON pt.TagID = t.ID JOIN Tags t ON pt.TagID = t.ID

34  Aggregate functions calculate value over multiple rows  COUNT(…), MIN(…), MAX(…), SUM(…), Aggregate Functions and Grouping SELECT MIN(Date) FROM Comments SELECT COUNT(*) AS [Comments Count] FROM Comments WHERE PostID = 1 SELECT PostID, COUNT(ID) AS CommentsCount FROM Comments GROUP BY PostID

35 Nested SELECT SELECT DISTINCT p.ID as PostID, p.Title DISTINCT p.ID as PostID, p.Title FROM Posts p JOIN Posts_Tags pt ON p.ID = pt.PostID JOIN Posts_Tags pt ON p.ID = pt.PostID WHERE pt.TagID IN (SELECT ID FROM Tags (SELECT ID FROM Tags WHERE Name IN ('programming', 'Web')) WHERE Name IN ('programming', 'Web')) ORDER BY p.Title

36  Insert a new post ( id and date will be auto-generated)  Insert a few new users SQL INSERT INSERT INTO Posts(Title, Body, AuthorID) VALUES ('New Title', 'New post content', 3) INSERT INTO Users(Username, PasswordHash) VALUES ('joe', HASHBYTES('SHA2_256', ('joe', HASHBYTES('SHA2_256', ('jeff', HASHBYTES('SHA2_256', 'SofiA!')), ('jeff', HASHBYTES('SHA2_256', 'SofiA!')), ('poly', HASHBYTES('SHA2_256', ('poly', HASHBYTES('SHA2_256',

37  Update existing post  change title  Update existing post  change date SQL UPDATE UPDATE Posts SET Date = ' T23:51:00' WHERE YEAR(date) = 2016; UPDATE Posts SET Title = 'Title Updated!' WHERE ID = 2;

38  Delete existing post by ID  Delete all comments from user 'maria' SQL DELETE DELETE FROM Posts WHERE ID = 9; DELETE FROM Comments WHERE AuthorID = (SELECT ID FROM Users (SELECT ID FROM Users WHERE Username = 'maria'); WHERE Username = 'maria');

39  Transact SQL (T-SQL) – adds conditions, loops, procedures, etc. T-SQL – Improved SQL USE master GO IF DB_ID('BlogDB') IS NOT NULL BEGIN ALTER DATABASE BlogDB ALTER DATABASE BlogDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE; SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE BlogDB DROP DATABASE BlogDBENDGO

40 CREATE / ALTER / DROP CREATE TABLE Tags( ID int IDENTITY NOT NULL, Name nvarchar(50) NOT NULL, CONSTRAINT PK_Tags PRIMARY KEY (ID)) CONSTRAINT PK_Tags PRIMARY KEY (ID)) ALTER TABLE Comments ADD CONSTRAINT DF_Comments_Date DEFAULT GETDATE() FOR Date DROP TABLE Tags

41  SQL Server is enterprise database system  Relational database: tables with relationships  LocalDB – free lightweight SQL Server version  Install SQL Server  SQL LocalDB + Management Studio (SSMS)  The SQL language  Query data: SELECT, WHERE, JOIN, GROUP BY  Modify data: UPDATE, DELETE, INSERT Summary

? ? ? ? ? ? ? ? ? SQL Server for Developers

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 43

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