Database Views & Stored Procedures. Hans-Petter Halvorsen, M.Sc.

Similar documents
Database Systems. S. Adams. Dilbert. Available: Hans-Petter Halvorsen

Database. Quiz with Explainations. Hans-Petter Halvorsen, M.Sc.

SQL Server and SQL Structured Query Language

Database Lab. Hans-Petter Halvorsen

Introduction to ERwin

Database Overview. Introduction to Database Systems, ERwin, SQL Server, SQL, etc. Hans-Petter Halvorsen, M.Sc.

Structured Query Language

Industrial IT Laboratory Work Hans-Petter Halvorsen

UML. Quiz with Explainations. Hans-Petter Halvorsen, M.Sc.

University College of Southeast Norway. Introduction to Database. Systems. Hans-Petter Halvorsen,

Create a Virtual Test Environment

Using SQL Server in C#

Visual Studio Team Services

Networks and Web for Health Informatics (HINF 6220)

Unit Testing. Quiz with Explainations. Hans-Petter Halvorsen, M.Sc.

Database Management Systems

Team Foundation Server Visual Studio Team Services. Hans-Petter Halvorsen, M.Sc.

MATLAB Examples. Simulink. Hans-Petter Halvorsen, M.Sc.

Simulation in LabVIEW. Hans-Petter Halvorsen, M.Sc.

Team Assignment. Final Software Delivery. IA4412 Software Engineering

Part 1: Access Privileges

Seminar 4. Functions. Views. System tables

University College of Southeast Norway. Database Communication. in LabVIEW. Hans-Petter Halvorsen,

MATLAB Examples. Flow Control and Loops. Hans-Petter Halvorsen, M.Sc.

MySQL Introduction. By Prof. B.A.Khivsara

Get Table Schema In Sql Server 2008 To Add Column If Not Exists >>>CLICK HERE<<<

Database Communication in Visual Studio/C# using Web Services

Introduction to Simulink

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Solutions. Discretization HANS-PETTER HALVORSEN,

A hypothetical M:M student schedule example

Software Implementation

MySQL. A practical introduction to database design

University College of Southeast Norway. Web Services. with Examples. Hans-Petter Halvorsen,

Datalogging and Monitoring

NULL. The special value NULL could mean: Unknown Unavailable Not Applicable

Week Assignment Source Code Control (SCC) & Bug Tracking Systems Hans-Petter Halvorsen

The appendix contains information about the Classic Models database. Place your answers on the examination paper and any additional paper used.

QUETZALANDIA.COM. 5. Data Manipulation Language

CS2300: File Structures and Introduction to Database Systems

Database Management Systems,

Create Installation Packages in Visual Studio

University College of Southeast Norway ASP.NET. Web Programming. Hans-Petter Halvorsen,

Creating databases using SQL Server Management Studio Express

University College of Southeast Norway. LM-900 Level Tank. Hans-Petter Halvorsen,

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Unit 27 Web Server Scripting Extended Diploma in ICT

15CSL58: DATABASE MANAGEMENT LABORATORY

Part I: Introduction. Outline. Introduction to Databases & SQL. Motivating Example Flat Files. Relational Databases. Problems with Flat Files

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

Software Platforms. Quiz with Explainations. Hans-Petter Halvorsen, M.Sc.

Programming. Languages & Frameworks. Hans-Petter Halvorsen, M.Sc. O. Widder. (2013). geek&poke. Available:

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

Week Assignment. Source Code Control (SCC) & Bug Tracking Systems. Hans-Petter Halvorsen

SQL CSCI 201 Principles of Software Development

Announcements (September 21) SQL: Part III. Triggers. Active data. Trigger options. Trigger example

CS127 Homework #3. Due: October 11th, :59 P.M. Consider the following set of functional dependencies, F, for the schema R(A, B, C, D, E)

Spring % of course grade

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

Topic of most interest

Source Code Control. Quiz with Explainations. Hans-Petter Halvorsen, M.Sc.

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Oracle Login Max Length Table Name 11g Column Varchar2

Source Code Control & Bug Tracking

MATLAB Examples. Interpolation and Curve Fitting. Hans-Petter Halvorsen

UML. Unified Modeling Language. Hans-Petter Halvorsen, M.Sc. O. Widder. (2013). geek&poke. Available:

EE221 Databases Practicals Manual

Software Architecture

Virtual Instruments with LabVIEW

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Oracle Class VII More on Exception Sequence Triggers RowID & Rownum Views

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

CLASS DISCUSSION AND NOTES

SQL Constraints and Triggers

Software Testing. Hans-Petter Halvorsen, M.Sc.

Databases CSCI 201 Principles of Software Development

II (The Sequel) We will use the following database as an example throughout this lab, found in students.db.

Create a simple database with MySQL

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Database Management Systems by Hanh Pham GOALS

1) Introduction to SQL

SQL Data Definition: Table Creation

Software Documentation

Foreign-Key Associations

Introduction to MATLAB

Chapter 1 SQL and Data

Information_schema Views And Identity Column Sql Server

A practical introduction to database design

Unit 1 - Chapter 4,5

Exam code: Exam name: Database Fundamentals. Version 16.0

A Flat file database. Problems with a flat file database (data redundancy)

CSC 453 Database Technologies. Tanu Malik DePaul University

Web Services Hans-Petter Halvorsen

Unit Assessment Guide

Sql Server 2008 Query Table Schema Name In

CSE 344 AUGUST 1 ST ENTITIES

PASCAL - RECORDS. To define a record type, you may use the type declaration statement. The record type is defined as

IS Datamodellering og databasesystemer

Triggers- View-Sequence

Transcription:

Database Views & Stored Procedures Hans-Petter Halvorsen, M.Sc.

SQL Server Hans-Petter Halvorsen, M.Sc.

Microsoft SQL Server 3 1 2 Your SQL Server Your Tables Your Database 4 Write your Query here 5 The Results from your Query

Microsoft SQL Server Create a New Database 2 1 Name you database according to your Project

Views: Views are virtual tables for easier access to data stored in multiple tables. Stored Procedures: A Stored Procedure is a precompiled collection of SQL statements. In a stored procedure you can use if sentence, declare variables, etc. (like a Method in C#) Triggers: A database trigger is code that is automatically executed in response to certain events on a particular table in a database. Functions: With SQL and SQL Server you can use lots of built-in functions or you may create your own functions

Database Design ER Diagram ER Diagram (Entity-Relationship Diagram) Used for Design and Modeling of Databases. Specify Tables and relationship between them (Primary Keys and Foreign Keys) Example: Table Name Table Name Column Names Primary Key Primary Key Foreign Key Relational Database. In a relational database all the tables have one or more relation with each other using Primary Keys (PK) and Foreign Keys (FK). Note! You can only have one PK in a table, but you may have several FK s.

Lets Create these Tables using SQL Server

Views Hans-Petter Halvorsen, M.Sc.

Get Data from multiple tables in a Example: single Query using Joins select SchoolName, CourseName from You link Primary Keys and Foreign Keys together SCHOOL inner join COURSE on SCHOOL.SchoolId = COURSE.SchoolId 10

1 Create View: IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CourseData' AND type = 'V') DROP VIEW CourseData CREATE VIEW CourseData AS SELECT SCHOOL.SchoolId, SCHOOL.SchoolName, COURSE.CourseId, COURSE.CourseName, COURSE.Description Creating Views using SQL code FROM SCHOOL INNER JOIN COURSE ON SCHOOL.SchoolId = COURSE.SchoolId Using the View: 2 select * from CourseData A View is a virtual table that can contain data from multiple tables This part is not necessary but if you make any changes, you need to delete the old version before you can update it The Name of the View Inside the View you join the different tables together using the JOIN operator You can Use the View as an ordinary table in Queries:

Creating Views using the Editor 3 1 Graphical Interface where you can select columns you need 2 Add necessary tables 4 Copy the SQL Code and Create a New Script in the Management Studio

View Template IF EXISTS (SELECT name FROM sysobjects WHERE name = '<ViewName>' AND type = 'V') DROP VIEW <ViewName> CREATE VIEW <ViewName> AS SELECT <TableName>.<ColumnName>, <TableName>.<ColumnName>, <TableName>.<ColumnName>, <TableName>.<ColumnName>, <TableName>.<ColumnName> FROM <TableName1> INNER JOIN <TableName2> ON <TableName1>.<PrimKeyColumnName1> = <TableName2>.<PrimKeyColumnName2>

Creating Views - Exercise GetBookChapters

Create the View GetBookChapters

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'GetBookChapters' AND type = 'V') DROP VIEW GetBookChapters GetBookChapters View CREATE VIEW GetBookChapters AS SELECT BOOK.BookId, BOOK.BookTitle, BOOK.Summary, CHAPTER.ChapterNumber, CHAPTER.ChapterTitle FROM BOOK INNER JOIN CHAPTER ON BOOK.BookId = CHAPTER.BookId

Stored Procedures Hans-Petter Halvorsen, M.Sc.

1 Create Stored Procedure: IF EXISTS (SELECT name FROM sysobjects WHERE name = 'StudentGrade' AND type = 'P') DROP PROCEDURE StudentGrade CREATE PROCEDURE StudentGrade @Student varchar(50), @Course varchar(10), @Grade varchar(1) AS DECLARE @StudentId int, @CourseId int Stored Procedure select @StudentId = StudentId from STUDENT where StudentName = @Student A Stored Procedure is like a Method in C# - it is a piece of code with SQL commands that do a specific task and you reuse it This part is not necessary but if you make any changes, you need to delete the old version before you can update it Procedure Name Input Arguments Internal/Local Variables Note! Each variable starts with @ 2 select @CourseId = CourseId from COURSE where CourseName = @Course insert into GRADE (StudentId, CourseId, Grade) values (@StudentId, @CourseId, @Grade) Using the Stored Procedure: execute StudentGrade 'John Wayne', 'SCE2006', 'B' SQL Code (the body of the Stored Procedure)

Stored Procedure Template IF EXISTS (SELECT name FROM sysobjects WHERE name = '<StoredProcedureName>' AND type = 'P') DROP PROCEDURE <StoredProcedureName> CREATE PROCEDURE <StoredProcedureName> @<InputVariable1> <DataType>, @<InputVariable2> <DataType> AS DECLARE @<InternalVariable1> <DataType>, @<InternalVariable2> <DataType> select @<InternalVariable1> = <ColumnName> from <TableName> where <ColumnName> = @<InputVariable1> insert into <TableName> (<ColumnName1>, <ColumnName2>,...) values (@<InternalVariable1>, @<Inputvariable1>,...)

Creating Stored Procedures - Exercise CreateBook(BookName, Summary) CreateChapter(BookName, ChapterNumber, ChapterTitle)

Create the Stored Procedure CreateBook(BookName, Summary)

CreateBook Stored Procedure IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CreateBook' AND type = 'P') DROP PROCEDURE CreateBook CREATE PROCEDURE CreateBook @BookTitle varchar(50), @Summary varchar(255) AS insert into BOOK (BookTitle, Summary) values (@BookTitle, @Summary)

Creating Stored Procedures - Exercise CreateBook(BookName, Summary) CreateChapter(BookName, ChapterNumber, ChapterTitle)

Create the Stored Procedure CreateChapter(BookName, ChapterNumber, ChapterTitle)

CreateChapter Stored Procedure IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CreateChapter' AND type = 'P') DROP PROCEDURE CreateChapter CREATE PROCEDURE CreateChapter @BookTitle varchar(50), @ChapterNumber int, @ChapterTitle varchar(50) AS DECLARE @BookId int select @BookId = BookId from BOOK where BookTitle = @BookTitle insert into CHAPTER(BookId, ChapterNumber, ChapterTitle) values (@BookId, @ChapterNumber, @ChapterTitle)

Hans-Petter Halvorsen, M.Sc. University College of Southeast Norway www.usn.no E-mail: hans.p.halvorsen@hit.no Blog: http://home.hit.no/~hansha/