BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Similar documents
Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Angela Henry. Data Types Do Matter

5. SQL Query Syntax 1. Select Statement. 6. CPS: Database Schema

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 1. Introduction to SQL Server Panayiotis Andreou

Building Better. SQL Server Databases

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

SQL Server 2008 Tutorial 3: Database Creation

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Lab 4: Tables and Constraints

Documentation Accessibility. Access to Oracle Support. Supported Browsers

Exact Numeric Data Types

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Project Database Rules

Database and table creation

Full file at

Building Better. SQL Server Databases

Every Byte Counts. Why Datatype Choices Matter. Andy Yun, Database Architect/Developer

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

ColdFusion Summit 2016

Code Centric: T-SQL Programming with Stored Procedures and Triggers

12 Rules Defined by Dr.Codd for a Relational Database Management System

user specifies what is wanted, not how to find it

ColumnStore Indexes UNIQUE and NOT DULL

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CST272 SQL Server, SQL and the SqlDataSource Page 1

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

SQL: Data Definition Language

Unit 1 - Chapter 4,5

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Basic SQL. Basic SQL. Basic SQL

Lecture 1. Monday, August 25, 2014

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Lab # 4. Data Definition Language (DDL)

System Pages (Setup Admins Only)

Introduction to SQL Server 2005/2008 and Transact SQL

New Features Bulletin Replication Server Options 15.6

Introduction to Relational Database Management Systems

Kaotii.

Basic SQL. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

MTAT Introduction to Databases

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

1) Introduction to SQL

COMP102: Introduction to Databases, 5 & 6

Data Definition Language (DDL)

COMP 430 Intro. to Database Systems

Compression Users Guide. Adaptive Server Enterprise 15.7

Basis Data Terapan. Yoannita

CIS430 /CIS530 Lab Assignment 6

Tasks for Installation / Introduction Concepts. 1. What are the Advantages of SQL Server? Data Integrity

2017/11/04 04:02 1/12 Coding Conventions

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Compression Users Guide. SAP Adaptive Server Enterprise 16.0

EDB Postgres Hadoop Data Adapter Guide

Data Warehouse Wiz. User's Guide

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

Oracle Login Max Length Table Name 11g Column Varchar2

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

About System Setup Pages

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Lecture 07. Spring 2018 Borough of Manhattan Community College

Programming and Database Fundamentals for Data Scientists

MySQL: an application

Advanced Constraints SQL. by Joe Celko copyright 2007

Experimental Finance, IEOR. Mike Lipkin, Alexander Stanton

Seminar 4. Functions. Views. System tables

Simple Quesries in SQL & Table Creation and Data Manipulation

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

SQL Data Definition Language

A QUICK LOOK AT T-SQL for MS-SQL Server

CSC Web Programming. Introduction to SQL

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

RDBMS Basics: What Makes Up a SQL Server Database?

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

How to use SQL to create a database

INFORMATION TECHNOLOGY NOTES

Lecture 04: SQL. Monday, April 2, 2007

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

Lecture 04: SQL. Wednesday, October 4, 2006

1RDBMS Basics: What Makes Up a SQL Server Database?

Topic of most interest

Insert Into Customer1 (ID, CustomerName, City, Country) Values(103, 'Marwa','Baghdad','Iraq')

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

/* --6). Name: Christopher Singleton Date: 02/12/2017 Class: PROG 140 Project: Module 04

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

Keys are fields in a table which participate in below activities in RDBMS systems:

How to Migrate Microsoft SQL Server Connections from the OLE DB to the ODBC Provider Type

MTAT Introduction to Databases

Transcription:

BEGINNING T-SQL Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

FIRST: GET READY 1. What to model? 2. What is T-SQL? 3. Books Online (BOL) 4. Transactions

WHAT TO MODEL? What kind of data should we store in OUR database? Past suggestions include: goats, socks, plants, whiskey, candy, mafia hits, insults, toothpaste flavor ratings, hair counts, hubcap styles, Dr. Who factoids, book sales

WHAT IS T-SQL? SQL Special purpose programming language for database management system. ANSI standard language. Transact-SQL (T-SQL) Same thing, mostly. Proprietary language for Microsoft SQL Server.

BOOKS ONLINE (BOL) The official word. The reference. For realz. At Technet.Microsoft.com

TRANSACTIONS Transaction = a unit of work. Total sink or swim. Implicit transaction - Any single T-SQL statement. Explicit transaction - Explicitly defined.

DEMO: TRANSACTIONS

5. Tables Demo: CREATE TABLE Demo: What do we want to know? 6. Data types Demo: Add data types Demo: Char vs. Varchar Demo: Add data types NEXT: START A TABLE

TABLES Has a name Stores data, rather like a spreadsheet: columns and rows Each column has a name and data type Each row is unique

DEMO: CREATE TABLE

DEMO: WHAT DO WE WANT TO KNOW?

DATA TYPES

DATA TYPES Data type determines what you can do with the data. Use appropriate data types. Value Type of Data SQL Data Type Susan String Varchar 197 Integer Int March 20, 1983 Date Date

DATA TYPES: NUMERIC SQL Datatype Description Example tinyint, smallint, int, bigint Integer data 0, -180, 1250467 decimal, numeric float, real Fixed precision numbers Non fixed precision number (like a variable decimal) 0.0004, 1.0, 3028.17 0.0004, 1.0, 3028.17 smallmoney, money Currency data 1604.38, 0, -12.11

DATA TYPES: DATE SQL Datatype Description Example smalldatetime, datetime Date and time data 2010-03-20 11:20:03.107, 2010-04-07 00:00:00 date Date data 2010-03-20 time Time data 11:20:03.1071974 datetime2 datetimeoffset Date and time data (expanded datetime) 2010-03-20 11:20:03.1071974

DATA TYPES: STRING SQL Datatype Description Example char Fixed length character data Sue, Jennifer varchar nchar nvarchar Variable length character data Fixed length Unicode character data Variable length Unicode character data Sue, Jennifer Sue, Jennifer Sue, Jennifer

DEMO: ADD DATA TYPES

DEMO: CHAR VS VARCHAR

7. NULLs Demo: NULLs Demo: Add NULL / NOT NULL Demo: Create the table! NEXT: FINISH A TABLE

NULL: NO KNOWN VALUE NULL means unknown value Nothing can be = NULL Nothing can be <> NULL We can only test to see if the field IS NULL or IS NOT NULL

DEMO: NULLS

DEMO: CREATE THE TABLE

STATEMENTS! SELECT INSERT UPDATE DELETE

SELECT SELECT columns FROM TableName SELECT retrieves data. SELECT * FROM TableName Retrieves all columns. But don t use it! WHERE The WHERE clause limits what rows we get back. WHERE value = 1, <> 1 WHERE value IN () WHERE value LIKE string%

Inserts a row into a table Use a column list! You can insert multiple rows with one statement. INSERT

UPDATE modifies data already in the table Example: UPDATE Table1 SET ishungry = 0 WHERE myname = Jen UPDATE WHERE

Delete data from the table DELETE WHERE DELETE

CONSTRAINTS

PRIMARY KEY (PK) CONSTRAINT A constraint is a rule for the table. PK is the column(s) that uniquely identifies a row 1 PK per table PK doesn t allow NULL Example: ALTER TABLE tablename ADD CONSTRAINT constrname PRIMARY KEY (columnnames)

Natural vs. surrogate PKs IDENTITY() GUIDs PRIMARY KEY (PK) CONSTRAINT

FOREIGN KEY (FK) CONSTRAINT An FK links two tables Prevents orphaned records Link FK on ChildTable to PK on ParentTable Vendor Product Sold #1 Product Sold #2 Product Sold #3

FOREIGN KEY (FK) CONSTRAINT An FK links two tables Prevents orphaned records Link FK on ChildTable to PK on ParentTable

INNER JOIN OUTER JOIN: Full, Left, Right CROSS JOIN (rarely used) JOIN

BONUS!

BONUS! GetDate() Varchar vs Nvarchar Varchar can t handle Unicode. For example, this won t work: ;'ڢθϦ DECLARE @reg VARCHAR(10) = ' SELEC @reg; Nvarchar can handle Unicode. For example, this WILL work: ;'ڢθϦ DECLARE @uni NVARCHAR(10) = N' SELEC @uni; Sp_help Sys.sql_modules Indexes!

Tutorials: Consulting: Software: www.midnightdba.com www.midnightsql.com www.minionware.net

DOWNLOADS AND VIDEOS www.midnightdba.com/jen/articles/